diff --git a/pkg/config/registry_test.go b/pkg/config/registry_test.go index 72a7ae3c..4cc857b2 100644 --- a/pkg/config/registry_test.go +++ b/pkg/config/registry_test.go @@ -75,6 +75,7 @@ func TestEcr_Identify_MissingDockerRegistry(t *testing.T) { func TestGitlab_Identify(t *testing.T) { defer pkg.SetEnv("CI_REGISTRY", "registry.gitlab.com")() + defer pkg.SetEnv("CI_REGISTRY_USER", "gitlab-ci-token")() defer pkg.SetEnv("CI_REGISTRY_IMAGE", "registry.gitlab.com/group/image")() defer pkg.SetEnv("CI_JOB_TOKEN", "token")() diff --git a/pkg/registry/gitlab.go b/pkg/registry/gitlab.go index ae19e973..297f5421 100644 --- a/pkg/registry/gitlab.go +++ b/pkg/registry/gitlab.go @@ -15,6 +15,7 @@ import ( type Gitlab struct { dockerRegistry `yaml:"-"` Registry string `yaml:"registry" env:"CI_REGISTRY"` + User string `yaml:"user" env:"CI_REGISTRY_USER"` Repository string `yaml:"repository" env:"CI_REGISTRY_IMAGE"` Token string `yaml:"token,omitempty" env:"CI_JOB_TOKEN"` } @@ -30,7 +31,7 @@ func (r Gitlab) Configured() bool { } func (r Gitlab) Login(client docker.Client) error { - if ok, err := client.RegistryLogin(context.Background(), types.AuthConfig{Username: "gitlab-ci-token", Password: r.Token, ServerAddress: r.Registry}); err == nil { + if ok, err := client.RegistryLogin(context.Background(), r.GetAuthConfig()); err == nil { log.Debugf("%s\n", ok.Status) return nil } else { @@ -39,7 +40,7 @@ func (r Gitlab) Login(client docker.Client) error { } func (r Gitlab) GetAuthConfig() types.AuthConfig { - return types.AuthConfig{Username: "gitlab-ci-token", Password: r.Token, ServerAddress: r.Registry} + return types.AuthConfig{Username: r.User, Password: r.Token, ServerAddress: r.Registry} } func (r Gitlab) GetAuthInfo() string { diff --git a/pkg/registry/gitlab_test.go b/pkg/registry/gitlab_test.go index e1ef766c..5d3c3f51 100644 --- a/pkg/registry/gitlab_test.go +++ b/pkg/registry/gitlab_test.go @@ -13,13 +13,13 @@ import ( func TestGitlab_LoginSuccess(t *testing.T) { client := &docker.MockDocker{} - registry := &Gitlab{Registry: "registry.gitlab.com", Repository: "registry.gitlab.com/group/repo", Token: "token"} + registry := &Gitlab{Registry: "registry.gitlab.com", Repository: "registry.gitlab.com/group/repo", Token: "token", User: "gitlab-user"} logMock := mocks.New() log.SetHandler(logMock) log.SetLevel(log.DebugLevel) err := registry.Login(client) assert.Nil(t, err) - assert.Equal(t, "gitlab-ci-token", client.Username) + assert.Equal(t, "gitlab-user", client.Username) assert.Equal(t, "token", client.Password) assert.Equal(t, "registry.gitlab.com", client.ServerAddress) logMock.Check(t, []string{"debug: Logged in\n"}) @@ -37,7 +37,7 @@ func TestGitlab_LoginError(t *testing.T) { } func TestGitlab_GetAuthInfo(t *testing.T) { - registry := &Gitlab{Registry: "registry.gitlab.com", Repository: "registry.gitlab.com/group/repo", Token: "token"} + registry := &Gitlab{Registry: "registry.gitlab.com", Repository: "registry.gitlab.com/group/repo", Token: "token", User: "gitlab-ci-token"} auth := registry.GetAuthInfo() assert.Equal(t, "eyJ1c2VybmFtZSI6ImdpdGxhYi1jaS10b2tlbiIsInBhc3N3b3JkIjoidG9rZW4iLCJzZXJ2ZXJhZGRyZXNzIjoicmVnaXN0cnkuZ2l0bGFiLmNvbSJ9", auth) }