安全服务帐户凭证 json 密钥 kotlin

发布于 2025-01-14 13:23:49 字数 668 浏览 4 评论 0 原文

因此,我试图在本地主机上获取服务应用程序凭据,但遇到了一些问题。我创建并下载了 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 文件在我的存储库中以纯文本形式公开。

So, I'm trying to get the Service Application Credentials on my localhost and I'm encountering some problems. I created and downloaded the json key and I want to secure them instead of letting them in plain text.

I want to know the best way to do this. I have this code:

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))
    }
}

The problem here is that my JSON file is exposed in plain text in my repository.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离去的眼神 2025-01-21 13:23:49

选项

  1. 加密它们,然后让应用程序解密它们。但是,如果解密密钥位于同一个存储库中,那么您只是将问题移到了另一个地方,尽管某人需要有人读取/执行您的代码才能获取它们。
  2. 您的应用程序将如何运行,例如在 Kubernetes 等 Docker 容器内或在您的数据中心运行的另一个应用程序?如果是这样,那么您将责任传递给应用程序容器管理器,以在启动时注入凭据(和其他环境细节),例如使用 Kubernetes 配置映射等。我们这样做并将生产配置映射保存在单独的 DevOps 存储库中。
  3. 有针对此问题的专业秘密管理解决方案,并且您的应用程序在运行时调用秘密管理器以获取其所需的秘密,例如 https://www.hashicorp.com/products/vault 或云供应商的一些自定义

Options

  1. Encrypt them, then let the app decrypt them. However, if the decrypt key is in the same repository then you have only moved the problem to another place, albeit the someone needs someone to read/execute your code to obtain them.
  2. How is your app going to be running, e.g. inside a Docker container in something like Kubernetes or another app running in your data centre? If so then you pass the responsibility down the line to that app container manager to inject at startup-time the credentials (and other environment specifics), e.g. using some like Kubernetes Configuration Maps. We do this and keep the production config maps in a separate DevOps-only repository.
  3. There are specialist secrets management solutions for exactly this problem and your app calls out at run time to a secrets manager for the secrets it needs, e.g. https://www.hashicorp.com/products/vault or some custom to cloud vendor
许一世地老天荒 2025-01-21 13:23:49

我找到了解决方案。

我从 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()
    }
}

I found a solution.

I activated the Secret Manager from GCP and I've added the JSON file there.

To take the secret in a java/kotlin application(without spring, if you have spring you can use "spring-cloud-gcp-starter-secretmanager"), I used the following piece of code.

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()
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文