def testExistingConfigurationAlreadyConfigured(self): self._WriteTestDockerConfig( json.dumps(cred_utils.GetGcloudCredentialHelperConfig(), indent=2)) self.WriteInput('Y\n') self.Run('auth configure-docker') self.AssertErrMatches(r'gcloud credential helpers already ' r'registered correctly.') self.AssertErrNotContains('Docker configuration file updated.')
def testDockerConfigNotFound(self): self.WriteInput('Y\n') self.Run('auth configure-docker') self.AssertErrContains('Docker configuration file updated.') self.AssertErrContains('config.json') config_info = cred_utils.Configuration.ReadFromDisk() self.assertEqual(cred_utils.GetGcloudCredentialHelperConfig(), config_info.contents)
def testConfigureSuccessWithServerSpecified(self): self.WriteInput('Y\n') self.Run('auth configure-docker us-west1-docker.pkg.dev') self.AssertErrContains('Docker configuration file updated.') self.AssertErrContains(self.test_config.replace('\\', '\\\\')) config_info = cred_utils.Configuration.ReadFromDisk() self.assertEqual( cred_utils.GetGcloudCredentialHelperConfig( ['us-west1-docker.pkg.dev']), config_info.contents)
def testConfigureSuccess(self): self._WriteTestDockerConfig('{}') self.WriteInput('Y\n') self.Run('auth configure-docker') self.AssertErrContains('Docker configuration file updated.') self.AssertErrContains(self.test_config.replace('\\', '\\\\')) config_info = cred_utils.Configuration.ReadFromDisk() self.assertEqual(cred_utils.GetGcloudCredentialHelperConfig(), config_info.contents)
def testExistingConfigurationOverwrite(self): test_mappings = {'credHelpers': {'us.gcr.io': 'gcloud'}} self._WriteTestDockerConfig(json.dumps(test_mappings, indent=2)) self.WriteInput('Y\n') self.Run('auth configure-docker') self.AssertErrMatches(r'Your config file at \[.*\] contains these ' r'credential helper entries') self.AssertErrContains('Docker configuration file updated.') config_info = cred_utils.Configuration.ReadFromDisk() self.assertEqual(cred_utils.GetGcloudCredentialHelperConfig(), config_info.contents)
def Run(self, args): """Run the configure-docker command.""" if not file_utils.SearchForExecutableOnPath( 'docker-credential-gcloud'): log.warning( '`docker-credential-gcloud` not in system PATH.\n' 'gcloud\'s Docker credential helper can be configured but ' 'it will not work until this is corrected.') current_config = cred_utils.Configuration.ReadFromDisk() if file_utils.SearchForExecutableOnPath('docker'): if not current_config.SupportsRegistryHelpers(): raise ConfigureDockerError( 'Invalid Docker version: The version of your Docker client is ' '[{}]; version [{}] or higher is required to support Docker ' 'credential helpers.'.format( current_config.DockerVersion(), cred_utils.MIN_DOCKER_CONFIG_HELPER_VERSION)) else: log.warning( '`docker` not in system PATH.\n' '`docker` and `docker-credential-gcloud` need to be in the same PATH ' 'in order to work correctly together.\n' 'gcloud\'s Docker credential helper can be configured but ' 'it will not work until this is corrected.') current_helpers = current_config.GetRegisteredCredentialHelpers() new_helpers = cred_utils.GetGcloudCredentialHelperConfig() if new_helpers == current_helpers: log.status.Print('gcloud credential helpers ' 'already registered correctly.') return if current_helpers: log.warning( 'Your config file at [{0}] contains these credential helper ' 'entries:\n\n{1}\nThese will be overwritten.'.format( current_config.path, json.dumps(current_helpers, indent=2))) console_io.PromptContinue( message='The following settings will be added to your Docker ' 'config file located at [{0}]:\n {1}'.format( current_config.path, json.dumps(new_helpers, indent=2)), cancel_on_no=True) current_config.RegisterCredentialHelpers() log.status.Print('Docker configuration file updated.')
def testOnlyAppendValidConfigurations(self): test_mappings = {'credHelpers': {'us.gcr.io': 'gcloud'}} self._WriteTestDockerConfig(json.dumps(test_mappings, indent=2)) self.WriteInput('Y\n') self.Run( 'auth configure-docker invalid-domain.com,us-west1-docker.pkg.dev') self.AssertErrMatches(r'Your config file at \[.*\] contains these ' r'credential helper entries') self.AssertErrContains( 'invalid-domain.com is not a supported registry') self.AssertErrContains('Docker configuration file updated.') config_info = cred_utils.Configuration.ReadFromDisk() self.assertEqual( cred_utils.GetGcloudCredentialHelperConfig( ['us.gcr.io', 'us-west1-docker.pkg.dev']), config_info.contents)
def testDockerCredentialGcloudNotOnPath(self): def executable_on_path(binary): if binary == 'docker-credential-gcloud' or binary == 'docker-credential-gcloud.cmd': return False return True self.StartObjectPatch(files, 'SearchForExecutableOnPath', side_effect=executable_on_path) self._WriteTestDockerConfig('{}') self.WriteInput('Y\n') self.Run('auth configure-docker') self.AssertErrContains( 'WARNING: `docker-credential-gcloud` not in system PATH.\n' 'gcloud\'s Docker credential helper can be configured but ' 'it will not work until this is corrected.') config_info = cred_utils.Configuration.ReadFromDisk() self.assertEqual(cred_utils.GetGcloudCredentialHelperConfig(), config_info.contents)
def testMergeConfigurations(self): test_mappings = { 'credHelpers': { 'us.gcr.io': 'gcloud', 'us-west1-docker.pkg.dev': 'not-gcloud' } } self._WriteTestDockerConfig(json.dumps(test_mappings, indent=2)) self.WriteInput('Y\n') self.Run( 'auth configure-docker us-west1-docker.pkg.dev,us-central1-docker.pkg.dev' ) self.AssertErrMatches(r'Your config file at \[.*\] contains these ' r'credential helper entries') self.AssertErrContains('Docker configuration file updated.') config_info = cred_utils.Configuration.ReadFromDisk() self.assertEqual( cred_utils.GetGcloudCredentialHelperConfig([ 'us.gcr.io', 'us-west1-docker.pkg.dev', 'us-central1-docker.pkg.dev' ]), config_info.contents)
def Run(self, args): """Run the configure-docker command.""" if not self.DockerCredentialGcloudExists(): log.warning( '`docker-credential-gcloud` not in system PATH.\n' 'gcloud\'s Docker credential helper can be configured but ' 'it will not work until this is corrected.') current_config = cred_utils.Configuration.ReadFromDisk() if self.DockerExists(): if not current_config.SupportsRegistryHelpers(): raise ConfigureDockerError( 'Invalid Docker version: The version of your Docker client is ' '[{}]; version [{}] or higher is required to support Docker ' 'credential helpers.'.format( current_config.DockerVersion(), cred_utils.MIN_DOCKER_CONFIG_HELPER_VERSION)) else: log.warning( '`docker` not in system PATH.\n' '`docker` and `docker-credential-gcloud` need to be in the same PATH ' 'in order to work correctly together.\n' 'gcloud\'s Docker credential helper can be configured but ' 'it will not work until this is corrected.') current_helpers = current_config.GetRegisteredCredentialHelpers() current_helper_map = {} if current_helpers: log.warning( 'Your config file at [{0}] contains these credential helper ' 'entries:\n\n{1}'.format(current_config.path, json.dumps(current_helpers, indent=2))) current_helper_map = current_helpers[ cred_utils.CREDENTIAL_HELPER_KEY] # Use the value from the argument, otherwise the default list. if args.registries: log.status.Print('Adding credentials for: {0}'.format( args.registries)) registries = filter(self.CheckValidRegistry, args.registries.split(',')) new_helpers = cred_utils.GetGcloudCredentialHelperConfig( registries) else: log.status.Print('Adding credentials for all GCR repositories.') log.warning( 'A long list of credential helpers may cause delays running ' '\'docker build\'. We recommend passing the registry name to ' 'configure only the registry you are using.') new_helpers = cred_utils.GetGcloudCredentialHelperConfig() # Merge in the new settings so that existing configs are preserved. merged_helper_map = current_helper_map.copy() merged_helper_map.update(new_helpers[cred_utils.CREDENTIAL_HELPER_KEY]) if current_helper_map == merged_helper_map: log.status.Print( 'gcloud credential helpers already registered correctly.') return merged_helpers = {cred_utils.CREDENTIAL_HELPER_KEY: merged_helper_map} console_io.PromptContinue( message='After update, the following will be written to your Docker ' 'config file located at [{0}]:\n {1}'.format( current_config.path, json.dumps(merged_helpers, indent=2)), cancel_on_no=True) current_config.RegisterCredentialHelpers(merged_helper_map) log.status.Print('Docker configuration file updated.')
def testGetGcloudCredentialHelperConfig(self): self.assertEqual(cred_utils.GetGcloudCredentialHelperConfig(), _TEST_CRED_HELPERS_CONTENT_DICT)