forked from openfaas/ofc-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidators.go
60 lines (50 loc) · 1.45 KB
/
validators.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
package validators
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"strings"
)
type AuthConfig struct {
Auth string `json:"auth,omitempty"`
}
type DockerConfigJson struct {
AuthConfigs map[string]AuthConfig `json:"auths"`
}
func unmarshalRegistryConfig(data []byte) (*DockerConfigJson, error) {
var registryConfig DockerConfigJson
err := json.Unmarshal(data, ®istryConfig)
if err != nil {
return nil, err
}
return ®istryConfig, nil
}
func ValidateRegistryAuth(registryEndpoint string, configFileBytes []byte) error {
registryData, unmarshalErr := unmarshalRegistryConfig(configFileBytes)
if unmarshalErr != nil {
return unmarshalErr
}
noAuthErr := validate(registryData, registryEndpoint)
if noAuthErr != nil {
return noAuthErr
}
return nil
}
func validate(registryData *DockerConfigJson, endpoint string) error {
var fileEndpoint string
if strings.HasPrefix(endpoint, "docker.io") {
fileEndpoint = "https://index.docker.io/v1/"
} else {
fileEndpoint = endpoint
}
if endpointConfig, ok := registryData.AuthConfigs[fileEndpoint]; ok {
if endpointConfig.Auth != "" {
_, err := base64.StdEncoding.DecodeString(endpointConfig.Auth)
return err
} else {
return errors.New("docker credentials file is not valid (no base64 credentials). Please re-create this file")
}
}
return errors.New(fmt.Sprintf("docker auth file does not contain registry %q that you specified in config. Please use docker login", endpoint))
}