---
schema_version: "1"
id: 3ddb5ffd0886cb211860827ed3dedc716d37a70f747e6ca98d21f95643b8b61c
title: リソースレベルの自動ポリシーを使用した API の保護
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/gateway/1.8/flex-local-secure-api-with-auto-policy.md
canonical_url: https://docs.mulesoft.com/jp/gateway/1.8/flex-local-secure-api-with-auto-policy
source_site: docs.mulesoft.com
content_type: documentation
content_hash: d80a2fdb68a64755463b274cf71de899a11065d155f91df7db8a433e6a2fe565
description: MuleSoft Documentation Site
release: "1.8"
latest: false
---
# リソースレベルの自動ポリシーを使用した API の保護

ローカルモードで Flex Gateway の背後で実行されている API をパブリッシュすることで、YAML 設定データを変更する方法について説明します。

- Linux および Docker: `.yaml`​ ファイル

下記の手順は、自動リソースレベル認証ポリシーによって保護されている複数のアップストリームサービスを使用した API のシンプルなリソースベースの YAML 設定を適用する方法を示しています。

詳細は、​[「自動ポリシー」](https://docs.mulesoft.com/jp/gateway/1.8/policies-automated-overview)​と​[「リソースレベルのポリシー」](https://docs.mulesoft.com/jp/gateway/1.8/policies-resource-level-overview)​を参照してください。

<table>
<tbody><tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
<div class="paragraph">
<p>自動ポリシーは、リソースベースの設定でのみ定義できます。インライン設定モデルはサポートされません。リソースベースとインラインの設定モデルの違いについては、​<a href="https://docs.mulesoft.com/jp/gateway/1.8/flex-local-configuration-reference-guide" class="xref page">「宣言型設定リファレンスガイド」</a>​を参照してください。</p>
</div>
</td>
</tr>
</tbody></table>

<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 50%;">
<col style="width: 50%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><span class="image"><a class="image" href="#publish-an-api-running-behind-flex-gateway-on-linux"><img src="https://docs.mulesoft.com/jp/gateway/1.8/_images/install-linux-logo.png" alt="20%" width="20%"></a></span></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><span class="image"><a class="image" href="#publish-an-api-running-behind-flex-gateway-in-a-docker-container"><img src="https://docs.mulesoft.com/jp/gateway/1.8/_images/install-docker-logo.png" alt="25%" width="25%"></a></span></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><a href="#publish-an-api-running-behind-flex-gateway-on-linux">Linux で Flex Gateway の背後で実行されている API のパブリッシュ</a>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><a href="#publish-an-api-running-behind-flex-gateway-in-a-docker-container">Docker コンテナで Flex Gateway の背後で実行されている API のパブリッシュ</a>​</p></td>
</tr>
</tbody>
</table>

## [](#linux-で-flex-gateway-の背後で実行されている-api-のパブリッシュ)Linux で Flex Gateway の背後で実行されている API のパブリッシュ

### [](#始める前に)始める前に

始める前に、次の準備が整っていることを確認します。

- Flex Gateway がインストールされていてローカルモードで実行されている。ゲートウェイのインストールと実行についての詳細は、[Flex Gateway のダウンロード](https://docs.mulesoft.com/jp/gateway/1.8/flex-install)を参照してください。
- アップストリームサービスの URL が用意されている。次の例では架空の ​`products-api`​ と ​`users-api`​ というサービスを使用していますが、​`metadata.name`​ には実際の API 名、​`spec.services`​ にはサービスの詳細を指定することができます。

### [](#api-のパブリッシュ)API のパブリッシュ

1. `.yaml`​ ファイル拡張子で設定ファイルを作成します。
    
    1. ファイルにカスタム名を付けます。
    2. ファイルを Flex Gateway の設定ディレクトリ ​`/etc/mulesoft/flex-gateway/conf.d/custom`​ に保存します。このディレクトリには複数の設定ファイルを保存できます。
2. 次の 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(/.\*)
3. 次の ​`PolicyBinding`​ リソースを ​`ApiInstance`​ リソースの後に追加して、自動​[基本認証ポリシー](https://docs.mulesoft.com/jp/gateway/1.8/policies-included-basic-auth-simple)​を定義します。
    
    \---
    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 インスタンスに適用するには、ポリシーバインドで ​`Selector` ​`kind`​ を指定する必要があります。サンプル ​`PolicyBinding`​ リソースでは、​`spec.targetRef.kind`​ の値が ​`Selector`​、​`spec.targetRef.selector.kind`​ の値が ​`ApiInstance`​ に設定されています。
    
    また、サンプルリソースでは基本認証ポリシーを 2 つの API インスタンスに適用しています。また、​`secured-by-automated-policy`​ ラベルを ​`spec.targetRef.selector.labels`​ で定義しています (ラベル名はカスタマイズ可能です)。このラベルは、自動ポリシーをラベルのリソースに関連付ける ​`ApiInstance` ​`metadata.labels`​ プロパティで定義されているラベルと一致します。
    
    ラベルを省略することで、自動ポリシーをすべての API インスタンスに適用することができます。
4. ファイルを保存します。ゲートウェイによって設定が自動的に更新されます。
5. 次のコマンドを実行してログを表示します。
    
    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)
6. `curl`​ などのツールで認証ログイン情報を指定して API をテストします。自動認証ポリシーは、両方の API を保護します。
7. `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`​ メソッドリソースにのみ適用されます。
8. ファイルを保存します。ゲートウェイによって設定が自動的に更新されます。

自動リソースレベル認証ポリシーで保護された 2 つの API が Flex Gateway の背後で実行されています。

## [](#docker-コンテナで-flex-gateway-の背後で実行されている-api-のパブリッシュ)Docker コンテナで Flex Gateway の背後で実行されている API のパブリッシュ

### [](#始める前に-2)始める前に

始める前に、次の準備が整っていることを確認します。

- Flex Gateway がインストールされている。詳細は、[Flex Gateway のダウンロード](https://docs.mulesoft.com/jp/gateway/1.8/flex-install)を参照してください。
- Flex Gateway がローカルモードで登録されて実行されている。詳細は​[「ローカルモードでの登録と実行」](https://docs.mulesoft.com/jp/gateway/1.8/flex-local-reg-run)​を参照してください。
- アップストリームサービスの URL が用意されている。次の例では架空の ​`products-api`​ と ​`users-api`​ というサービスを使用していますが、​`metadata.name`​ には実際の API 名、​`spec.services`​ にはサービスの詳細を指定することができます。

### [](#api-のパブリッシュ-2)API のパブリッシュ

1. ターミナルを開いて、Flex Gateway 設定ファイルが保存されているディレクトリに移動します。このディレクトリは、Flex Gateway を起動したときに指定されています。
2. `.yaml`​ ファイル拡張子で設定ファイルを作成します。
    
    1. ファイルにカスタム名を付けます。
    2. ファイルを保存します。
3. 次の 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(/.\*)
4. 次の ​`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 インスタンスに適用するには、ポリシーバインドで ​`Selector` ​`kind`​ を指定する必要があります。サンプル ​`PolicyBinding`​ リソースでは、​`spec.targetRef.kind`​ の値が ​`Selector`​、​`spec.targetRef.selector.kind`​ の値が ​`ApiInstance`​ に設定されています。
    
    また、サンプルリソースでは基本認証ポリシーを 2 つの API インスタンスに適用しています。また、​`secured-by-automated-policy`​ ラベルを ​`spec.targetRef.selector.labels`​ で定義しています (ラベル名はカスタマイズ可能です)。このラベルは、自動ポリシーをラベルのリソースに関連付ける ​`ApiInstance` ​`metadata.labels`​ プロパティで定義されているラベルと一致します。
    
    ラベルを省略することで、自動ポリシーをすべての API インスタンスに適用することができます。
5. ファイルを保存します。ゲートウェイによって設定が自動的に更新されます。
6. 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)
7. `curl`​ などのツールで認証ログイン情報を指定して API をテストします。自動認証ポリシーは、両方の API を保護します。
8. `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`​ メソッドリソースにのみ適用されます。
9. ファイルを保存します。ゲートウェイによって設定が自動的に更新されます。

自動リソースレベル認証ポリシーで保護された 2 つの API が Flex Gateway の背後で実行されています。

## [](#関連情報)関連情報

- [内部ポリシーディレクトリ](https://docs.mulesoft.com/jp/gateway/1.8/policies-included-directory)