github.com/hustcat/docker@v1.3.3-0.20160314103604-901c67a8eeab/cliconfig/credentials/file_store.go (about) 1 package credentials 2 3 import ( 4 "strings" 5 6 "github.com/docker/docker/cliconfig" 7 "github.com/docker/engine-api/types" 8 ) 9 10 // fileStore implements a credentials store using 11 // the docker configuration file to keep the credentials in plain text. 12 type fileStore struct { 13 file *cliconfig.ConfigFile 14 } 15 16 // NewFileStore creates a new file credentials store. 17 func NewFileStore(file *cliconfig.ConfigFile) Store { 18 return &fileStore{ 19 file: file, 20 } 21 } 22 23 // Erase removes the given credentials from the file store. 24 func (c *fileStore) Erase(serverAddress string) error { 25 delete(c.file.AuthConfigs, serverAddress) 26 return c.file.Save() 27 } 28 29 // Get retrieves credentials for a specific server from the file store. 30 func (c *fileStore) Get(serverAddress string) (types.AuthConfig, error) { 31 authConfig, ok := c.file.AuthConfigs[serverAddress] 32 if !ok { 33 // Maybe they have a legacy config file, we will iterate the keys converting 34 // them to the new format and testing 35 for registry, ac := range c.file.AuthConfigs { 36 if serverAddress == convertToHostname(registry) { 37 return ac, nil 38 } 39 } 40 41 authConfig = types.AuthConfig{} 42 } 43 return authConfig, nil 44 } 45 46 func (c *fileStore) GetAll() (map[string]types.AuthConfig, error) { 47 return c.file.AuthConfigs, nil 48 } 49 50 // Store saves the given credentials in the file store. 51 func (c *fileStore) Store(authConfig types.AuthConfig) error { 52 c.file.AuthConfigs[authConfig.ServerAddress] = authConfig 53 return c.file.Save() 54 } 55 56 func convertToHostname(url string) string { 57 stripped := url 58 if strings.HasPrefix(url, "http://") { 59 stripped = strings.Replace(url, "http://", "", 1) 60 } else if strings.HasPrefix(url, "https://") { 61 stripped = strings.Replace(url, "https://", "", 1) 62 } 63 64 nameParts := strings.SplitN(stripped, "/", 2) 65 66 return nameParts[0] 67 }