This repository has been archived by the owner on Jul 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
auth.go
50 lines (39 loc) · 1.15 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
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
docker "github.com/fsouza/go-dockerclient"
)
var ErrCfgNotFound = fmt.Errorf("dockercfg is missing entry for registry")
// DockerCfg is the 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
}