运行git clone命令针对AWS CodeCommits时,我遇到了403错误。

71

我的本地笔记本电脑是一台Mac。

  1. ssh密钥已正确配置。这是~/.ssh/config的内容。
Host barthea
Hostname git-codecommit.us-east-1.amazonaws.com
User AVVVVVVVVVVVVVVVVVQ
IdentityFile ~/.ssh/aws-aws.pem

运行ssh barthea会得到:

You have successfully authenticated over SSH. You can use Git to interact with AWS CodeCommit. Interactive shells are not supported.Connection to git-codecommit.us-east-1.amazonaws.com closed by remote host.
  1. 我创建了一个名为bruce666的IAM用户,设置了密码和访问密钥,并将该用户作为“team”组的一部分。然后我创建了一个策略,其中包括“AWSCodeCommitPowerUsers”,并将此策略分配给“team”。最后,我将bruce666分配给“team”。此时,bruce666可以通过管理控制台访问CodeCommit中的任何存储库。

  2. 我运行了aws config --profile bruce666,输入了他的访问密钥、密钥、他的区域并指定为json格式。此时,我能够通过运行aws codecommit get-repository --repository-name rekha --profile bruce666在CodeCommmit中创建rekha存储库。

  3. 我可以创建一些虚拟文件,运行git initgit add .git commit -m "1"git add origin https://git-gitcode.amzonaws.com/repos/v1/rekhagit push -u origin master,并且该操作将成功。

  4. 但是,当我运行git clone ssh://git-gitcode.amazonaws.com/repos/v1/rekha时,我得到了“致命错误:无法访问'https://git-codecommit.us-east-1.amazonaws.com/v1/repos/barthia/':请求的URL返回错误:403”是什么原因呢?

18个回答

104

如果上述技巧在MAC上不起作用,请执行以下操作:

  1. 打开钥匙串访问
  2. 搜索CodeCommit。您应该会找到这个:

输入图像描述

  1. 选择“git-codecommit....”并按删除键
  2. 确认删除。

现在再试一次,它应该能够工作。当您面对403错误时,您可能还需要再次执行此操作。

此问题的一个可能原因是钥匙串密码与您在MAC上登录的密码不同。


这个出了点问题,不知道有什么想法吗? - Mike M
3
Keychain Access 中如此讨厌的功能。 - Varun Chandak
对我有用。我无法再将代码推送到我的仓库中。我不得不重新提交我的密码。 - Andrea Gorrieri
3
当我点击删除时,什么也不会发生 - 没有提示,也没有删除。 - SUR4IDE
这对我有用,但是在每次远程操作(推送、获取等)之前,我需要再次将其删除。有人知道如何永久修复这个问题吗? - Lewis Donovan
显示剩余2条评论

62

在Windows中使用git push命令时,我也遇到了403错误。我按照AWS文档中提到的所有设置都做了,但问题仍未得到解决。后来我检查了通过Windows凭据设置的git凭据,如屏幕所示。我发现它设置了访问密钥/秘密密钥而不是git https凭据(不知道为什么)。

Control Panel\All Control Panel Items\Credential Manager

输入图像描述

点击编辑链接,使用为AWS用户生成的正确的git凭据更新凭据,一切都正常。


10
请投票选择这个选项,它是修复Windows 10的解决方案。 - SilentTremor
1
为这个投一票! - Abdul Rehman Khan
在我的情况下,它将URL列为用户名 - 不知道怎么发生的。直接插入用户和密码即可解决问题。 - Raffael
1
我没有更新凭据,而是将其删除。然后Windows再次询问登录和密码,我取消了它,最终Git开始使用凭据助手并成功克隆了AWS存储库。 - Ilya Sheroukhov
我需要做和@IlyaSheroukhov一样的事情,不知道为什么但这个方法起作用了:\ 真的很遗憾这个错误。 - Jorge Freitas
谢谢这个提示,在Windows 10上像魔法一样起作用了。 - kolyaiks

