この記事は公開されてから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ファイルを作成します。import-config.json
としました。
import-config.json
{
"AUTH0_DOMAIN": "<YOUR_TENANT>.auth0.com",
"AUTH0_CLIENT_ID": "<client_id>",
"AUTH0_CLIENT_SECRET": "<client_secret>",
"AUTH0_ALLOW_DELETE": false
}
インポート用の設定ファイル(YAML)の作成
インポート用の設定ファイルをYAMLで作ります。
YAMLの構成はこちらで紹介されていますので、そのテンプレートに日本語解説コメントを加えてみました。
# テナント設定
tenant:
# テナントの表示名
friendly_name: 'Auth0 Deploy Example'
# Universal LoginのHTMLファイル
pages:
# ログイン
- name: "login"
html: "pages/login.html"
# パスワードリセット
- name: "password_reset"
html: "pages/password_reset.html"
# MFA
- name: "guardian_multifactor"
html: "pages/guardian_multifactor.html"
enabled: false
# エラーページ
- name: "error_page"
html: "pages/error_page.html"
# クライアント(Application)
clients:
# SPAの場合
- name: "My SPA"
app_type: "spa"
# M2Mの場合
- name: "My M2M"
app_type: "non_interactive"
# Database設定
databases:
- name: "users"
# 有効にするクライアントを羅列する
enabled_clients:
- "My SPA"
# Custom Databaseを使う場合
options:
enabledDatabaseCustomization: true
customScripts:
login: "databases/users/login.js"
create: "databases/users/create.js"
delete: "databases/users/delete.js"
get_user: "databases/users/get_user.js"
change_email: "databases/users/change_email.js"
change_password: "databases/users/change_password.js"
verify: "databases/users/verify.js"
# Connection設定(ソーシャルコネクションなど)
connections:
# Rule設定
rules:
- name: "Common-Functions"
order: 10
script: "rules/enrich_tokens.js"
# Ruleで使える環境変数
rulesConfigs:
# Key/Value pairs for Rule configuration settings
- key: "SOME_SECRET"
value: 'some_key'
# リソースサーバーの定義
resourceServers:
- name: "My API"
identifier: "https://##ENV##.myapp.com/api/v1"
scopes:
- value: "update:account"
description: "update account"
- value: "read:account"
description: "read account"
# Add other resource server settings (https://auth0.com/docs/api/management/v2#!/Resource_Servers/post_resource_servers)
# Emailプロバイダ設定
emailProvider:
name: "smtp"
enabled: true
credentials:
smtp_host: "smtp.mailtrap.io"
smtp_port: 2525
smtp_user: "smtp_user"
smtp_pass: "smtp_secret_password"
# Emailのテンプレート
emailTemplates:
# 承認メール
- template: "verify_email"
enabled: true
syntax: "liquid"
from: "test@email.com"
subject: "something"
body: "emails/change_email.html"
# ようこそメール
- template: "welcome_email"
enabled: true
syntax: "liquid"
from: "test@email.com"
subject: "something"
body: "emails/change_email.html"
# クライアントに対する認可(認可機能を使う場合)
clientGrants:
- client_id: "My M2M"
audience: "https://##ENV##.myapp.com/api/v1"
scope:
- "update:account"
# MFA設定
guardianFactors:
# SMS(電話番号)
- name: sms
enabled: true
# プッシュ通知
- name: push-notification
enabled: true
# One Time Password(Authy, Google Authenticator, 1Passwordなど)
- name: otp
enabled: true
# Email
- name: email
enabled: false
# Duo
- name: duo
enabled: false
# MFAのプロバイダ設定
guardianFactorProviders:
# SMS(Twilio)の場合
- name: sms
provider: twilio
auth_token: "some_token"
sid: "some_sid"
messaging_service_sid: "some_message_sid"
# MFAのテンプレート設定
guardianFactorTemplates:
# SMSの場合
- name: sms
enrollment_message: >-
{{code}} is your verification code for {{tenant.friendly_name}}. Please
enter this code to verify your enrollment.
verification_message: '{{code}} is your verification code for {{tenant.friendly_name}}'
この設定値、およびエクスポートで確認できた設定値を元にシンプルな設定を作成してみました。SPAでログインできるようにするだけのシンプルな構成です。
tenant.yaml
tenant:
friendly_name: 'Auth0 Deploy Example'
enabled_locales:
- ja
clients:
- name: My App
app_type: spa
allowed_logout_urls:
- 'http://localhost:3000'
callbacks:
- 'http://localhost:3000/callback'
web_origins:
- 'http://localhost:3000'
custom_login_page_on: true
grant_types:
- authorization_code
- implicit
- refresh_token
jwt_configuration:
alg: RS256
lifetime_in_seconds: 36000
secret_encoded: false
databases:
- name: Username-Password-Authentication
strategy: auth0
enabled_clients:
- My App
options:
mfa:
active: true
return_enroll_settings: true
passwordPolicy: good
brute_force_protection: true
strategy_version: 2
realms:
- Username-Password-Authentication
connections:
- name: google-oauth2
strategy: google-oauth2
enabled_clients:
- My App
options:
email: true
profile: true
scope:
- email
- profile
client_id: ''
client_secret: ''
allowed_audiences: ''
次のコマンドでインポートを実行します。
$ a0deploy import -c import-config.json -i import/tenant.yaml
Applicationの設定を確認すると、正しく設定できていることが確認できます。
インポート前の設定を消したい場合
インポート前の設定を消してしまいたい場合は import-config.json
の AUTH0_ALLOW_DELETE
を true
に設定しましょう。ただし、書き漏れなどがあると消えてしまうのでご注意ください。
テナントは設定ファイルで管理しよう
テナント設定をファイルで管理することには、運用上様々なメリットがあります。
例えば、YAMLの設定ファイルをGitリポジトリで管理することで、テナント設定のCD(継続的デプロイ)環境を作ることもできます。
テナント設定は是非ともファイルで管理するようにしましょう!