encode registry credentials in docker config format

pull/1/head
Brad Rydzewski 5 years ago
parent 792f6cdb88
commit 005c105d56

@ -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

@ -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"`

@ -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)

@ -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()
}

@ -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)
}
}
Loading…
Cancel
Save