0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Authorization Management Service (AMS)を利用したCAPの権限管理 - アップデート

Last updated at Posted at 2025-02-20

はじめに

以下の記事で、Authorization Management Service (AMS)を利用した権限管理について説明しました。

上の記事を書いた時点では、CAPでAMSを利用する設定をしてみてもビルドが失敗するという状況でした。その後cds add amsコマンドが利用できるようになり、デプロイまでできるようになったので手順を紹介したいと思います。

ステップ

  1. プロジェクトを作成
  2. UIを追加
  3. デプロイのための設定
  4. AMSを使用する設定
  5. デプロイ
  6. Cloud Identity Servicesでの設定
  7. SAP Build Work Zoneから利用

※cds-dkのバージョン:8.7.2

ソースコードは以下にアップしています。
https://github.com/miyasuta/cap-ams

1. プロジェクトを作成

以下のコマンドでプロジェクトを作成します。

cds init cap-ams
cd cap-ams
cds add tiny-sample //サンプルを追加

生成されたサービスのBooksエンティティから@readonlyを外してドラフトを有効にします。さらに、権限制御を追加します。

using my.bookshop as my from '../db/schema';

@requres: ['Admin', 'Viewer']
service CatalogService {
    @restrict: [{grant: ['READ','WRITE'], to: 'Admin'},
                {grant: ['READ'], to: 'Viewer'}]
    entity Books as projection on my.Books;
}

annotate CatalogService.Books with @odata.draft.enabled;

2. UIを追加

Fiori toolsでList Reportの画面を作成します。
image.png
image.png
image.png

ローカルで実行するため、package.jsonにモックユーザを追加します。

  "cds": {
    "requires": {
      "[development]": {
        "auth": {
          "kind": "mocked",
          "users": {
            "alice": {
              "roles": [
                "Admin"
              ]
            },
            "bob": {
              "roles": [
                "Viewer"
              ]
            }
          }
        }
      }
    }
  }

cds watchを実行し、Adminロールをつけたユーザ"alice"でCRUD処理ができることを確認します。
image.png

3. デプロイのための設定

デプロイのために以下の設定を追加します。

cds add hana --for production
cds add workzone
cds add mta

Cloud Foundryにデプロイするため、app/<uiフォルダ>/webapp/manifest.jsonでODataのuriから先頭のスラッシュを削除します。

    "dataSources": {
      "mainService": {
        "uri": "odata/v4/catalog/",
        "type": "OData",
        "settings": {
          "annotations": [],
          "odataVersion": "4.0"
        }
      }
    }

4. AMSを利用する設定

4.1. AMSの設定を追加

以下のコマンドを実行します。

cds add ams --for production

その結果、package.jsonに3つのdependencyが追加されます。
image.png

また、cds.requiresセクションに以下の設定が追加されます。

  "cds": {
    "requires": {
    ...
      "[production]": {
        "auth": {
          "kind": "ias"
        }
      }

mta.yamlにも設定が追加されます。
image.png
image.png
image.png

4.2. マニュアルでの調整

以下の調整を行います。

①xs-app.json
  • xs-app.jsonに"authenticationType": "ias"を追加
app/uiフォルダ/xs-app.json
{
  "welcomeFile": "/index.html",
  "authenticationMethod": "route",
  "routes": [
    {
      "source": "^/?odata/(.*)$",
      "target": "/odata/$1",
      "destination": "cap-ams-srv-api",
      "csrfProtection": true,
      "authenticationType": "ias"
    },
    {
      "source": "^(.*)$",
      "target": "$1",
      "service": "html5-apps-repo-rt",
      "authenticationType": "ias"
    }
  ]
}
②mta.yaml
  • destinationモジュールから"auth"の設定を削除(cds add workzoneにより自動で追加されるが、IAS認証では使用しない)
    image.png

  • IASのサービスインスタンスにprovided-apisを追加

  - name: cap-ams-auth
    type: org.cloudfoundry.managed-service
    parameters:
      service: identity
      service-name: cap-ams-auth
      service-plan: application
      config:
        display-name: cap-ams
        authorization:
          enabled: true
        # 追加
        provided-apis:
          - name: catalog-api
            description: api exposed by catalog service app  
  • DestinationにプロパティHTML5.IASDependencyNameを追加し、前のステップで指定したAPIの名前を設定
resources:
  - name: cap-ams-destination
    type: org.cloudfoundry.managed-service
    parameters:
      service: destination
      service-plan: lite
      config:
        HTML5Runtime_enabled: true
        init_data:
          instance:
            existing_destinations_policy: update
            destinations:
              - Name: cap-ams-srv-api
                URL: ~{srv-api/srv-url}
                Authentication: NoAuthentication
                Type: HTTP
                ProxyType: Internet
                HTML5.ForwardAuthToken: true
                HTML5.DynamicDestination: true
                # 追加
                HTML5.IASDependencyName: catalog-api

5. デプロイ

以下のコマンドでビルド、デプロイします。

//dependencyをインストール
npm i --prefix app/<uiフォルダ>
npm i
//ビルド、デプロイ
mbt build -t gen --mtar mta.tar
cf deploy gen/mta.tar

※ビルドの結果、amsというフォルダが作成され、中にポリシーが設定されています。
image.png

6. Cloud Identity Servicesでの設定

以下のドキュメントに従って、Cloud Identity Servicesで設定を行います。
https://github.com/SAP-samples/btp-developer-guide-cap/blob/main/documentation/xsuaa-to-ams/3-deploy-to-cf.md#check-your-ias-application-and-uploaded-ams-policies

6.1. ポリシーをユーザに割り当て

登録されたアプリケーションの"Authorization Policies"タブでポリシーが登録されていることを確認します。
image.png

ポリシーを選択してユーザに割り当てます。
image.png

6.2. SAP Build Work ZoneからCAPのAPIを利用するための設定

SAP Build Work ZoneとCAPはそれぞれ別のIASアプリケーションに紐づいています。Work ZoneにログインしたユーザがCAPのAPIを使用する際、ApprouterはAPIを利用する権限をもったトークンをリクエストする必要があります(トークンの交換)。このために以下のステップが必要になります。

  1. プロバイダーアプリケーション(CAP)は、Provided APIを定義する
  2. コンシュマーアプリケーション(SAP Build Work Zone)は、利用するAPIをDependencyとして登録する
  3. SAP Build Work Zoneから利用するDestinationは、IASDependencyNameというプロパティに利用するAPIを指定する
  4. SAP Build Work ZoneからAPIにアクセスする際、ApprouterはDestinationに設定されたIASDependencyNameを利用してトークンの交換を行う

image.png

以下で設定を見ていきます。

①CAPのIASアプリケーション

デプロイ時に"Provided API"が登録されています。
image.png

Trust -> OpenID Connect Configuration -> Advanced Settings -> Access Token Formatより、JSON Web Tokenを選択します。
image.png

②SAP Build Work ZoneのIASアプリケーション

DependencyにCAPのProviced APIで定義したAPIを選択します。
image.png

③Destination

デプロイ時に"IASDependencyName"が設定されています。
image.png

7. SAP Build Work Zoneから利用

SAP Build Work Zoneにタイルを配置し、アプリケーションを起動します。
image.png
アプリケーションにアクセスできました。
image.png

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?