Auth0 Deploy CLIが使用する必要十分な権限を付与したM2Mアプリを簡単に作成する方法

2021.06.26

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは、CX事業本部の若槻です。

認証管理プラットフォームAuth0では、テナントの管理をCLIで実施することができるAuth0 Deploy CLIが公式提供されています。

これにより、Auth0テナントの設定情報をyamlファイルで管理することができ、また開発/検証/本番のように用途ごと用意した複数のAuth0テナントに対するCI/CDフローを実装することが可能となります。

さて、この Auth0 Deploy CLI を利用する際には、CLIがテナントの設定を更新するためのM2Mアプリ(Machine to Machine Application)を同テナント上に作成する必要があります。手順は下記ドキュメントに示されています。

上記ドキュメントでは、作成するM2Mアプリへ付与する必要がある権限(Permission)の一覧が示されていますが、その数が60個近くあるため、GUIからそれらの権限を一つずつ選択してアプリに付与するのはとても根気がいる作業となります。選択可能な権限を一括ですべて付与する操作であれば難なく出来るのですが、その場合は必要以上の権限がアプリ割り当てられることになってしまいます。

そこで、Auth0 Deploy CLI が使用する必要十分な権限を付与したM2Mアプリを簡単に作成する方法を考えてみました。

やってみた

以下の要領で必要十分な権限を付与したM2Mアプリを作成してみます。

  1. GUIを使用してすべて(必要以上)の権限を付与したM2MアプリTemp Deploy CLI Appを一時的に作成
  2. Temp Deploy CLI Appをデプロイに使用するように Deploy CLI Toolを設定
  3. Deploy CLI Tool を使用して必要十分な権限を付与したM2MアプリDeploy CLI Appを作成
  4. Deploy CLI Appをデプロイに使用するように Deploy CLI Toolを設定
  5. Temp Deploy CLI Appを削除

Deploy CLI Tool のインストール

未導入の場合はインストールします。

% npm i -g auth0-deploy-cli

Temp Deploy CLI App の作成

1.に対応する手順です。

1. GUIを使用してすべて(必要以上)の権限を付与したM2MアプリTemp Deploy CLI Appを一時的に作成

Auth0 Dashboard > Applications > Applicationsへ移動し、[Create Application]をクリックします。

アプリ名Temp Deploy CLI Appを指定し、アプリケーションタイプでMachine to Machine Applicationsを選択し、[Create]をクリックします。

APIの指定を要求されるので、テナント上に既定で作成されているAuth0 Management APIを選択します。

Permissionの選択で[All]をクリックして全選択とし、[Authorize]をクリックします。(ここで必要十分な権限のみを選択するのが辛いのです)

すべて(必要以上)の権限を付与したアプリが作成できました。[Settings]タブで「Domain」、「Client ID」、「Client Secret」の値を控えます。

Deploy CLI App の作成

2.および3.に対応する手順です。

2. Temp Deploy CLI Appをデプロイに使用するように Deploy CLI Toolを設定
3. Deploy CLI Tool を使用して必要十分な権限を付与したM2MアプリDeploy CLI Appを作成

下記のconfig.jsonファイルを作成し、AUTH0_DOMAINAUTH0_CLIENT_IDAUTH0_CLIENT_SECRETおよびAUTH0_KEYWORD_REPLACE_MAPPINGS.AUTH0_DOMAINの値に先ほど控えた一時的なM2Mアプリの情報をそれぞれ指定します。

config.json

{
  "AUTH0_DOMAIN": "YOUR_DOMAIN",
  "AUTH0_CLIENT_ID": "YOUR_CLIENT_ID",
  "AUTH0_CLIENT_SECRET": "YOUR_CLIENT_SECRET",
  "AUTH0_KEYWORD_REPLACE_MAPPINGS": {
    "AUTH0_DOMAIN": "YOUR_DOMAIN"
  },
  "AUTH0_ALLOW_DELETE": true,
  "AUTH0_EXCLUDED_RULES": []
}

