How to debug "Could not add identity : agent refused operation" on Windows?

Viewed 4774

I'm trying to write a PowerShell script on Gitlab CI using Windows that will imitate my Linux build. As a first step, I need to add a private key so that I can download all of required submodules:

mkdir C:\Users\$env:UserName\.ssh
$key_path = "C:\Users\$env:UserName\.ssh\id_rsa"
echo "$DEPLOY_PRIVATE_KEY" > $key_path
(Get-Content $key_path -Raw).Replace("`r`n","`n") | Set-Content $key_path -Force
Set-Service -Name ssh-agent -StartupType Manual
Start-Service ssh-agent
ssh-add 

However, I'm getting:

Could not add identity "C:\Users\gitlab_runner/.ssh/id_rsa": agent refused operation

and because I don't really know Windows, I am not sure how to approch this. Unfortunately, Windows mirror of ssh-add does not have verbose mode -v. How can I get more info about agent refusal? What could be the reason for the refusal?

Cheers!

EDIT

Following @VonC suggestion, I made sure permissions of the folder/key are not too open and that my agent is indeed running:

$ Cmd /c Icacls  %UserProfile%\.ssh /c /t /Inheritance:d
 processed file: C:\Users\gitlab_runner\.ssh
 processed file: C:\Users\gitlab_runner\.ssh\id_rsa
 Successfully processed 2 files; Failed processing 0 files
$ Cmd /c Icacls  %UserProfile%\.ssh /c /t /Grant %UserName%:F
 processed file: C:\Users\gitlab_runner\.ssh
 processed file: C:\Users\gitlab_runner\.ssh\id_rsa
 Successfully processed 2 files; Failed processing 0 files
$ Cmd /c Icacls  %UserProfile%\.ssh /c /t /Remove Administrator "Authenticated Users" BUILTIN\Administrators BUILTIN Everyone System Users
 processed file: C:\Users\gitlab_runner\.ssh
 processed file: C:\Users\gitlab_runner\.ssh\id_rsa
 Successfully processed 2 files; Failed processing 0 files
$ Get-Service ssh-agent
 Status   Name               DisplayName                           
 ------   ----               -----------                           
 Running  ssh-agent          OpenSSH Authentication Agen

From a code perspective, I looked into ssh-add.c of openssh repo and , if I'm not wrong, found that error SSH_ERR_AGENT_FAILURE is thrown on fetching identity list.

1 Answers

Just in case, make sure to the permissions of the %USERPROFILE%\.ssh you just created are not too opened: see this answer to fix it.

Check the state of the SSH-Agent service to confirm it was indeed started, as in this issue.

Related