mirror of
https://github.com/drone/drone-kaniko.git
synced 2026-06-14 22:11:19 +08:00
support dockerhub credentials when pulling with kaniko-ecr (#27)
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type (
|
||||
Auth struct {
|
||||
Auth string `json:"auth"`
|
||||
}
|
||||
|
||||
Config struct {
|
||||
Auths map[string]Auth `json:"auths"`
|
||||
CredHelpers map[string]string `json:"credHelpers"`
|
||||
}
|
||||
)
|
||||
|
||||
func NewConfig() *Config {
|
||||
return &Config{
|
||||
Auths: map[string]Auth{},
|
||||
CredHelpers: map[string]string{},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Config) SetAuth(registry, username, password string) {
|
||||
authBytes := []byte(fmt.Sprintf("%s:%s", username, password))
|
||||
encodedString := base64.StdEncoding.EncodeToString(authBytes)
|
||||
c.Auths[registry] = Auth{Auth: encodedString}
|
||||
}
|
||||
|
||||
func (c *Config) SetCredHelper(registry, helper string) {
|
||||
c.CredHelpers[registry] = helper
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConfig(t *testing.T) {
|
||||
c := NewConfig()
|
||||
|
||||
c.SetAuth(RegistryV1, "test", "password")
|
||||
c.SetCredHelper(RegistryECRPublic, "ecr-login")
|
||||
|
||||
bytes, err := json.Marshal(c)
|
||||
if err != nil {
|
||||
t.Error("json marshal failed")
|
||||
}
|
||||
|
||||
want := `{"auths":{"https://index.docker.io/v1/":{"auth":"dGVzdDpwYXNzd29yZA=="}},"credHelpers":{"public.ecr.aws":"ecr-login"}}`
|
||||
got := string(bytes)
|
||||
|
||||
if want != got {
|
||||
t.Errorf("unexpected json output:\n want: %s\n got: %s", want, got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package docker
|
||||
|
||||
const (
|
||||
RegistryV1 string = "https://index.docker.io/v1/"
|
||||
RegistryV2 string = "https://index.docker.io/v2/"
|
||||
RegistryECRPublic string = "public.ecr.aws"
|
||||
)
|
||||
Reference in New Issue
Block a user