下記コマンドを実行して現在のテナントの設定をエクスポートします。

% a0deploy export -c config.json -f yaml -o ./src

テナントの現在の設定情報がyamlファイルで下記のように出力されます。

src/tenant.yaml

rules: []
rulesConfigs: []
hooks: []
pages: []
resourceServers: []
clients:
  - name: Temp Deploy CLI App
    app_type: non_interactive
    cross_origin_auth: false
    custom_login_page_on: true
    grant_types:
      - client_credentials
    is_first_party: true
    is_token_endpoint_ip_header_trusted: false
    jwt_configuration:
      alg: RS256
      lifetime_in_seconds: 36000
      secret_encoded: false
    oidc_conformant: true
    refresh_token:
      expiration_type: non-expiring
      leeway: 0
      infinite_token_lifetime: true
      infinite_idle_token_lifetime: true
      token_lifetime: 31557600
      idle_token_lifetime: 2592000
      rotation_type: non-rotating
    sso_disabled: false
    token_endpoint_auth_method: client_secret_post
databases:
  - name: Username-Password-Authentication
    strategy: auth0
    enabled_clients:
      - Temp Deploy CLI App
    is_domain_connection: false
    options:
      mfa:
        active: true
        return_enroll_settings: true
      passwordPolicy: good
      strategy_version: 2
      brute_force_protection: true
    realms:
      - Username-Password-Authentication
connections:
  - name: google-oauth2
    strategy: google-oauth2
    enabled_clients: []
    is_domain_connection: false
    options:
      email: true
      scope:
        - email
        - profile
      profile: true
tenant:
  enabled_locales:
    - en
  flags:
    new_universal_login_experience_enabled: true
    universal_login: true
    revoke_refresh_token_grant: false
    disable_clickjack_protection_headers: false
emailProvider: {}
emailTemplates: []
clientGrants: []
guardianFactors:
  - name: duo
    enabled: false
  - name: email
    enabled: false
  - name: otp
    enabled: false
  - name: push-notification
    enabled: false
  - name: recovery-code
    enabled: false
  - name: sms
    enabled: false
  - name: webauthn-platform
    enabled: false
  - name: webauthn-roaming
    enabled: false
guardianFactorProviders: []
guardianFactorTemplates: []
guardianPolicies:
  policies: []
guardianPhoneFactorSelectedProvider:
  provider: auth0
guardianPhoneFactorMessageTypes:
  message_types: []
roles: []
branding: {}
prompts:
  universal_login_experience: new
migrations: {}

上記yamlファイルのclientsclientGrantsに、Deploy CLI App の情報を追記します。

src/tenant.yaml

rules: []
rulesConfigs: []
hooks: []
pages: []
resourceServers: []
clients:
  - name: Temp Deploy CLI App
    app_type: non_interactive
    cross_origin_auth: false
    custom_login_page_on: true
    grant_types:
      - client_credentials
    is_first_party: true
    is_token_endpoint_ip_header_trusted: false
    jwt_configuration:
      alg: RS256
      lifetime_in_seconds: 36000
      secret_encoded: false
    oidc_conformant: true
    refresh_token:
      expiration_type: non-expiring
      leeway: 0
      infinite_token_lifetime: true
      infinite_idle_token_lifetime: true
      token_lifetime: 31557600
      idle_token_lifetime: 2592000
      rotation_type: non-rotating
    sso_disabled: false
    token_endpoint_auth_method: client_secret_post
  # Deploy CLI App の定義を追加
  - name: Deploy CLI App
    app_type: non_interactive
    cross_origin_auth: false
    custom_login_page_on: true
    grant_types:
      - client_credentials
    is_first_party: true
    is_token_endpoint_ip_header_trusted: false
    jwt_configuration:
      alg: RS256
      lifetime_in_seconds: 36000
      secret_encoded: false
    oidc_conformant: true
    refresh_token:
      expiration_type: non-expiring
      leeway: 0
      infinite_token_lifetime: true
      infinite_idle_token_lifetime: true
      token_lifetime: 31557600
      idle_token_lifetime: 2592000
      rotation_type: non-rotating
    sso_disabled: false
    token_endpoint_auth_method: client_secret_post
