def __init__(self, origin=IEMLDB_DEFAULT_GIT_ADDRESS, credentials=pygit2.Username('git'), branch='master', commit_id=None, folder=None): """ :param origin: :param credentials: :param branch: the branch to checkout :param commit_id: the commit to checkout :param folder: """ self.origin = origin # self.remotes = {'origin': origin} self.credentials = credentials if folder: self.folder = os.path.abspath(folder) else: self.folder = get_local_cache_dir(origin) # clone repository _ = self.repo # self.checkout(branch, None) # update database # self.pull(remote='origin') self.checkout(branch, commit_id)
def credentials(self, url, username_from_url, allowed_types): if allowed_types & pygit2.credentials.GIT_CREDTYPE_USERNAME: return pygit2.Username("git") elif allowed_types & pygit2.credentials.GIT_CREDTYPE_SSH_KEY: return pygit2.Keypair(username_from_url, self.config.SSH_PUB_KEY, self.config.SSH_PRI_KEY, self.config.SSH_PRI_KEY) else: return None
def _get_credentials(_auth): if not _auth: return None _agent = _auth.get("agent", False) _public_key = _auth.get("public_key", None) _private_key = _auth.get("private_key", None) _username = _auth.get("user", None) _passphrase = _auth.get("pass", None) if _agent: credentials = pygit2.KeypairFromAgent(_username or "git") elif _public_key and _private_key: if os.path.exists(_public_key) and os.path.exists(_private_key): credentials = pygit2.Keypair(_username or "git", _public_key, _private_key, _passphrase or "") else: credentials = pygit2.KeypairFromMemory(_username or "git", _public_key, _private_key, _passphrase or "") elif _username and _passphrase: credentials = pygit2.UserPass(_username, _passphrase) elif _username: credentials = pygit2.Username(_username) else: credentials = None return credentials
def credentials(self, url, username_from_url, allowed_types): if allowed_types & pygit2.credentials.GIT_CREDTYPE_USERNAME: return pygit2.Username(username_from_url) elif pygit2.credentials.GIT_CREDTYPE_SSH_KEY & allowed_types: if "SSH_AUTH_SOCK" in os.environ: # Use ssh agent for authentication print( f"SSH_AUTH_SOCK set {username_from_url} {url} {os.environ['SSH_AUTH_SOCK']}" ) return pygit2.KeypairFromAgent(username_from_url) else: print("default") ssh = os.path.join(os.path.expanduser("~"), ".ssh") pubkey = os.path.join(ssh, "id_rsa.pub") privkey = os.path.join(ssh, "id_rsa") # check if ssh key is available in the directory if os.path.isfile(pubkey) and os.path.isfile(privkey): return pygit2.Keypair(username_from_url, pubkey, privkey, "") else: raise Exception( "No SSH keys could be found, please specify SSH_AUTH_SOCK or add keys to " + "your ~/.ssh/") # end # end raise Exception( "Only unsupported credential types allowed by remote end")
def credentials(self, url, username_from_url, allowed_types): if allowed_types & pygit2.credentials.GIT_CREDENTIAL_SSH_KEY: return pygit2.Keypair(username_from_url, str(Path.home() / '.ssh' / 'id_rsa.pub'), str(Path.home() / '.ssh' / 'id_rsa'), '') elif allowed_types & pygit2.credentials.GIT_CREDENTIAL_USERNAME: return pygit2.Username(username_from_url) return None
def credentials(self, url, username_from_url, allowed_types): if allowed_types & pygit2.credentials.GIT_CREDTYPE_USERNAME: return pygit2.Username(GIT_USERNAME) elif allowed_types & pygit2.credentials.GIT_CREDTYPE_SSH_KEY: return pygit2.Keypair(GIT_USERNAME, join(HOME, SSH_PUBLIC_KEY), join(HOME, SSH_PRIVATE_KEY), "") else: return None
def credentials(self, url, username_from_url, allowed_types): if allowed_types & pygit2.credentials.GIT_CREDENTIAL_USERNAME: return pygit2.Username(self.username) if allowed_types & pygit2.credentials.GIT_CREDENTIAL_SSH_KEY: return pygit2.Keypair(self.username, self.ssh_key_public_path, self.ssh_key_private_path, '') return None
def credentials(self, url, username_from_url, allowed_types): if allowed_types & pygit2.credentials.GIT_CREDENTIAL_USERNAME: return pygit2.Username('git') elif allowed_types & pygit2.credentials.GIT_CREDENTIAL_SSH_KEY: if self.passphrase is None: self.passphrase = getpass('passphrase: ') return pygit2.Keypair('git', os.path.expanduser('~/.ssh/id_rsa.pub'), os.path.expanduser('~/.ssh/id_rsa'), self.passphrase) else: return None
def credentials( self, url: str, username_from_url: typing.Optional[str], allowed_types: int ) -> typing.Optional[ typing.Union[pygit2.Keypair, pygit2.UserPass, pygit2.Username] ]: """ If the remote server requires authentication, this function will be called and its return value used for authentication. """ if allowed_types & pygit2.credentials.GIT_CREDTYPE_SSH_KEY: return pygit2.KeypairFromAgent(username_from_url) elif allowed_types & pygit2.credentials.GIT_CREDTYPE_USERPASS_PLAINTEXT: username = username_from_url or click.prompt("Username") password = click.prompt("Password", hide_input=True) return pygit2.UserPass(username=username, password=password) elif allowed_types & pygit2.credentials.GIT_CREDTYPE_USERNAME: return pygit2.Username(username_from_url) else: return None
def credentials(self, url, username_from_url, allowed_types): if allowed_types & pygit2.credentials.GIT_CREDENTIAL_USERNAME: return pygit2.Username("git") elif allowed_types & pygit2.credentials.GIT_CREDENTIAL_SSH_KEY: return pygit2.Keypair( "git", os.path.expanduser('~/.ssh/id_rsa.pub'), os.path.expanduser('~/.ssh/id_rsa'), "") elif allowed_types & pygit2.credentials.GIT_CREDENTIAL_USERPASS_PLAINTEXT: # noqa # try to read from git-credential-helper parts = urllib.parse.urlparse(url) logging.debug( 'Tring to get credential from git-credential: %s %s', parts.scheme, parts.netloc ) stdin = 'protocol={0.scheme}\nhost={0.netloc}\n' \ .format(parts) try: result = subprocess.run( ['git', 'credential', 'fill'], input=stdin.encode('utf-8'), capture_output=True, check=True) for line in result.stdout.decode('utf-8').split('\n'): logging.debug('Got line: %s', line) if line.startswith('username='******'=', 1)[1] elif line.startswith('password='******'=', 1)[1] logging.debug('Got %r %r', username, password) return pygit2.UserPass(username, password) except Exception: # When libgit2 callback do not print traceback when # error, we do it manually. logging.exception( 'Error when get credential from git-credential') return None else: return None