ローカルモードで Flex Gateway の背後で実行されている API をパブリッシュすることで、YAML 設定データを変更する方法について説明します。
-
Linux および Docker:
.yaml ファイル
下記の手順は、自動リソースレベル認証ポリシーによって保護されている複数のアップストリームサービスを使用した API のシンプルなリソースベースの YAML 設定を適用する方法を示しています。
詳細は、「自動ポリシー」と「リソースレベルのポリシー」を参照してください。
|
自動ポリシーは、リソースベースの設定でのみ定義できます。インライン設定モデルはサポートされません。リソースベースとインラインの設定モデルの違いについては、「宣言型設定リファレンスガイド」を参照してください。 |
Linux で Flex Gateway の背後で実行されている API のパブリッシュ
始める前に
始める前に、次の準備が整っていることを確認します。
-
Flex Gateway がインストールされていてローカルモードで実行されている。ゲートウェイのインストールと実行についての詳細は、Flex Gateway のダウンロードを参照してください。
-
アップストリームサービスの URL が用意されている。次の例では架空の
products-api と users-api というサービスを使用していますが、metadata.name には実際の API 名、spec.services にはサービスの詳細を指定することができます。
API のパブリッシュ
-
.yaml ファイル拡張子で設定ファイルを作成します。-
ファイルにカスタム名を付けます。
-
ファイルを Flex Gateway の設定ディレクトリ
/etc/mulesoft/flex-gateway/conf.d/custom に保存します。このディレクトリには複数の設定ファイルを保存できます。
-
-
次の 2 つの
ApiInstance リソースをコピーしてファイルに貼り付け、必要な箇所を適切な値に変更します。--- apiVersion: gateway.mulesoft.com/v1alpha1 kind: ApiInstance metadata: name: products-api labels: secured-by-automated-policy: yes spec: address: http://0.0.0.0:8080 services: products: address: https://<your products URL>:<your port>/ routes: - rules: - path: /products(/.*) - path: /products-featured(/.*) config: destinationPath: /api --- apiVersion: gateway.mulesoft.com/v1alpha1 kind: ApiInstance metadata: name: users-api labels: secured-by-automated-policy: yes spec: address: http://0.0.0.0:8081 services: users: address: https://<your users URL>:<your port>/ routes: - rules: - path: /api/users(/.*) -
次の
PolicyBinding リソースを ApiInstance リソースの後に追加して、自動基本認証ポリシーを定義します。--- apiVersion: gateway.mulesoft.com/v1alpha1 kind: PolicyBinding metadata: name: security-protection spec: targetRef: kind: Selector selector: kind: ApiInstance labels: secured-by-automated-policy: yes policyRef: name: http-basic-authentication-flex config: username: chris password: admin自動ポリシーを複数の API インスタンスに適用するには、ポリシーバインドで
Selectorkind を指定する必要があります。サンプル PolicyBinding リソースでは、spec.targetRef.kind の値が Selector、spec.targetRef.selector.kind の値が ApiInstance に設定されています。また、サンプルリソースでは基本認証ポリシーを 2 つの API インスタンスに適用しています。また、
secured-by-automated-policy ラベルを spec.targetRef.selector.labels で定義しています (ラベル名はカスタマイズ可能です)。このラベルは、自動ポリシーをラベルのリソースに関連付ける ApiInstancemetadata.labels プロパティで定義されているラベルと一致します。ラベルを省略することで、自動ポリシーをすべての API インスタンスに適用することができます。
-
ファイルを保存します。ゲートウェイによって設定が自動的に更新されます。
-
次のコマンドを実行してログを表示します。
journalctl -u flex-gateway-*
応答は次のようになります。
[agent][info] Generating config [agent][info] Gateway default/21cf4286e38f: Adding ApiInstance default/products-api http://0.0.0.0:8080 [agent][info] Gateway default/21cf4286e38f: Adding Policy default/security-protection [agent][info] Gateway default/21cf4286e38f: Adding Route: &{host: path:/api/products(/.*) methods: headerConditions:[] profile:0xc00004f750} => {Kind:Service Name:products-api-products Namespace:default} [agent][info] Gateway default/21cf4286e38f: Adding Policy default/envoy.filters.http.router [agent][info] Gateway default/21cf4286e38f: Adding ApiInstance default/users-api http://0.0.0.0:8081 [agent][info] Gateway default/21cf4286e38f: Adding Policy default/security-protection [agent][info] Gateway default/21cf4286e38f: Adding Route: &{host: path:/api/users(/.*) methods: headerConditions:[] profile:0xc0061823f0} => {Kind:Service Name:users-api-users Namespace:default} [agent][info] Gateway default/21cf4286e38f: Adding Policy default/envoy.filters.http.router [agent][info] Gateway default/21cf4286e38f: Adding Service default/products-api-products https://<your products URL>:<your port>/ [agent][debug] generating service products-api-products.default.svc hostname: https://<your products URL> port: <your port> [agent][info] Gateway default/21cf4286e38f: Adding Service default/users-api-users https://<your users URL>:<your port>/ [agent][debug] generating service users-api-users.default.svc hostname: https://<your users URL> port: <your port> [agent][info] Writing envoy bootstrap configuration to /tmp/envoy.json [envoy][info] cds: add 2 cluster(s), remove 3 cluster(s) [envoy][info] cds: added/updated 0 cluster(s), skipped 2 unmodified cluster(s) -
curl などのツールで認証ログイン情報を指定して API をテストします。自動認証ポリシーは、両方の API を保護します。 -
PolicyBinding リソースを以下に置き換えることで、リソースレベルの粒度を自動認証ポリシーに適用します。--- apiVersion: gateway.mulesoft.com/v1alpha1 kind: PolicyBinding metadata: name: security-protection spec: targetRef: kind: Selector selector: kind: ApiInstance labels: secured-by-automated-policy: yes policyRef: name: http-basic-authentication-flex config: username: chris password: admin rules: - methods: GETこの例では、
spec.rules で指定されているように、自動認証ポリシーは GET メソッドリソースにのみ適用されます。 -
ファイルを保存します。ゲートウェイによって設定が自動的に更新されます。
自動リソースレベル認証ポリシーで保護された 2 つの API が Flex Gateway の背後で実行されています。
Docker コンテナで Flex Gateway の背後で実行されている API のパブリッシュ
始める前に
始める前に、次の準備が整っていることを確認します。
-
Flex Gateway がインストールされている。詳細は、Flex Gateway のダウンロードを参照してください。
-
Flex Gateway がローカルモードで登録されて実行されている。詳細は「ローカルモードでの登録と実行」を参照してください。
-
アップストリームサービスの URL が用意されている。次の例では架空の
products-api と users-api というサービスを使用していますが、metadata.name には実際の API 名、spec.services にはサービスの詳細を指定することができます。
API のパブリッシュ
-
ターミナルを開いて、Flex Gateway 設定ファイルが保存されているディレクトリに移動します。このディレクトリは、Flex Gateway を起動したときに指定されています。
-
.yaml ファイル拡張子で設定ファイルを作成します。-
ファイルにカスタム名を付けます。
-
ファイルを保存します。
-
-
次の 2 つの
ApiInstance リソースをコピーしてファイルに貼り付け、必要な箇所を適切な値に変更します。--- apiVersion: gateway.mulesoft.com/v1alpha1 kind: ApiInstance metadata: name: products-api labels: secured-by-automated-policy: yes spec: address: http://0.0.0.0:8080 services: products: address: https://<your products URL>:<your port>/ routes: - rules: - path: /products(/.*) - path: /products-featured(/.*) config: destinationPath: /api --- apiVersion: gateway.mulesoft.com/v1alpha1 kind: ApiInstance metadata: name: users-api labels: secured-by-automated-policy: yes spec: address: http://0.0.0.0:8081 services: users: address: https://<your users URL>:<your port>/ routes: - rules: - path: /api/users(/.*) -
次の
PolicyBinding リソースを ApiInstance リソースの後に追加して、自動基本認証ポリシーを定義します。--- apiVersion: gateway.mulesoft.com/v1alpha1 kind: PolicyBinding metadata: name: security-protection spec: targetRef: kind: Selector selector: kind: ApiInstance labels: secured-by-automated-policy: yes policyRef: name: http-basic-authentication-flex config: username: chris password: admin自動ポリシーを複数の API インスタンスに適用するには、ポリシーバインドで
Selectorkind を指定する必要があります。サンプル PolicyBinding リソースでは、spec.targetRef.kind の値が Selector、spec.targetRef.selector.kind の値が ApiInstance に設定されています。また、サンプルリソースでは基本認証ポリシーを 2 つの API インスタンスに適用しています。また、
secured-by-automated-policy ラベルを spec.targetRef.selector.labels で定義しています (ラベル名はカスタマイズ可能です)。このラベルは、自動ポリシーをラベルのリソースに関連付ける ApiInstancemetadata.labels プロパティで定義されているラベルと一致します。ラベルを省略することで、自動ポリシーをすべての API インスタンスに適用することができます。
-
ファイルを保存します。ゲートウェイによって設定が自動的に更新されます。
-
Docker コンテナログを表示します。内容は次のようになっています。
[agent][info] Generating config [agent][info] Gateway default/21cf4286e38f: Adding ApiInstance default/products-api http://0.0.0.0:8080 [agent][info] Gateway default/21cf4286e38f: Adding Policy default/security-protection [agent][info] Gateway default/21cf4286e38f: Adding Route: &{host: path:/api/products(/.*) methods: headerConditions:[] profile:0xc00004f750} => {Kind:Service Name:products-api-products Namespace:default} [agent][info] Gateway default/21cf4286e38f: Adding Policy default/envoy.filters.http.router [agent][info] Gateway default/21cf4286e38f: Adding ApiInstance default/users-api http://0.0.0.0:8081 [agent][info] Gateway default/21cf4286e38f: Adding Policy default/security-protection [agent][info] Gateway default/21cf4286e38f: Adding Route: &{host: path:/api/users(/.*) methods: headerConditions:[] profile:0xc0061823f0} => {Kind:Service Name:users-api-users Namespace:default} [agent][info] Gateway default/21cf4286e38f: Adding Policy default/envoy.filters.http.router [agent][info] Gateway default/21cf4286e38f: Adding Service default/products-api-products https://<your products URL>:<your port>/ [agent][debug] generating service products-api-products.default.svc hostname: https://<your products URL> port: <your port> [agent][info] Gateway default/21cf4286e38f: Adding Service default/users-api-users https://<your users URL>:<your port>/ [agent][debug] generating service users-api-users.default.svc hostname: https://<your users URL> port: <your port> [agent][info] Writing envoy bootstrap configuration to /tmp/envoy.json [envoy][info] cds: add 2 cluster(s), remove 3 cluster(s) [envoy][info] cds: added/updated 0 cluster(s), skipped 2 unmodified cluster(s) -
curl などのツールで認証ログイン情報を指定して API をテストします。自動認証ポリシーは、両方の API を保護します。 -
PolicyBinding リソースを以下に置き換えることで、リソースレベルの粒度を自動認証ポリシーに適用します。--- apiVersion: gateway.mulesoft.com/v1alpha1 kind: PolicyBinding metadata: name: security-protection spec: targetRef: kind: Selector selector: kind: ApiInstance labels: secured-by-automated-policy: yes policyRef: name: http-basic-authentication-flex config: username: chris password: admin rules: - methods: GETこの例では、
spec.rules で指定されているように、自動認証ポリシーは GET メソッドリソースにのみ適用されます。 -
ファイルを保存します。ゲートウェイによって設定が自動的に更新されます。
自動リソースレベル認証ポリシーで保護された 2 つの API が Flex Gateway の背後で実行されています。

