def LoginAs(self, account, creds, project, activate, brief): """Logs in with valid credentials.""" if not activate: return creds properties.PersistProperty(properties.VALUES.core.account, account) if project: properties.PersistProperty(properties.VALUES.core.project, project) if not config.Paths().workspace_dir: google_creds = client.GoogleCredentials( creds.access_token, creds.client_id, creds.client_secret, creds.refresh_token, creds.token_expiry, creds.token_uri, creds.user_agent, creds.revoke_uri) try: client.save_to_well_known_file(google_creds) except IOError as e: raise c_exc.ToolException( 'error saving Application Default Credentials: ' + str(e)) if not brief: log.status.write('Saved Application Default Credentials.\n') if not brief: log.status.write( '\nYou are now logged in as [{account}].\n' 'Your current project is [{project}]. You can change this setting ' 'by running:\n $ gcloud config set project PROJECT_ID\n'.format( account=account, project=properties.VALUES.core.project.Get())) return creds
def LoginAs(self, account, creds, project, activate, brief): """Logs in with valid credentials.""" if not activate: return creds properties.PersistProperty(properties.VALUES.core.account, account) if project: properties.PersistProperty(properties.VALUES.core.project, project) google_creds = client.GoogleCredentials( creds.access_token, creds.client_id, creds.client_secret, creds.refresh_token, creds.token_expiry, creds.token_uri, creds.user_agent, creds.revoke_uri) try: auth_util.CreateWellKnownFileDir() client.save_to_well_known_file(google_creds) except IOError as e: raise c_exc.ToolException( 'error saving Application Default Credentials: ' + str(e)) if not brief: log.status.write('Saved Application Default Credentials.\n') log.warning( '`gcloud auth login` will stop writing application default ' 'credentials\nin a future release. See:\n ' 'https://developers.google.com/identity/protocols/' 'application-default-credentials#toolcloudsdk' '\nfor more information.') if not brief: log.status.write( '\nYou are now logged in as [{account}].\n' 'Your current project is [{project}]. You can change this setting ' 'by running:\n $ gcloud config set project PROJECT_ID\n'.format( account=account, project=properties.VALUES.core.project.Get())) return creds
def test_save_to_well_known_file(self): import os ORIGINAL_ISDIR = os.path.isdir try: os.path.isdir = lambda path: True credentials = gce.AppAssertionCredentials() with self.assertRaises(NotImplementedError): client.save_to_well_known_file(credentials) finally: os.path.isdir = ORIGINAL_ISDIR
def test_refuses_to_save_to_well_known_file(self): ORIGINAL_ISDIR = os.path.isdir try: os.path.isdir = lambda path: True with _AuthReferenceServer(): creds = DevshellCredentials() with self.assertRaises(NotImplementedError): save_to_well_known_file(creds) finally: os.path.isdir = ORIGINAL_ISDIR
def test_refuses_to_save_to_well_known_file(self): ORIGINAL_ISDIR = os.path.isdir try: os.path.isdir = lambda path: True with _AuthReferenceServer(): creds = devshell.DevshellCredentials() with self.assertRaises(NotImplementedError): client.save_to_well_known_file(creds) finally: os.path.isdir = ORIGINAL_ISDIR
def test_save_to_well_known_file(self): import os ORIGINAL_ISDIR = os.path.isdir try: os.path.isdir = lambda path: True credentials = AppAssertionCredentials() with self.assertRaises(NotImplementedError): save_to_well_known_file(credentials) finally: os.path.isdir = ORIGINAL_ISDIR
def test_save_to_well_known_file_service_account(self): credential_file = datafile(os.path.join("gcloud", "application_default_credentials.json")) credentials = _get_application_default_credential_from_file(credential_file) temp_credential_file = datafile(os.path.join("gcloud", "temp_well_known_file_service_account.json")) save_to_well_known_file(credentials, temp_credential_file) with open(temp_credential_file) as f: d = json.load(f) self.assertEqual("service_account", d["type"]) self.assertEqual("123", d["client_id"]) self.assertEqual("*****@*****.**", d["client_email"]) self.assertEqual("ABCDEF", d["private_key_id"]) os.remove(temp_credential_file)
def test_save_to_well_known_file_authorized_user(self): credentials_file = datafile(os.path.join("gcloud", "application_default_credentials_authorized_user.json")) credentials = _get_application_default_credential_from_file(credentials_file) temp_credential_file = datafile(os.path.join("gcloud", "temp_well_known_file_authorized_user.json")) save_to_well_known_file(credentials, temp_credential_file) with open(temp_credential_file) as f: d = json.load(f) self.assertEqual("authorized_user", d["type"]) self.assertEqual("123", d["client_id"]) self.assertEqual("secret", d["client_secret"]) self.assertEqual("alabalaportocala", d["refresh_token"]) os.remove(temp_credential_file)
def test_save_to_well_known_file_service_account(self): credential_file = datafile( os.path.join('gcloud', 'application_default_credentials.json')) credentials = _get_application_default_credential_from_file( credential_file) temp_credential_file = datafile( os.path.join('gcloud', 'temp_well_known_file_service_account.json')) save_to_well_known_file(credentials, temp_credential_file) with open(temp_credential_file) as f: d = json.load(f) self.assertEqual('service_account', d['type']) self.assertEqual('123', d['client_id']) self.assertEqual('*****@*****.**', d['client_email']) self.assertEqual('ABCDEF', d['private_key_id']) os.remove(temp_credential_file)
def test_save_to_well_known_file_authorized_user(self): credentials_file = datafile( os.path.join('gcloud', 'application_default_credentials_authorized_user.json')) credentials = _get_application_default_credential_from_file( credentials_file) temp_credential_file = datafile( os.path.join('gcloud', 'temp_well_known_file_authorized_user.json')) save_to_well_known_file(credentials, temp_credential_file) with open(temp_credential_file) as f: d = json.load(f) self.assertEqual('authorized_user', d['type']) self.assertEqual('123', d['client_id']) self.assertEqual('secret', d['client_secret']) self.assertEqual('alabalaportocala', d['refresh_token']) os.remove(temp_credential_file)
def SaveCredentials(self, creds, brief): """Saves the credentials in the well-known file for ADC.""" try: client.save_to_well_known_file(creds) except IOError as e: raise c_exc.ToolException( 'Error saving Application Default Credentials: ' + str(e)) if not brief: log.status.write( '\nApplication Default Credentials are now saved, and will\n' 'use the given service account.\n') log.status.write( '\nThis does not affect any credentials that you may have\n' 'set up for the Google Cloud SDK.\n') return creds
def SaveCredentials(self, creds, brief): """Saves the credentials in the well-known file for ADC.""" google_creds = client.GoogleCredentials( creds.access_token, creds.client_id, creds.client_secret, creds.refresh_token, creds.token_expiry, creds.token_uri, creds.user_agent, creds.revoke_uri) try: client.save_to_well_known_file(google_creds) except IOError as e: raise c_exc.ToolException( 'error saving Application Default Credentials: ' + str(e)) if not brief: log.status.write( '\nApplication Default Credentials are now saved, and will\n' 'use the provided account.\n') log.status.write( '\nThis does not affect any credentials that you may have\n' 'set up for the Google Cloud SDK.\n') return creds
def test_save_to_well_known_file(self, get_metadata): with tempfile.NamedTemporaryFile() as temp: credentials = AppAssertionCredentials() save_to_well_known_file(credentials, well_known_file=temp.name) self.assertIsNotNone(temp.read())
def test_save_to_well_known_file(self): os.environ[_CLOUDSDK_CONFIG_ENV_VAR] = tempfile.mkdtemp() credentials = AppAssertionCredentials([]) with self.assertRaises(NotImplementedError): save_to_well_known_file(credentials) del os.environ[_CLOUDSDK_CONFIG_ENV_VAR]
def test_save_to_well_known_file(self): os.environ[client._CLOUDSDK_CONFIG_ENV_VAR] = tempfile.mkdtemp() credentials = appengine.AppAssertionCredentials([]) with self.assertRaises(NotImplementedError): client.save_to_well_known_file(credentials) del os.environ[client._CLOUDSDK_CONFIG_ENV_VAR]