Esempio n. 1
0
  def CreateRequests(self, args):

    name = args.name
    if not name:
      name = gaia_utils.GetDefaultAccountName(self.http)

    user_ref = self.CreateAccountsReference(name, resource_type='users')

    if args.fingerprints:
      fingerprints = args.fingerprints
    else:
      fingerprints = [k.fingerprint for k in
                      self.LookupUser(user_ref.Name()).publicKeys]

    # Generate warning before deleting.
    prompt_list = ['[{0}]'.format(fingerprint) for fingerprint in fingerprints]
    prompt_title = ('The following public keys will be removed from the user ' +
                    user_ref.Name())
    utils.PromptForDeletionHelper(None, prompt_list, prompt_title=prompt_title)

    requests = []
    for fingerprint in fingerprints:
      request = self.messages.ComputeaccountsUsersRemovePublicKeyRequest(
          project=self.project,
          fingerprint=fingerprint,
          user=user_ref.Name())
      requests.append(request)

    return requests
Esempio n. 2
0
    def CreateRequests(self, args):
        """Returns a list of requests necessary for describing users."""

        user = args.name
        if not user:
            user = gaia_utils.GetDefaultAccountName(self.http)

        user_ref = self.CreateAccountsReference(user, resource_type='users')

        request = self.messages.ComputeaccountsUsersGetRequest(
            project=self.project, user=user_ref.Name())

        return [request]
Esempio n. 3
0
  def CreateRequests(self, args):

    name = args.name
    if not name:
      name = gaia_utils.GetDefaultAccountName(self.http)

    user_ref = self.CreateAccountsReference(
        name, resource_type='users')

    valid_key_types = [
        'ssh-rsa', 'ssh-dss', 'ecdsa-sha2-nistp256', 'ssh-ed25519']

    public_keys = []
    for key_file in args.public_key_files:
      key_text = file_utils.ReadFile(key_file, 'public-key')

      if key_text.split(' ', 1)[0] not in valid_key_types:
        raise exceptions.ToolException(
            'You must specify a public key file that contains a key of a '
            'supported form. Supported forms are {0}.'
            .format(', '.join(valid_key_types))
        )
      public_keys.append(key_text)

    formatted_expiration = time_utils.CalculateExpiration(args.expire)

    requests = []
    for key in public_keys:
      public_key_message = self.messages.PublicKey(
          description=args.description,
          expirationTimestamp=formatted_expiration,
          key=key)

      request = self.messages.ComputeaccountsUsersAddPublicKeyRequest(
          project=self.project,
          publicKey=public_key_message,
          user=user_ref.Name())
      requests.append(request)

    return requests
Esempio n. 4
0
    def Run(self, args):
        super(SshGA, self).Run(args)

        parts = args.user_host.split('@')
        if len(parts) == 1:
            if self._use_accounts_service:  # Using Account Service.
                user = gaia_utils.GetDefaultAccountName(self.http)
            else:  # Uploading keys through metadata.
                user = getpass.getuser()
            instance = parts[0]
        elif len(parts) == 2:
            user, instance = parts
        else:
            raise exceptions.ToolException(
                'Expected argument of the form [USER@]INSTANCE; received [{0}].'
                .format(args.user_host))

        instance_ref = self.CreateZonalReference(instance, args.zone)
        external_ip_address = self.GetInstanceExternalIpAddress(instance_ref)

        ssh_args = [self.ssh_executable]
        if not args.plain:
            ssh_args.extend(self.GetDefaultFlags())
            # Allocates a tty if no command was provided and a container was provided.
            if args.container and not args.command:
                ssh_args.append('-t')

        if args.ssh_flag:
            for flag in args.ssh_flag:
                dereferenced_flag = (flag.replace('%USER%', user).replace(
                    '%INSTANCE%', external_ip_address))
                ssh_args.append(dereferenced_flag)

        ssh_args.append(ssh_utils.UserHost(user, external_ip_address))

        interactive_ssh = False
        if args.implementation_args:
            # Backslash-escape shell special characters -- ultra conservative.
            pattern = re.compile(r'(\W)')
            ssh_args.append(' '.join(
                pattern.sub(r'\\\1', arg) for arg in args.implementation_args))
        if args.container:
            ssh_args.append('--')
            ssh_args.append('container_exec')
            ssh_args.append(args.container)
            # Runs the given command inside the given container if --command was
            # specified, otherwise runs /bin/sh.
            if args.command:
                ssh_args.append(args.command)
            else:
                ssh_args.append('/bin/sh')

        elif args.command:
            ssh_args.append('--')
            ssh_args.append(args.command)

        else:
            interactive_ssh = True

        self.ActuallyRun(args,
                         ssh_args,
                         user,
                         external_ip_address,
                         interactive_ssh=interactive_ssh,
                         use_account_service=self._use_accounts_service)