I would like to be able to add a public key for SSH access to Azure DevOps via the API but I can't seem to find a way to do it in the doco. Doing it manually via the UI is not feasible since this is for many users and many projects. Thanks
2 Answers
The API is not documented, however we can track it with tools... You can add SSH public keys by calling below REST API:
Write a script to create the SSH keys with the ssh-keygen command for users, please see Use SSH key authentication for details.
Then call the REST API to add the public keys:
POST https://{Account}.visualstudio.com/_details/security/keys/Edit
Content-Type: application/json
Request body:
{"Description":"Test1001","__RequestVerificationToken":"","AuthorizationId":"","Data":"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDGZyIoG6eH9nTm/Cu2nVDa7hTNfaMWkwayghFmYTvqCkOwao2YJesGVih1fA3oR4tPsVv4+Vr8wxPCfJCboUrL9NDoH1tAMsIlkQZHqgaJwnGNWnPrnp0r2+wjLQJFPq/pPd8xKwr6QU0BxzZ4RuLDfMFz/MR1cQ2iWWKJuO/TXYrSPtY9XqsmMC8Zo4zJln40PGZt+ecOyQCNHCXsEJ3C+QIUXSqAkb8yknZ4apLf1oqfFRngtV4w84Ua/ZLpNduPZrBcm/mCU5Jq6H37jxhx4kluheJrfpAXbvbQlPTKa2zaOHp7wb3B2E2HvESJmx5ExNuAHoygcq/QGjsRsiUR andy@xxx@ws0068"}
4 Comments
If someone is seeing this after 2024, the url for devops has changed to dev.azure.com/orgname
And for the auth part, I just successfully added an SSH key with
ssh-keygen -t rsa
...
TOKEN=$(az account get-access-token | jq -r .accessToken)
KEY=$(cat [keyfile])
http post https://dev.azure.com/[orgname]/_details/security/keys/Edit Authorization:"Bearer $TOKEN" Description="my-new-key" Data="$KEY"
this uses Httpie cli.
I had problems adding the keys via Web UI, and after looking at the API responses it was clear that somehow DevOps had a public key with the signature i was trying to add, but refused to show it in the UI or accept it for Git. So I generated a new key and it worked.
