Skip to content

Commit

Permalink
push code with ssh_config issue
Browse files Browse the repository at this point in the history
Signed-off-by: Tony Worm <tony@hofstadter.io>
  • Loading branch information
verdverm committed Sep 1, 2021
1 parent a894c2c commit b6b20da
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/mod/testdata/authd/sshconfig/vendor__priv_github.txt
@@ -1,5 +1,5 @@
# hof mod vendor - with deps
env GITHUB_TOKEN="thistokendoesnotexist"
env GITHUB_TOKEN=""
exec hof mod vendor

-- cue.mods --
Expand Down
2 changes: 1 addition & 1 deletion lib/mod/testdata/authd/sshkey/vendor__priv_github.txt
@@ -1,5 +1,5 @@
# hof mod vendor - with deps
env GITHUB_TOKEN="thistokendoesnotexist"
env GITHUB_TOKEN=""
exec hof mod vendor

-- cue.mods --
Expand Down
4 changes: 4 additions & 0 deletions lib/yagu/repos/git/fetch.go
Expand Up @@ -113,15 +113,19 @@ func Fetch(FS billy.Filesystem, remote, owner, repo, tag string, private bool) e
}

if private {
fmt.Println("git.Fetch")
if netrc, err := yagu.NetrcCredentials(remote); err == nil {
fmt.Println("NetRC")
gco.Auth = &http.BasicAuth{
Username: netrc.Login,
Password: netrc.Password,
}
} else if ssh, err := yagu.SSHCredentials(remote); err == nil {
fmt.Println("SSHCreds")
gco.Auth = ssh.Keys
gco.URL = fmt.Sprintf("%s@%s:%s", ssh.User, remote, srcRepo)
} else {
fmt.Println("NoAuth")
gco.URL = fmt.Sprintf("%s@%s:%s", "git", remote, srcRepo)
}
}
Expand Down
14 changes: 8 additions & 6 deletions lib/yagu/repos/github/fetch.go
Expand Up @@ -12,13 +12,17 @@ import (
"github.com/parnurzeal/gorequest"

"github.com/hofstadter-io/hof/lib/yagu"
"github.com/hofstadter-io/hof/lib/yagu/repos/git"
)

func Fetch(FS billy.Filesystem, owner, repo, tag string, private bool) (error) {
// TODO, ensure private requests force auth
// by default, we look for a token
// how can we fall back to ssh if needed?
// note, GitHub has deprecated basic auth

// If private, and no token auth, try git protocol
// need to catch auth errors and suggest how to setup
if private && os.Getenv(TokenEnv) == "" {
fmt.Println("github git fallback")
return git.Fetch(FS, "github.com", owner, repo, tag, private)
}

client, err := NewClient()
if err != nil {
Expand Down Expand Up @@ -79,8 +83,6 @@ func FetchTagZip(tag *github.RepositoryTag) (*zip.Reader, error) {

url := *tag.ZipballURL

fmt.Println("url:", url)

req := gorequest.New().Get(url)

// TODO, process auth logic here better, maybe find a way to DRY
Expand Down
10 changes: 9 additions & 1 deletion lib/yagu/ssh.go
@@ -1,6 +1,7 @@
package yagu

import (
"fmt"
"os"
"path/filepath"
"strings"
Expand All @@ -15,8 +16,11 @@ type SSHMachine struct {
}

func SSHCredentials(machine string) (SSHMachine, error) {
fmt.Println("ssh.CredsLookup")
// try to ssh config file
pk, err := ssh_config.GetStrict(machine, "IdentityFile")
pk := ""
pka, err := ssh_config.GetAllStrict(machine, "IdentityFile")
fmt.Println(pka, err)
if err != nil {
// try to load id_rsa.pub
hdir, err := os.UserHomeDir()
Expand All @@ -28,6 +32,8 @@ func SSHCredentials(machine string) (SSHMachine, error) {
// set pk file name to git's expected default, often the one uploaded per GitHub's docs
pk = filepath.Join(hdir, ".ssh", "id_rsa.pub")
}


if strings.HasPrefix(pk, "~") {
if hdir, err := os.UserHomeDir(); err == nil {
pk = strings.Replace(pk, "~", hdir, 1)
Expand All @@ -38,6 +44,8 @@ func SSHCredentials(machine string) (SSHMachine, error) {
usr = "git"
}

fmt.Println(" ", usr, pk)

pks, err := ssh.NewPublicKeysFromFile(usr, pk, "")
if err != nil {
return SSHMachine{}, err
Expand Down

0 comments on commit b6b20da

Please sign in to comment.