Auth0の設定をYAMLファイルとしてエクスポートする #Auth0JP

Auth0にはテナントの設定をインポート/エクスポートできる機能があります。この機能を使うことでYAMLファイルとしてインポートして取り込んだり、現在の設定をエクスポートしたりすることができます。
2019.09.18

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

Auth0の設定はインポート/エクスポートできる

Auth0を使い始める際、各種設定はダッシュボード上で操作すると思います。しかし実際の運用を考えると、AWSで言うところのCloudFormationのように設定ファイルで管理したいですよね。

Auth0にはテナントの設定をインポート/エクスポートできる機能があります。この機能を使うことでYAMLファイルとしてインポートして取り込んだり、現在の設定をエクスポートしたりすることができます。また、設定したRuleはそれぞれJavaScriptファイルとして書き出されたりと、ほとんどの設定をエクスポートでき大変便利です。

ということで今回はまずエクスポートを試していきたいと思います。

auth0-deploy-cli

インポート/エクスポートには auth0-deploy-cli を使います。

$ yarn global add auth0-deploy-cli

エクスポート実行用のApplicationの作成

auth0-deploy-cli は、実行する際にテナントの設定を読み取る必要があります。そこでAuth0のManagement APIにアクセス可能なApplicationを作成します。

「Machine to Machine Applications」を選択します。

「Scope」は「All」にします。

テナント設定ファイル(JSON)の作成

まず auth0-deploy-cli の実行先となるテナントを指定するため、JSONファイルを作成します。export-config.json としました。

export-config.json

{
  "AUTH0_DOMAIN": "<YOUR_TENANT>.auth0.com",
  "AUTH0_CLIENT_ID": "<client_id>",
  "AUTH0_CLIENT_SECRET": "<client_secret>",
  "AUTH0_ALLOW_DELETE": false
}

実行

それではエクスポートします。以下のコマンドを実行します。

$ a0deploy export -c export-config.json -f yaml -o exported

最後の exported はエクスポート先のディレクトリです。

以下のような構成でエクスポートできました。

.
├── connections
│   └── email.html
├── databases
│   └── Custom
│       ├── create.js
│       ├── get_user.js
│       └── login.js
├── rules
│   └── Email\ domain\ whitelist.js
└── tenant.yaml

rules のほか、databases などカスタムスクリプトも .js ファイルとして書き出されていることがわかります。

tenant.yaml は下記のようになっています(長いので一部省略)。

emailProvider: {}
rules:
  - name: Email domain whitelist
    script: ./rules/Email domain whitelist.js
    stage: login_success
    enabled: true
    order: 1
pages: []
resourceServers:
  - name: Example
    identifier: 'https://classmethod.jp'
    allow_offline_access: false
    enforce_policies: true
    scopes:
      - value: 'read:example'
        description: 'read:example'
    signing_alg: RS256
    skip_consent_for_verifiable_first_party_clients: true
    token_dialect: access_token_authz
    token_lifetime: 86400
    token_lifetime_for_web: 7200
clients:
  - name: API Explorer Application
    app_type: non_interactive
    cross_origin_auth: false
    custom_login_page_on: true
    grant_types:
      - authorization_code
      - implicit
      - refresh_token
      - 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
    sso_disabled: false
  - name: Import/Export
    app_type: non_interactive
    cross_origin_auth: false
    custom_login_page_on: true
    grant_types:
      - authorization_code
      - implicit
      - refresh_token
      - 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
    sso_disabled: false
  - name: My App
    allowed_clients: []
    allowed_logout_urls:
      - 'http://localhost:3000'
    app_type: spa
    callbacks:
      - 'http://localhost:3000'
    client_aliases: []
    cross_origin_auth: false
    custom_login_page_on: true
    grant_types:
      - authorization_code
      - implicit
      - refresh_token
    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
    sso_disabled: false
    token_endpoint_auth_method: none
    web_origins:
      - 'http://localhost:3000'
databases:
  - name: Username-Password-Authentication
    strategy: auth0
    enabled_clients:
      - My App
    is_domain_connection: false
    options:
      mfa:
        active: true
        return_enroll_settings: true
      passwordPolicy: good
      brute_force_protection: true
      strategy_version: 2
    realms:
      - Username-Password-Authentication
