file_store.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package credentials
  2. import (
  3. "github.com/docker/docker/api/types"
  4. "github.com/docker/docker/cli/config/configfile"
  5. "github.com/docker/docker/registry"
  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 r, ac := range c.file.AuthConfigs {
  30. if serverAddress == registry.ConvertToHostname(r) {
  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. }