-
Notifications
You must be signed in to change notification settings - Fork 324
update docker config for base image #446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jamie-harness
merged 6 commits into
drone-plugins:master
from
Aishwarya-Lad:CI-11718-part2
Jun 28, 2024
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e9b38c9
update docker config
Aishwarya-Lad ced9875
add only docker registry auths to docker config
Aishwarya-Lad 9a4e1ba
address review comments
Aishwarya-Lad 5639b70
dependencies
Aishwarya-Lad 74d5558
resolve comments
Aishwarya-Lad 0a2f635
typos
Aishwarya-Lad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,91 @@ | ||
| package docker | ||
|
|
||
| import ( | ||
| "encoding/base64" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io/ioutil" | ||
| "os" | ||
|
|
||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| const ( | ||
| v2HubRegistryURL string = "https://registry.hub.docker.com/v2/" | ||
| v1RegistryURL string = "https://index.docker.io/v1/" // Default registry | ||
| v2RegistryURL string = "https://index.docker.io/v2/" // v2 registry is not supported | ||
| ) | ||
|
|
||
| type ( | ||
| Auth struct { | ||
| Auth string `json:"auth"` | ||
| } | ||
|
|
||
| Config struct { | ||
| Auths map[string]Auth `json:"auths"` | ||
| CredHelpers map[string]string `json:"credHelpers,omitempty"` | ||
| } | ||
| ) | ||
|
|
||
| type RegistryCredentials struct { | ||
| Registry string | ||
| Username string | ||
| Password string | ||
| } | ||
|
|
||
| func NewConfig() *Config { | ||
| return &Config{ | ||
| Auths: make(map[string]Auth), | ||
| CredHelpers: make(map[string]string), | ||
| } | ||
| } | ||
|
|
||
| func (c *Config) SetAuth(registry, username, password string) { | ||
| authBytes := []byte(username + ":" + password) | ||
| encodedString := base64.StdEncoding.EncodeToString(authBytes) | ||
| log.Printf("auth : %s", encodedString) | ||
| c.Auths[registry] = Auth{Auth: encodedString} | ||
| } | ||
|
|
||
| func (c *Config) SetCredHelper(registry, helper string) { | ||
| c.CredHelpers[registry] = helper | ||
| } | ||
|
|
||
| func (c *Config) CreateDockerConfigJson(credentials []RegistryCredentials) ([]byte, error) { | ||
| for _, cred := range credentials { | ||
| if cred.Registry != "" && strings.Contains(cred.Registry, "docker") { | ||
Aishwarya-Lad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if cred.Username == "" { | ||
| return nil, fmt.Errorf("Username must be specified for registry: %s", cred.Registry) | ||
| } | ||
| if cred.Password == "" { | ||
| return nil, fmt.Errorf("Password must be specified for registry: %s", cred.Registry) | ||
| } | ||
| c.SetAuth(cred.Registry, cred.Username, cred.Password) | ||
| } | ||
| } | ||
|
|
||
| jsonBytes, err := json.Marshal(c) | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "failed to serialize docker config json") | ||
| } | ||
|
|
||
| return jsonBytes, nil | ||
| } | ||
|
|
||
| func WriteDockerConfig(data []byte, path string) (string error) { | ||
Aishwarya-Lad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| err := os.MkdirAll(path, 0600) | ||
| if err != nil { | ||
| if !os.IsExist(err) { | ||
| return errors.Wrap(err, fmt.Sprintf("failed to create %s directory", path)) | ||
| } | ||
| } | ||
|
|
||
| filePath := path + "/config.json" | ||
| log.Printf("Config data is %s", data) | ||
| err = ioutil.WriteFile(filePath, data, 0644) | ||
| if err != nil { | ||
| return errors.Wrap(err, fmt.Sprintf("failed to create docker config file at %s", path)) | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or 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,64 @@ | ||
| package docker | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "io/ioutil" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| const ( | ||
| RegistryV1 string = "https://index.docker.io/v1/" | ||
| RegistryV2 string = "https://index.docker.io/v2/" | ||
| RegistryECRPublic string = "public.ecr.aws" | ||
| ) | ||
|
|
||
| func TestConfig(t *testing.T) { | ||
| c := NewConfig() | ||
| assert.NotNil(t, c.Auths) | ||
| assert.NotNil(t, c.CredHelpers) | ||
|
|
||
| c.SetAuth(RegistryV1, "test", "password") | ||
| expectedAuth := Auth{Auth: "dGVzdDpwYXNzd29yZA=="} | ||
| assert.Equal(t, expectedAuth, c.Auths[RegistryV1]) | ||
|
|
||
| c.SetCredHelper(RegistryECRPublic, "ecr-login") | ||
| assert.Equal(t, "ecr-login", c.CredHelpers[RegistryECRPublic]) | ||
|
|
||
| tempDir, err := ioutil.TempDir("", "docker-config-test") | ||
| assert.NoError(t, err) | ||
| defer os.RemoveAll(tempDir) | ||
|
|
||
| credentials := []RegistryCredentials{ | ||
| { | ||
| Registry: "https://index.docker.io/v1/", | ||
| Username: "user1", | ||
| Password: "pass1", | ||
| }, | ||
| { | ||
| Registry: "gcr.io", | ||
| Username: "user2", | ||
| Password: "pass2", | ||
| }, | ||
| } | ||
|
|
||
| jsonBytes, err := c.CreateDockerConfigJson(credentials) | ||
| assert.NoError(t, err) | ||
|
|
||
| configPath := filepath.Join(tempDir, "config.json") | ||
| err = ioutil.WriteFile(configPath, jsonBytes, 0644) | ||
| assert.NoError(t, err) | ||
|
|
||
| data, err := ioutil.ReadFile(configPath) | ||
| assert.NoError(t, err) | ||
|
|
||
| var configFromFile Config | ||
| err = json.Unmarshal(data, &configFromFile) | ||
| assert.NoError(t, err) | ||
|
|
||
| assert.Equal(t, c.Auths, configFromFile.Auths) | ||
| assert.Equal(t, c.CredHelpers, configFromFile.CredHelpers) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.