forked from die-net/deployer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
51 lines (41 loc) · 1.14 KB
/
auth.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
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
docker "github.com/fsouza/go-dockerclient"
"io/ioutil"
)
var (
ErrCfgNotFound = fmt.Errorf("Dockercfg is missing entry for registry.")
)
// Undocumented format for ~/.dockercfg file.
type DockerCfg map[string]struct {
docker.AuthConfiguration // Decode most fields directly into our return format.
Auth string `json:"auth"` // Allow "auth" field too, so we can convert.
}
func AuthFromDockerCfg(file, registry string) (docker.AuthConfiguration, error) {
empty := docker.AuthConfiguration{}
content, err := ioutil.ReadFile(file)
if err != nil {
return empty, err
}
cfgs := DockerCfg{}
if err := json.Unmarshal(content, &cfgs); err != nil {
return empty, err
}
cfg, ok := cfgs[registry]
if !ok {
return empty, ErrCfgNotFound
}
// Split apart base64 "auth" field into Username and Password.
if cfg.Auth != "" {
creds, err := base64.StdEncoding.DecodeString(cfg.Auth)
if err != nil {
return empty, err
}
cfg.Username, cfg.Password = splitTwo(string(creds), ":")
}
cfg.ServerAddress = registry
return cfg.AuthConfiguration, nil
}