databases:
  - name: Username-Password-Authentication
    strategy: auth0
    enabled_clients:
      - Temp Deploy CLI App
    is_domain_connection: false
    options:
      mfa:
        active: true
        return_enroll_settings: true
      passwordPolicy: good
      strategy_version: 2
      brute_force_protection: true
    realms:
      - Username-Password-Authentication
connections:
  - name: google-oauth2
    strategy: google-oauth2
    enabled_clients: []
    is_domain_connection: false
    options:
      email: true
      scope:
        - email
        - profile
      profile: true
tenant:
  enabled_locales:
    - en
  flags:
    new_universal_login_experience_enabled: true
    universal_login: true
    revoke_refresh_token_grant: false
    disable_clickjack_protection_headers: false
emailProvider: {}
emailTemplates: []
clientGrants: 
  # Deploy CLI App が使用する必要十分な権限の定義を追加
  - client_id: Deploy CLI App
    audience: https://##AUTH0_DOMAIN##/api/v2/
    scope:
      - read:client_grants
      - create:client_grants
      - delete:client_grants
      - update:client_grants
      - read:clients
      - update:clients
      - delete:clients
      - create:clients
      - read:client_keys
      - update:client_keys
      - delete:client_keys
      - create:client_keys
      - read:connections
      - update:connections
      - delete:connections
      - create:connections
      - read:resource_servers
      - update:resource_servers
      - delete:resource_servers
      - create:resource_servers
      - read:rules
      - update:rules
      - delete:rules
      - create:rules
      - read:hooks
      - update:hooks
      - delete:hooks
      - create:hooks
      - read:rules_configs
      - update:rules_configs
      - delete:rules_configs
      - read:email_provider
      - update:email_provider
      - delete:email_provider
      - create:email_provider
      - read:tenant_settings
      - update:tenant_settings
      - read:grants
      - delete:grants
      - read:guardian_factors
      - update:guardian_factors
      - read:mfa_policies
      - update:mfa_policies
      - read:email_templates
      - create:email_templates
      - update:email_templates
      - read:roles
      - update:roles
      - delete:roles
      - create:roles
      - read:prompts
      - update:prompts
      - read:branding
      - update:branding
guardianFactors:
  - name: duo
    enabled: false
  - name: email
    enabled: false
  - name: otp
    enabled: false
  - name: push-notification
    enabled: false
  - name: recovery-code
    enabled: false
  - name: sms
    enabled: false
  - name: webauthn-platform
    enabled: false
  - name: webauthn-roaming
    enabled: false
guardianFactorProviders: []
guardianFactorTemplates: []
guardianPolicies:
  policies: []
guardianPhoneFactorSelectedProvider:
  provider: auth0
guardianPhoneFactorMessageTypes:
  message_types: []
roles: []
branding: {}
prompts:
  universal_login_experience: new
migrations: {}

更新したtenant.yamlをインポートします。

% a0deploy import -c config.json -i ./src/tenant.yaml

Deploy CLI App がちゃんと作成されていますね。

Deploy CLI Tool が使用するアプリを Deploy CLI App に切り替える

4.および5.に対応する手順です。

4. Deploy CLI Appをデプロイに使用するように Deploy CLI Toolを設定
5. Temp Deploy CLI Appを削除

作成された「Deploy CLI App」の「Client ID」と「Client Secret」の値を控えます。

控えた Deploy CLI App の情報でconfig.jsonを更新します。

config.json

