はじめに
Apigee の組織が使えなくなり、デベロッパーアプリをバックアップから新しい組織へ復元する必要がありました。
普通に作成すると API キーが変わり、利用者全員にキーの差し替えをお願いすることになります。
そこで、元のキーのままアプリを復元できるか試してみました。
既存のキーを取り込むとは
Apigee には、既存のキーとシークレットを指定してアプリに登録する API があります。
Creates a custom consumer key and secret for a developer app. This is particularly useful if you want to migrate existing consumer keys and secrets to Apigee from another system.
手順はドキュメントにまとまっていて、キーを登録したあとに API プロダクトを紐付けます。
https://docs.cloud.google.com/apigee/docs/api-platform/publish/import-existing-consumer-keys-and-secrets
試した手順と結果
元のキーは、apps?expand=true でアプリ一覧を取得すると credentials に平文で入っています。
今回は、定期的にバックアップしておいたこの JSON から復元しました。
新しい組織は Terraform で管理しているので、google_apigee_developer_app の consumer_key / consumer_secret にキーを渡しました。
resource "google_apigee_developer_app" "app" {
for_each = local.developer_apps
org_id = local.org_id
developer_email = each.value.developer_email
name = each.value.name
api_products = each.value.api_products
consumer_key = try(var.restore_keys[each.key].consumer_key, null)
consumer_secret = try(var.restore_keys[each.key].consumer_secret, null)
lifecycle {
ignore_changes = [key_expires_in, consumer_key, consumer_secret]
}
}
apply のあと、管理 API で取得したキー・シークレットを元の JSON と突き合わせました。
RESULT: PASS — 13 件すべて復元キーと一致
さらに、VerifyAPIKey ポリシーを置いたプロキシにそのキーでリクエストを送りました。
[T5] gate POST 正規キー → 200(ゲート通過) status : 200 (期待 200)
元のキーのまま、アプリの復元とプロキシの通過ができることが確認できました。
注意点
ignore_changes が無いとキーが失効する
consumer_key / consumer_secret は作成時だけ使われ、Apigee から読み戻されません。
そのため、キーを渡すのをやめた次の plan で差分になり、apply するとアプリが再作成されてキーが失効します。
ignore_changes に入れておけば、作成時の指定はそのまま効き、以後の差分も出ません。
キーを渡し忘れても止まらない
キーを渡さないと、Terraform は自動発行のキーでアプリを作ります。
エラーにならないため、apply 後の突き合わせは省略しないほうが良さそうです。
まとめ
公式ドキュメントでは「他システムからの移行」向けの機能ですが、バックアップからの復元にもそのまま使えました。
元のキーをバックアップしておくことと、作成後に突き合わせることが大事だと感じました。
参考
- https://docs.cloud.google.com/apigee/docs/api-platform/publish/import-existing-consumer-keys-and-secrets
- https://docs.cloud.google.com/apigee/docs/reference/apis/apigee/rest/v1/organizations.developers.apps.keys/create
- https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/apigee_developer_app