TechPythonGCP-FirebaseCloud FunctionsJapanese

Google Secret Manager に 辞書型データを格納し、Cloud Functions から アクセスする方法

Tech

こちらの記事 で Google Secret Manager に パスワード や API キー といった 「他者に知られたくない」あるいは 「公開したくない」 情報を格納する方法を紹介しました。 今回はより実践的な管理方法として 辞書型 の データ を Secret に登録して Python で 作成した Cloud Functions 関数 で 意図通り 辞書型 として扱う方法について紹介していきます。 


前提

[ads]

本記事を読みすすめるにあたっては、以下を実施済みであることが前提となっています。

  • Python 開発環境の設定
  • サービスアカウント作成済み
  • Secret Manager API の有効化
  • Secret Manager への アクセス ロール 付与済み

これらが未実施の場合は 以下の 関連記事 を参照してください。


Secret の作成

[ads]

それでは早速 辞書型の データ を Secret Manager に登録していきます。
本記事では 以下のような 構造をもつ Dict 型 の データ を登録してみます。

{
   "headers": {
       "key": "key value",
       "host": "host name"
   }
}

以下の リンク を クリック して GCP コンソール から Secret Manager を表示し、「 シークレットを作成 」を クリック します。

Google Cloud Platform
GoogleCloudPlatformletsyoubuild,deploy,andscaleapplications,websites,andservicesonthesameinfrastructureasGoogle.

シークレット の詳細として 以下を設定します。

  • 名前: 任意の名称(ここでは、 my-dict-secret )
  • シークレットの値: 上述した 辞書型 の データ

他の項目は デフォルト のままで 「シークレット を作成」 をクリックし、 Secret の作成を完了させます。

Secret 作成後 Secret の値を確認してみます。

特に フォーマット が変換されることもなく、そのままの テキスト データ として格納されています。


Secret Manager に登録した 辞書型 Secret へ Cloud Functions から アクセス

[ads]

それでは、 作成した 辞書型 の Secret データ に Cloud Functions から アクセス していきます。こちらの記事 で作成した 関数 をベースにします。

Secret に登録した値は純粋な テキストデータ として扱われるため、取得した値を json を用いて 文字列 から 辞書型 に変換することで意図通りの dict 型 として扱うことが可能になります。

def hello_http(request):
  import json
 
  response = access_secret_version('sample-for-blog', 'my-dict-secret', 'latest')
  payload = response.payload.data.decode("UTF-8")
  # convert str to dict
  d = json.loads(payload)
  print("payload type is ", type(payload), "converted type is ", type(d))
 
  return d

Cloud Functions での テスト

それでは Cloud Functions の テスト 機能を利用して動作を確認していきます。

作成した hello_http は特に引数を必要としないため 「トリガーとなるイベント」は初期値のままとし、 「関数をテストする」をクリックして 出力とログを確認してみます。

出力については 変換後 の 辞書型 を出力するようにしていますが、見た目では分かりませんね。

ログは以下の通りです。 そのままの payload だと str 型となっていることがわかります。


まとめ

[ads]
  • Google Secret Manager に登録したデータは 純粋な テキストデータ として扱われる
  • 辞書型データを登録する場合は、値を取得した際に文字列から辞書型に変換する

関連記事

今回使用したサンプルソース

def access_secret_version(project_id, secret_id, version_id):
   """
   Access the payload for the given secret version if one exists. The version
   can be a version number as a string (e.g. "5") or an alias (e.g. "latest").
   """
 
   # Import the Secret Manager client library.
   from google.cloud import secretmanager
 
   # Create the Secret Manager client.
   client = secretmanager.SecretManagerServiceClient()
 
   # Build the resource name of the secret version.
   name = f"projects/{project_id}/secrets/{secret_id}/versions/{version_id}"
   print(name)
 
   # Access the secret version.
   response = client.access_secret_version(request={"name": name})
 
   # Print the secret payload.
   #
   # WARNING: Do not print the secret in a production environment - this
   # snippet is showing how to access the secret material.
   payload = response.payload.data.decode("UTF-8")
   print("Plaintext: {}".format(payload))
 
   return response

def hello_http(request):
   import json

   response = access_secret_version('sample-for-blog', 'my-dict-secret', 'latest')
   payload = response.payload.data.decode("UTF-8")
   # convert str to dict
   d = json.loads(payload)
   print("payload type is ", type(payload), "converted type is ", type(d))

   return d
[ads]
Ads
Rambling and Delving