From 005c105d56461f90f9d791a72a22345d9ca540a6 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Tue, 5 Nov 2019 14:03:13 -0800 Subject: [PATCH] encode registry credentials in docker config format --- CHANGELOG.md | 4 ++++ registry/auths/auth.go | 21 +++++++++++++-------- registry/auths/auth_test.go | 4 ++-- registry/auths/encode.go | 29 +++++++++++++++++++++++++++++ registry/auths/encode_test.go | 28 ++++++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 10 deletions(-) create mode 100644 registry/auths/encode.go create mode 100644 registry/auths/encode_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index a43354e..d8f6ad0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.4.0] - 2019-11-05 +### Added +- function to encode registry credentials in docker config.json format + ## [1.3.1] - 2019-11-01 ### Fixed - check if last exit code greater than 0 in powershell diff --git a/registry/auths/auth.go b/registry/auths/auth.go index 7cef9ce..6ba26d2 100644 --- a/registry/auths/auth.go +++ b/registry/auths/auth.go @@ -16,13 +16,18 @@ import ( "github.com/drone/drone-go/drone" ) -// config represents the Docker client configuration, -// typically located at ~/.docker/config.json -type config struct { - Auths map[string]struct { +type ( + // config represents the Docker client configuration, + // typically located at ~/.docker/config.json + config struct { + Auths map[string]auth `json:"auths"` + } + + // auth stores the registry authentication string. + auth struct { Auth string `json:"auth"` - } `json:"auths"` -} + } +) // Parse parses the registry credential from the reader. func Parse(r io.Reader) ([]*drone.Registry, error) { @@ -63,10 +68,10 @@ func ParseBytes(b []byte) ([]*drone.Registry, error) { return Parse(bytes.NewReader(b)) } -// Encode returns the json marshaled, base64 encoded +// Header returns the json marshaled, base64 encoded // credential string that can be passed to the docker // registry authentication header. -func Encode(username, password string) string { +func Header(username, password string) string { v := struct { Username string `json:"username,omitempty"` Password string `json:"password,omitempty"` diff --git a/registry/auths/auth_test.go b/registry/auths/auth_test.go index fde63d1..a65d604 100644 --- a/registry/auths/auth_test.go +++ b/registry/auths/auth_test.go @@ -103,10 +103,10 @@ func TestDecodeInvalid(t *testing.T) { } } -func TestEncode(t *testing.T) { +func TestHeader(t *testing.T) { username := "octocat" password := "correct-horse-battery-staple" - result := Encode(username, password) + result := Header(username, password) got, err := base64.URLEncoding.DecodeString(result) if err != nil { t.Error(err) diff --git a/registry/auths/encode.go b/registry/auths/encode.go new file mode 100644 index 0000000..0166270 --- /dev/null +++ b/registry/auths/encode.go @@ -0,0 +1,29 @@ +// Copyright 2019 Drone.IO Inc. All rights reserved. +// Use of this source code is governed by the Polyform License +// that can be found in the LICENSE file. + +package auths + +import ( + "bytes" + "encoding/json" + + "github.com/drone/drone-go/drone" +) + +// Encode encodes the registry credentials to using the +// docker config json format and returns the resulting +// data in string format. +func Encode(registry ...*drone.Registry) string { + c := new(config) + c.Auths = map[string]auth{} + for _, r := range registry { + c.Auths[r.Address] = auth{ + Auth: encode(r.Username, r.Password), + } + } + buf := new(bytes.Buffer) + enc := json.NewEncoder(buf) + enc.Encode(c) + return buf.String() +} diff --git a/registry/auths/encode_test.go b/registry/auths/encode_test.go new file mode 100644 index 0000000..d552efe --- /dev/null +++ b/registry/auths/encode_test.go @@ -0,0 +1,28 @@ +// Copyright 2019 Drone.IO Inc. All rights reserved. +// Use of this source code is governed by the Polyform License +// that can be found in the LICENSE file. + +package auths + +import ( + "strings" + "testing" + + "github.com/drone/drone-go/drone" +) + +func TestEncode(t *testing.T) { + endpoint := "docker.io" + username := "octocat" + password := "correct-horse-battery-staple" + registry := &drone.Registry{ + Username: username, + Password: password, + Address: endpoint, + } + got := Encode(registry, registry, registry) + want := `{"auths":{"docker.io":{"auth":"b2N0b2NhdDpjb3JyZWN0LWhvcnNlLWJhdHRlcnktc3RhcGxl"}}}` + if strings.TrimSpace(got) != strings.TrimSpace(want) { + t.Errorf("Unexpected encoding: %q want %q", got, want) + } +}