【发布时间】:2020-09-22 13:02:17
【问题描述】:
我一直在尝试使用 Google Cloud Functions (python 3.7) 来克隆 Google Source Repository。我正在使用 GitPython 库,并且该 Cloud Functions 的服务帐户对我要克隆的存储库具有 Source Repository Reader 访问权限。
最初,我尝试将gcloud.sh credential.helper 传递给 git config,但似乎 Cloud SDK 未安装在 Cloud Functions 环境中(至少在 Python 3.7 环境中)。这是我的代码的要点:
from git import Repo
import tempfile
def git_clone():
local_repo = tempfile.gettempdir()+'/localrepo'
repo = Repo.init(local_repo)
origin = repo.create_remote('origin', 'https://source.developers.google.com/p/PROJECT/r/REPO')
repo.config_writer().set_value("user", "name", "gsr-to-gcs-backup-function").release()
repo.config_writer().set_value("user", "email", "gsr-to-gcs-backup-function@PROJECT.iam.gserviceaccount.com").release()
repo.config_writer().set_value("credential \"https://source.developers.google.com\"", "helper", "gcloud.sh").release()
assert origin.exists()
assert origin == repo.remotes.origin == repo.remotes['origin']
origin.fetch()
如果在 Cloud Functions 上运行,该函数将引发以下错误,因为默认情况下,如果没有凭据帮助程序,https 方法将询问 Username 和 密码。
git.exc.GitCommandError: Cmd('git') failed 由于: exit code(128) cmdline: git fetch -v origin stderr: 'fatal: 无法读取用户名 对于'https://source.developers.google.com':输入/输出错误'
我只能找到以下答案来传递令牌以及 git clone 命令,但它没有回答如何传递令牌:
Clone Google Source Repository without gcloud
如果我从云外壳启动该命令,它将被挂起:
gcloud auth git-helper store --account=YOUR_ACCOUNT --ignore-unknown $@
这是我计划使用上述实现的类似功能(不是正确的代码):
Repo.clone_from("https://source.developers.google.com/p/PROJECT/r/REPO",tempfile.gettempdir(), multi_options=['--config credential.helper={}'.format(MYTOKEN)], branch='master')
我不想将 SSH 密钥作为一种克隆方法,因为我想稍后将其部署到生产环境中,而轮换密钥会很麻烦。
【问题讨论】:
-
我个人非常喜欢 git 的 ssh 密钥,因为身份验证过程很简单,而且如果操作正确,比共享密钥更安全。旋转 ssh 密钥真的比旋转密码更麻烦吗?显然,这个选择取决于你。
-
我们可以为服务帐户创建 SSH 密钥吗?我认为我们从我们的帐户中添加的那个将拥有我的特权。那将是一个太大的安全风险。如果我的理解不同并且有办法为服务帐户添加密钥,我仍然可以使用 SSH 密钥。
标签: git google-cloud-functions gitpython google-cloud-source-repos google-source-repositories