EnvoyのOAuth2 Filterを試してみました。残念ながら期待通りに動きませんでした。メモだけ残しておきます。
構成
- EKS 1.18
- Envoy 1.17.0-dev-483dd3
以下のトラフィックパスを構築します。
Browser ↓ Internet-facing ALB ↓ Service/NodePort ↓ Pod (envoy) ↓ Service ↓ Pod (ingress-nginx) ↓ Ingress (echoserver) ↓ Service (echoserver) ↓ Pod (echoserver)
Route53やALBを設定してインターネットからドメイン名でアクセス可能にしておきます。
また、Google Identity PlatformであらかじめClient IDを作成しておきます。Redirect URIsには https://echoserver.example.com/callback
を指定しておきます(example.com
は自分のドメインに置き換えてください)。
Envoyの設定
Example configuration を参考にYAMLを書きます。まずは検証のために必要最小限に設定にします。分かりにくいのでコメントを入れました。
# /etc/envoy/envoy.yaml static_resources: listeners: - name: listener_0 address: socket_address: { address: 0.0.0.0, port_value: 10000 } filter_chains: - filters: - name: envoy.filters.network.http_connection_manager typed_config: "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager codec_type: "AUTO" stat_prefix: ingress_http route_config: virtual_hosts: # すべてのトラフィックを service cluster に転送する - name: service domains: ["*"] routes: - match: prefix: "/" route: cluster: service timeout: 5s http_filters: # OAuth2 Filterを適用する - name: envoy.filters.http.oauth2 typed_config: "@type": type.googleapis.com/envoy.extensions.filters.http.oauth2.v3alpha.OAuth2 config: # Google OAuth2 Endpointを定義する token_endpoint: cluster: auth uri: oauth2.googleapis.com/token timeout: 3s authorization_endpoint: https://accounts.google.com/o/oauth2/v2/auth redirect_uri: "https://%REQ(:authority)%/callback" redirect_path_matcher: path: exact: /callback signout_path: path: exact: /signout credentials: # Client ID/Secretを定義する client_id: REDACTED.apps.googleusercontent.com token_secret: name: token sds_config: path: "/etc/envoy/secret.yaml" hmac_secret: name: hmac sds_config: path: "/etc/envoy/secret.yaml" - name: envoy.router clusters: - name: service connect_timeout: 5s type: LOGICAL_DNS lb_policy: ROUND_ROBIN load_assignment: cluster_name: service endpoints: - lb_endpoints: - endpoint: address: socket_address: # 後続のバックエンドに転送する address: ingress-nginx-controller.ingress-nginx.svc.cluster.local port_value: 80 - name: auth connect_timeout: 5s type: LOGICAL_DNS lb_policy: ROUND_ROBIN load_assignment: cluster_name: auth endpoints: - lb_endpoints: - endpoint: address: socket_address: address: oauth2.googleapis.com port_value: 443
# /etc/envoy/secret.yaml resources: - "@type": "type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.Secret" name: token generic_secret: secret: inline_string: REDACTED - "@type": "type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.Secret" name: hmac generic_secret: secret: inline_string: REDACTED
Envoyのデプロイ
以下のYAMLをデプロイします。
apiVersion: apps/v1 kind: Deployment metadata: name: envoy spec: replicas: 1 selector: matchLabels: app: envoy template: metadata: labels: app: envoy spec: containers: - args: - --config-path - /etc/envoy/envoy.yaml - --bootstrap-version - "3" - --service-node - ingress-envoy - --service-cluster - ingress-envoy image: envoyproxy/envoy-dev:483dd3007f15e47deed0a29d945ff776abb37815 name: envoy ports: - name: http containerPort: 10000 protocol: TCP resources: limits: memory: 256Mi requests: cpu: 10m memory: 256Mi volumeMounts: - mountPath: /etc/envoy name: envoy volumes: - name: envoy configMap: name: envoy --- apiVersion: v1 kind: Service metadata: name: envoy spec: type: NodePort ports: - name: http port: 80 nodePort: 30280 protocol: TCP targetPort: http selector: app: envoy
Envoy v3 APIを利用するには引数に --bootstrap-version 3
が必要です。OAuth2 Filter APIはv3で定義されています。
また、 generic_secret
を定義するには引数に --service-node
--service-cluster
が必要です。これらを指定しないと以下のエラーが出ます。
GenericSecretSdsApi: node 'id' and 'cluster' are required. Set it either in 'node' config or via --service-node and --service-cluster options.
動作確認
ブラウザで https://echoserver.example.com
にアクセスします。OAuth2 authorization endpointへのリダイレクトまでは動作したのですが、残念ながら以下のエラー画面になってしまいました。
承認エラー エラー 400: invalid_scope Some requested scopes were invalid. {invalid=[user]}
ブラウザのログを確認すると、以下のようなリクエストが飛んでいました。
https://echoserver.example.com ↓302 https://accounts.google.com/o/oauth2/v2/auth?client_id=REDACTED&scope=user&response_type=code&redirect_uri=https%3A%2F%2Fechoserver.example.com%2Fcallback&state=https%3A%2F%2Fechoserver.example.com%2F ↓302 https://accounts.google.com/signin/oauth/error?authError=REDACTED
OAuth2 scopeをemailなどに変更すれば動作すると思いますが、Envoyのドキュメントには設定項目が見当たりませんでした。v1.16時点ではoauth.protoに定義がないようです。ここでお手上げになりました。
(12/25追記) OAuth2 scopeを指定できるようにするPull Requestがあるようです。
このPull Requestを実際に動かしているコードもあるようです。
今後の課題
実用になるまでは以下の課題がありそうです。
pass_through_matcher
でFilterの除外条件を定義できるが実用に耐えられるか。authority
,path
のregex matchが使えるので大丈夫そう。email
などでリソースにアクセスできる条件を絞り込めるか。現状は UserInfo から属性を取得する実装がなさそうなので無理そう。