UGA Boxxx

つぶやきの延長のつもりで、知ったこと思ったこと書いてます

【Spring Cloud GCP】Secret Manager に登録した秘密情報をアプリケーションの設定値として設定する - Spring Cloud 1.2.3.RELEASEの場合

前回の記事でSpring Cloud GCP でSecret Manager に登録した秘密情報を取得する方法を調べたが、みるべきドキュメントのバージョンが違っていた

uga-box.hatenablog.com

パッチバージョンの違いなので問題ないと思っていたが、かなり大きな変更があって実装も変更する必要があったので改めてやり方を調査する

1.2.3.RELEASEの場合

https://cloud.spring.io/spring-cloud-static/spring-cloud-gcp/1.2.3.RELEASE/reference/html/#secret-manager

Dependencyの設定は変わらず必要

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-gcp-starter-secretmanager</artifactId>
</dependency>

しかし、1.2.3.RELEASEではbootstrap.yml内でspring.cloud.gcp.secretmanager.bootstrap.enabledspring.cloud.gcp.secretmanager.secret-name-prefixをあえて指定する必要がなくなった

さらに、bootstrap.ymlまたはapplication.yml内で以下のシンタックスを使用してシークレットを指定することができるようになった

# 1. Long form - specify the project ID, secret ID, and version
sm://projects/<project-id>/secrets/<secret-id>/versions/<version-id>}

# 2.  Long form - specify project ID, secret ID, and use latest version
sm://projects/<project-id>/secrets/<secret-id>

# 3. Short form - specify project ID, secret ID, and version
sm://<project-id>/<secret-id>/<version-id>

# 4. Short form - default project; specify secret + version
#
# The project is inferred from the spring.cloud.gcp.secretmanager.project-id setting
# in your bootstrap.properties (see Configuration) or from application-default credentials if
# this is not set.
sm://<secret-id>/<version>

# 5. Shortest form - specify secret ID, use default project and latest version.
sm://<secret-id>

1.2.2.RELEASEの場合はsecret.というprefixを用意し、@ConfigurationProperties("secrets")で受け取るConfigurationPropertiesクラスを新たに用意する必要があったが、既に利用していたapplication.yml内で以下のように指定することができるようになった

...
  service:
    oauth2:
      id: ${sm://my-secret-oauth2-id}
      secret: ${sm://my-secret-oauth2-password}
...

結果的に1.2.3.RELEASE のバージョンアップで簡単に使えるようになったのでよかった