Esempio n. 1
0
  def Run(self, args):
    """Run the authentication command."""

    if c_devshell.IsDevshellEnvironment():
      message = """
          You are already authenticated with gcloud when running
          inside the Cloud Shell and so do not need to run this
          command.

          Do you wish to proceed anyway?
        """
      answer = console_io.PromptContinue(message=message)
      if not answer:
        return None
    elif c_gce.Metadata().connected:
      message = textwrap.dedent("""
          You are running on a Google Compute Engine virtual machine.
          It is recommended that you use service accounts for authentication.

          You can run:

            $ gcloud config set account ``ACCOUNT''

          to switch accounts if necessary.

          Your credentials may be visible to others with access to this
          virtual machine. Are you sure you want to authenticate with
          your personal account?
          """)
      answer = console_io.PromptContinue(message=message)
      if not answer:
        return None

    account = args.account

    if account and not args.force:
      creds = c_store.LoadIfValid(account=account)
      if creds:
        # Account already has valid creds, just switch to it.
        return self.LoginAs(account, creds, args.project, args.activate,
                            args.brief)

    # No valid creds, do the web flow.
    launch_browser = auth_util.ShouldLaunchBrowser(args.launch_browser)
    creds = self.DoInstalledAppBrowserFlow(launch_browser)
    web_flow_account = creds.id_token['email']
    if account and account.lower() != web_flow_account.lower():
      raise c_exc.ToolException(
          'You attempted to log in as account [{account}] but the received '
          'credentials were for account [{web_flow_account}].\n\n'
          'Please check that your browser is logged in as account [{account}] '
          'and that you are using the correct browser profile.'.format(
              account=account, web_flow_account=web_flow_account))

    account = web_flow_account
    # We got new creds, and they are for the correct user.
    c_store.Store(creds, account)
    return self.LoginAs(account, creds, args.project, args.activate,
                        args.brief)
Esempio n. 2
0
  def Run(self, args):
    log.status.write('Welcome! This command will take you through '
                     'the configuration of gcloud.\n\n')

    creds = c_store.LoadIfValid()
    if not creds:
      log.status.write('To start, you must login.  When the web browser opens, '
                       'login with your google account and hit accept.\n\n')
      answer = console_io.PromptContinue()
      if not answer:
        return
      creds = self.cli.Execute(['auth', 'login'])

    log.status.write('\nYou are now logged in as: [{0}]\n'
                     .format(creds.id_token['email']))
Esempio n. 3
0
    def Run(self, args):
        """Run the authentication command."""

        if c_gce.Metadata().connected:
            message = textwrap.dedent("""
          You are running on a GCE VM. It is recommended that you use
          service accounts for authentication.

          You can run:

            $ gcloud config set account ``ACCOUNT''

          to switch accounts if necessary.

          Your credentials may be visible to others with access to this
          virtual machine. Are you sure you want to authenticate with
          your personal account?
          """)
            answer = console_io.PromptContinue(message=message)
            if not answer:
                return None

        account = args.account

        if account and not args.force:
            creds = c_store.LoadIfValid(account=account)
            if creds:
                # Account already has valid creds, just switch to it.
                return self.LoginAs(account, creds, args.project,
                                    args.do_not_activate)

        # No valid creds, do the web flow.
        creds = self.DoWebFlow(args.launch_browser)
        web_flow_account = creds.token_response['id_token']['email']
        if account and account != web_flow_account:
            raise c_exc.ToolException(
                'You attempted to log in as account [{account}] but the received '
                'credentials were for account [{web_flow_account}].\n\n'
                'Please check that your browser is logged in as account [{account}] '
                'and that you are using the correct browser profile.'.format(
                    account=account, web_flow_account=web_flow_account))

        account = web_flow_account
        # We got new creds, and they are for the correct user.
        c_store.Store(creds, account)
        return self.LoginAs(account, creds, args.project, args.do_not_activate)