2016-12-25 14:31:52 -05:00
|
|
|
package credentials
|
|
|
|
|
|
|
|
import (
|
2017-10-15 15:39:56 -04:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/config/types"
|
2016-12-25 14:31:52 -05:00
|
|
|
)
|
|
|
|
|
2017-06-21 16:47:06 -04:00
|
|
|
type store interface {
|
|
|
|
Save() error
|
|
|
|
GetAuthConfigs() map[string]types.AuthConfig
|
2018-03-26 10:18:32 -04:00
|
|
|
GetFilename() string
|
2017-06-21 16:47:06 -04:00
|
|
|
}
|
|
|
|
|
2016-12-25 14:31:52 -05:00
|
|
|
// fileStore implements a credentials store using
|
|
|
|
// the docker configuration file to keep the credentials in plain text.
|
|
|
|
type fileStore struct {
|
2017-06-21 16:47:06 -04:00
|
|
|
file store
|
2016-12-25 14:31:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFileStore creates a new file credentials store.
|
2017-06-21 16:47:06 -04:00
|
|
|
func NewFileStore(file store) Store {
|
|
|
|
return &fileStore{file: file}
|
2016-12-25 14:31:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Erase removes the given credentials from the file store.
|
|
|
|
func (c *fileStore) Erase(serverAddress string) error {
|
2017-06-21 16:47:06 -04:00
|
|
|
delete(c.file.GetAuthConfigs(), serverAddress)
|
2016-12-25 14:31:52 -05:00
|
|
|
return c.file.Save()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get retrieves credentials for a specific server from the file store.
|
|
|
|
func (c *fileStore) Get(serverAddress string) (types.AuthConfig, error) {
|
2017-06-21 16:47:06 -04:00
|
|
|
authConfig, ok := c.file.GetAuthConfigs()[serverAddress]
|
2016-12-25 14:31:52 -05:00
|
|
|
if !ok {
|
|
|
|
// Maybe they have a legacy config file, we will iterate the keys converting
|
|
|
|
// them to the new format and testing
|
2017-06-21 16:47:06 -04:00
|
|
|
for r, ac := range c.file.GetAuthConfigs() {
|
2017-10-15 15:39:56 -04:00
|
|
|
if serverAddress == ConvertToHostname(r) {
|
2016-12-25 14:31:52 -05:00
|
|
|
return ac, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
authConfig = types.AuthConfig{}
|
|
|
|
}
|
|
|
|
return authConfig, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fileStore) GetAll() (map[string]types.AuthConfig, error) {
|
2017-06-21 16:47:06 -04:00
|
|
|
return c.file.GetAuthConfigs(), nil
|
2016-12-25 14:31:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store saves the given credentials in the file store.
|
|
|
|
func (c *fileStore) Store(authConfig types.AuthConfig) error {
|
2017-06-21 16:47:06 -04:00
|
|
|
c.file.GetAuthConfigs()[authConfig.ServerAddress] = authConfig
|
2016-12-25 14:31:52 -05:00
|
|
|
return c.file.Save()
|
|
|
|
}
|
2018-03-26 10:18:32 -04:00
|
|
|
|
|
|
|
func (c *fileStore) GetFilename() string {
|
|
|
|
return c.file.GetFilename()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fileStore) IsFileStore() bool {
|
|
|
|
return true
|
|
|
|
}
|
2017-10-15 15:39:56 -04:00
|
|
|
|
|
|
|
// ConvertToHostname converts a registry url which has http|https prepended
|
|
|
|
// to just an hostname.
|
|
|
|
// Copied from github.com/docker/docker/registry.ConvertToHostname to reduce dependencies.
|
|
|
|
func ConvertToHostname(url string) string {
|
|
|
|
stripped := url
|
|
|
|
if strings.HasPrefix(url, "http://") {
|
|
|
|
stripped = strings.TrimPrefix(url, "http://")
|
|
|
|
} else if strings.HasPrefix(url, "https://") {
|
|
|
|
stripped = strings.TrimPrefix(url, "https://")
|
|
|
|
}
|
|
|
|
|
2022-12-27 11:45:19 -05:00
|
|
|
hostName, _, _ := strings.Cut(stripped, "/")
|
|
|
|
return hostName
|
2017-10-15 15:39:56 -04:00
|
|
|
}
|