forked from fluxcd/flux
/
credentials.go
159 lines (143 loc) · 3.89 KB
/
credentials.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
package registry
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"strings"
"github.com/pkg/errors"
)
// Registry Credentials
type creds struct {
username, password string
registry, provenance string
}
func (c creds) String() string {
if (creds{}) == c {
return "<zero creds>"
}
return fmt.Sprintf("<registry creds for %s@%s, from %s>", c.username, c.registry, c.provenance)
}
// Credentials to a (Docker) registry.
type Credentials struct {
m map[string]creds
}
// NoCredentials returns a usable but empty credentials object.
func NoCredentials() Credentials {
return Credentials{
m: map[string]creds{},
}
}
func ParseCredentials(from string, b []byte) (Credentials, error) {
var config struct {
Auths map[string]struct {
Auth string
}
}
if err := json.Unmarshal(b, &config); err != nil {
return Credentials{}, err
}
// If it's in k8s format, it won't have the surrounding "Auth". Try that too.
if len(config.Auths) == 0 {
if err := json.Unmarshal(b, &config.Auths); err != nil {
return Credentials{}, err
}
}
m := map[string]creds{}
for host, entry := range config.Auths {
decodedAuth, err := base64.StdEncoding.DecodeString(entry.Auth)
if err != nil {
return Credentials{}, err
}
authParts := strings.SplitN(string(decodedAuth), ":", 2)
if len(authParts) != 2 {
return Credentials{},
fmt.Errorf("decoded credential for %v has wrong number of fields (expected 2, got %d)", host, len(authParts))
}
// Some users were passing in credentials in the form of
// http://docker.io and http://docker.io/v1/, etc.
// So strip everything down to the host.
// Also, the registry might be local and on a different port.
// So we need to check for that because url.Parse won't parse the ip:port format very well.
u, err := url.Parse(host)
if err != nil {
return Credentials{}, err
}
if u.Host == "" && u.Path == "" && !strings.Contains(host, ":") || host == "http://" || host == "https://" {
return Credentials{}, errors.New("Empty registry auth url")
}
if u.Host == "" { // If there's no https:// prefix, it won't parse the host.
u, err = url.Parse(fmt.Sprintf("https://%s/", host))
if err != nil {
return Credentials{}, err
}
// If the host is still empty, then there's probably a rogue /
if u.Host == "" {
return Credentials{}, errors.New("Invalid registry auth url. Must be a valid http address (e.g. https://gcr.io/v1/)")
}
}
host = u.Host
m[host] = creds{
registry: host,
provenance: from,
username: authParts[0],
password: authParts[1],
}
}
return Credentials{m: m}, nil
}
func ImageCredsWithDefaults(lookup func() ImageCreds, configPath string) (func() ImageCreds, error) {
// pre-flight check
bs, err := ioutil.ReadFile(configPath)
if err == nil {
_, err = ParseCredentials(configPath, bs)
}
if err != nil {
return nil, err
}
return func() ImageCreds {
var defaults Credentials
bs, err := ioutil.ReadFile(configPath)
if err == nil {
defaults, _ = ParseCredentials(configPath, bs)
}
imageCreds := lookup()
for k, v := range imageCreds {
newCreds := NoCredentials()
newCreds.Merge(defaults)
newCreds.Merge(v)
imageCreds[k] = newCreds
}
return imageCreds
}, nil
}
// ---
// credsFor yields an authenticator for a specific host.
func (cs Credentials) credsFor(host string) creds {
if cred, found := cs.m[host]; found {
return cred
}
if host == "gcr.io" || strings.HasSuffix(host, ".gcr.io") {
if cred, err := GetGCPOauthToken(host); err == nil {
return cred
}
}
return creds{}
}
// Hosts returns all of the hosts available in these credentials.
func (cs Credentials) Hosts() []string {
hosts := []string{}
for host := range cs.m {
hosts = append(hosts, host)
}
return hosts
}
func (cs Credentials) Merge(c Credentials) {
for k, v := range c.m {
cs.m[k] = v
}
}
func (cs Credentials) String() string {
return fmt.Sprintf("{%v}", cs.m)
}