-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from buildtool/gcr
feat: initial support for GCP Container registry
- Loading branch information
Showing
5 changed files
with
239 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package registry | ||
|
||
import ( | ||
"context" | ||
"docker.io/go-docker/api/types" | ||
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/buildtool/build-tools/pkg/docker" | ||
"io" | ||
) | ||
|
||
type GCR struct { | ||
dockerRegistry | ||
Url string `yaml:"url" env:"GCR_URL"` | ||
KeyFileContent string `yaml:"keyfileContent" env:"GCR_KEYFILE_CONTENT"` | ||
} | ||
|
||
var _ Registry = &GCR{} | ||
|
||
func (r *GCR) Name() string { | ||
return "GCR" | ||
} | ||
|
||
func (r *GCR) Configured() bool { | ||
if len(r.Url) > 0 { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (r *GCR) Login(client docker.Client, out io.Writer) error { | ||
auth := r.GetAuthConfig() | ||
auth.ServerAddress = r.Url | ||
if ok, err := client.RegistryLogin(context.Background(), auth); err == nil { | ||
_, _ = fmt.Fprintln(out, ok.Status) | ||
return nil | ||
} else { | ||
return err | ||
} | ||
} | ||
|
||
func (r *GCR) GetAuthConfig() types.AuthConfig { | ||
decoded, err := base64.StdEncoding.DecodeString(r.KeyFileContent) | ||
if err != nil { | ||
return types.AuthConfig{} | ||
} | ||
return types.AuthConfig{Username: "_json_key", Password: string(decoded)} | ||
} | ||
|
||
func (r *GCR) GetAuthInfo() string { | ||
authBytes, _ := json.Marshal(r.GetAuthConfig()) | ||
return base64.URLEncoding.EncodeToString(authBytes) | ||
} | ||
|
||
func (r GCR) RegistryUrl() string { | ||
return r.Url | ||
} | ||
|
||
func (r GCR) Create(repository string) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package registry | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestGcr_Name(t *testing.T) { | ||
registry := &GCR{ | ||
Url: "url", | ||
KeyFileContent: "a2V5ZmlsZSBjb250ZW50Cg==", | ||
} | ||
assert.Equal(t, "GCR", registry.Name()) | ||
} | ||
|
||
func TestGcr_Configured(t *testing.T) { | ||
registry := &GCR{ | ||
Url: "url", | ||
KeyFileContent: "a2V5ZmlsZSBjb250ZW50Cg==", | ||
} | ||
|
||
assert.True(t, registry.Configured()) | ||
} | ||
|
||
func TestGcr_GetAuthInfo(t *testing.T) { | ||
registry := &GCR{ | ||
Url: "url", | ||
KeyFileContent: "a2V5ZmlsZSBjb250ZW50Cg==", | ||
} | ||
|
||
auth := registry.GetAuthInfo() | ||
assert.Equal(t, "eyJ1c2VybmFtZSI6Il9qc29uX2tleSIsInBhc3N3b3JkIjoia2V5ZmlsZSBjb250ZW50XG4ifQ==", auth) | ||
} | ||
|
||
// | ||
//func TestEcr_LoginAuthRequestFailed(t *testing.T) { | ||
// client := &docker.MockDocker{} | ||
// registry := &ECR{Url: "ecr-url", Region: "eu-west-1", svc: &MockECR{loginError: fmt.Errorf("auth failure")}} | ||
// out := &bytes.Buffer{} | ||
// err := registry.Login(client, out) | ||
// assert.EqualError(t, err, "auth failure") | ||
// assert.Equal(t, "", out.String()) | ||
//} | ||
// | ||
//func TestEcr_LoginInvalidAuthData(t *testing.T) { | ||
// client := &docker.MockDocker{} | ||
// registry := &ECR{Url: "ecr-url", Region: "eu-west-1", svc: &MockECR{authData: "aaabbb"}} | ||
// out := &bytes.Buffer{} | ||
// err := registry.Login(client, out) | ||
// assert.EqualError(t, err, "illegal base64 data at input byte 4") | ||
// assert.Equal(t, "", out.String()) | ||
//} | ||
// | ||
//func TestEcr_LoginFailed(t *testing.T) { | ||
// client := &docker.MockDocker{LoginError: fmt.Errorf("invalid username/password")} | ||
// registry := &ECR{Url: "ecr-url", Region: "eu-west-1", svc: &MockECR{authData: "QVdTOmFiYzEyMw=="}} | ||
// out := &bytes.Buffer{} | ||
// err := registry.Login(client, out) | ||
// assert.EqualError(t, err, "invalid username/password") | ||
// assert.Equal(t, "", out.String()) | ||
//} | ||
// | ||
//func TestEcr_LoginSuccess(t *testing.T) { | ||
// client := &docker.MockDocker{} | ||
// registry := &ECR{Url: "ecr-url", Region: "eu-west-1", svc: &MockECR{authData: "QVdTOmFiYzEyMw=="}} | ||
// out := &bytes.Buffer{} | ||
// err := registry.Login(client, out) | ||
// assert.Nil(t, err) | ||
// assert.Equal(t, "AWS", client.Username) | ||
// assert.Equal(t, "abc123", client.Password) | ||
// assert.Equal(t, "ecr-url", client.ServerAddress) | ||
// assert.Equal(t, "Logged in\n", out.String()) | ||
//} | ||
// | ||
//func TestEcr_GetAuthInfo(t *testing.T) { | ||
// registry := &ECR{Url: "ecr-url", Region: "eu-west-1", username: "AWS", password: "abc123"} | ||
// auth := registry.GetAuthInfo() | ||
// assert.Equal(t, "eyJ1c2VybmFtZSI6IkFXUyIsInBhc3N3b3JkIjoiYWJjMTIzIn0=", auth) | ||
//} | ||
// | ||
//func TestEcr_ExistingRepository(t *testing.T) { | ||
// mock := &MockECR{repoExists: true} | ||
// registry := &ECR{svc: mock} | ||
// repo := "repo" | ||
// err := registry.Create(repo) | ||
// assert.Nil(t, err) | ||
// assert.Equal(t, []*string{&repo}, mock.describeRepositoriesInput.RepositoryNames) | ||
//} | ||
// | ||
//func TestEcr_NewRepositoryCreateError(t *testing.T) { | ||
// registry := &ECR{svc: &MockECR{createError: fmt.Errorf("create error")}} | ||
// err := registry.Create("repo") | ||
// assert.EqualError(t, err, "create error") | ||
//} | ||
// | ||
//func TestEcr_NewRepositoryPutError(t *testing.T) { | ||
// registry := &ECR{svc: &MockECR{putError: fmt.Errorf("put error")}} | ||
// err := registry.Create("repo") | ||
// assert.EqualError(t, err, "put error") | ||
//} | ||
// | ||
//func TestEcr_NewRepository(t *testing.T) { | ||
// mock := &MockECR{} | ||
// registry := &ECR{svc: mock} | ||
// repo := "repo" | ||
// err := registry.Create(repo) | ||
// assert.Nil(t, err) | ||
// assert.Equal(t, &repo, mock.createRepositoryInput.RepositoryName) | ||
// policyText := `{"rules":[{"rulePriority":10,"description":"Only keep 20 images","selection":{"tagStatus":"untagged","countType":"imageCountMoreThan","countNumber":20},"action":{"type":"expire"}}]}` | ||
// assert.Equal(t, &policyText, mock.putLifecyclePolicyInput.LifecyclePolicyText) | ||
//} | ||
// | ||
//func TestEcr_ParseECRUrlIfNoRegionIsSet(t *testing.T) { | ||
// ecr := ECR{ | ||
// Url: "12345678.dkr.ecr.eu-west-1.amazonaws.com", | ||
// } | ||
// assert.Equal(t, "eu-west-1", *ecr.region()) | ||
//} | ||
// | ||
//func TestEcr_UseRegionIfSet(t *testing.T) { | ||
// ecr := ECR{ | ||
// Url: "12345678.dkr.ecr.eu-west-1.amazonaws.com", | ||
// Region: "region", | ||
// } | ||
// assert.Equal(t, "region", *ecr.region()) | ||
//} | ||
// | ||
//type MockECR struct { | ||
// ecriface.ECRAPI | ||
// loginError error | ||
// authData string | ||
// describeRepositoriesInput *awsecr.DescribeRepositoriesInput | ||
// repoExists bool | ||
// createError error | ||
// createRepositoryInput *awsecr.CreateRepositoryInput | ||
// putLifecyclePolicyInput *awsecr.PutLifecyclePolicyInput | ||
// putError error | ||
//} | ||
// | ||
//func (r MockECR) GetAuthorizationToken(input *awsecr.GetAuthorizationTokenInput) (*awsecr.GetAuthorizationTokenOutput, error) { | ||
// if r.loginError != nil { | ||
// return &awsecr.GetAuthorizationTokenOutput{AuthorizationData: []*awsecr.AuthorizationData{}}, r.loginError | ||
// } | ||
// return &awsecr.GetAuthorizationTokenOutput{AuthorizationData: []*awsecr.AuthorizationData{{AuthorizationToken: &r.authData}}}, nil | ||
//} | ||
// | ||
//func (r *MockECR) DescribeRepositories(input *awsecr.DescribeRepositoriesInput) (*awsecr.DescribeRepositoriesOutput, error) { | ||
// r.describeRepositoriesInput = input | ||
// if r.repoExists { | ||
// return &awsecr.DescribeRepositoriesOutput{Repositories: []*awsecr.Repository{}}, nil | ||
// } | ||
// return &awsecr.DescribeRepositoriesOutput{Repositories: []*awsecr.Repository{}}, fmt.Errorf("NoDockerRegistry repository found") | ||
//} | ||
// | ||
//func (r *MockECR) CreateRepository(input *awsecr.CreateRepositoryInput) (*awsecr.CreateRepositoryOutput, error) { | ||
// r.createRepositoryInput = input | ||
// return &awsecr.CreateRepositoryOutput{}, r.createError | ||
//} | ||
// | ||
//func (r *MockECR) PutLifecyclePolicy(input *awsecr.PutLifecyclePolicyInput) (*awsecr.PutLifecyclePolicyOutput, error) { | ||
// r.putLifecyclePolicyInput = input | ||
// return &awsecr.PutLifecyclePolicyOutput{}, r.putError | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters