Skip to content

Commit

Permalink
Merge pull request #215 from buildtool/gitlab-registry-user
Browse files Browse the repository at this point in the history
chore: use Gitlab registry user from environment
  • Loading branch information
argoyle authored Apr 21, 2022
2 parents 2688848 + 94b4c8b commit a180c6c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
1 change: 1 addition & 0 deletions pkg/config/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")()

Expand Down
5 changes: 3 additions & 2 deletions pkg/registry/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions pkg/registry/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand All @@ -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)
}
Expand Down

0 comments on commit a180c6c

Please sign in to comment.