从 GCP SecretManagerServiceClient 获取机密后关闭它
Close GCP SecretManagerServiceClient after getting secrets from it
如何在以下代码中关闭客户端:
object GoogleSecret {
def apply(
project: String,
version: String = SecretVersion,
credentials: CredentialsProvider = defaultProvider
): Try[SecretService] = Try {
require(project != null && project.nonEmpty, "project is null or empty")
val settings = SecretManagerServiceSettings.newBuilder
.setCredentialsProvider(credentialsProvider)
.build()
val client = SecretManagerServiceClient.create(settings)
logger.info(s"connected to project: $project")
secrets => secrets.flatMap {
secret =>
logger.debug(s"getting secret: $secret")
try {
val value = client
.accessSecretVersion(SecretVersionName.of(project, secret, version))
.getPayload
.getData
.toStringUtf8
Some(secret -> value)
} catch {
case e: Throwable => {
logger.error(s"google error ($secret): ${e.getMessage}")
client.shutdown()
client.awaitTermination(30, TimeUnit.SECONDS)
client.close()
None
}
}
}.toMap
}
上述方法创建了一个连接到 GCP Secret Manager 的客户端,以获取机密值,但我有一些问题:
- 这个片段应该做什么:
secrets => secrets.flatMap {
secret =>
logger.debug(s"getting secret: $secret")
try {
val value = client
.accessSecretVersion(SecretVersionName.of(project, secret, version))
.getPayload
.getData
.toStringUtf8
Some(secret -> value)
} catch {
case e: Throwable => {
logger.error(s"google error ($secret): ${e.getMessage}")
client.shutdown()
client.awaitTermination(30, TimeUnit.SECONDS)
client.close()
None
}
}
}.toMap
- 我如何将代码转换为 return Map[String, String] 中的秘密值,并关闭客户端连接(类似的东西):
def apply(
project: String,
version: String = SecretVersion,
credentials: CredentialsProvider = defaultProvider
): Try[SecretService] = Try {
.
.
.
some code with the solution
.
.
.
client.shutdown()
client.awaitTermination(30, TimeUnit.SECONDS)
client.close()
secrets
}
提前致谢;)
如果我没理解错的话,你拿到地图后需要关闭客户端
val client = SecretManagerServiceClient.create(settings)
// TODO: Set the type of the secrets parameter
val result = (secrets: ???) => { secrets.flatMap
...
}.toMap
}
client.shutdown()
client.awaitTermination(30, TimeUnit.SECONDS)
client.close()
return result
在这里,result
是一个函数句柄,您正在 returning
但是,请记住,如果您到达异常块,客户端可能已经关闭
或者,您可以
- 将客户端传递给函数,而不是项目详细信息。函数调用后关闭客户端
- 从函数定义中删除 Try,并实际处理异常或在成功时关闭客户端,然后 return
Some[Map]
如何在以下代码中关闭客户端:
object GoogleSecret {
def apply(
project: String,
version: String = SecretVersion,
credentials: CredentialsProvider = defaultProvider
): Try[SecretService] = Try {
require(project != null && project.nonEmpty, "project is null or empty")
val settings = SecretManagerServiceSettings.newBuilder
.setCredentialsProvider(credentialsProvider)
.build()
val client = SecretManagerServiceClient.create(settings)
logger.info(s"connected to project: $project")
secrets => secrets.flatMap {
secret =>
logger.debug(s"getting secret: $secret")
try {
val value = client
.accessSecretVersion(SecretVersionName.of(project, secret, version))
.getPayload
.getData
.toStringUtf8
Some(secret -> value)
} catch {
case e: Throwable => {
logger.error(s"google error ($secret): ${e.getMessage}")
client.shutdown()
client.awaitTermination(30, TimeUnit.SECONDS)
client.close()
None
}
}
}.toMap
}
上述方法创建了一个连接到 GCP Secret Manager 的客户端,以获取机密值,但我有一些问题:
- 这个片段应该做什么:
secrets => secrets.flatMap {
secret =>
logger.debug(s"getting secret: $secret")
try {
val value = client
.accessSecretVersion(SecretVersionName.of(project, secret, version))
.getPayload
.getData
.toStringUtf8
Some(secret -> value)
} catch {
case e: Throwable => {
logger.error(s"google error ($secret): ${e.getMessage}")
client.shutdown()
client.awaitTermination(30, TimeUnit.SECONDS)
client.close()
None
}
}
}.toMap
- 我如何将代码转换为 return Map[String, String] 中的秘密值,并关闭客户端连接(类似的东西):
def apply(
project: String,
version: String = SecretVersion,
credentials: CredentialsProvider = defaultProvider
): Try[SecretService] = Try {
.
.
.
some code with the solution
.
.
.
client.shutdown()
client.awaitTermination(30, TimeUnit.SECONDS)
client.close()
secrets
}
提前致谢;)
如果我没理解错的话,你拿到地图后需要关闭客户端
val client = SecretManagerServiceClient.create(settings)
// TODO: Set the type of the secrets parameter
val result = (secrets: ???) => { secrets.flatMap
...
}.toMap
}
client.shutdown()
client.awaitTermination(30, TimeUnit.SECONDS)
client.close()
return result
在这里,result
是一个函数句柄,您正在 returning
但是,请记住,如果您到达异常块,客户端可能已经关闭
或者,您可以
- 将客户端传递给函数,而不是项目详细信息。函数调用后关闭客户端
- 从函数定义中删除 Try,并实际处理异常或在成功时关闭客户端,然后 return
Some[Map]