使用Terraform来管理Auth0

虽然Auth0非常方便,但由于所谓的基础设施即代码无法立即实现,因此我们进行了备忘录记录。

把 auth0 provider 添加到 terraform。

省略terraform的安装。

首先,随意准备一个文件。

provider "auth0" {
  domain          = "moge.com"
  client_id       = "aaa"
  client_secret   = "bbb"
}

所以,当执行terraform init时,出现错误。难道官方没有提供这个功能吗!

Initializing provider plugins...
- Checking for available provider plugins...

Provider "auth0" not available for installation.

A provider named "auth0" could not be found in the Terraform Registry.

This may result from mistyping the provider name, or the given provider may
be a third-party provider that cannot be installed automatically.

In the latter case, the plugin must be installed manually by locating and
downloading a suitable distribution package and placing the plugin's executable
file in the following directory:
    terraform.d/plugins/darwin_amd64

Terraform detects necessary plugins by inspecting the configuration and state.
To view the provider versions requested by each module, run
"terraform providers".


Warning: Skipping backend initialization pending configuration upgrade

The root module configuration contains errors that may be fixed by running the
configuration upgrade tool, so Terraform is skipping backend initialization.
See below for more information.


Error: no provider exists with the given name

因为这是一个野生插件,所以需要下载并放置在 terraform.d/plugins/darwin_amd64 文件夹中。因此,需要获取tar文件并进行解压缩。

mkdir temp
cd temp

curl -OL https://github.com/alexkappa/terraform-provider-auth0/releases/download/v0.2.1/terraform-provider-auth0_v0.2.1_darwin_amd64.tar.gz

tar xzvf terraform-provider-auth0_v0.2.1_darwin_amd64.tar.gz
mv terraform-provider-auth0_v0.2.1 ..
cd ..
rm -Rf temp

在试验中,发现不一定非得安装在 terraform.d/plugins/darwin_amd64 目录下,当前目录应该也可以。虽然在进行 CI 时可能需要正确配置,但在本地进行操作应该在当前目录下也没有问题。

在之后运行 terraform apply,会出现 Apply complete! Resources: 0 added, 0 changed, 0 destroyed. 的消息,这意味着 auth0 provider 已经成功安装。

用terraform来配置auth0

获取并执行令牌。

从一开始看起来,将Auth0完全置于Terraform之中似乎是不可能的。

这是因为Terraform在使用的API中需要先获取必要的令牌。
大致的步骤如下。

    • アカウント作成orログインしてテナントを作る(手動)

 

    • トークン取得用のApplicationを作成(手動)

 

    • 作ったApplicationからtokenを取得(手動)

 

    取得したtokenをterraformで使う(ここから自動)

而且使用A租户的令牌无法创建一个新的B租户。因此,我们先自行创建租户,然后委托terraform完成后续的创建工作。
根据上述的auth0提供程序,貌似可以创建租户,但这很奇怪。

虽然与在AWS中使用时不同,但经常在认证环境中完全更改名称并创建不同环境的情况似乎并不常见,所以或许并不需要太过努力将所有事情都自动化。

把设置保存下来很重要,但是花太多时间自动化可能不太好。如果你要做100件的话另当别论,但是到要做100件的时候再考虑吧。

创建 Auth0 租户

右上菜单 -> 创建租户 -> 输入您喜欢的域名 -> 创建

创建应用程序

    • 左メニュー -> Application -> 右上の Create Application

 

    • Nameは適当に。application typeは Machine to Machine Application

APIの選択 Auth0 Management API を選択 -> Select All -> Authorize ボタン
作ったApplicationのページに行く -> Settingタブから Client ID と Client Secret を取って来てterraformに設定

记事

    • 普通にSPAで認証するときはSingle Page Applicationを使うけれど、ここではterraform用のapi keyが欲しいのでMachine to Machineを選ぶ

Auth0 Management API を選ぶのが重要。SPAで使用する際は認証を管理してもらうだけなので不要だが、今回はauth0を外部から操作するための権限が欲しいので Auth0 Management API を選ぶ。

Auth0 Management API にあたる部分は自分でも作成可能なので、よしなにドメインを作成して、自分でscopeを決めれば自分のアプリでauth0に任せっきりでRBACができる。今度試す。

Auth0 Management API で Select All は危険なので開発終わったら適宜権限を絞る。

已完成的tf文件

variable "auth0_domain" {}
variable "auth0_client_id" {}
variable "auth0_client_secret" {}

variable "application_name" { default = "Test" }
variable "application_domain" { default = "https://example.com" }

provider "auth0" {
  domain        = var.auth0_domain
  client_id     = var.auth0_client_id
  client_secret = var.auth0_client_secret
}

resource "auth0_client" "app" {
  name                  = var.application_name
  description           = "(Managed by Terraform)"
  app_type              = "spa"
  is_first_party        = true
  oidc_conformant       = true
  callbacks             = [ "${var.application_domain}/callback" ]
  allowed_origins       = [ var.application_domain ]
  allowed_logout_urls   = [ var.application_domain ]
  web_origins           = [ var.application_domain ]
  grant_types           = [ "authorization_code", "implicit", "refresh_token" ]
  custom_login_page_on  = true
  token_endpoint_auth_method = "none"
  is_token_endpoint_ip_header_trusted = false

  jwt_configuration {
    lifetime_in_seconds = 36000
    secret_encoded = true
    alg = "RS256"
  }
}

resource "auth0_resource_server" "api" {
  name        = "${var.application_name} (Managed by Terraform)"
  identifier  = "https://api.example.com"
  signing_alg = "RS256"

  scopes {
    value       = "create:foo"
    description = "Create foos"
  }

  scopes {
    value       = "create:bar"
    description = "Create bars"
  }

  allow_offline_access                            = false
  token_lifetime                                  = 86400
  skip_consent_for_verifiable_first_party_clients = true
}

由于想在自己的应用程序中使用RBAC,所以顺便创建API。设置完成了。现在让我们测试一下客户端。

请提供下列内容的原文:

“Can you give me a reference for that?”
(可以给我提供参考吗?)

请将以下内容用中文进行简化:

https://github.com/alexkappa/terraform-provider-auth0

bannerAds