Mathlight
Mathlight

Reputation: 6653

Gitlab API add SSH-key

I've problems adding an SSH key to my gitlab server trough the API (It works well trough the webpage).

Gitlab information: Gitlab Information

I came across this issue (which was fixed here) which was related to an "wrong" openssh implementation. They've fixed this in milestone 7.10. Only thing... My server has openssh 6.6 installed:

OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.3, OpenSSL 1.0.1f 6 Jan 2014

Now, I don't know if that fix is backwards compatible or not, but maybe good to mention.

Also, the logs show no warning or errors or whatsoever. The /tmp/gitlab_key* files are generated on the server: output of /tmp/gitlab_key*

The problem I'm facing is that gitlab can't create the fingerprint trough the API. This is the responce I get from the API:

{
    "message": {
        "fingerprint": ["cannot be generated"]
    }
}

So right now I have no idea what the problem could be. I've been struggling with this for almost a week now, so I really hope that his problem could be fixed.

-just for the record, here's the script I'm using to add the ssh-key trough the API

#!/bin/bash
 
jsonFile="jsonResponce"
 
echo `curl http://gitserver/api/v3/session --data 'login=****&password=****'` > $jsonFile
userToken=$(jq '.private_token' $jsonFile)
 
finalUserToken=$(echo "$userToken" | tr -d '"')
 
echo "user token: $finalUserToken"
 
# Below key is for testing, will use output of cat ~/.ssh/id_rsa.pub later on
# sshKey="ssh-rsa AAAAB3N***** ****@***.com
 
# curl --data "private_token=$userToken&title=keyName&key=$sshKey" "http://gitserver/api/v3/user/keys"
 
rm $jsonFile

Upvotes: 5

Views: 8343

Answers (3)

linarnan
linarnan

Reputation: 112

id_rsa.pub is base64 encoded file, it contains + character

http post with application/x-www-form-urlencoded, need encode it's content preventing + being convert to (space)

try

curl --data-urlencode "key=$key_pub" --data-urlencode "title=$hostname" \
http://gitlabserver/api/v3/user/keys?private_token=$Token

see: this

Upvotes: 6

Sandeep
Sandeep

Reputation: 29341

Improving on @Mathlight's answer the following snippet uploads public ssh key to gitlab.com

curl -X POST -F "private_token=${GITLAB_TOKEN}" -F "title=$(hostname)" -F "key=$(cat ~/.ssh/id_rsa.pub)" "https://gitlab.com/api/v3/user/keys"

Upvotes: 5

Mathlight
Mathlight

Reputation: 6653

OP here

In the mean time I've updated the server to version 8.8 and changed the curl code a bit and now it's working like a charm:

curl -X POST -F "private_token=${userToken}" -F "title=${sshName}" -F "key=${sshKey}" "${gitServer}/user/keys"

Just in case anybody needs this in the future...

Upvotes: 2

Related Questions