17

这个有帮助的文本在 AWS CodeCommit 和 Windows 集成的文档中找到

如果你安装的 Git for Windows 包括 Git Credential Manager 实用程序,则在前几次连接尝试后,你将看到 403 错误或提示提供凭据到凭据管理器实用程序。解决此问题最可靠的方法是卸载然后重新安装 Git for Windows,但不选择 Git Credential Manager 实用程序选项,因为它与 AWS CodeCommit 不兼容。

如果你想保留 Git Credential Manager 实用程序,则必须执行其他配置步骤以使用 AWS CodeCommit,包括手动修改 .gitconfig 文件以指定在连接到 AWS CodeCommit 时使用凭证助手来连接 AWS CodeCommit。

从凭据管理器实用程序中删除所有存储的凭据(可以在控制面板中找到此实用程序)。

一旦你删除了任何存储的凭据,请将以下内容添加到你的 .gitconfig 文件中,保存它,然后从新的命令提示符窗口再次尝试连接:

[credential "https://git-codecommit.us-east-1.amazonaws.com"]
    helper = !aws codecommit credential-helper $@
    UseHttpPath = true
此外,您可能需要重新配置git配置设置,通过在所有连接正常工作之前指定--system而不是--global或--local。尽管如此,在我的情况下,当我运行git config --system时,它没有像预期的那样工作,而是在aws codecommit命令之前添加了aws configure。因此,我不得不在git中运行此命令以查找系统配置文件的位置。
git config --list --show-origin

我随后将AWS建议的部分添加到了我的 c:/users/username/.gitconfig 和 c:/ProgramData/Git/config 文件中。

之后,尽管我在响应前面遇到了虚假的错误信息,但 git push 开始工作了。

"git: 'credential-aws' is not a git command. See 'git --help'."

4
这是对我来说最有用的答案。我犯了一个错误,安装了带凭据管理器实用程序的Git。为了解决这个问题,你需要卸载Git,然后重新安装并取消勾选凭据管理器实用程序。之后就可以像魅力一样工作了。谢谢。 - user4237179
在Linux上更改.gitconfig文件:sudo nano ~/.gitconfig - Gabriel Petersson
不需要使用sudo @GabrielPetersson - Mike Crowe

8

我解决了它。

403错误信息是Git特定的错误信息。我加入了两个AWS指定的辅助命令:

git config --global credential.helper '!aws --profile bruce666 codecommit credential-helper $@'
git config --global credentials.helper UseHttpPath=true

问题已经得到解决。

在你克隆刚刚创建的Codecommit仓库之前,你本地目录中的.git/config文件应该长这样:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = false
[credential]
    helper = !aws --profile bruce666 codecommit credential-helper $@
    UseHttpPath = true
[remote "origin"]
    url = https://git-codecommit.us-east-1.amazonaws.com/v1/repos/barthea
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

根据.git/config文件中的规定,您正在使用https而不是ssh进行克隆。我可能没有使用OSX附带的默认版本的git,因为我没有遇到任何工具链问题。


不需要添加 aws profile(--profile bruce666),只需使用!aws codecommit credential-helper $@即可。+1 - jmcg

7
在运行以下命令后,我不得不为我的IAM用户添加下面提到的策略来解决这个问题。参考文献
git config --global credential.helper '!aws codecommit credential-helper $@'
git config --global credential.UseHttpPath true

政策:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codecommit:*"
            ],
            "Resource": "*"
        }
    ]
}

4

问题(Ubuntu系统)

$ git clone https://git-codecommit.ap-southeast-1.amazonaws.com/v1/repos/xx
正在克隆 'xx'...
致命错误:无法访问 'https://git-codecommit.ap-southeast-1.amazonaws.com/v1/repos/xx/': 所请求的 URL 返回错误:403

解决方案:

步骤 1:

git config --global credential.helper '!aws codecommit credential-helper $@'
git config --global credential.UseHttpPath true

检查:

