/
dockerconfig.go
68 lines (58 loc) · 1.87 KB
/
dockerconfig.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
// Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package utils
import (
// Import shas that are used for docker image validation.
_ "crypto/sha256"
_ "crypto/sha512"
"encoding/json"
"fmt"
"strings"
"github.com/docker/distribution/reference"
"github.com/juju/errors"
)
// These Docker Config datatypes have been pulled from
// "k8s.io/kubernetes/pkg/credentialprovider".
// multiple k8s packages import the same package, we don't yet have the tooling
// to flatten the deps.
// The specific package in this case is golog.
// DockerConfigJSON represents ~/.docker/config.json file info.
type DockerConfigJSON struct {
Auths DockerConfig `json:"auths"`
}
// DockerConfig represents the config file used by the docker CLI.
type DockerConfig map[string]DockerConfigEntry
// DockerConfigEntry represents an Auth entry in the dockerconfigjson.
type DockerConfigEntry struct {
Username string
Password string
Email string
}
func CreateDockerConfigJSON(username, password, imagePath string) ([]byte, error) {
dockerEntry := DockerConfigEntry{
Username: username,
Password: password,
}
registryURL, err := ExtractRegistryURL(imagePath)
if err != nil {
return nil, errors.Trace(err)
}
dockerConfig := DockerConfigJSON{
Auths: map[string]DockerConfigEntry{
registryURL: dockerEntry,
},
}
return json.Marshal(dockerConfig)
}
// ExtractRegistryName returns the registry URL part of an images path
func ExtractRegistryURL(imagePath string) (string, error) {
imageNamed, err := reference.ParseNormalizedNamed(imagePath)
if err != nil {
return "", errors.Annotate(err, "extracting registry from path")
}
domain := reference.Domain(imageNamed)
if domain == "docker.io" && !strings.HasPrefix(strings.ToLower(imagePath), "docker.io") {
return "", fmt.Errorf("oci reference %q must have a domain", imagePath)
}
return domain, nil
}