@@ -11,8 +11,10 @@ import (
11
11
"github.com/pkg/errors"
12
12
"go.uber.org/zap"
13
13
"gopkg.in/src-d/go-git.v4/plumbing/transport"
14
+ "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
14
15
15
16
"github.com/picostack/pico/config"
17
+ "github.com/picostack/pico/secret"
16
18
"github.com/picostack/pico/task"
17
19
)
18
20
@@ -24,7 +26,7 @@ type GitWatcher struct {
24
26
directory string
25
27
bus chan task.ExecutionTask
26
28
checkInterval time.Duration
27
- ssh transport. AuthMethod
29
+ secrets secret. Store
28
30
29
31
targetsWatcher * gitwatch.Session
30
32
state config.State
@@ -42,13 +44,13 @@ func NewGitWatcher(
42
44
directory string ,
43
45
bus chan task.ExecutionTask ,
44
46
checkInterval time.Duration ,
45
- ssh transport. AuthMethod ,
47
+ secrets secret. Store ,
46
48
) * GitWatcher {
47
49
return & GitWatcher {
48
50
directory : directory ,
49
51
bus : bus ,
50
52
checkInterval : checkInterval ,
51
- ssh : ssh ,
53
+ secrets : secrets ,
52
54
53
55
initialise : make (chan bool ),
54
56
newState : make (chan config.State , 16 ),
@@ -161,11 +163,16 @@ func (w *GitWatcher) watchTargets() (err error) {
161
163
if t .Branch != "" {
162
164
dir = fmt .Sprintf ("%s_%s" , t .Name , t .Branch )
163
165
}
166
+ auth , err := w .getAuthForTarget (t )
167
+ if err != nil {
168
+ return err
169
+ }
164
170
zap .L ().Debug ("assigned target" , zap .String ("url" , t .RepoURL ), zap .String ("directory" , dir ))
165
171
targetRepos [i ] = gitwatch.Repository {
166
172
URL : t .RepoURL ,
167
173
Branch : t .Branch ,
168
174
Directory : dir ,
175
+ Auth : auth ,
169
176
}
170
177
}
171
178
@@ -177,7 +184,7 @@ func (w *GitWatcher) watchTargets() (err error) {
177
184
targetRepos ,
178
185
w .checkInterval ,
179
186
w .directory ,
180
- w . ssh ,
187
+ nil ,
181
188
false )
182
189
if err != nil {
183
190
return errors .Wrap (err , "failed to watch targets" )
@@ -211,6 +218,31 @@ func (w *GitWatcher) handle(e gitwatch.Event) (err error) {
211
218
return nil
212
219
}
213
220
221
+ func (w GitWatcher ) getAuthForTarget (t task.Target ) (transport.AuthMethod , error ) {
222
+ for _ , a := range w .state .AuthMethods {
223
+ if a .Name == t .Auth {
224
+ s , err := w .secrets .GetSecretsForTarget (a .Path )
225
+ if err != nil {
226
+ return nil , err
227
+ }
228
+ username , ok := s [a .UserKey ]
229
+ if ! ok {
230
+ return nil , errors .Errorf ("auth object 'user_key' did not point to a valid element in the specified secret at '%s'" , a .Path )
231
+ }
232
+ password , ok := s [a .PassKey ]
233
+ if ! ok {
234
+ return nil , errors .Errorf ("auth object 'pass_key' did not point to a valid element in the specified secret at '%s'" , a .Path )
235
+ }
236
+ zap .L ().Debug ("using auth method for target" , zap .String ("name" , a .Name ))
237
+ return & http.BasicAuth {
238
+ Username : username ,
239
+ Password : password ,
240
+ }, nil
241
+ }
242
+ }
243
+ return nil , nil
244
+ }
245
+
214
246
func (w GitWatcher ) executeTargets (targets []task.Target , shutdown bool ) {
215
247
zap .L ().Debug ("executing all targets" ,
216
248
zap .Bool ("shutdown" , shutdown ),
0 commit comments