2016-12-25 14:31:52 -05:00
|
|
|
package credentials
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/registry"
|
|
|
|
)
|
|
|
|
|
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() {
|
2016-12-25 14:31:52 -05:00
|
|
|
if serverAddress == registry.ConvertToHostname(r) {
|
|
|
|
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
|
|
|
|
}
|