2016-02-07 19:55:17 -05:00
|
|
|
package credentials
|
|
|
|
|
|
|
|
import (
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/cliconfig/configfile"
|
2016-09-06 14:46:37 -04:00
|
|
|
"github.com/docker/docker/registry"
|
2016-02-07 19:55:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// fileStore implements a credentials store using
|
|
|
|
// the docker configuration file to keep the credentials in plain text.
|
|
|
|
type fileStore struct {
|
2016-04-21 18:37:08 -04:00
|
|
|
file *configfile.ConfigFile
|
2016-02-07 19:55:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFileStore creates a new file credentials store.
|
2016-04-21 18:37:08 -04:00
|
|
|
func NewFileStore(file *configfile.ConfigFile) Store {
|
2016-02-07 19:55:17 -05:00
|
|
|
return &fileStore{
|
|
|
|
file: file,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Erase removes the given credentials from the file store.
|
|
|
|
func (c *fileStore) Erase(serverAddress string) error {
|
|
|
|
delete(c.file.AuthConfigs, serverAddress)
|
|
|
|
return c.file.Save()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get retrieves credentials for a specific server from the file store.
|
|
|
|
func (c *fileStore) Get(serverAddress string) (types.AuthConfig, error) {
|
|
|
|
authConfig, ok := c.file.AuthConfigs[serverAddress]
|
|
|
|
if !ok {
|
|
|
|
// Maybe they have a legacy config file, we will iterate the keys converting
|
|
|
|
// them to the new format and testing
|
2016-05-30 06:47:49 -04:00
|
|
|
for r, ac := range c.file.AuthConfigs {
|
|
|
|
if serverAddress == registry.ConvertToHostname(r) {
|
2016-02-07 19:55:17 -05:00
|
|
|
return ac, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
authConfig = types.AuthConfig{}
|
|
|
|
}
|
|
|
|
return authConfig, nil
|
|
|
|
}
|
|
|
|
|
2016-03-01 12:04:35 -05:00
|
|
|
func (c *fileStore) GetAll() (map[string]types.AuthConfig, error) {
|
|
|
|
return c.file.AuthConfigs, nil
|
|
|
|
}
|
|
|
|
|
2016-02-07 19:55:17 -05:00
|
|
|
// Store saves the given credentials in the file store.
|
|
|
|
func (c *fileStore) Store(authConfig types.AuthConfig) error {
|
|
|
|
c.file.AuthConfigs[authConfig.ServerAddress] = authConfig
|
|
|
|
return c.file.Save()
|
|
|
|
}
|