encode registry credentials in docker config format
parent
792f6cdb88
commit
005c105d56
@ -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…
Reference in New Issue