检测安全密钥

任何时候不得将安全密钥存储在版本控制系统中。Cloud Source Repositories 可以帮助您防止用户将安全密钥存储在 Google Cloud 代码库中。Cloud Source Repositories 可以检查以下类型的安全密钥:

  • Google Cloud 服务账号凭据(JSON 格式)
  • PEM 编码的私钥(包括 RSA、DSA 和 PGP)

此检查功能可免费用于所有代码库。

安全密钥检查功能如何工作

当用户执行 git push 命令时,检查功能将查找可能是安全密钥的数据。如果找到匹配项,则该功能将阻止 git 推送,并通知用户找到的内容和位置。例如:

The push has been rejected because we detect that it contains a private key.
Please check the following commands and confirm that it's intentional:

git show [COMMIT]

You can use `git rev-list --objects --all` to find the files.

To push these files, please run `git push -o nokeycheck`.

准备工作

在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

转到“项目选择器”

停用安全密钥检测

要停用安全密钥检测,请使用以下 gcloud 命令:

    gcloud init
    gcloud source project-configs update --disable-pushblock

启用安全密钥检测

要启用私钥检测,请使用以下 gcloud 命令:

    gcloud init
    gcloud source project-configs update --enable-pushblock

覆盖安全密钥检测

要覆盖安全密钥检测功能,请使用以下 git 命令:

    git push -o nokeycheck

后续步骤

设置 Google Cloud 代码库后,您可能会发现以下主题会有所帮助: