因此,我试图在本地主机上获取服务应用程序凭据,但遇到了一些问题。我创建并下载了json密钥,我希望保护它们,而不是让它们成为纯文本。
我想知道最好的方法。我有这样的代码:
fun getServiceAccountCredentials(
pathToFallBackCredentialsFile: String
): ServiceAccountCredentials {
return try {
getApplicationDefault() as ServiceAccountCredentials
} catch (e: IOException) {
return ServiceAccountCredentials.fromStream(FileInputStream(pathToFallBackCredentialsFile))
} catch (e: ClassCastException) {
return ServiceAccountCredentials.fromStream(this::class.java.classLoader.getResourceAsStream(pathToFallBackCredentialsFile))
}
}这里的问题是,我的JSON文件在我的存储库中以纯文本形式公开。
发布于 2022-03-16 14:23:40
选项
的一些自定义。
发布于 2022-03-28 10:25:57
我找到了解决办法。
我从GCP中激活了秘密管理器,并在那里添加了JSON文件。
要获取java/kotlin应用程序中的秘密(如果没有spring,则可以使用“spring gcp-初学者-分泌管理器”),我使用了以下代码。
fun accessSecretVersion(secretId: String?, versionId: String?): String {
val googleCredentials = GoogleCredentials.getApplicationDefault() as UserCredentials
val projectId = googleCredentials.quotaProjectId
return getSecret(projectId, secretId, versionId)
}
fun getSecret(projectId: String?, secretId: String?, versionId: String?): String {
SecretManagerServiceClient.create().use { client ->
val secretVersionName = SecretVersionName.of(projectId, secretId, versionId)
val response = client.accessSecretVersion(secretVersionName)
return response.payload.data.toStringUtf8()
}
}https://stackoverflow.com/questions/71497577
复制相似问题