[英]Can someone else create Service account for me and pass me JSON key to set env variable GOOGLE_APPLICATION_CREDENTIALS?
[英]Secure Service Account Credentials json key kotlin
因此,我試圖在本地主機上獲取服務應用程序憑據,但遇到了一些問題。 我創建並下載了 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 文件在我的存儲庫中以純文本形式公開。
選項
我找到了解決辦法。
我從 GCP 激活了 Secret Manager,並在那里添加了 JSON 文件。
為了在 java/kotlin 應用程序中獲取秘密(沒有 spring,如果你有 spring,你可以使用“spring-cloud-gcp-starter-secretmanager”),我使用了以下代碼。
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()
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.