@@ -11,8 +11,10 @@ import (
1111 "github.com/pkg/errors"
1212 "go.uber.org/zap"
1313 "gopkg.in/src-d/go-git.v4/plumbing/transport"
14+ "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
1415
1516 "github.com/picostack/pico/config"
17+ "github.com/picostack/pico/secret"
1618 "github.com/picostack/pico/task"
1719)
1820
@@ -24,7 +26,7 @@ type GitWatcher struct {
2426 directory string
2527 bus chan task.ExecutionTask
2628 checkInterval time.Duration
27- ssh transport. AuthMethod
29+ secrets secret. Store
2830
2931 targetsWatcher * gitwatch.Session
3032 state config.State
@@ -42,13 +44,13 @@ func NewGitWatcher(
4244 directory string ,
4345 bus chan task.ExecutionTask ,
4446 checkInterval time.Duration ,
45- ssh transport. AuthMethod ,
47+ secrets secret. Store ,
4648) * GitWatcher {
4749 return & GitWatcher {
4850 directory : directory ,
4951 bus : bus ,
5052 checkInterval : checkInterval ,
51- ssh : ssh ,
53+ secrets : secrets ,
5254
5355 initialise : make (chan bool ),
5456 newState : make (chan config.State , 16 ),
@@ -161,11 +163,16 @@ func (w *GitWatcher) watchTargets() (err error) {
161163 if t .Branch != "" {
162164 dir = fmt .Sprintf ("%s_%s" , t .Name , t .Branch )
163165 }
166+ auth , err := w .getAuthForTarget (t )
167+ if err != nil {
168+ return err
169+ }
164170 zap .L ().Debug ("assigned target" , zap .String ("url" , t .RepoURL ), zap .String ("directory" , dir ))
165171 targetRepos [i ] = gitwatch.Repository {
166172 URL : t .RepoURL ,
167173 Branch : t .Branch ,
168174 Directory : dir ,
175+ Auth : auth ,
169176 }
170177 }
171178
@@ -177,7 +184,7 @@ func (w *GitWatcher) watchTargets() (err error) {
177184 targetRepos ,
178185 w .checkInterval ,
179186 w .directory ,
180- w . ssh ,
187+ nil ,
181188 false )
182189 if err != nil {
183190 return errors .Wrap (err , "failed to watch targets" )
@@ -211,6 +218,31 @@ func (w *GitWatcher) handle(e gitwatch.Event) (err error) {
211218 return nil
212219}
213220
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+
214246func (w GitWatcher ) executeTargets (targets []task.Target , shutdown bool ) {
215247 zap .L ().Debug ("executing all targets" ,
216248 zap .Bool ("shutdown" , shutdown ),
0 commit comments