{
  "AUTH0_DOMAIN": "YOUR_DOMAIN",
  "AUTH0_CLIENT_ID": "YOUR_CLIENT_ID", //更新
  "AUTH0_CLIENT_SECRET": "YOUR_CLIENT_SECRET", //更新
  "AUTH0_KEYWORD_REPLACE_MAPPINGS": {
    "AUTH0_DOMAIN": "YOUR_DOMAIN"
  },
  "AUTH0_ALLOW_DELETE": true,
  "AUTH0_EXCLUDED_RULES": []
}

tenant.yamlを下記のように更新します。

src/tenant.yaml

rules: []
rulesConfigs: []
hooks: []
pages: []
resourceServers: []
clients:
  # Temp Deploy CLI App の定義を削除
  - name: Deploy CLI App
    app_type: non_interactive
    cross_origin_auth: false
    custom_login_page_on: true
    grant_types:
      - client_credentials
    is_first_party: true
    is_token_endpoint_ip_header_trusted: false
    jwt_configuration:
      alg: RS256
      lifetime_in_seconds: 36000
      secret_encoded: false
    oidc_conformant: true
    refresh_token:
      expiration_type: non-expiring
      leeway: 0
      infinite_token_lifetime: true
      infinite_idle_token_lifetime: true
      token_lifetime: 31557600
      idle_token_lifetime: 2592000
      rotation_type: non-rotating
    sso_disabled: false
    token_endpoint_auth_method: client_secret_post
databases:
  - name: Username-Password-Authentication
    strategy: auth0
    enabled_clients:
      - Deploy CLI App # Deploy CLI Appに変更
    is_domain_connection: false
    options:
      mfa:
        active: true
        return_enroll_settings: true
      passwordPolicy: good
      strategy_version: 2
      brute_force_protection: true
    realms:
      - Username-Password-Authentication
connections:
  - name: google-oauth2
    strategy: google-oauth2
    enabled_clients: [] # Deploy CLI App の指定を削除
    is_domain_connection: false
    options:
      email: true
      scope:
        - email
        - profile
      profile: true
tenant:
  enabled_locales:
    - en
  flags:
    new_universal_login_experience_enabled: true
    universal_login: true
    revoke_refresh_token_grant: false
    disable_clickjack_protection_headers: false
emailProvider: {}
emailTemplates: []
clientGrants: [] # Deploy CLI App の権限付与の定義を削除
guardianFactors:
  - name: duo
    enabled: false
  - name: email
    enabled: false
  - name: otp
    enabled: false
  - name: push-notification
    enabled: false
  - name: recovery-code
    enabled: false
  - name: sms
    enabled: false
  - name: webauthn-platform
    enabled: false
  - name: webauthn-roaming
    enabled: false
guardianFactorProviders: []
guardianFactorTemplates: []
guardianPolicies:
  policies: []
guardianPhoneFactorSelectedProvider:
  provider: auth0
guardianPhoneFactorMessageTypes:
  message_types: []
roles: []
branding: {}
prompts:
  universal_login_experience: new
migrations: {}

更新したtenant.yamlを再度インポートします。

% a0deploy import -c config.json -i ./src/tenant.yaml

アプリケーション一覧から Temp Deploy CLI App が削除され、Deploy CLI App のみ残りました。

Deploy CLI APP の[APIs]タブでAuth0 Managemant APIの[Premissions]を見てみると、先ほどyamlで指定した必要最低限の権限のみが割り当たっています。

これで Auth0 Deploy CLI が使用する必要十分な権限を付与したM2Mアプリを作成できました。あとはこのtenant.yamlを使用して他のテナント設定を行ったり、CI/CDにそのまま使用したりすれば良さげですね。

補足

アプリが自分自身の権限を変更することはできなかった

Temp Deploy CLI App を作成せずに、いきなり全ての権限を持つ Deploy CLI App を作成し、 Deploy CLI App 自身によるデプロイで Deploy CLI App の権限を変更できるかどうか試してみましたが出来ませんでした。アプリが自分自身の権限を変更することは出来ないようです。

参考

以上