目前将我的应用程序迁移到Micronaut 3,我遇到了micronaut-gcp的一个问题。尤其是谷歌的秘密经理。我在用gradle和Kotlin DSL。
实际配置:(不工作,使用插件io.micronaut.library版本2.0.4)
以前的配置:(不使用插件,使用micronaut-bom)
I/问题
我的目标是将一些秘密作为键/值对加载到属性源中。我按照文档的要求使用bootstrap.yml,如下所示:
micronaut:
config-client:
enabled: true
gcp:
secret-manager:
keys:
- MY_SECRET然后,在我的类中,我应该能够使用@Value注释注入这个秘密值。实际上我是在我的测试中这么做的。我使用的是来自@MicronautTest的jUnit5注释,因此我有如下所示:
@MicronautTest
public class MyClass {
@Value("${my.secret}")
private String mySecret;
}然后出现了一个问题,在运行任何测试时,它在初始化过程中都会失败,如下所示:
Error instantiating bean of type [io.micronaut.gcp.secretmanager.client.DefaultSecretManagerClient]
Message: getTransportChannel() called when needsExecutor() is true
Path Taken: new DistributedPropertySourceLocator(ConfigurationClient configurationClient,Duration readTimeout) --> new DistributedPropertySourceLocator([ConfigurationClient configurationClient],Duration readTimeout) --> new DefaultCompositeConfigurationClient([ConfigurationClient[] configurationClients]) --> new SecretManagerConfigurationClient([SecretManagerClient secretManagerClient],SecretManagerConfigurationProperties configurationProperties) --> new DefaultSecretManagerClient([SecretManagerServiceClient client],Environment environment,GoogleCloudConfiguration googleCloudConfiguration,ExecutorService executorService)
io.micronaut.context.exceptions.BeanInstantiationException: Error instantiating bean of type [io.micronaut.gcp.secretmanager.client.DefaultSecretManagerClient]II/我试过什么?
当我看到这一点时,我在想,也许bootstrap.yaml不起作用--我试图通过注入SecretManagerServiceClient来访问秘密管理器,如下所示:
@MicronautTest
public class MyClass {
@Inject
private SecretManagerServiceClient client;
private void someTest() {
String mySecret = client.getSecret("MY_SECRET");
}
}但在相同路径下得到相同的误差Message: getTransportChannel() called when needsExecutor() is true。
我也试图升级到Micronaut3.0.1,但没有改变任何东西。
III/我的解决方案来处理这个
因为我的问题是在ci/cd过程的测试阶段以及使用Google构建时检索一个秘密。我可以在我的availableSecrets中使用cloudbuild.yaml特性传递秘密
steps:
...
- name: 'gradle'
entrypoint: 'bash'
args: ['./gradlew', 'test']
secretEnv: ['MY_SECRET']
...
availableSecrets:
secretManager:
- versionName: projects/PROJECT_ID/secrets/MY_SECRET/versions/latest
env: 'MY_SECRET'然后把它传递到我的application.yml中
my:
secret: ${MY_SECRET}然后在我的代码中:
@MicronautTest
public class MyClass {
@Value("${my.secret}")
private String mySecret;
}但是我觉得这个解决方案并不令人满意,因为我觉得bootstrap.yaml更方便。
如果有人为我找到了解决办法或想法/建议,我会乐意接受的。
祝你今天愉快!
发布于 2021-09-30 16:59:09
我去过那个兔子洞。长话短说,我通过将google-cloud-secretmanager依赖从1.6.4升级到2.0.2来克服这一问题。
就像这样:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-secretmanager</artifactId>
<version>2.0.2</version>
</dependency>https://stackoverflow.com/questions/69155870
复制相似问题