Create github repo bash file, NOT be prompted for password - bash

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.

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>

Bash script for automating cntlm proxy password generation

I want to use the tool cntlm to authenticate via basic authentication against our corporate proxy server, which uses the ntlm protocol.
CNTLM uses a cntlm.conf file to get your proxy authentication credentials.
If you don't want to use your password in plain text, you can generate hashes with the command. (That's what I want!)
cntlm -u <user> -d <domain> -f -H.
The problem here is, that we use a rolling password policy here and I want to write a bash script for automating the hash generation, inserting in the config file and restarting the cntlm service.
Unfortunately the above shown command generates the password hashes interactively and I couldn't find a way to wrap this in a bash script.
I am a bash scripting newbie though and could use some advice here ;)
For my cntlm version (0.92.3) a simple echo pipe works like charm:
echo "PASSWORD" | cntlm -u USER -d DOMAIN -f -H

cURL to call REST Api

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.

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

Resources