Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use auth info (if present) when updating pipelines #62

Merged
merged 5 commits into from
Aug 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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