cURL to call REST Api - bash

So I want to call a REST API from Bamboo after a deployment has completed.
This API needs a username and password but it can't be stored in Bamboo as it seems it can be viewed in the Bash History of the Build agent.
I intended to use a script task and execute something like
curl -f -v -k --user "${bamboo.user}":"${bamboo.password}" -X POST https://bamboo.url/builds/rest/api/latest/queue/project_name"/
This would make the REST call. But the username and password is a problem.
I do have the option, however of using a PEM file. It can be provided so does anyone know if this can be used in conjunction with the cURL?
--OR--
One other thought- could I encrypt a password within a file in my source control, and somehow decrypt it on the build agent, and then have curl use the file instead of reading the password from the command line? How would this look in cURL?
Any ideas how this could be achieved?

Your command seems to have an extra quote at the end of your command
Using a pem file to authenticate with curl:
curl -E /path/to/user-cert.pem -X POST https://bamboo.url/builds/rest/api/latest/queue/project_name
The file should have both private key and public key inside.

Related

Automate Git clone from Private repo using SSH

I have been writing bash script to clone private Github repo using SSH.
Steps need to be followed -
Generate SSH key using ssh-keygen -t rsa -b 4096 -C "your email".
Copy the output of cat ~/.ssh/id_rsa.pub
Store it within - https://github.com/settings/keys
How do I automate step 3 in bash script.
Any help would be appreciated.
You would need to use the GitHub API POST /user/keys to add a public SSH key to the authenticated user's GitHub account.
It requires that you are authenticated via Basic Auth, or OAuth with at least write:public_key scope.
So your script must take as parameter.
Result: example
# Add a SSH-Key (type "user:passwd ^D"), output: JSON object, or JSON error
curl -X POST -u <user[:passwd]> https://api.github.com/user/keys \
--data "{\"title\": \"<title>\", \"key\": \"$(cat $HOME/.ssh/id_rsa.pub)\"}"
(other example here)

Verify User Has Added SSH Key To Git Profil Before Commiting/Pushing Projects In Bash

I'm wanting to write a bash script that verifies a user has created their SSH key on a gitlab server before the user clones or pushes any projects. Is there a git command that returns a true/false (0 or 1) that says "This person has their keys"?
I've tried a simple ssh gitlab.server.url and that seems to work, but I'm not sure if this is the best solution as I'm not wanting to verify they can ssh to the server, but rather their gitlab account has their SSH key(s) created to have the ability to clone/push projects. I was wondering if there was a specific git command.
You can make a cURL request to GitLab Users API and get the info on SSH keys for any user, given that you know their userId
curl --header "Private-Token: <your_PAT_here>" -X GET <your_GitLab_URL>/api/v4/users/<userId>/keys
In theory it works also with username, instead of userId, but I couldn't make it work. In order to get userId, I made an additional API call
curl --header "Private-Token: <your_PAT_here>" -X GET <your_GitLab_URL>/api/v4/users?username=<username_here>

Accessing commit history from github via terminal?

Need some quick help. Novice terminal user here. Trying to use these instructions: https://developer.github.com/v3/repos/statistics/#commit-activity to get commit history for a specific user.
However, I don't know what to do with this:
GET /repos/:owner/:repo/stats/contributors
When I replace the owner and repo with the specific names i'm using, nothing happens because I get this error in my terminal:
-bash: GET: command not found
This is a very time sensitive issue, help! Thanks!
You can follow this curl tutorial using GitHub's API to see how you would translate
GET /repos/:owner/:repo/stats/contributors
As you notice in the comments, the ":" shouldn't be included.
curl --include https://api.github.com/users/caspyin
Pass user credential to basic auth to access protected resources like a users starred gists, or private info associated with their profile
curl --user "caspyin:PASSWD" https://api.github.com/gists/starred
curl --user "caspyin:PASSWD" https://api.github.com/users/caspyin
Passing just the username without the colon (:) will cause you to be prompted for your account password.
This avoids having your password in your command line history
curl --user "caspyin" https://api.github.com/users/caspyin
In your case, replacing <owner> and <reponame> by the right owner and repo names:
curl https://api.github.com/repos/<owner>/<reponame>/stats/contributors

Create github repo bash file, NOT be prompted for password

I need to be able to create github repositories via bash scripts that run from a php page, so I need to be able to pass the password in the curl command or the API Key.
However I can not seem to find the API key as I believe this may be redundant now with V3 of the github API
I followed Is it possible to create a remote repo on GitHub from the CLI without opening browser? and it got me as far as being prompted for the password
Bash file looks like this:
#! /bin/bash
a=$1
curl="-u 'USERNAME' -p 'PASSWORD' https://api.github.com/user/repos -d '{\"name\":\""$a"\"}'"
curl $curl
This does not work as it is not liking the -p parameter it seems, tried -u 'USERNAME:PASSWORD' and it did not like that either and I can not seem to find the answer on github pages. Ideally I would use the API key as this would not leave my repo password exposed in my bash file correct?
Many thanks
curl -u 'dmalikov:my_password' https://api.github.com/user/repos -d '{"name":"HI"}' works fine for me, now I have this HI repo.

Using cURL to send JSON within a BASH script

Alright, here's what I'm trying to do. I'm attempting to write a quick build script in bash that will check out a private repository from GitHub on a remote server. To do this a "hands off" as possible, I want to generate a local RSA key set on the remote server and add the public key as a Deploy Key for that particular repository. I know how to do this using GitHub's API, but I'm having trouble building the JSON payload using Bash.
So far, I have this particular process included below:
#!/bin/bash
ssh-keygen -t rsa -N '' -f ~/.ssh/keyname -q
public_key=`cat ~/.ssh/keyname.pub`
curl -u 'username:password' -d '{"title":"Test Deploy Key", "key":"'$public_key'"}' -i https://api.github.com/repos/username/repository/keys
It's just not properly building the payload. I'm not an expert when it comes to string manipulation in Bash, so I could seriously use some assistance. Thanks!
It's not certain, but it may help to quote where you use public_key, i.e.
curl -u 'username:password' \
-d '{"title":"Test Deploy Key", "key":"'"$public_key"'"}' \
-i https://api.github.com/repos/username/repository/keys
Otherwise it will be much easier to debug if you use the shell's debugging options set -vx near the top of your bash script.
You'll see each line of code (or block (for, while, etc) as it is in your file. Then you see each line of code with the variables expanded to their values.
If you're still stuck, edit your post to show the expanded values of variables for the problem line in your script. What you have looks reasonable at first glance.

Resources