add better generate

Signed-off-by: Jess Frazelle <acidburn@microsoft.com>
This commit is contained in:
Jess Frazelle 2018-03-20 01:33:56 -04:00
parent 3fc6abf56b
commit cdd93563f5
5655 changed files with 1187011 additions and 392 deletions

View file

@ -0,0 +1,141 @@
package repoutils
import (
"fmt"
"strings"
"github.com/docker/distribution/reference"
"github.com/docker/docker-ce/components/cli/cli/config"
"github.com/docker/docker/api/types"
)
const (
// DefaultDockerRegistry is the default docker registry address.
DefaultDockerRegistry = "https://registry-1.docker.io"
latestTagSuffix = ":latest"
)
// GetAuthConfig returns the docker registry AuthConfig.
// Optionally takes in the authentication values, otherwise pulls them from the
// docker config file.
func GetAuthConfig(username, password, registry string) (types.AuthConfig, error) {
if username != "" && password != "" && registry != "" {
return types.AuthConfig{
Username: username,
Password: password,
ServerAddress: registry,
}, nil
}
dcfg, err := config.Load(config.Dir())
if err != nil {
return types.AuthConfig{}, fmt.Errorf("Loading config file failed: %v", err)
}
// return error early if there are no auths saved
if !dcfg.ContainsAuth() {
// If we were passed a registry, just use that.
if registry != "" {
return setDefaultRegistry(types.AuthConfig{
ServerAddress: registry,
}), nil
}
// Otherwise, just use an empty auth config.
return types.AuthConfig{}, nil
}
authConfigs, err := dcfg.GetAllCredentials()
if err != nil {
return types.AuthConfig{}, fmt.Errorf("Getting credentials failed: %v", err)
}
// if they passed a specific registry, return those creds _if_ they exist
if registry != "" {
// try with the user input
if creds, ok := authConfigs[registry]; ok {
return creds, nil
}
// remove https:// from user input and try again
if strings.HasPrefix(registry, "https://") {
if creds, ok := authConfigs[strings.TrimPrefix(registry, "https://")]; ok {
return creds, nil
}
}
// remove http:// from user input and try again
if strings.HasPrefix(registry, "http://") {
if creds, ok := authConfigs[strings.TrimPrefix(registry, "http://")]; ok {
return creds, nil
}
}
// add https:// to user input and try again
// see https://github.com/genuinetools/reg/issues/32
if !strings.HasPrefix(registry, "https://") && !strings.HasPrefix(registry, "http://") {
if creds, ok := authConfigs["https://"+registry]; ok {
return creds, nil
}
}
fmt.Printf("Using registry %q with no authentication\n", registry)
// Otherwise just use the registry with no auth.
return setDefaultRegistry(types.AuthConfig{
ServerAddress: registry,
}), nil
}
// Just set the auth config as the first registryURL, username and password
// found in the auth config.
for _, creds := range authConfigs {
fmt.Printf("No registry passed. Using registry %q\n", creds.ServerAddress)
return creds, nil
}
// Don't use any authentication.
// We should never get here.
fmt.Println("Not using any authentication")
return types.AuthConfig{}, nil
}
// GetRepoAndRef parses the repo name and reference.
func GetRepoAndRef(image string) (repo, ref string, err error) {
if image == "" {
return "", "", reference.ErrNameEmpty
}
image = addLatestTagSuffix(image)
var parts []string
if strings.Contains(image, "@") {
parts = strings.Split(image, "@")
} else if strings.Contains(image, ":") {
parts = strings.Split(image, ":")
}
repo = parts[0]
if len(parts) > 1 {
ref = parts[1]
}
return
}
// addLatestTagSuffix adds :latest to the image if it does not have a tag
func addLatestTagSuffix(image string) string {
if !strings.Contains(image, ":") {
return image + latestTagSuffix
}
return image
}
func setDefaultRegistry(auth types.AuthConfig) types.AuthConfig {
if auth.ServerAddress == "docker.io" {
auth.ServerAddress = DefaultDockerRegistry
}
return auth
}

View file

