I have a control-repo in gitlab and I want to automatically generate an sshkey and send it to my repository throu the gitlab api(using Deploy Token).
It is actually a straight forward question I want answered. What is the (or is there any)"standard" with puppet on how to send an sshkey to gitlab via its api? I have tried using this module(https://forge.puppet.com/abrader/gms), but it doest work. Below I explain what I have done and tried.
I have generated a deploy token by going to Settings->Repository->Deploy Tokens. Here I got a random string that I have now saved.
I have been able to automatically generate a key named 'manager-deploy-key'. I use this module to generate the key: https://forge.puppet.com/puppet/ssh_keygen
Ssh-keygen code
ssh_keygen { 'root':
bits => 4096,
type => 'rsa',
filename => '/root/.ssh/manager-deploy-key',
}
I then wanted to use this module: "https://forge.puppet.com/abrader/gms" to automatically send the newly generated key to my repo as a deploy key.
Should send deploykey
git_deploy_key { 'add_deploy_key_to_puppet_control':
ensure => present,
name => $::fqdn,
path => '/root/.ssh/manager-deploy-key.pub',
token => 'DEPLOY_TOKEN_HERE',
project_name => 'user/control-repo',
server_url => 'https://gitlab.com',
provider => 'gitlab',
}
This failed and therefore I chose to debug by adding --debug in my command --> "puppet apply --debug /file/test.pp"
Looking into the debugging information the response to the GET request is
"{\"error\":\"API V3 is no longer supported. Use API V4 instead.\"}"
The module doesnt work... Therefore I am now about to use curl to automate this myself. However, I really want to know if there is an easier alternative.
Seems like the only way really is to use curl since the module is deprecated. I have set up a command for it if someone needs an example.
curl -H "PRIVATE-TOKEN: ${git_api_token}" -H "Content-Type: application/json" \
-X POST -d "{\"title\":\"${git_ssl_keyname}\",\"key\":\"${sslpub}\", \"can_push\":\"true\"}" \
"https://gitlab.com/api/v4/projects/${project_id}/deploy_keys"
git_api_token is a token you generate on your account. I was unable to make this work with a deploy token that you can generate in a project.
git_ssl_keyname is the name of your ssh key. This can be anything you want.
sslpub is the actual key you want imported into your project.
project_id is the id of your project. If you visit your main project page it will be near the top.
If you want more information on gitlabs access token api visit https://docs.gitlab.com/ee/api/deploy_keys.html