kakakakakku blog

Weekly Tech Blog: Keep on Learning!

Envoy で HTTPS 接続をする設定を学べる「Securing traffic with HTTPS and SSL/TLS」を試した

どんどんと「Try Envoy」を進めていく.今回は Envoy で HTTPS 接続を試すコンテンツ「Securing traffic with HTTPS and SSL/TLS」を紹介する.今まで nginx を使う場合は ssl_certificatessl_certificate_key を設定したり,HTTP から HTTPS にリダイレクトをするために 301 を返していたけど,Envoy を使う場合はどのように envoy.yaml を設定するのか?を学べる.

Securing traffic with HTTPS and SSL/TLS

手順は以下の「計4種類」ある.

  • Step.1 「SSL Certificates」
  • Step.2 「Securing Traffic」
  • Step.3 「Redirecting HTTP Traffic」
  • Step.4 「Start Proxy」

www.envoyproxy.io

www.katacoda.com

Step.1 「SSL Certificates」

まず,検証のために example.com に対するオレオレ証明書を作成する.以下のコマンドを実行し,証明書 example-com.key と署名リクエスト example-com.crt を作成する.

$ openssl req -nodes -new -x509 \
  -keyout example-com.key -out example-com.crt \
  -days 365 \
  -subj '/CN=example.com/O=My Company Name LTD./C=US';

Step.2 「Securing Traffic」

Envoy で HTTPS 接続をする場合,filterstls_context を設定する.以下の envoy.yaml を読み解くと,8443 Port でリクエストを受けて,/service/1/service/2 のパスごとに異なる Cluster に転送する.そして tls_context に Step.1 で作成した example-com.keyexample-com.crt を設定することにより,HTTPS 接続となる.

- name: listener_https
  address:
    socket_address: { address: 0.0.0.0, port_value: 8443 }
  filter_chains:
  - filters:
    - name: envoy.http_connection_manager
      config:
        codec_type: auto
        stat_prefix: ingress_http
        route_config:
          name: local_route
          virtual_hosts:
          - name: backend
            domains:
            - "example.com"
            routes:
            - match:
                prefix: "/service/1"
              route:
                cluster: service1
            - match:
                prefix: "/service/2"
              route:
                cluster: service2
        http_filters:
        - name: envoy.router
          config: {}
    tls_context:
      common_tls_context:
        tls_certificates:
          - certificate_chain:
              filename: "/etc/envoy/certs/example-com.crt"
            private_key:
              filename: "/etc/envoy/certs/example-com.key"

Step.3 「Redirecting HTTP Traffic」

Envoy で HTTP から HTTPS にリダイレクトをする場合は routeredirect を設定する.

- name: listener_http
  address:
    socket_address: { address: 0.0.0.0, port_value: 8080 }
  filter_chains:
  - filters:
    - name: envoy.http_connection_manager
      config:
        codec_type: auto
        stat_prefix: ingress_http
        route_config:
          virtual_hosts:
          - name: backend
            domains:
            - "example.com"
            routes:
            - match:
                prefix: "/"
              redirect:
                path_redirect: "/"
                https_redirect: true
        http_filters:
        - name: envoy.router
          config: {}

実際にドキュメントを読むと,例えば response_code でレスポンスコードを 301 以外に設定したり,strip_query: true でクエリパラメータを削除したり,他のパラメータも用意されていた.

www.envoyproxy.io

Step.4 「Start Proxy」

最後は Envoy を起動する.今回は Envoy に対して「計3種類」のポートに接続できるようになっている.さらに,前回と同様にホスト名(コンテナ ID)を返す HTTP コンテナ katacoda/docker-http-server を2個起動し,最終的に以下の構成図のようになる.

  • 8080 (HTTP)
  • 8443 (HTTPS)
  • 8001 (管理画面)
$ docker run -it --name proxy1 -p 80:8080 -p 443:8443 -p 8001:8001 -v /root/:/etc/envoy/ envoyproxy/envoy
$ docker run -d katacoda/docker-http-server
$ docker run -d katacoda/docker-http-server

f:id:kakku22:20191206135928p:plain

さっそく HTTP にリクエストを送ると,期待通りに 301 となる(date は修正している).

$ curl -H "Host: example.com" http://localhost -i
HTTP/1.1 301 Moved Permanently
location: https://example.com/
date: Thu, 05 Dec 2019 00:00:00 GMT
server: envoy
content-length: 0

次に curl -k で HTTPS にリクエストを送ると,期待通りに 200 となり,パスごとに異なる HTTP コンテナからリクエストが返っている(date は修正している).

$ curl -k -H "Host: example.com" https://localhost/service/1 -i
HTTP/1.1 200 OK
date: Thu, 05 Dec 2019 00:00:00 GMT
content-length: 58
content-type: text/html; charset=utf-8
x-envoy-upstream-service-time: 0
server: envoy

<h1>This request was processed by host: eb8b87e0649a</h1>

$ curl -k -H "Host: example.com" https://localhost/service/2 -i
HTTP/1.1 200 OK
date: Thu, 05 Dec 2019 00:00:00 GMT
content-length: 58
content-type: text/html; charset=utf-8
x-envoy-upstream-service-time: 0
server: envoy

<h1>This request was processed by host: 3f660d280e19</h1>

まとめ

  • 「Try Envoy」のコンテンツ「Securing traffic with HTTPS and SSL/TLS」を試した
  • 今回も nginx で経験のある HTTPS 接続とリダイレクトを Envoy で試したため,イメージしやすかった

Try Envoy 関連