gitpython how to add credentials to repo object - credentials

I want to push changes to git
but I need to use specific credentials (username, pass) not ssh key.
For example I use command
repo = git.Repo(my_folder)
repo.remote().push(...)
$ permission denied
Is there a way to add these credentials to repo object?

Related

Git pull/clone with username and password in AWS Code Commit

I Need to do a git pull using https url as a oneline command . this command I need to integrate in a bash script . But all the time it is asking the usernmae and password .
The repository is in AWS codecommit
Try this:
git clone https://username:password#git-codecommit.us-east-1.amazonaws.com/v1../repos../..
This way worked for me for CodeCommit (AWS) repository
Check this link: Enter user password in command
As is described perfectly in that post, you basically have three options:
Store the password in .netrc file (with 600 permissions). Make sure you clone the repo specifying the username in the url.
Clone the repo with https://user:pass#domain/repo . Take into account that your password will be visible in several places...
Use the credential helper.
As an update, AWS has released their remote git remote codecommit. With proper IAM setup, you can do oneline pulls without even passing username and passwords. This is now the recommended method by AWS. It can be setup on your local or on a container that's running in an AWS Pipeline for example.
i.e. git clone codecommit://HelloWorldRepo myLocalHelloWorldRepo
And then you can git pull as normal.
Full documentation is here:
https://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-git-remote-codecommit.html

Gitlab: While cloning a repo via ssh it keeps asking for password

I have installed gitlab on LINUX(RHEL 6.7). While cloning the repo via ssh it keeps asking for the password for git, however it is working fine by using http:
$ git clone http://MGJV67#il06epclin1.am.mot-solutions.com/MGJV67/NewPROJ.git
Cloning into 'NewPROJ'...
warning: You appear to have cloned an empty repository.
mgjv67#mgjv67-430 MINGW64 /c/TCS/Gitlab/NewPROJ (master)
$ git clone git#il06epclin1.am.mot-solutions.com:MGJV67/NewPROJ.git
Cloning into 'NewPROJ'...
git#il06epclin1.am.mot-solutions.com's password:
Permission denied, please try again.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
While you can clone anonymously over http, you can only clone over SSH with a valid SSH identity.
Create an SSH key and add the public key to your Gitlab user account, or add it as a deploy key for your project in case you only need read-only access.

Not able to access git repo from jenkins

I have configured Jenkins on a Linux machine and my git repo is on an another Linux server. But when I try to give the URL of the repo to Jenkins I get the following error.
Failed to connect to repository : Command "git -c core.askpass=true ls-remote -h ssh://user#ip/~/export1 HEAD" returned status code 128:
stdout:
stderr: Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
What could be the cause for this?
I have seen that both the systems' rsa key is present in each other's .ssh/known_hosts folder.
I am able to pull or clone code from the repo to a folder in Jenkins system as well. So why is Jenkins not able to take it?
I have also tried the URL user#ip:/fullpath/to/repo
You need to make sure Jenkins is running as the right user (the one who has the keys in ~/.ssh/id_rsa(.pub)
That user might not be the same as the one used in the ssh url: user#ip means you are connecting to ip using an account which has your public key in ~user/.ssh/authorized_key.
So the Jenkins process must be run by an account which has the private and public key, whose public key is in ~user/.ssh/authorized_key on the git server side.
And that account should have done (only once) an ssh-keyscan -H ip >> ~account/.ssh/known_hosts before any ssh attempt, in order to record the git server ip as a known host.
Does it being a bare repo make any difference or change in the URL?
No. The .git at the end of the bare repo folder is optional when used as an url.
Further to #VonC's reply, you can also use the Credentials plugin to define a set of credentials on your Jenkins master that your Jenkins job uses to access your Git repo. This allows you to run Jenkins itself as a different user from there one that has access to the Git repo.
The main problem was the security of the systems. I hadnt checked the authentication mechanisms on my server. The password authentication to the git server was causing the problem because the jenkins machine tries to directly fire a ls-remote to the path. When you do the same thing on the terminal you will be prompted for a password and then itl accept. When I set the password authentication and UsePAM to no and enabled the RSA authentication, pubkey authentication and authorised key setting to yes in the sshd_config file, and restarted, it was able to access the repo and I dint get this error.

How to push code using Github API?

I want to push a directory to Github via nodejs app. Here's what I currently do.
I take username, password and new repository name from user. Then, I create new repository via Github API(https://developer.github.com/v3/repos/#create).
Now I want to push some specific code to this repository. I can't use node.js child process (git push origin master), because that requires username and password.
How can I push code to Github via API. (And if via command line, then without entering password manually)
To skip the password on each commit you need to tell the git to store the credential in a separate place. This can be done by using the following command:
git config --global credential.helper cache
This will store the password for 15 minutes. If you wish you can extend this time by specifying the amount of duration in milliseconds:
git config --global credential.helper "cache --timeout=3600"

Git: How to change password of credentials used to clone a repository

I am working on linux and I clone a private repository using my github account credentials. But over the period of time my password has changed for github and whenever I try to use git pull it is giving me an error
remote: Invalid username or password.
How can I change the password which I used while cloning the repository for the first time?
You the issue an git remote -v and check what kind of auth you are using. I always use git protocol (which uses SSH). You can freely edit those remote urls in ./git/config file. I believe you cloned it using HTTP (or using SSH w/o .ssh key file being present).
If you want to use SSH, you can follow this: https://help.github.com/articles/generating-ssh-keys
Then you will never need to worry about passwords again.

Resources