forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scmauths.go
75 lines (64 loc) · 1.8 KB
/
scmauths.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package scmauth
import (
"fmt"
"io/ioutil"
"net/url"
"os"
"github.com/golang/glog"
s2igit "github.com/openshift/source-to-image/pkg/scm/git"
)
type SCMAuths []SCMAuth
func GitAuths(sourceURL *s2igit.URL) SCMAuths {
auths := SCMAuths{
&SSHPrivateKey{},
&UsernamePassword{SourceURL: *sourceURL},
&CACert{SourceURL: *sourceURL},
&GitConfig{},
}
return auths
}
func (a SCMAuths) present(files []os.FileInfo) SCMAuths {
scmAuthsPresent := map[string]SCMAuth{}
for _, file := range files {
glog.V(4).Infof("Finding auth for %q", file.Name())
for _, scmAuth := range a {
if scmAuth.Handles(file.Name()) {
glog.V(4).Infof("Found SCMAuth %q to handle %q", scmAuth.Name(), file.Name())
scmAuthsPresent[scmAuth.Name()] = scmAuth
}
}
}
auths := SCMAuths{}
for _, auth := range scmAuthsPresent {
auths = append(auths, auth)
}
return auths
}
func (a SCMAuths) doSetup(secretsDir string) (*defaultSCMContext, error) {
context := NewDefaultSCMContext()
for _, auth := range a {
glog.V(4).Infof("Setting up SCMAuth %q", auth.Name())
err := auth.Setup(secretsDir, context)
if err != nil {
return nil, fmt.Errorf("cannot set up source authentication method %q: %v", auth.Name(), err)
}
}
return context, nil
}
func (a SCMAuths) Setup(secretsDir string) (env []string, overrideURL *url.URL, err error) {
files, err := ioutil.ReadDir(secretsDir)
if err != nil {
return nil, nil, err
}
// Filter the list of SCMAuths based on the secret files that are present
presentAuths := a.present(files)
if len(presentAuths) == 0 {
return nil, nil, fmt.Errorf("no auth handler was found for secrets in %s", secretsDir)
}
// Setup the present SCMAuths
context, err := presentAuths.doSetup(secretsDir)
if err != nil {
return nil, nil, err
}
return context.Env(), context.OverrideURL(), nil
}