cat ~/.gitconfig 
[user]
    email = your-email
    name = your-name
[credential]
    helper = !aws codecommit credential-helper $@
    UseHttpPath = true

步骤2:

  1. 服务 > IAM
  2. 点击“用户”(左侧面板)
  3. 点击您的用户名(主面板)
  4. 点击添加权限
  5. 点击直接附加现有策略
  6. 筛选策略中,键入AWSCodeCommitFullAccess
  7. 选择策略旁边的复选框,然后单击下一步:审核
  8. 点击添加权限

结果

$ git clone https://git-codecommit.ap-southeast-1.amazonaws.com/v1/repos/xx
正在克隆到 'xx'...
远程: 正在计数对象:3,完成。
正在拆包对象:100%(3/3),223 字节 | 223.00 KiB/s, 完成。

参考文献

  1. 使用 Git 和 AWS CodeCommit 入门

1
完美的解决方案,对我在运行于Windows WSL2上的Ubuntu系统进行了修复。 - Elias Strehle

3

对我来说,导致出现错误的根本原因是无论我在OSX上使用哪个版本的git,GIT始终从以下文件中挑选使用osxkeychain作为凭证助手配置:

/Applications/Xcode.app/Contents/Developer/usr/share/git-core/gitconfig

摆脱这个问题对我很有帮助,而且没有破坏任何东西。

谢谢!在我的情况下,第一个克隆和提交工作了,不知何故 - 但是后来就无法操作了。 - Kvisle

3
以下方案适用于使用Windows 10上的Git Bash的一小部分开发者。
如果您在“连接到存储库”选项卡中选择Windows作为操作系统,则命令的格式如下:
git config --global credential.helper "!aws codecommit credential-helper $@"
git config --global credential.UseHttpPath true

然而,如果您使用的是Git Bash,请选择“Linux、MacOS或Unix”选项。请注意,第一行使用单引号而不是双引号。

git config --global credential.helper '!aws codecommit credential-helper $@'
git config --global credential.UseHttpPath true

祝你好运。

参考: https://forums.aws.amazon.com/thread.jspa?threadID=198356 发布者:mwhardesty


3

My case in OSX.

The first step:

git config --global credential.helper '!aws codecommit credential-helper $@'
git config --global credentials.helper UseHttpPath=true

然而,验证 git config --list --show-origin

file:/usr/local/etc/gitconfig   credential.https://git-codecommit.eu-west-1.amazonaws.com.helper=!aws codecommit credential-helper $@
file:/usr/local/etc/gitconfig   credential.helper=osxkeychain
file:/Users/mine/.gitconfig  user.name=my-user
file:/Users/mine/.gitconfig  user.email=my@gmail.com
file:/Users/mine/.gitconfig  filter.lfs.clean=git-lfs clean -- %f
file:/Users/mine/.gitconfig  filter.lfs.smudge=git-lfs smudge -- %f
file:/Users/mine/.gitconfig  filter.lfs.process=git-lfs filter-process
file:/Users/mine/.gitconfig  filter.lfs.required=true
file:/Users/mine/.gitconfig  credential.helper=!aws codecommit credential-helper $@
file:/Users/mine/.gitconfig  credential.usehttppath=true

之前没有第一行,git使用osxkeychain具有优先权。因此,我必须执行git config --system ...

cat /usr/local/etc/gitconfig                                             
[credential "https://git-codecommit.eu-west-1.amazonaws.com"]
    helper = !aws codecommit credential-helper $@
    UseHttpPath = true
[credential]
    helper = osxkeychain

为了在存储其他凭据的情况下进行回退,因此指定了URL。

更新 https://docs.aws.amazon.com/codecommit/latest/userguide/troubleshooting-ch.html

由于某种原因,UseHttpPath = true 有时似乎没有被添加。因此,它可以(应该)在[credential]下面添加。


1

按照 AWS 文档 中的步骤进行操作, 在完成步骤后尝试运行此命令。

git config --system --unset credential.helper

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接