github.com/jen20/docker@v1.13.1/cliconfig/credentials/file_store.go (about)

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