file_store.go 1.8 KB

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