@ -0,0 +1,200 @@
package repoutils
import (
"errors"
"os"
"path/filepath"
"strings"
"testing"
"github.com/docker/distribution/reference"
"github.com/docker/docker-ce/components/cli/cli/config"
"github.com/docker/docker/api/types"
"github.com/google/go-cmp/cmp"
)
func TestGetAuthConfig(t *testing.T) {
configTestcases := []struct {
name string
username, password, registry string
configdir string
err error
config types.AuthConfig
}{
{
name: "pass in all details",
username: "jess",
password: "password",
registry: "r.j3ss.co",
config: types.AuthConfig{
Username: "jess",
Password: "password",
ServerAddress: "r.j3ss.co",
},
},
{
name: "invalid config dir",
configdir: "testdata/invalid",
err: errors.New("Loading config file failed: "),
config: types.AuthConfig{},
},
{
name: "empty config",
configdir: "testdata/empty",
config: types.AuthConfig{},
},
{
name: "empty config with docker.io",
registry: "docker.io",
configdir: "testdata/empty",
config: types.AuthConfig{
ServerAddress: DefaultDockerRegistry,
},
},
{
name: "empty config with registry",
registry: "r.j3ss.co",
configdir: "testdata/empty",
config: types.AuthConfig{
ServerAddress: "r.j3ss.co",
},
},
{
name: "valid with multiple",
registry: "r.j3ss.co",
configdir: "testdata/valid",
config: types.AuthConfig{
ServerAddress: "r.j3ss.co",
Username: "user",
Password: "blah\n",
},
},
{
name: "valid with multiple and https:// prefix",
registry: "https://r.j3ss.co",
configdir: "testdata/valid",
config: types.AuthConfig{
ServerAddress: "r.j3ss.co",
Username: "user",
Password: "blah\n",
},
},
{
name: "valid with multiple and http:// prefix",
registry: "http://r.j3ss.co",
configdir: "testdata/valid",
config: types.AuthConfig{
ServerAddress: "r.j3ss.co",
Username: "user",
Password: "blah\n",
},
},
{
name: "valid with multiple and no https:// prefix",
registry: "reg.j3ss.co",
configdir: "testdata/valid",
config: types.AuthConfig{
ServerAddress: "https://reg.j3ss.co",
Username: "joe",
Password: "otherthing\n",
},
},
{
name: "valid with multiple and but registry not found",
registry: "otherreg.j3ss.co",
configdir: "testdata/valid",
config: types.AuthConfig{
ServerAddress: "otherreg.j3ss.co",
},
},
{
name: "valid and no registry passed",
configdir: "testdata/singlevalid",
config: types.AuthConfig{
ServerAddress: "https://index.docker.io/v1/",
Username: "user",
Password: "thing\n",
},
},
}
for _, testcase := range configTestcases {
if testcase.configdir != "" {
// Set the config directory.
wd, err := os.Getwd()
if err != nil {
t.Fatalf("get working directory failed: %v", err)
}
config.SetDir(filepath.Join(wd, testcase.configdir))
}
cfg, err := GetAuthConfig(testcase.username, testcase.password, testcase.registry)
if err != nil || testcase.err != nil {
if err == nil || testcase.err == nil {
t.Fatalf("%q: expected err (%v), got err (%v)", testcase.name, testcase.err, err)
}
if !strings.Contains(err.Error(), testcase.err.Error()) {
t.Fatalf("%q: expected err (%v), got err (%v)", testcase.name, testcase.err, err)
}
continue
}
if diff := cmp.Diff(testcase.config, cfg); diff != "" {
t.Errorf("%s: authconfig differs: (-got +want)\n%s", testcase.name, diff)
}
}
}
func TestGetRepoAndRef(t *testing.T) {
imageTestcases := []struct {
// input is the repository name or name component testcase
input string
// err is the error expected from Parse, or nil
err error
// repository is the string representation for the reference
repository string
// ref the reference
ref string
}{
{
input: "alpine",
repository: "alpine",
ref: "latest",
},
{
input: "docker:dind",
repository: "docker",
ref: "dind",
},
{
input: "",
err: reference.ErrNameEmpty,
},
{
input: "chrome@sha256:2a6c8ad38c41ae5122d76be59b34893d7fa1bdfaddd85bf0e57d0d16c0f7f91e",
repository: "chrome",
ref: "sha256:2a6c8ad38c41ae5122d76be59b34893d7fa1bdfaddd85bf0e57d0d16c0f7f91e",
},
}
for _, testcase := range imageTestcases {
repo, ref, err := GetRepoAndRef(testcase.input)
if err != nil || testcase.err != nil {
if err == nil || testcase.err == nil {
t.Fatalf("%q: expected err (%v), got err (%v)", testcase.input, testcase.err, err)
}
if err.Error() != testcase.err.Error() {
t.Fatalf("%q: expected err (%v), got err (%v)", testcase.input, testcase.err, err)
}
continue
}
if testcase.repository != repo {
t.Fatalf("%q: expected repo (%s), got repo (%s)", testcase.input, testcase.repository, repo)
}
if testcase.ref != ref {
t.Fatalf("%q: expected ref (%s), got ref (%s)", testcase.input, testcase.ref, ref)
}
}
}