def authenticate_user(clear_output=True): """Ensures that the given user is authenticated. Currently, this ensures that the Application Default Credentials (https://developers.google.com/identity/protocols/application-default-credentials) are available and valid. Args: clear_output: (optional, default: True) If True, clear any output related to the authorization process if it completes successfully. Any errors will remain (for debugging purposes). Returns: None. Raises: errors.AuthorizationError: If authorization fails. """ if _check_adc(): return os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = _get_adc_path() if not _check_adc(): context_manager = output.temporary if clear_output else _noop with context_manager(): _gcloud_login() _install_adc() if _check_adc(): return raise errors.AuthorizationError('Failed to fetch user credentials')
def authenticate_user(clear_output=True): """Ensures that the given user is authenticated. Currently, this ensures that the Application Default Credentials (https://developers.google.com/identity/protocols/application-default-credentials) are available and valid. Args: clear_output: (optional, default: True) If True, clear the output after successfully completing the authorization process. NOTE: this currently clears ALL cell output. Returns: None. Raises: errors.AuthorizationError: If authorization fails. """ if _check_adc(): return os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = _get_adc_path() if not _check_adc(): _gcloud_login(clear_output=clear_output) _install_adc() if _check_adc(): return raise errors.AuthorizationError('Failed to fetch user credentials')
def authenticate_user(clear_output=True): """Ensures that the given user is authenticated. Currently, this ensures that the Application Default Credentials (https://developers.google.com/identity/protocols/application-default-credentials) are available and valid. Args: clear_output: (optional, default: True) If True, clear any output related to the authorization process if it completes successfully. Any errors will remain (for debugging purposes). Returns: None. Raises: errors.AuthorizationError: If authorization fails. """ if _check_adc(): return _os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = _get_adc_path() if not _check_adc(): context_manager = _output.temporary if clear_output else _noop with context_manager(): _gcloud_login() _install_adc() colab_tpu_addr = _os.environ.get('COLAB_TPU_ADDR', '') if 'COLAB_SKIP_AUTOMATIC_TPU_AUTH' not in _os.environ and colab_tpu_addr: # If we've got a TPU attached, we want to run a TF operation to provide # our new credentials to the TPU for GCS operations. import tensorflow as tf # pylint: disable=g-import-not-at-top if tf.__version__.startswith('1'): with tf.compat.v1.Session( 'grpc://{}'.format(colab_tpu_addr)) as sess: with open(_get_adc_path()) as auth_info: tf.contrib.cloud.configure_gcs( sess, credentials=_json.load(auth_info)) else: # pytype: skip-file tf.config.experimental_connect_to_cluster( tf.distribute.cluster_resolver.TPUClusterResolver()) import tensorflow_gcs_config as _tgc # pylint: disable=g-import-not-at-top _tgc.configure_gcs_from_colab_auth() if _check_adc(): return raise _errors.AuthorizationError('Failed to fetch user credentials')
def _gcloud_login(clear_output): """Call `gcloud auth login` with custom input handling.""" # We want to run gcloud and provide user input on stdin; in order to do this, # we explicitly buffer the gcloud output and print it ourselves. gcloud_command = [ 'gcloud', 'auth', 'login', '--enable-gdrive-access', '--no-launch-browser', '--quiet', ] f, name = tempfile.mkstemp() gcloud_process = subprocess.Popen(gcloud_command, stdin=subprocess.PIPE, stdout=f, stderr=subprocess.STDOUT, universal_newlines=True) try: while True: time.sleep(0.2) os.fsync(f) prompt = open(name).read() if 'https' in prompt: break # Combine the URL with the verification prompt to work around # https://github.com/jupyter/notebook/issues/3159 prompt = prompt.rstrip() code = getpass.getpass(prompt + '\n\nEnter verification code: ') gcloud_process.communicate(code.strip()) finally: os.close(f) os.remove(name) if gcloud_process.returncode: raise errors.AuthorizationError('Error fetching credentials') # TODO(b/67784756): Switch to using tagged outputs. if clear_output: display.clear_output(wait=False)
def _gcloud_login(): """Call `gcloud auth login` with custom input handling.""" # We want to run gcloud and provide user input on stdin; in order to do this, # we explicitly buffer the gcloud output and print it ourselves. gcloud_command = [ 'gcloud', 'auth', 'login', '--enable-gdrive-access', '--no-launch-browser', '--quiet', ] f, name = _tempfile.mkstemp() gcloud_process = _subprocess.Popen(gcloud_command, stdin=_subprocess.PIPE, stdout=f, stderr=_subprocess.STDOUT, universal_newlines=True) try: while True: _time.sleep(0.2) _os.fsync(f) prompt = open(name).read() if 'https' in prompt: break # TODO(b/147296819): Delete this line. get_code = input if _sys.version_info[0] == 3 else raw_input # pylint: disable=undefined-variable # Combine the URL with the verification prompt to work around # https://github.com/jupyter/notebook/issues/3159 prompt = prompt.rstrip() code = get_code(prompt + ' ') gcloud_process.communicate(code.strip()) finally: _os.close(f) _os.remove(name) if gcloud_process.returncode: raise _errors.AuthorizationError('Error fetching credentials')