def _RegisterGcsdkCredProviders(): from googlecloudsdk.core.credentials import store try: # DevShellCredentialProvider() has been removed, no longer needed? store.DevShellCredentialProvider().Register() except AttributeError: pass store.GceCredentialProvider().Register()
def __enter__(self): """Registers sources for credentials and project for use by commands.""" self._credential_providers = self._credential_providers or [ store.DevShellCredentialProvider(), store.GceCredentialProvider(), ] for provider in self._credential_providers: provider.Register() # Register support for service account impersonation. store.IMPERSONATION_TOKEN_PROVIDER = ( iamcred_util.ImpersonationAccessTokenProvider()) return self
def main(gcloud_cli=None, credential_providers=None): if not platforms.PythonVersion().IsCompatible( allow_py3=properties.VALUES.core.allow_py3.GetBool()): sys.exit(1) metrics.Started(START_TIME) # TODO(b/36049857): Put a real version number here metrics.Executions( 'gcloud', local_state.InstallationState.VersionForInstalledComponent('core')) if gcloud_cli is None: gcloud_cli = CreateCLI([]) # Register some other sources for credentials and project. credential_providers = credential_providers or [ creds_store.DevShellCredentialProvider(), creds_store.GceCredentialProvider(), ] for provider in credential_providers: provider.Register() # Register support for service account impersonation. creds_store.IMPERSONATION_TOKEN_PROVIDER = ( iamcred_util.ImpersonationAccessTokenProvider()) try: try: gcloud_cli.Execute() except IOError as err: # We want to ignore EPIPE IOErrors. # By default, Python ignores SIGPIPE (see # http://utcc.utoronto.ca/~cks/space/blog/python/SignalExceptionSurprise). # This means that attempting to write any output to a closed pipe (e.g. in # the case of output piped to `head` or `grep -q`) will result in an # IOError, which gets reported as a gcloud crash. We don't want this # behavior, so we ignore EPIPE (it's not a real error; it's a normal thing # to occur). # Before, we restore the SIGPIPE signal handler, but that caused issues # with scripts/programs that wrapped gcloud. if err.errno != errno.EPIPE: raise except Exception as err: # pylint:disable=broad-except crash_handling.HandleGcloudCrash(err) if properties.VALUES.core.print_unhandled_tracebacks.GetBool(): # We want to see the traceback as normally handled by Python raise else: # This is the case for most non-Cloud SDK developers. They shouldn't see # the full stack trace, but just the nice "gcloud crashed" message. sys.exit(1) finally: for provider in credential_providers: provider.UnRegister()
def SetUp(self): # get some real credentials so we can feed tests something that works. real_creds = c_store.Load() c_store.Refresh(real_creds) self.devshell_proxy = devshell_test_base.AuthReferenceServer( self.GetPort(), response=devshell.CredentialInfoResponse( user_email='*****@*****.**', project_id='fooproj', access_token=real_creds.access_token)) self.devshell_proxy.Start() self._devshell_provider = c_store.DevShellCredentialProvider() self._devshell_provider.Register() properties.VALUES.core.account.Set('*****@*****.**')
def _getCreds(): if not GOOGLE_CLOUD_SDK_ROOT: raise Exception("Unable to find the Google Cloud SDK - make sure it's installed, " "gcloud utility is in the PATH and configured properly") global GOOGLE_CLOUD_SDK_CREDS if not GOOGLE_CLOUD_SDK_CREDS: from googlecloudsdk.core.credentials import store store.DevShellCredentialProvider().Register() store.GceCredentialProvider().Register() GOOGLE_CLOUD_SDK_CREDS = store.LoadIfEnabled() elif GOOGLE_CLOUD_SDK_CREDS.access_token_expired: print('DEBUG: Updating credentials token') from googlecloudsdk.core.credentials import store GOOGLE_CLOUD_SDK_CREDS = store.LoadIfEnabled() return GOOGLE_CLOUD_SDK_CREDS
""" if version is None and component_id: version = local_state.InstallationState.VersionForInstalledComponent( component_id) metrics.Executions(command_name, version) def GetActiveProjectAndAccount(): """Get the active project name and account for the active credentials. For use with wrapping legacy tools that take projects and credentials on the command line. Returns: (str, str), A tuple whose first element is the project, and whose second element is the account. """ project_name = properties.VALUES.core.project.Get(validate=False) account = properties.VALUES.core.account.Get(validate=False) return (project_name, account) def ReadFileContents(*path_parts): """Returns file content at specified relative path wrt SDK root path.""" return files.ReadFileContents(os.path.join(SDK_ROOT, *path_parts)).strip() # Register some other sources for credentials and project. c_store.DevShellCredentialProvider().Register() c_store.GceCredentialProvider().Register()
def main(gcloud_cli=None, credential_providers=None): if not platforms.PythonVersion().IsCompatible( allow_py3=properties.VALUES.core.allow_py3.GetBool()): sys.exit(1) metrics.Started(START_TIME) # TODO(b/36049857): Put a real version number here metrics.Executions( 'gcloud', local_state.InstallationState.VersionForInstalledComponent('core')) if gcloud_cli is None: gcloud_cli = CreateCLI([]) # Register some other sources for credentials and project. credential_providers = credential_providers or [ creds_store.DevShellCredentialProvider(), creds_store.GceCredentialProvider(), ] for provider in credential_providers: provider.Register() # Register support for service account impersonation. creds_store.IMPERSONATION_TOKEN_PROVIDER = ( iamcred_util.ImpersonationAccessTokenProvider()) try: try: gcloud_cli.Execute() # Flush stdout so that if we've received a SIGPIPE we handle the broken # pipe within this try block, instead of potentially during interpreter # shutdown. sys.stdout.flush() except IOError as err: # We want to ignore EPIPE IOErrors (as of Python 3.3 these can be caught # specifically with BrokenPipeError, but we do it this way for Python 2 # compatibility). # # By default, Python ignores SIGPIPE (see # http://utcc.utoronto.ca/~cks/space/blog/python/SignalExceptionSurprise). # This means that attempting to write any output to a closed pipe (e.g. in # the case of output piped to `head` or `grep -q`) will result in an # IOError, which gets reported as a gcloud crash. We don't want this # behavior, so we ignore EPIPE (it's not a real error; it's a normal thing # to occur). # # Before, we restored the SIGPIPE signal handler, but that caused issues # with scripts/programs that wrapped gcloud. if err.errno == errno.EPIPE: # At this point we've caught the broken pipe, but since Python flushes # standard streams on exit, it's still possible for a broken pipe error # to happen during interpreter shutdown. The interpreter will catch this # but in Python 3 it still prints a warning to stderr saying that the # exception was ignored (see https://bugs.python.org/issue11380): # # Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' # encoding='UTF-8'> # BrokenPipeError: [Errno 32] Broken pipe # # To prevent this from happening, we redirect any remaining output to # devnull as recommended here: # https://docs.python.org/3/library/signal.html#note-on-sigpipe. devnull = os.open(os.devnull, os.O_WRONLY) os.dup2(devnull, sys.stdout.fileno()) else: raise except Exception as err: # pylint:disable=broad-except crash_handling.HandleGcloudCrash(err) if properties.VALUES.core.print_unhandled_tracebacks.GetBool(): # We want to see the traceback as normally handled by Python raise else: # This is the case for most non-Cloud SDK developers. They shouldn't see # the full stack trace, but just the nice "gcloud crashed" message. sys.exit(1) finally: for provider in credential_providers: provider.UnRegister()
import logging import pkg_resources from googlecloudsdk.core.credentials import store as creds_store # Setup logging and expose Logger object to the rest of the project LOG = logging.getLogger("cft") LOG.addHandler(logging.StreamHandler()) LOG.propagate = False __VERSION__ = pkg_resources.get_distribution(__name__).version # Register credentials providers - for instance SA, etc credential_providers = [ creds_store.DevShellCredentialProvider(), creds_store.GceCredentialProvider(), ] for provider in credential_providers: provider.Register()
def SetUp(self): self._devshell_provider = store.DevShellCredentialProvider() self._devshell_provider.Register() self.assertIsNone(properties.VALUES.core.project.Get()) self._CreateAndStartDevshellProxy() self.assertEqual('fooproj', properties.VALUES.core.project.Get())
def SetUp(self): self._CreateAndStartDevshellProxy() self._devshell_provider = c_store.DevShellCredentialProvider() self._devshell_provider.Register()