connections:
  - name: email
    strategy: email
    enabled_clients: []
    is_domain_connection: false
    options:
      disable_signup: false
      name: email
      email:
        syntax: liquid
        from: ''
        subject: 'Welcome to {{ application.name }}'
        body: ./email.html
      authParams: ''
      totp:
        time_step: 300
        length: 6
      brute_force_protection: true
  - name: facebook
    strategy: facebook
    enabled_clients: []
    is_domain_connection: false
    options:
      client_id: 'xxx'
      client_secret: 'xxx'
      email: false
      groups_access_member_info: false
      publish_to_groups: false
      user_birthday: false
      public_profile: true
      user_age_range: false
      user_events: false
      user_friends: false
      user_gender: false
      user_hometown: false
      user_likes: false
      user_link: false
      user_location: false
      user_photos: false
      user_posts: false
      user_tagged_places: false
      user_videos: false
      ads_management: false
      ads_read: false
      business_management: false
      leads_retrieval: false
      manage_pages: false
      pages_manage_cta: false
      pages_manage_instant_articles: false
      pages_show_list: false
      pages_messaging: false
      pages_messaging_phone_number: false
      pages_messaging_subscriptions: false
      publish_pages: false
      publish_video: false
      read_audience_network_insights: false
      read_insights: false
      read_page_mailboxes: false
      manage_notifications: false
      publish_actions: false
      read_stream: false
      read_mailbox: false
      user_groups: false
      user_managed_groups: false
      user_status: false
      allow_context_profile_field: false
      scope: public_profile
  - name: google-oauth2
    strategy: google-oauth2
    enabled_clients:
      - My App
    is_domain_connection: false
    options:
      email: true
      profile: true
      scope:
        - email
        - profile
      client_id: ''
      client_secret: ''
      allowed_audiences: ''
      contacts: false
      blogger: false
      calendar: false
      gmail: false
      google_plus: false
      orkut: false
      picasa_web: false
      tasks: false
      youtube: false
      adsense_management: false
      google_affiliate_network: false
      analytics: false
      google_books: false
      google_cloud_storage: false
      content_api_for_shopping: false
      chrome_web_store: false
      document_list: false
      google_drive: false
      google_drive_files: false
      latitude_best: false
      latitude_city: false
      moderator: false
      sites: false
      spreadsheets: false
      url_shortener: false
      webmaster_tools: false
      coordinate: false
      coordinate_readonly: false
  - name: line
    strategy: line
    enabled_clients:
      - My App
      - Import/Export
    is_domain_connection: false
    options:
      client_id: 'xxx'
      client_secret: 'xxx'
      email: true
      profile: true
      scope:
        - openid
        - email
        - profile
tenant:
  enabled_locales:
    - ja
  flags:
    universal_login: true
    disable_clickjack_protection_headers: true
    enable_public_signup_user_exists_error: true
  friendly_name: Demo Tenant
  picture_url: ''
  support_email: ''
  support_url: ''
  universal_login:
    colors:
      primary: '#0059d6'
      page_background: '#000000'
rulesConfigs: []
emailTemplates: []
clientGrants:
  - client_id: API Explorer Application
    audience: 'https://dev-cw0xum-z.auth0.com/api/v2/'
    scope:
      - 'read:client_grants'
      - ...
guardianFactors:
  - name: duo
    enabled: false
  - name: email
    enabled: false
  - name: otp
    enabled: false
  - name: push-notification
    enabled: false
  - name: sms
    enabled: false
guardianFactorProviders: []
guardianFactorTemplates: []
roles:
  - name: example-role
    description: Example
    permissions: []
branding: {}
prompts: {}

ApplicationやConnectionの設定情報が適切に書き出されていることが分かりますね。

テナント移行の際はまずエクスポートを

インポート/エクスポートを活用すると、テナントの移行や複製などをスムーズに行うことができます。

はじめにDashboardで管理していた場合も、エクスポート機能を使うことで設定ファイルで管理する方式に後から切り替えることができます。