Skip to content

Commit

Permalink
fix: better credential handling
Browse files Browse the repository at this point in the history
  • Loading branch information
chaaz committed Sep 17, 2020
1 parent a7140f6 commit da09bf8
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/snippets/common-env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ key: common-env
value:
RUSTFLAGS: '-D warnings'
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_USER: ${{ github.actor }}
2 changes: 2 additions & 0 deletions .github/workflows-src/pr.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
# Edit this file, and then use `yambler`
# (github.com/chaaz/versio-actions/tree/main/yambler) to stitch.
name: pr
on:
- workflow_dispatch # pull_request
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows-src/release.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
# Edit this file, and then use `yambler`
# (github.com/chaaz/versio-actions/yambler) to assemble the snippets.
# (github.com/chaaz/versio-actions/tree/main/yambler) to stitch.
name: release
on:
- workflow_dispatch
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ name: pr
env:
RUSTFLAGS: "-D warnings"
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
GITHUB_USER: "${{ github.actor }}"
jobs:
project-matrixes:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ name: release
env:
RUSTFLAGS: "-D warnings"
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
GITHUB_USER: "${{ github.actor }}"
jobs:
project-matrixes:
runs-on: ubuntu-latest
Expand Down
31 changes: 24 additions & 7 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ use error_chain::bail;
use git2::build::CheckoutBuilder;
use git2::string_array::StringArray;
use git2::{
AnnotatedCommit, AutotagOption, Blob, Commit, Cred, Diff, DiffOptions, FetchOptions, Index, Object, ObjectType, Oid,
PushOptions, Reference, ReferenceType, Remote, RemoteCallbacks, Repository, RepositoryOpenFlags, RepositoryState,
ResetType, Revwalk, Signature, Sort, Status, StatusOptions, Time
AnnotatedCommit, AutotagOption, Blob, Commit, Cred, CredentialType, Diff, DiffOptions, FetchOptions, Index, Object,
ObjectType, Oid, PushOptions, Reference, ReferenceType, Remote, RemoteCallbacks, Repository, RepositoryOpenFlags,
RepositoryState, ResetType, Revwalk, Signature, Sort, Status, StatusOptions, Time
};
use log::{error, info, trace, warn};
use regex::Regex;
use serde::Deserialize;
use std::cell::RefCell;
use std::cmp::{min, Ord};
use std::collections::HashMap;
use std::env::var;
use std::ffi::OsStr;
use std::fmt;
use std::io::{stdout, Write};
Expand Down Expand Up @@ -1084,8 +1085,7 @@ fn do_fetch(remote: &mut Remote, refs: &[&str], all_tags: bool) -> Result<()> {

let mut cb = RemoteCallbacks::new();

cb.credentials(|_url, username_from_url, _allowed_types| Cred::ssh_key_from_agent(username_from_url.unwrap()));

cb.credentials(find_creds);
cb.transfer_progress(|stats| {
if stats.received_objects() == stats.total_objects() {
info!("Resolving deltas {}/{}", stats.indexed_deltas(), stats.total_deltas());
Expand Down Expand Up @@ -1134,12 +1134,29 @@ fn do_fetch(remote: &mut Remote, refs: &[&str], all_tags: bool) -> Result<()> {
Ok(())
}

fn find_creds(
_url: &str, username_from_url: Option<&str>, _allowed_types: CredentialType
) -> std::result::Result<Cred, git2::Error> {
if let Some(username_from_url) = username_from_url {
if let Ok(v) = Cred::ssh_key_from_agent(username_from_url) {
return Ok(v);
}
}

if let Ok((user, token)) = var("GITHUB_TOKEN").and_then(|token| var("GITHUB_USER").map(|user| (user, token))) {
if let Ok(v) = Cred::userpass_plaintext(&user, &token) {
return Ok(v);
}
}

Err(git2::Error::from_str("Unable to authenticate"))
}

pub fn do_push(repo: &Repository, remote_name: &str, specs: &[String]) -> Result<()> {
info!("Pushing specs {:?} to remote {}", specs, remote_name);
let mut cb = RemoteCallbacks::new();

cb.credentials(|_url, username_from_url, _allowed_types| Cred::ssh_key_from_agent(username_from_url.unwrap()));

cb.credentials(find_creds);
cb.push_update_reference(|rref, status| {
if let Some(status) = status {
error!("Couldn't push reference {}: {}", rref, status);
Expand Down

0 comments on commit da09bf8

Please sign in to comment.