def ReadFromDisk(cls, path=None): """Reads configuration file and meta-data from default Docker location. Reads configuration file and meta-data from default Docker location. Returns a Configuration object containing the full contents of the configuration file, and the configuration file path. Args: path: string, path to look for the Docker config file. If empty will attempt to read from the new config location (default). Returns: A Configuration object Raises: ValueError: path or is_new_format are not set. InvalidDockerConfigError: config file could not be read as JSON. """ path = path or client_utils.GetDockerConfigPath(True)[0] try: content = client_utils.ReadConfigurationFile(path) except (ValueError, client_utils.DockerError) as err: raise client_utils.InvalidDockerConfigError( ('Docker configuration file [{}] could not be read as JSON: {}' ).format(path, str(err))) return cls(content, path)
def testReadConfigurationFileEmptyFile(self): contents = 'FOO' test_path = self.Touch(self.test_dir, 'test_config.json', contents) with self.assertRaisesRegex( client_lib.InvalidDockerConfigError, r'Docker configuration file \[.*\] ' r'could not be read as JSON'): client_lib.ReadConfigurationFile(test_path)
def ReadDockerAuthConfig(): """Retrieve the contents of the Docker authorization entry. NOTE: This is public only to facilitate testing. Returns: The map of authorizations used by docker. """ # Not using DockerConfigInfo here to be backward compatible with # UpdateDockerCredentials which should still work if Docker is not installed path, new_format = client_lib.GetDockerConfigPath() structure = client_lib.ReadConfigurationFile(path) if new_format: return structure['auths'] if 'auths' in structure else {} else: return structure
def WriteDockerAuthConfig(structure): """Write out a complete set of Docker authorization entries. This is public only to facilitate testing. Args: structure: The dict of authorization mappings to write to the Docker configuration file. """ # Not using DockerConfigInfo here to be backward compatible with # UpdateDockerCredentials which should still work if Docker is not installed path, is_new_format = client_lib.GetDockerConfigPath() contents = client_lib.ReadConfigurationFile(path) if is_new_format: full_cfg = contents full_cfg['auths'] = structure file_contents = json.dumps(full_cfg, indent=2) else: file_contents = json.dumps(structure, indent=2) files.WriteFileAtomically(path, file_contents)
def _GCRCredHelperConfigured(): """Returns True if docker-credential-gcr is the docker credential store. Returns: True if docker-credential-gcr is specified in the docker config. False if the config file does not exist, does not contain a 'credsStore' key, or if the credstore is not docker-credential-gcr. """ try: # Not using DockerConfigInfo here to be backward compatible with # UpdateDockerCredentials which should still work if Docker is not installed path, is_new_format = client_lib.GetDockerConfigPath() contents = client_lib.ReadConfigurationFile(path) if is_new_format and (_CREDENTIAL_STORE_KEY in contents): return contents[_CREDENTIAL_STORE_KEY] == 'gcr' else: # Docker <1.7.0 (no credential store support) or credsStore == null return False except IOError: # Config file doesn't exist or can't be parsed. return False
def _CredentialStoreConfigured(): """Returns True if a credential store is specified in the docker config. Returns: True if a credential store is specified in the docker config. False if the config file does not exist or does not contain a 'credsStore' key. """ try: # Not Using DockerConfigInfo here to be backward compatiable with # UpdateDockerCredentials which should still work if Docker is not installed path, is_new_format = client_lib.GetDockerConfigPath() contents = client_lib.ReadConfigurationFile(path) if is_new_format: return _CREDENTIAL_STORE_KEY in contents else: # The old format is for Docker <1.7.0. # Older Docker clients (<1.11.0) don't support credential helpers. return False except IOError: # Config file doesn't exist. return False
def testReadConfigurationFileMissingPath(self): with self.assertRaisesRegex(ValueError, 'Docker configuration file path is empty'): client_lib.ReadConfigurationFile(None)
def testReadConfigurationFilePathNotFound(self): path_mock = self.StartObjectPatch(os.path, 'exists') path_mock.return_value = False self.assertEqual({}, client_lib.ReadConfigurationFile('//fake/path')) self.assertTrue(path_mock.called)
def testReadConfigurationFileInvalidFile(self): test_path = self.Touch(self.test_dir, 'test_config.json', '') self.assertEqual({}, client_lib.ReadConfigurationFile(test_path))
def testReadConfigurationFile(self): contents = {'x': 'y'} test_path = self.Touch(self.test_dir, 'test_config.json', json.dumps(contents)) self.assertEqual(contents, client_lib.ReadConfigurationFile(test_path))