Skip to content

Commit

Permalink
feat: allowing fogg to run with a github app token (#679)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakeyheath authored Jul 15, 2022
1 parent 56930f1 commit 161a7c5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
41 changes: 39 additions & 2 deletions util/module_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,54 @@ func convertSSHToGithubHTTPURL(sURL, token string) string {
return fmt.Sprintf("git::%s", u.String())
}

// Changes a URL to use the git protocol over HTTPS instead of SSH.
// If the URL was not a remote URL or an git/SSH protocol,
// it will return the path that was passed in. If it does
// convert it properly, it will add Github credentials to the path
func convertSSHToGithubAppHTTPURL(sURL, token string) string {
// only detect the remote destinations
s, err := getter.Detect(sURL, token, []getter.Detector{
&getter.GitLabDetector{},
&getter.GitHubDetector{},
&getter.GitDetector{},
&getter.BitBucketDetector{},
&getter.S3Detector{},
&getter.GCSDetector{},
})
if err != nil {
logrus.Debug(err)
return sURL
}

splits := strings.Split(s, "git::ssh://git@")
if len(splits) != 2 {
return sURL
}
u, err := url.Parse(fmt.Sprintf("https://%s", splits[1]))
if err != nil {
logrus.Debug(err)
return sURL
}
u.User = url.UserPassword("x-access-token", token)

// we want to force the git protocol
return fmt.Sprintf("git::%s", u.String())
}

func MakeDownloader(src string) (*Downloader, error) {
type HTTPAuth struct {
GithubToken *string
GithubToken *string
GithubAppToken *string
}
var httpAuth HTTPAuth
err := envconfig.Process("fogg", &httpAuth)
if err != nil {
return nil, errs.WrapUser(err, "unable to get env FOGG_GITHUBTOKEN flag")
return nil, errs.WrapUser(err, "unable to parse Github tokens")
}
if httpAuth.GithubToken != nil {
src = convertSSHToGithubHTTPURL(src, *httpAuth.GithubToken)
} else if httpAuth.GithubAppToken != nil {
src = convertSSHToGithubAppHTTPURL(src, *httpAuth.GithubAppToken)
}
return &Downloader{Source: src}, nil
}
Expand Down
14 changes: 14 additions & 0 deletions util/module_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ func TestMakeDownloader(t *testing.T) {
r.Equal(fmt.Sprintf("git::https://%[email protected]/chanzuckerberg/test-repo//terraform/modules/eks-airflow?ref=v0.80.0", creds), downloader.Source)
}

func TestMakeDownloaderGithubApp(t *testing.T) {
r := require.New(t)
creds := "REDACTED"
downloader, err := MakeDownloader("[email protected]:chanzuckerberg/test-repo//terraform/modules/eks-airflow?ref=v0.80.0")
r.NoError(err)
r.Equal("[email protected]:chanzuckerberg/test-repo//terraform/modules/eks-airflow?ref=v0.80.0", downloader.Source)

os.Setenv("FOGG_GITHUBAPPTOKEN", creds)
defer os.Unsetenv("FOGG_GITHUBAPPTOKEN")
downloader, err = MakeDownloader("[email protected]:chanzuckerberg/test-repo//terraform/modules/eks-airflow?ref=v0.80.0")
r.NoError(err)
r.Equal(fmt.Sprintf("git::https://x-access-token:%[email protected]/chanzuckerberg/test-repo//terraform/modules/eks-airflow?ref=v0.80.0", creds), downloader.Source)
}

func TestConvertSSHToHTTP(t *testing.T) {
r := require.New(t)

Expand Down

0 comments on commit 161a7c5

Please sign in to comment.