Skip to content

Commit

Permalink
Use auth info (if present) when updating pipelines (#62)
Browse files Browse the repository at this point in the history
* abstract out code to get auth info to a function

* use auth info (if present) when updating pipelines

* change pollTicket to pollTicker

* add test for getAuthInfo

* split tests for getAuthInfo
  • Loading branch information
bidhan-a authored and michelvocks committed Aug 1, 2018
1 parent 7a905f4 commit fa633e1
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 29 deletions.
58 changes: 32 additions & 26 deletions pipeline/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,9 @@ func GitLSRemote(repo *gaia.GitRepo) error {
}

// Attach credentials if provided
var auth transport.AuthMethod
if repo.Username != "" && repo.Password != "" {
// Basic auth provided
auth = &http.BasicAuth{
Username: repo.Username,
Password: repo.Password,
}
} else if repo.PrivateKey.Key != "" {
auth, err = ssh.NewPublicKeys(repo.PrivateKey.Username, []byte(repo.PrivateKey.Key), repo.PrivateKey.Password)
if err != nil {
return err
}
auth, err := getAuthInfo(repo)
if err != nil {
return err
}

// Create client
Expand Down Expand Up @@ -78,23 +69,13 @@ func GitLSRemote(repo *gaia.GitRepo) error {
// The destination will be attached to the given repo obj.
func gitCloneRepo(repo *gaia.GitRepo) error {
// Check if credentials were provided
var auth transport.AuthMethod
if repo.Username != "" && repo.Password != "" {
// Basic auth provided
auth = &http.BasicAuth{
Username: repo.Username,
Password: repo.Password,
}
} else if repo.PrivateKey.Key != "" {
var err error
auth, err = ssh.NewPublicKeys(repo.PrivateKey.Username, []byte(repo.PrivateKey.Key), repo.PrivateKey.Password)
if err != nil {
return err
}
auth, err := getAuthInfo(repo)
if err != nil {
return err
}

// Clone repo
_, err := git.PlainClone(repo.LocalDest, false, &git.CloneOptions{
_, err = git.PlainClone(repo.LocalDest, false, &git.CloneOptions{
Auth: auth,
URL: repo.URL,
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
Expand Down Expand Up @@ -131,9 +112,16 @@ func updateAllCurrentPipelines() {
}
gaia.Cfg.Logger.Debug("checking pipeline: ", pipe.Name)
gaia.Cfg.Logger.Debug("selected branch : ", pipe.Repo.SelectedBranch)
auth, err := getAuthInfo(&pipe.Repo)
if err != nil {
// It's also an error if the repo is already up to date so we just move on.
gaia.Cfg.Logger.Error("error getting auth info while doing a pull request : ", err.Error())
return
}
tree, _ := r.Worktree()
err = tree.Pull(&git.PullOptions{
RemoteName: "origin",
Auth: auth,
})
if err != nil {
// It's also an error if the repo is already up to date so we just move on.
Expand All @@ -152,3 +140,21 @@ func updateAllCurrentPipelines() {
}
wg.Wait()
}

func getAuthInfo(repo *gaia.GitRepo) (transport.AuthMethod, error) {
var auth transport.AuthMethod
if repo.Username != "" && repo.Password != "" {
// Basic auth provided
auth = &http.BasicAuth{
Username: repo.Username,
Password: repo.Password,
}
} else if repo.PrivateKey.Key != "" {
var err error
auth, err = ssh.NewPublicKeys(repo.PrivateKey.Username, []byte(repo.PrivateKey.Key), repo.PrivateKey.Password)
if err != nil {
return nil, err
}
}
return auth, nil
}
61 changes: 61 additions & 0 deletions pipeline/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,64 @@ func TestUpdateAllPipelinesHundredPipelines(t *testing.T) {
t.Fatal("log output did not contain error message that the repo is up-to-date.: ", b.String())
}
}

func TestGetAuthInfoWithUsernameAndPassword(t *testing.T) {
repoWithUsernameAndPassword := &gaia.GitRepo{
URL: "https://github.com/gaia-pipeline/go-test-example",
LocalDest: "tmp",
Username: "username",
Password: "password",
}

auth, _ := getAuthInfo(repoWithUsernameAndPassword)
if auth == nil {
t.Fatal("auth should not be nil when username and password is provided")
}
}

func TestGetAuthInfoWithPrivateKey(t *testing.T) {
samplePrivateKey := `
-----BEGIN RSA PRIVATE KEY-----
MD8CAQACCQDB9DczYvFuZQIDAQABAgkAtqAKvH9QoQECBQDjAl9BAgUA2rkqJQIE
Xbs5AQIEIzWnmQIFAOEml+E=
-----END RSA PRIVATE KEY-----
`
repoWithValidPrivateKey := &gaia.GitRepo{
URL: "https://github.com/gaia-pipeline/go-test-example",
LocalDest: "tmp",
PrivateKey: gaia.PrivateKey{
Key: samplePrivateKey,
Username: "username",
Password: "password",
},
}
_, err := getAuthInfo(repoWithValidPrivateKey)
if err != nil {
t.Fatal(err)
}

repoWithInvalidPrivateKey := &gaia.GitRepo{
URL: "https://github.com/gaia-pipeline/go-test-example",
LocalDest: "tmp",
PrivateKey: gaia.PrivateKey{
Key: "random_key",
Username: "username",
Password: "password",
},
}
auth, _ := getAuthInfo(repoWithInvalidPrivateKey)
if auth != nil {
t.Fatal("auth should be nil for invalid private key")
}
}

func TestGetAuthInfoEmpty(t *testing.T) {
repoWithoutAuthInfo := &gaia.GitRepo{
URL: "https://github.com/gaia-pipeline/go-test-example",
LocalDest: "tmp",
}
auth, _ := getAuthInfo(repoWithoutAuthInfo)
if auth != nil {
t.Fatal("auth should be nil when no authentication info is provided")
}
}
6 changes: 3 additions & 3 deletions pipeline/ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ func InitTicker(store *store.Store, scheduler *scheduler.Scheduler) {
gaia.Cfg.Logger.Info(errorMessage)
gaia.Cfg.PVal = 1
}
pollTicket := time.NewTicker(time.Duration(gaia.Cfg.PVal) * time.Minute)
pollTicker := time.NewTicker(time.Duration(gaia.Cfg.PVal) * time.Minute)
go func() {
defer pollTicket.Stop()
defer pollTicker.Stop()
for {
select {
case <-pollTicket.C:
case <-pollTicker.C:
updateAllCurrentPipelines()
}
}
Expand Down

0 comments on commit fa633e1

Please sign in to comment.