简体   繁体   English

在为 Google Source Repository 调用 git clone over HTTPS 时,有没有办法传递访问令牌

[英]Is there a way to pass access tokens when calling git clone over HTTPS for Google Source Repository

I have been trying to use Google Cloud Functions (python 3.7) to basically clone a Google Source Repository.我一直在尝试使用 Google Cloud Functions (python 3.7) 来基本上克隆 Google Source Repository。 I'm using GitPython library and the service account for that Cloud Functions has Source Repository Reader access on the repository that I want to clone.我正在使用GitPython库,并且 Cloud Functions 的服务帐户对我要克隆的存储库具有Source Repository Reader访问权限。

Initially, I tried passing gcloud.sh credential.helper to git config, but it seems that Cloud SDK is not installed on Cloud Functions environment (at least in Python 3.7 environment).最初,我尝试将gcloud.sh credential.helper传递给 git 配置,但似乎 Cloud SDK 未安装在 Cloud Functions 环境中(至少在 Python 3.7 环境中)。 Here is a gist of my code:这是我的代码的要点:

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

The function will throw the below error if run on Cloud Functions since by default if there is no credential helper, the https method will ask for Username and Password .如果在 Cloud Functions 上运行,function 将抛出以下错误,因为默认情况下,如果没有凭证助手, https方法将询问用户名和密码

git.exc.GitCommandError: Cmd('git') failed due to: exit code(128) cmdline: git fetch -v origin stderr: 'fatal: could not read Username for 'https://source.developers.google.com': Input/output error' git.exc.GitCommandError: Cmd('git') 失败,原因是:退出代码 (128) cmdline: git fetch -v origin stderr: 'fatal: could not read Username for 'https://source.developers.google.com ':输入/输出错误'

I could only find the below answer to pass token along with the git clone command but it doesn't answer how to pass the token:我只能找到以下答案来传递令牌以及git 克隆命令,但它没有回答如何传递令牌:

Clone Google Source Repository without gcloud 在没有 gcloud 的情况下克隆 Google Source Repository

If I initiate that command from cloud shell, it will be just hung:如果我从云 shell 启动该命令,它将被挂起:

gcloud auth git-helper store --account=YOUR_ACCOUNT --ignore-unknown $@

Here's something similar that I plan to achieve using the above (not a correct code):这是我计划使用上述方法实现的类似内容(不是正确的代码):

Repo.clone_from("https://source.developers.google.com/p/PROJECT/r/REPO",tempfile.gettempdir(), multi_options=['--config credential.helper={}'.format(MYTOKEN)], branch='master')

I don't want to put an SSH key instead as a method to clone as I want to deploy it to production later on and rotating keys would be cumbersome.我不想将 SSH 密钥作为一种克隆方法,因为我想稍后将其部署到生产环境中,而旋转密钥会很麻烦。

The Username that is asked is nothing but the service account address and credentials can be a temporarily generated OAuth 2.0 access token.询问的用户名不过是服务帐户地址和凭据,可以是临时生成的 OAuth 2.0 访问令牌。 So, what I ended up doing was:所以,我最终做的是:

from git import Repo
import tempfile
import urllib.parse
import google.auth
import google.auth.transport.requests

def get_token():
    creds, project = google.auth.default()
    # creds.valid is False, and creds.token is None
    # # Need to refresh credentials to populate those
    auth_req = google.auth.transport.requests.Request()
    creds.refresh(auth_req)
    # Now you can use creds.token
    return creds.token

def url_parse():
    query = 'gsr-to-gcs-backup-function@PROJECT.iam.gserviceaccount.com'
    return urllib.parse.quote(query)

def git_clone():
    encoded_url = url_parse()
    credentials = get_token()
    
    local_repo = tempfile.gettempdir()+'/localrepo' 
    Repo.clone_from(f"https://{encoded_url}:{credentials}@source.developers.google.com/p/PROJECT/r/REPO",local_repo, branch='master')

def main(*args):
    git_clone()

NOTE: This is only a part of my code.注意:这只是我的代码的一部分。 What I want is to copy this to a GCS bucket.我想要的是将其复制到 GCS 存储桶中。 But that is out of purview of this question.但这超出了这个问题的范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用 https 和 Z765553E6C7AC85A00AZ 链接9ACB8 克隆 Eclipse 中的 git 存储库 - Unable to clone git repository in Eclipse using https and SSH links 使用 Google Source Repository 的 Dataform git 连接 - Dataform git connection using Google Source Repository git 执行 https 克隆时出现 lfs 涂抹错误 - git lfs smudge error when doing an https clone 使用 vscode 克隆云源存储库 - Clone cloud source repository using vscode 使用 Git 存储库作为 Terraform 中的 AWS Lambda 源的最佳方法? - Best way to use Git repository as an AWS Lambda source within Terraform? git 命令(推、拉、克隆)不适用于 android studio - 谷歌云存储库 - git commands(push, pull, clone) not working on android studio - google cloud repository 有什么方法可以仅使用 API 获取 Google 访问令牌? - Is there any way that I can get Google access tokens only using API? Google OAuth 刷新令牌未返回有效访问令牌 - Google OAuth Refresh Tokens not returning Valid Access Tokens 使用谷歌云 SDK Windows 10 上的身份验证方法获取 Git 推送到谷歌云源存储库的权限被拒绝(公钥) - Get Permission denied (publickey) for Git push to Google Cloud Source Repository with Google Cloud SDK authentication method on Windows 10 除非我以 root 身份运行,否则从内部 gitlab 存储库克隆 git 不起作用 - git clone from internal gitlab repository doesn't work unless I'm running as root
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM