-
Notifications
You must be signed in to change notification settings - Fork 405
/
registry.go
106 lines (85 loc) · 2.35 KB
/
registry.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
package docker
import (
"net/url"
"os"
"strings"
"github.com/fsouza/go-dockerclient"
"github.com/sirupsen/logrus"
)
var (
defaultPrivateRegistries = []string{"hub.docker.com", "index.docker.io"}
)
func registryFromEnv() (map[string]driverAuthConfig, error) {
var auths *docker.AuthConfigurations
var err error
if reg := os.Getenv("FN_DOCKER_AUTH"); reg != "" {
auths, err = docker.NewAuthConfigurations(strings.NewReader(reg))
} else {
auths, err = docker.NewAuthConfigurationsFromDockerCfg()
}
if err != nil {
logrus.WithError(err).Info("no docker auths from config files found (this is fine)")
return map[string]driverAuthConfig{}, nil
}
return preprocessAuths(auths)
}
func preprocessAuths(auths *docker.AuthConfigurations) (map[string]driverAuthConfig, error) {
drvAuths := make(map[string]driverAuthConfig)
for key, v := range auths.Configs {
u, err := url.Parse(v.ServerAddress)
if err != nil {
return drvAuths, err
}
if u.Scheme == "" {
// url.Parse won't return an error for urls who do not provide a scheme, and
// host field will be unset. docker defaults to bare hosts without scheme
// in its configs, so support this here as well.
u.Host = v.ServerAddress
}
drvAuths[key] = driverAuthConfig{
auth: v,
subdomains: getSubdomains(u.Host),
}
}
return drvAuths, nil
}
func getSubdomains(hostname string) map[string]bool {
subdomains := make(map[string]bool)
tokens := strings.Split(hostname, ".")
if len(tokens) <= 2 {
subdomains[hostname] = true
} else {
for i := 0; i <= len(tokens)-2; i++ {
joined := strings.Join(tokens[i:], ".")
subdomains[joined] = true
}
}
return subdomains
}
func findRegistryConfig(reg string, configs map[string]driverAuthConfig) *docker.AuthConfiguration {
var config docker.AuthConfiguration
if reg != "" {
res := lookupRegistryConfig(reg, configs)
if res != nil {
return res
}
} else {
for _, reg := range defaultPrivateRegistries {
res := lookupRegistryConfig(reg, configs)
if res != nil {
return res
}
}
}
return &config
}
func lookupRegistryConfig(reg string, configs map[string]driverAuthConfig) *docker.AuthConfiguration {
// if any configured host auths match task registry, try them (task docker auth can override)
for _, v := range configs {
_, ok := v.subdomains[reg]
if ok {
return &v.auth
}
}
return nil
}