Skip to content

Commit fa633e1

Browse files
bidhan-amichelvocks
authored andcommitted
Use auth info (if present) when updating pipelines (#62)
* 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
1 parent 7a905f4 commit fa633e1

File tree

3 files changed

+96
-29
lines changed

3 files changed

+96
-29
lines changed

pipeline/git.go

+32-26
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,9 @@ func GitLSRemote(repo *gaia.GitRepo) error {
2929
}
3030

3131
// Attach credentials if provided
32-
var auth transport.AuthMethod
33-
if repo.Username != "" && repo.Password != "" {
34-
// Basic auth provided
35-
auth = &http.BasicAuth{
36-
Username: repo.Username,
37-
Password: repo.Password,
38-
}
39-
} else if repo.PrivateKey.Key != "" {
40-
auth, err = ssh.NewPublicKeys(repo.PrivateKey.Username, []byte(repo.PrivateKey.Key), repo.PrivateKey.Password)
41-
if err != nil {
42-
return err
43-
}
32+
auth, err := getAuthInfo(repo)
33+
if err != nil {
34+
return err
4435
}
4536

4637
// Create client
@@ -78,23 +69,13 @@ func GitLSRemote(repo *gaia.GitRepo) error {
7869
// The destination will be attached to the given repo obj.
7970
func gitCloneRepo(repo *gaia.GitRepo) error {
8071
// Check if credentials were provided
81-
var auth transport.AuthMethod
82-
if repo.Username != "" && repo.Password != "" {
83-
// Basic auth provided
84-
auth = &http.BasicAuth{
85-
Username: repo.Username,
86-
Password: repo.Password,
87-
}
88-
} else if repo.PrivateKey.Key != "" {
89-
var err error
90-
auth, err = ssh.NewPublicKeys(repo.PrivateKey.Username, []byte(repo.PrivateKey.Key), repo.PrivateKey.Password)
91-
if err != nil {
92-
return err
93-
}
72+
auth, err := getAuthInfo(repo)
73+
if err != nil {
74+
return err
9475
}
9576

9677
// Clone repo
97-
_, err := git.PlainClone(repo.LocalDest, false, &git.CloneOptions{
78+
_, err = git.PlainClone(repo.LocalDest, false, &git.CloneOptions{
9879
Auth: auth,
9980
URL: repo.URL,
10081
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
@@ -131,9 +112,16 @@ func updateAllCurrentPipelines() {
131112
}
132113
gaia.Cfg.Logger.Debug("checking pipeline: ", pipe.Name)
133114
gaia.Cfg.Logger.Debug("selected branch : ", pipe.Repo.SelectedBranch)
115+
auth, err := getAuthInfo(&pipe.Repo)
116+
if err != nil {
117+
// It's also an error if the repo is already up to date so we just move on.
118+
gaia.Cfg.Logger.Error("error getting auth info while doing a pull request : ", err.Error())
119+
return
120+
}
134121
tree, _ := r.Worktree()
135122
err = tree.Pull(&git.PullOptions{
136123
RemoteName: "origin",
124+
Auth: auth,
137125
})
138126
if err != nil {
139127
// It's also an error if the repo is already up to date so we just move on.
@@ -152,3 +140,21 @@ func updateAllCurrentPipelines() {
152140
}
153141
wg.Wait()
154142
}
143+
144+
func getAuthInfo(repo *gaia.GitRepo) (transport.AuthMethod, error) {
145+
var auth transport.AuthMethod
146+
if repo.Username != "" && repo.Password != "" {
147+
// Basic auth provided
148+
auth = &http.BasicAuth{
149+
Username: repo.Username,
150+
Password: repo.Password,
151+
}
152+
} else if repo.PrivateKey.Key != "" {
153+
var err error
154+
auth, err = ssh.NewPublicKeys(repo.PrivateKey.Username, []byte(repo.PrivateKey.Key), repo.PrivateKey.Password)
155+
if err != nil {
156+
return nil, err
157+
}
158+
}
159+
return auth, nil
160+
}

pipeline/git_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,64 @@ func TestUpdateAllPipelinesHundredPipelines(t *testing.T) {
156156
t.Fatal("log output did not contain error message that the repo is up-to-date.: ", b.String())
157157
}
158158
}
159+
160+
func TestGetAuthInfoWithUsernameAndPassword(t *testing.T) {
161+
repoWithUsernameAndPassword := &gaia.GitRepo{
162+
URL: "https://github.com/gaia-pipeline/go-test-example",
163+
LocalDest: "tmp",
164+
Username: "username",
165+
Password: "password",
166+
}
167+
168+
auth, _ := getAuthInfo(repoWithUsernameAndPassword)
169+
if auth == nil {
170+
t.Fatal("auth should not be nil when username and password is provided")
171+
}
172+
}
173+
174+
func TestGetAuthInfoWithPrivateKey(t *testing.T) {
175+
samplePrivateKey := `
176+
-----BEGIN RSA PRIVATE KEY-----
177+
MD8CAQACCQDB9DczYvFuZQIDAQABAgkAtqAKvH9QoQECBQDjAl9BAgUA2rkqJQIE
178+
Xbs5AQIEIzWnmQIFAOEml+E=
179+
-----END RSA PRIVATE KEY-----
180+
`
181+
repoWithValidPrivateKey := &gaia.GitRepo{
182+
URL: "https://github.com/gaia-pipeline/go-test-example",
183+
LocalDest: "tmp",
184+
PrivateKey: gaia.PrivateKey{
185+
Key: samplePrivateKey,
186+
Username: "username",
187+
Password: "password",
188+
},
189+
}
190+
_, err := getAuthInfo(repoWithValidPrivateKey)
191+
if err != nil {
192+
t.Fatal(err)
193+
}
194+
195+
repoWithInvalidPrivateKey := &gaia.GitRepo{
196+
URL: "https://github.com/gaia-pipeline/go-test-example",
197+
LocalDest: "tmp",
198+
PrivateKey: gaia.PrivateKey{
199+
Key: "random_key",
200+
Username: "username",
201+
Password: "password",
202+
},
203+
}
204+
auth, _ := getAuthInfo(repoWithInvalidPrivateKey)
205+
if auth != nil {
206+
t.Fatal("auth should be nil for invalid private key")
207+
}
208+
}
209+
210+
func TestGetAuthInfoEmpty(t *testing.T) {
211+
repoWithoutAuthInfo := &gaia.GitRepo{
212+
URL: "https://github.com/gaia-pipeline/go-test-example",
213+
LocalDest: "tmp",
214+
}
215+
auth, _ := getAuthInfo(repoWithoutAuthInfo)
216+
if auth != nil {
217+
t.Fatal("auth should be nil when no authentication info is provided")
218+
}
219+
}

pipeline/ticker.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ func InitTicker(store *store.Store, scheduler *scheduler.Scheduler) {
6060
gaia.Cfg.Logger.Info(errorMessage)
6161
gaia.Cfg.PVal = 1
6262
}
63-
pollTicket := time.NewTicker(time.Duration(gaia.Cfg.PVal) * time.Minute)
63+
pollTicker := time.NewTicker(time.Duration(gaia.Cfg.PVal) * time.Minute)
6464
go func() {
65-
defer pollTicket.Stop()
65+
defer pollTicker.Stop()
6666
for {
6767
select {
68-
case <-pollTicket.C:
68+
case <-pollTicker.C:
6969
updateAllCurrentPipelines()
7070
}
7171
}

0 commit comments

Comments
 (0)