簡體   English   中英

無法使用 SSH 訪問 AWS CodeCommit

[英]Can't access AWS CodeCommit with SSH

在弄清楚如何讓 AWS CodeCommit 與標准 SSH 身份驗證一起工作時遇到了難以置信的困難。 看到另一個類似的話題,但沒有答案,我還不能發表評論。 這是在 Windows 上使用 Git Bash。

復制步驟

  1. 創建了具有完全權限的 IAM 用戶 (AwsAdministrator)
  2. 來自 Git Bash ~/.ssh
  3. “cat id_rsa.pub”並將 output 復制到剪貼板
  4. 在 IAM 控制台中,單擊按鈕以添加 SSH 密鑰並粘貼到輸入字段中。 單擊保存。
  5. 嘗試訪問 CodeCommit 存儲庫(在本例中嘗試推送)並獲得“權限被拒絕”。

Git + SSH output

這是我從 SSH 使用 DEBUG3 日志記錄得到的:

debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /c/Users/Dan/.ssh/id_rsa
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Server accepts key: pkalg ssh-rsa blen 535
debug2: input_userauth_pk_ok: fp SHA256:<omitted>
debug3: sign_and_send_pubkey: RSA SHA256:<same-as-above>
debug1: Authentications that can continue: publickey
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
Permission denied (publickey).
fatal: Could not read from remote repository.

為了比較,這是我使用與 GitHub 完全相同的 SSH 鍵得到的結果:

debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /c/Users/Dan/.ssh/id_rsa
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Server accepts key: pkalg ssh-rsa blen 535
debug2: input_userauth_pk_ok: fp SHA256:<same-as-above>
debug3: sign_and_send_pubkey: RSA SHA256:<same-as-above>
debug1: Authentication succeeded (publickey).
Authenticated to github.com ([192.30.252.130]:22).

上面的 output 來自運行正常的 git 命令,例如git push origin master ,在.ssh/config中啟用了 ssh 調試日志記錄:

Host git-codecommit.us-east-1.amazonaws.com
  LogLevel DEBUG3

Host github.com
  LogLevel DEBUG3

您似乎錯過了設置 SSH 的一步。 您需要將此信息添加到您的 .ssh/config 文件中:

Host git-codecommit.us-east-1.amazonaws.com
   User Your-SSH-Key-ID, such as APKAEIBAERJR2EXAMPLE
   IdentityFile Your-Private-Key-File, such as ~/.ssh/codecommit_rsa or ~/.ssh/id_rsa

您可以從 IAM 控制台獲取 Your-SSH-Key-ID。

我的情況有點不同。

“安全憑證”選項卡同時包含“訪問密鑰”和“AWS CodeCommit 的 SSH 密鑰”。 確保您使用的是 CodeCommit 密鑰 ID。

確保配置文件沒有保存為 txt 文件並使用 SSH Key ID,而不是 Access

在此處輸入圖片說明 這有效

Host git-codecommit.us-west-2.amazonaws.com
    User SSHKEYID
    IdentityFile ~/.ssh/id_rsa

請遵循 AWS 中的以下文檔

Linux

https://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-ssh-unixes.html

視窗

https://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-ssh-windows.html

我已經測試了僅在 Windows 上設置 SSH 連接,請按照上述鏈接測試 Linux 的 SSH 連接;

以下是在 Windows 10 上為 codecommit 設置 SSH 連接的步驟。

第1步

CodeCommit 的初始配置(請參閱文檔鏈接)

第2步

安裝 Git(請參考文檔鏈接)

第 3 步

通過運行以下命令生成 ssh 密鑰:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

運行此命令后,它應該在 C:\\Users{user name here}.ssh 目錄中生成公鑰(.pub)和私鑰

第四步

登錄 aws 管理控制台並轉到IAM--> 用戶 --> 選擇目標用戶 --> 安全憑證選項卡 --> AWS CodeCommit 的 SSH 密鑰,然后單擊上傳 ssh 密鑰。 復制粘貼公鑰的內容,例如 C:\\Users{user name here}.ssh\\id_rsa.pub 並保存更改。但是您可以在使用命令ssh-keygen -t rsa -b 4096 -C "your_email@example.com".為您的公鑰(id_rsa.pub)選擇不同的名稱ssh-keygen -t rsa -b 4096 -C "your_email@example.com".

第 5 步:

上傳公鑰后,復制SSH密鑰ID。

第 6 步

創建 C:\\Users{user name here}.ssh\\config 文件(沒有文件擴展名,在 Windows 中將其另存為 config 並選擇所有文件類型選項。Config.txt 很可能不起作用)。 將以下內容粘貼到 C:\\Users{user name here}.ssh\\config 文件中:

Host git-codecommit.*.amazonaws.com
  User {copy SSH key ID here}
  IdentityFile ~/.ssh/{copy name of the public key here }
  
Host git-codecommit.*.amazonaws.com
  User THISISEXMAPLE123
  IdentityFile ~/.ssh/id_rsa
  

第 7 步

  git clone ssh://Your-SSH-Key-ID@git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDemoRepo my-demo-repo

制作git clone時嘗試使用 sshkey-ID 在此處輸入圖片說明

所以 git clone 應該是這樣的:

git clone ssh://<SSHKEY-ID>@git-codecommit.<REGION>.amazonaws.com/v1/repos/<REPO-NAME>

AWS 核心配置的變化各不相同。 您可能會正確完成所有配置,但仍然存在連接問題。 要直接和明確。

嘗試將默認的 git url 來源從 old 更改為 <KEY_ID>@git-codecomitxxxxxxxx\/v1\/repos\/xxxxx 確保密鑰服務作為存儲庫的用戶所有者

並再次推動。

在我的例子中,我配置了~/.ssh/config如下

Host remote
  HostName  git-codecommit.us-east-1.amazonaws.com
  User APLAY5AJ2VA0XEXAMPLE
  IdentityFile ~/.ssh/id_rsa

我嘗試用命令克隆

git clone ssh://git-codecommit.us-east-1.amazonaws.com/v1/repos/repo-name

並導致權限被拒絕。

但是通過運行以下命令完成了工作。

git clone ssh://remote/v1/repos/repo-name

您遇到的問題是 Windows 在 AWS 文檔的第 8 步到第 9 步期間自動將配置文件保存為 config.txt。

https://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-ssh-windows.html?icmpid=docs_acc_console_connect_np#setting-up-ssh-windows-keys-windows

您可以按照以下說明完成此操作

  1. 開通cmd
  2. cd Users\Mr.S>cd C:\Users\Mr.S.ssh(這里導航到config.txt文件所在的.ssh目錄)
  3. 鍵入 copy config.txt config(此處您使用副本 function 將 config.txt 文件轉換為配置文件

完成上述操作后,繼續下一步。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM