forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
password.go
160 lines (138 loc) · 3.93 KB
/
password.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package scmauth
import (
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/openshift/origin/pkg/util/file"
)
const (
DefaultUsername = "builder"
UsernamePasswordName = "password"
UsernameSecret = "username"
PasswordSecret = "password"
TokenSecret = "token"
UserPassGitConfig = `# credential git config
[credential]
helper = store --file=%s
`
)
// UsernamePassword implements SCMAuth interface for using Username and Password credentials
type UsernamePassword struct {
SourceURL url.URL
}
// Setup creates a gitconfig fragment that includes a substitution URL with the username/password
// included in the URL. Returns source URL stripped of username/password credentials.
func (u UsernamePassword) Setup(baseDir string, context SCMAuthContext) error {
// Only apply to https and http URLs
if scheme := strings.ToLower(u.SourceURL.Scheme); scheme != "http" && scheme != "https" {
return nil
}
// Read data from secret files
usernameSecret, err := readSecret(baseDir, UsernameSecret)
if err != nil {
return err
}
passwordSecret, err := readSecret(baseDir, PasswordSecret)
if err != nil {
return err
}
tokenSecret, err := readSecret(baseDir, TokenSecret)
if err != nil {
return err
}
// Determine overrides
overrideSourceURL, gitconfigURL, err := doSetup(u.SourceURL, usernameSecret, passwordSecret, tokenSecret)
if err != nil {
return err
}
if overrideSourceURL != nil {
if err := context.SetOverrideURL(overrideSourceURL); err != nil {
return err
}
}
// Write git config if needed
if gitconfigURL != nil {
gitcredentials, err := ioutil.TempFile("", "gitcredentials.")
if err != nil {
return err
}
defer gitcredentials.Close()
gitconfig, err := ioutil.TempFile("", "gitcredentialscfg.")
if err != nil {
return err
}
defer gitconfig.Close()
fmt.Fprintf(gitconfig, UserPassGitConfig, gitcredentials.Name())
fmt.Fprintf(gitcredentials, "%s", gitconfigURL.String())
return ensureGitConfigIncludes(gitconfig.Name(), context)
}
return nil
}
func doSetup(sourceURL url.URL, usernameSecret, passwordSecret, tokenSecret string) (*url.URL, *url.URL, error) {
// Extract auth from the source URL
urlUsername := ""
urlPassword := ""
if sourceURL.User != nil {
urlUsername = sourceURL.User.Username()
urlPassword, _ = sourceURL.User.Password()
}
// Determine username in this order: secret, url
username := usernameSecret
if username == "" {
username = urlUsername
}
// Determine password in this order: token secret, password secret, url
password := tokenSecret
if password == "" {
password = passwordSecret
}
if password == "" {
password = urlPassword
}
// If we have no password, and the username matches what is already in the URL, no overrides or config are needed
if password == "" && username == urlUsername {
return nil, nil, nil
}
// If we're going to write config, ensure we have a username
if username == "" {
username = DefaultUsername
}
// Remove user/pw from the source url
overrideSourceURL := sourceURL
overrideSourceURL.User = nil
// Set user/pw in the config url
configURL := sourceURL
configURL.User = url.UserPassword(username, password)
return &overrideSourceURL, &configURL, nil
}
// Name returns the name of this auth method.
func (_ UsernamePassword) Name() string {
return UsernamePasswordName
}
// Handles returns true if a username, password or token secret is present
func (_ UsernamePassword) Handles(name string) bool {
switch name {
case UsernameSecret, PasswordSecret, TokenSecret:
return true
}
return false
}
// readSecret reads the contents of a secret file
func readSecret(baseDir, fileName string) (string, error) {
path := filepath.Join(baseDir, fileName)
lines, err := file.ReadLines(path)
if err != nil {
if os.IsNotExist(err) {
return "", nil
}
return "", err
}
// If the file is empty, simply return empty string
if len(lines) == 0 {
return "", nil
}
return lines[0], nil
}