-
Notifications
You must be signed in to change notification settings - Fork 321
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 #446 from Aishwarya-Lad/CI-11718-part2
update docker config for base image
- Loading branch information
Showing
6 changed files
with
223 additions
and
8 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
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,71 @@ | ||
package docker | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
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) | ||
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") { | ||
|
||
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.New("failed to serialize docker config json") | ||
} | ||
|
||
return jsonBytes, 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,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) | ||
} |