5

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
2
  • I don't see any errors here, or any question. Can you clarify what you're asking about? Commented Sep 1, 2015 at 12:56
  • @larsks hmm, I'm sorry. I've added the error I get. Hope that it's clear? Or do you need more information? Commented Sep 1, 2015 at 13:06

3 Answers 3

7

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" \
https://gitlabserver/api/v4/user/keys?private_token=$Token

see: this

Sign up to request clarification or add additional context in comments.

Comments

5

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"

1 Comment

BTW, updated "https://gitlab.com/api/v4/user/keys" API route works too (is necessary in version >= 15 of GitLab).
3

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.