Ejemplo n.º 1
0
def MakeGetAssetsHistoryHttpRequests(args,
                                     service,
                                     api_version=DEFAULT_API_VERSION):
    """Manually make the get assets history request."""
    messages = GetMessages(api_version)

    encoding.AddCustomJsonFieldMapping(
        messages.CloudassetBatchGetAssetsHistoryRequest,
        'readTimeWindow_startTime', 'readTimeWindow.startTime')
    encoding.AddCustomJsonFieldMapping(
        messages.CloudassetBatchGetAssetsHistoryRequest,
        'readTimeWindow_endTime', 'readTimeWindow.endTime')

    content_type = arg_utils.ChoiceToEnum(
        args.content_type, messages.CloudassetBatchGetAssetsHistoryRequest.
        ContentTypeValueValuesEnum)
    parent = asset_utils.GetParentNameForGetHistory(args.organization,
                                                    args.project)
    start_time = times.FormatDateTime(args.start_time)
    end_time = None
    if args.IsSpecified('end_time'):
        end_time = times.FormatDateTime(args.end_time)

    response = service.BatchGetAssetsHistory(
        messages.CloudassetBatchGetAssetsHistoryRequest(
            assetNames=args.asset_names,
            relationshipTypes=args.relationship_types,
            contentType=content_type,
            parent=parent,
            readTimeWindow_endTime=end_time,
            readTimeWindow_startTime=start_time,
        ))

    for asset in response.assets:
        yield asset
Ejemplo n.º 2
0
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Yields:
      Some value that we want to have printed later.
    """

        cli = util.GetAdminClient()
        # cluster_ref: A resource reference to the cluster to search for hottablets.
        cluster_ref = args.CONCEPTS.cluster.Parse()

        # Create a ListHotTablets Request and send through Admin Client.
        msg = (util.GetAdminMessages(
        ).BigtableadminProjectsInstancesClustersHotTabletsListRequest(
            parent=cluster_ref.RelativeName(),
            startTime=args.start_time
            and times.FormatDateTime(args.start_time),
            endTime=args.end_time and times.FormatDateTime(args.end_time)))

        for hot_tablet in list_pager.YieldFromList(
                cli.projects_instances_clusters_hotTablets,
                msg,
                field='hotTablets',
                batch_size_attribute=None):
            yield hot_tablet
Ejemplo n.º 3
0
def MakeInstanceSchedulePolicy(policy_ref, args, messages):
    """Creates an Instance Schedule Policy message from args."""

    vm_start_schedule = None
    if args.vm_start_schedule:
        vm_start_schedule = messages.ResourcePolicyInstanceSchedulePolicySchedule(
            schedule=args.vm_start_schedule)

    vm_stop_schedule = None
    if args.vm_stop_schedule:
        vm_stop_schedule = messages.ResourcePolicyInstanceSchedulePolicySchedule(
            schedule=args.vm_stop_schedule)

    instance_schedule_policy = messages.ResourcePolicyInstanceSchedulePolicy(
        timeZone=args.timezone,
        vmStartSchedule=vm_start_schedule,
        vmStopSchedule=vm_stop_schedule)

    if args.initiation_date:
        instance_schedule_policy.startTime = times.FormatDateTime(
            args.initiation_date)

    if args.end_date:
        instance_schedule_policy.expirationTime = times.FormatDateTime(
            args.end_date)

    return messages.ResourcePolicy(
        name=policy_ref.Name(),
        description=args.description,
        region=policy_ref.region,
        instanceSchedulePolicy=instance_schedule_policy)
Ejemplo n.º 4
0
    def SetUp(self):
        self.ascii_armored_key = textwrap.dedent("""
        -----BEGIN PGP PUBLIC KEY BLOCK-----
        Key1
        Key1
        Key1
        -----END PGP PUBLIC KEY BLOCK-----
    """)
        self.name = 'bar'
        proj = self.Project()
        self.fingerprint = '0638AttestorDD940361EA2D7F14C58C124F0E663DA097'
        self.existing_pub_key = self.messages.AttestorPublicKey(
            asciiArmoredPgpPublicKey=self.ascii_armored_key,
            comment=None,
            id=self.fingerprint)
        try:
            self.attestor = self.messages.Attestor(
                name='projects/{}/attestors/{}'.format(proj, self.name),
                updateTime=times.FormatDateTime(datetime.datetime.utcnow()),
                userOwnedGrafeasNote=self.messages.UserOwnedGrafeasNote(
                    noteReference='projects/{}/notes/{}'.format(
                        proj, self.name),
                    publicKeys=[self.existing_pub_key],
                ))
        except AttributeError:
            self.attestor = self.messages.Attestor(
                name='projects/{}/attestors/{}'.format(proj, self.name),
                updateTime=times.FormatDateTime(datetime.datetime.utcnow()),
                userOwnedDrydockNote=self.messages.UserOwnedDrydockNote(
                    noteReference='projects/{}/notes/{}'.format(
                        proj, self.name),
                    publicKeys=[self.existing_pub_key],
                ))

        self.updated_pub_key = copy.deepcopy(self.existing_pub_key)
        self.updated_ascii_armored_key = (self.ascii_armored_key.replace(
            'Key1', 'Key2'))
        self.updated_pub_key.asciiArmoredPgpPublicKey = (
            self.updated_ascii_armored_key)
        self.updated_attestor = copy.deepcopy(self.attestor)
        try:
            self.updated_attestor.userOwnedGrafeasNote.publicKeys = [
                self.updated_pub_key
            ]
        except AttributeError:
            self.updated_attestor.userOwnedDrydockNote.publicKeys = [
                self.updated_pub_key
            ]

        self.fname = self.Touch(directory=self.cwd_path,
                                contents=self.updated_ascii_armored_key)

        self.req = self.messages.BinaryauthorizationProjectsAttestorsGetRequest(
            name=self.attestor.name)
Ejemplo n.º 5
0
    def testSuccess_WithPublicKey(self):
        ascii_armored_key = textwrap.dedent("""
        -----BEGIN PGP PUBLIC KEY BLOCK-----
        aBcDeFg
        aBcDeFg
        aBcDeFg
        -----END PGP PUBLIC KEY BLOCK-----
    """)

        name = 'bar'
        proj = self.Project()
        try:
            attestor = self.messages.Attestor(
                name='projects/{}/attestors/{}'.format(proj, name),
                updateTime=times.FormatDateTime(datetime.datetime.utcnow()),
                userOwnedGrafeasNote=self.messages.UserOwnedGrafeasNote(
                    noteReference='projects/{}/notes/{}'.format(proj, name),
                    publicKeys=[
                        self.messages.AttestorPublicKey(
                            asciiArmoredPgpPublicKey=ascii_armored_key,
                            comment=None,
                            id='new_key'),
                    ],
                ))
        except AttributeError:
            attestor = self.messages.Attestor(
                name='projects/{}/attestors/{}'.format(proj, name),
                updateTime=times.FormatDateTime(datetime.datetime.utcnow()),
                userOwnedDrydockNote=self.messages.UserOwnedDrydockNote(
                    noteReference='projects/{}/notes/{}'.format(proj, name),
                    publicKeys=[
                        self.messages.AttestorPublicKey(
                            asciiArmoredPgpPublicKey=ascii_armored_key,
                            comment=None,
                            id='new_key'),
                    ],
                ))
        req = self.messages.BinaryauthorizationProjectsAttestorsGetRequest(
            name='projects/{}/attestors/{}'.format(proj, name), )

        self.mock_client.projects_attestors.Get.Expect(req, response=attestor)

        response = self.RunBinauthz(
            'attestors describe {name}'.format(name=name))

        self.assertEqual(response, attestor)

        # Assert ascii-armored key appears in output.
        self.AssertOutputMatches(
            r'\n[ ]*'.join([''] + ascii_armored_key.splitlines()).rstrip())
Ejemplo n.º 6
0
 def Export(self, args):
     """Export assets with the asset export method."""
     content_type = ContentTypeTranslation(args.content_type)
     content_type = getattr(
         self.message_module.ExportAssetsRequest.ContentTypeValueValuesEnum,
         content_type)
     # TODO(b/131354776): After flag --output_path_prefix is rolled out to GA, we
     # can replace this if-else block with GcsDestination(uri=args.output_path,
     # uriPrefix=args.output_path_prefix). Before that,
     # GcsDestination(uri=args.output_path, uriPrefix=args.output_path_prefix)
     # would break the GA track command since flag --output_path_prefix is not
     # registered for GA track.
     if args.output_path:
         output_config = self.message_module.OutputConfig(
             gcsDestination=self.message_module.GcsDestination(
                 uri=args.output_path))
     else:
         output_config = self.message_module.OutputConfig(
             gcsDestination=self.message_module.GcsDestination(
                 uriPrefix=args.output_path_prefix))
     snapshot_time = None
     if args.snapshot_time:
         snapshot_time = times.FormatDateTime(args.snapshot_time)
     export_assets_request = self.message_module.ExportAssetsRequest(
         assetTypes=args.asset_types,
         contentType=content_type,
         outputConfig=output_config,
         readTime=snapshot_time)
     request_message = self.message_module.CloudassetExportAssetsRequest(
         parent=self.parent, exportAssetsRequest=export_assets_request)
     operation = self.service.ExportAssets(request_message)
     return operation
Ejemplo n.º 7
0
 def SamplePubsubMetadata(self):
     """Returns a PubsubMetadata object for use in tests."""
     expire_time = times.FormatDateTime(
         times.ParseDateTime('2019-08-14 15:44:10'))
     return MESSAGE_MODULE.PubsubSnapshotMetadata(topicName='topic',
                                                  snapshotName='snapshot',
                                                  expireTime=expire_time)
Ejemplo n.º 8
0
  def testAlreadyExistsPkix(self):
    pem = textwrap.dedent("""
        -----BEGIN PUBLIC KEY-----
        aBcDeFg
        aBcDeFg
        aBcDeFg
        -----END PUBLIC KEY-----
    """)
    fname = self.Touch(directory=self.cwd_path, contents=pem)

    name = 'bar'
    proj = self.Project()
    new_pub_key = self.messages.AttestorPublicKey(
        pkixPublicKey=self.messages.PkixPublicKey(
            publicKeyPem=pem,
            signatureAlgorithm=(
                self.messages.PkixPublicKey.SignatureAlgorithmValueValuesEnum
                .ECDSA_P256_SHA256),
        ),
        comment=None,
        id='ni://sha256;0638attestordd940361ea2d7f14c58c124f0e663da097')

    # Handle multiple API versions: GA has userOwnedGrafeasNote, beta has has
    # userOwnedDrydockNote (also below).
    try:
      attestor = self.messages.Attestor(
          name='projects/{}/attestors/{}'.format(proj, name),
          updateTime=None,
          userOwnedGrafeasNote=self.messages.UserOwnedGrafeasNote(
              noteReference='projects/{}/notes/{}'.format(proj, name),
              publicKeys=[new_pub_key],
          ))
    except AttributeError:
      attestor = self.messages.Attestor(
          name='projects/{}/attestors/{}'.format(proj, name),
          updateTime=None,
          userOwnedDrydockNote=self.messages.UserOwnedDrydockNote(
              noteReference='projects/{}/notes/{}'.format(proj, name),
              publicKeys=[new_pub_key],
          ))

    updated_attestor = copy.deepcopy(attestor)
    try:
      updated_attestor.userOwnedGrafeasNote.publicKeys.append(new_pub_key)
    except AttributeError:
      updated_attestor.userOwnedDrydockNote.publicKeys.append(new_pub_key)
    updated_attestor.updateTime = times.FormatDateTime(
        datetime.datetime.utcnow())

    req = self.messages.BinaryauthorizationProjectsAttestorsGetRequest(
        name=attestor.name)
    self.mock_client.projects_attestors.Get.Expect(req, response=attestor)

    with self.assertRaises(exceptions.AlreadyExistsError):
      self.RunBinauthz(
          'attestors public-keys add '
          '--pkix-public-key-algorithm=ecdsa-p256-sha256 '
          '--public-key-id-override={id_override} '
          '--attestor={name} --pkix-public-key-file={fname}'.format(
              name=name, fname=fname, id_override=new_pub_key.id))
Ejemplo n.º 9
0
    def _FormatFromParts():
        """Returns the formatted time from broken down time parts in r.

    Raises:
      TypeError: For invalid time part errors.
      ValueError: For time conversion errors or not enough valid time parts.

    Returns:
      The formatted time from broken down time parts in r.
    """
        valid = 0
        parts = []
        now = datetime.datetime.now(tz_in)
        for part in ('year', 'month', 'day', 'hour', 'minute', 'second'):
            value = resource_property.Get(r, [part], None)
            if value is None:
                # Missing parts default to now.
                value = getattr(now, part, 0)
            else:
                valid += 1
            parts.append(int(value))
        # The last value is microseconds. Add in any subsecond parts but don't count
        # them in the validity check.
        parts.append(0)
        for i, part in enumerate(['nanosecond', 'microsecond', 'millisecond']):
            value = resource_property.Get(r, [part], None)
            if value is not None:
                parts[-1] += int(int(value) * 1000**(i - 1))
        # year&month&day or hour&minute&second would be OK, "3" covers those and any
        # combination of 3 non-subsecond date/time parts.
        if valid < 3:
            raise ValueError
        parts.append(tz_in)
        dt = datetime.datetime(*parts)
        return times.FormatDateTime(dt, format, tz_out)
Ejemplo n.º 10
0
    def Run(self, args):
        cert_ref = Revoke.ParseCertificateResource(args)

        if not console_io.PromptContinue(
                message='You are about to revoke Certificate [{}]'.format(
                    cert_ref.RelativeName()),
                default=True):
            log.status.Print('Aborted by user.')
            return

        reason = flags_v1.ParseRevocationChoiceToEnum(args.reason)

        client = privateca_base.GetClientInstance(api_version='v1')
        messages = privateca_base.GetMessagesModule(api_version='v1')

        certificate = client.projects_locations_caPools_certificates.Revoke(
            messages.
            PrivatecaProjectsLocationsCaPoolsCertificatesRevokeRequest(
                name=cert_ref.RelativeName(),
                revokeCertificateRequest=messages.RevokeCertificateRequest(
                    reason=reason,
                    requestId=request_utils.GenerateRequestId())))

        revoke_time = times.ParseDateTime(
            certificate.revocationDetails.revocationTime)
        log.status.Print('Revoked certificate [{}] at {}.'.format(
            certificate.name,
            times.FormatDateTime(revoke_time, tzinfo=times.LOCAL)))
Ejemplo n.º 11
0
    def _GetByDatetime(self, datetime, args):
        # Backwards compatibility during deprecation period.
        # If user passes DUE_TIME instead of ID, use v1beta3 API.
        client = api_util.SqlClient(api_util.API_VERSION_FALLBACK)
        sql_client = client.sql_client
        sql_messages = client.sql_messages
        log.warning(
            'Starting on 2017-06-30, DUE_TIME will no longer be valid: Use '
            'the ID argument instead to retrieve a backup. You can start '
            'using ID now.')

        instance_ref = client.resource_parser.Parse(
            args.instance,
            params={'project': properties.VALUES.core.project.GetOrFail},
            collection='sql.instances')

        instance = sql_client.instances.Get(
            sql_messages.SqlInstancesGetRequest(
                project=instance_ref.project, instance=instance_ref.instance))

        backup_config = instance.settings.backupConfiguration[0].id
        request = sql_messages.SqlBackupRunsGetRequest(
            project=instance_ref.project,
            instance=instance_ref.instance,
            backupConfiguration=backup_config,
            dueTime=times.FormatDateTime(datetime, tzinfo=times.UTC))
        return sql_client.backupRuns.Get(request)
Ejemplo n.º 12
0
  def Format(self, entry):
    """Safely formats a log entry into human readable text.

    Args:
      entry: A log entry message emitted from the V2 API client.

    Returns:
      A string without line breaks respecting the `max_length` property.
    """
    text = self._LogEntryToText(entry)
    text = text.strip().replace('\n', '  ')

    try:
      time = times.FormatDateTime(times.ParseDateTime(entry.timestamp),
                                  self.api_time_format)
    except times.Error:
      log.warning('Received timestamp [{0}] does not match expected'
                  ' format.'.format(entry.timestamp))
      time = '????-??-?? ??:??:??'

    out = '{timestamp} {log_text}'.format(
        timestamp=time,
        log_text=text)
    if self.max_length and len(out) > self.max_length:
      out = out[:self.max_length - 3] + '...'
    return out
Ejemplo n.º 13
0
    def Run(self, args):
        cert_ref = _ParseCertificateResource(args)

        reason = flags.ParseRevocationChoiceToEnum(args.reason)

        client = privateca_base.GetClientInstance()
        messages = privateca_base.GetMessagesModule()

        operation = client.projects_locations_certificateAuthorities_certificates.Revoke(
            messages.
            PrivatecaProjectsLocationsCertificateAuthoritiesCertificatesRevokeRequest(
                name=cert_ref.RelativeName(),
                revokeCertificateRequest=messages.RevokeCertificateRequest(
                    reason=reason,
                    requestId=request_utils.GenerateRequestId())))

        response = operations.Await(operation, 'Revoking Certificate.')
        certificate = operations.GetMessageFromResponse(
            response, messages.Certificate)

        log.status.Print('Publishing a new Certificate Revocation List.')
        client.projects_locations_certificateAuthorities.PublishCrl(
            messages.
            PrivatecaProjectsLocationsCertificateAuthoritiesPublishCrlRequest(
                name=cert_ref.Parent().RelativeName(),
                publishCertificateRevocationListRequest=messages.
                PublishCertificateRevocationListRequest()))

        revoke_time = times.ParseDateTime(
            certificate.revocationDetails.revocationTime)
        log.status.Print('Revoked certificate [{}] at {}.'.format(
            certificate.name,
            times.FormatDateTime(revoke_time, tzinfo=times.LOCAL)))
Ejemplo n.º 14
0
 def Export(self, args):
     """Export assets with the asset export method."""
     content_type = ContentTypeTranslation(args.content_type)
     content_type = getattr(
         self.message_module.ExportAssetsRequest.ContentTypeValueValuesEnum,
         content_type)
     if args.output_path or args.output_path_prefix:
         output_config = self.message_module.OutputConfig(
             gcsDestination=self.message_module.GcsDestination(
                 uri=args.output_path, uriPrefix=args.output_path_prefix))
     else:
         source_ref = args.CONCEPTS.bigquery_table.Parse()
         output_config = self.message_module.OutputConfig(
             bigqueryDestination=self.message_module.BigQueryDestination(
                 dataset='projects/' + source_ref.projectId + '/datasets/' +
                 source_ref.datasetId,
                 table=source_ref.tableId,
                 force=args.force_))
     snapshot_time = None
     if args.snapshot_time:
         snapshot_time = times.FormatDateTime(args.snapshot_time)
     export_assets_request = self.message_module.ExportAssetsRequest(
         assetTypes=args.asset_types,
         contentType=content_type,
         outputConfig=output_config,
         readTime=snapshot_time)
     request_message = self.message_module.CloudassetExportAssetsRequest(
         parent=self.parent, exportAssetsRequest=export_assets_request)
     operation = self.service.ExportAssets(request_message)
     return operation
Ejemplo n.º 15
0
    def RewriteTerm(self, key, op, operand, key_type):
        """Rewrites <key op operand>.

    Args:
      key: The dotted resource name.
      op: The operator name.
      operand: The operand string value.
      key_type: The type of key, None if not known.

    Returns:
      A rewritten expression node or None if not supported server side.
    """
        # TODO(b/77934881) compute API labels filter workaround
        if key.split('.')[0] == 'labels':
            # server side labels matching is currently problematic
            return None

        if isinstance(operand, list):
            # foo:(bar,baz) needs OR
            return None

        # Determine if the operand is matchable or a literal string.
        if not key_type:
            key_type = _GuessOperandType(operand)
        matchable = key_type is six.text_type

        # Convert time stamps to ISO RFC 3339 normal form.
        if key.endswith('Timestamp') or key.endswith('_timestamp'):
            try:
                operand = times.FormatDateTime(times.ParseDateTime(operand))
            except (times.DateTimeSyntaxError, times.DateTimeValueError):
                pass
            else:
                matchable = False

        # Transform input field to List API field.
        if matchable and key.lower() in self._FIELD_MAPPING:
            key = self._FIELD_MAPPING[key]

        if operand.lower() in ('true', 'false'):
            operand = operand.lower()
        if op == ':':
            op = 'eq'
            if matchable:
                operand = ConvertHASPatternToFullMatch(operand)
        elif op in ('=', '!='):
            op = 'ne' if op.startswith('!') else 'eq'
            if matchable:
                operand = ConvertEQPatternToFullMatch(operand)
        elif op in ('~', '!~'):
            # All re match operands are strings.
            op = 'ne' if op.startswith('!') else 'eq'
            operand = ConvertREPatternToFullMatch(operand,
                                                  wordmatch=key
                                                  in ('region', 'zone'))
        else:
            return None

        return [key, ' ', op, ' ', operand]
Ejemplo n.º 16
0
def ParseExpireTime(expiration_value):
    """Parse flag value into Datetime format for expireTime."""
    # expiration_value could be in Datetime format or Duration format.
    datetime = times.ParseDateTime(expiration_value)
    parsed_datetime = times.FormatDateTime(datetime,
                                           '%Y-%m-%dT%H:%M:%S.%6f%Ez',
                                           times.UTC)
    return parsed_datetime
Ejemplo n.º 17
0
    def Run(self, args):
        """Run a command that watches a variable.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
          with.

    Returns:
      The WatchVariable response.

    Raises:
      HttpException: An http error response was received while executing api
          request.
    """
        # Disable retries and configure the timeout.
        variable_client = util.VariableClient(num_retries=0,
                                              timeout=args.max_wait)
        messages = util.Messages()

        var_resource = util.ParseVariableName(args.name, args)
        project = var_resource.projectsId
        config = var_resource.configsId
        name = var_resource.Name()

        if args.newer_than:
            newer_than = times.FormatDateTime(args.newer_than)
        else:
            newer_than = None

        with progress_tracker.ProgressTracker(
                'Waiting for variable [{0}] to change'.format(name)):
            try:
                return util.FormatVariable(
                    variable_client.Watch(
                        messages.
                        RuntimeconfigProjectsConfigsVariablesWatchRequest(
                            projectsId=project,
                            configsId=config,
                            variablesId=name,
                            watchVariableRequest=messages.WatchVariableRequest(
                                newerThan=newer_than, ))))

            except apitools_exceptions.HttpError as error:
                # For deadline exceeded or bad gateway errors,
                # we return a status code of 2.
                # In some cases, the GFE will timeout before the backend
                # responds with a 504 Gateway Timeout (DEADLINE_EXCEEDED).
                # In that scenario, GFE responds first with a 502 BAD GATEWAY error.
                if util.IsDeadlineExceededError(
                        error) or util.IsBadGatewayError(error):
                    _RaiseTimeout()
                raise

            except socket.error as error:
                if util.IsSocketTimeout(error):
                    _RaiseTimeout()
                raise
Ejemplo n.º 18
0
def _AddSSHKeyToMetadataMessage(message_classes,
                                user,
                                public_key,
                                metadata,
                                expiration=None,
                                legacy=False):
    """Adds the public key material to the metadata if it's not already there.

  Args:
    message_classes: An object containing API message classes.
    user: The username for the SSH key.
    public_key: The SSH public key to add to the metadata.
    metadata: The existing metadata.
    expiration: If provided, a datetime after which the key is no longer valid.
    legacy: If true, store the key in the legacy "sshKeys" metadata entry.

  Returns:
    An updated metadata API message.
  """
    if expiration is None:
        entry = '{user}:{public_key}'.format(
            user=user, public_key=public_key.ToEntry(include_comment=True))
    else:
        # The client only supports a specific format. See
        # https://github.com/GoogleCloudPlatform/compute-image-packages/blob/master/packages/python-google-compute-engine/google_compute_engine/accounts/accounts_daemon.py#L118
        expire_on = times.FormatDateTime(expiration, '%Y-%m-%dT%H:%M:%S+0000',
                                         times.UTC)
        entry = '{user}:{public_key} google-ssh {jsondict}'.format(
            user=user,
            public_key=public_key.ToEntry(include_comment=False),
            # The json blob has strict encoding requirements by some systems.
            # Order entries to meet requirements.
            # Any spaces produces a Pantheon Invalid Key Required Format error:
            # cs/java/com/google/developers/console/web/compute/angular/ssh_keys_editor_item.ng
            jsondict=json.dumps(
                collections.OrderedDict([('userName', user),
                                         ('expireOn', expire_on)
                                         ])).replace(' ', ''))

    ssh_keys, ssh_legacy_keys = _GetSSHKeysFromMetadata(metadata)
    all_ssh_keys = ssh_keys + ssh_legacy_keys
    log.debug('Current SSH keys in project: {0}'.format(all_ssh_keys))

    if entry in all_ssh_keys:
        return metadata

    if legacy:
        metadata_key = constants.SSH_KEYS_LEGACY_METADATA_KEY
        updated_ssh_keys = ssh_legacy_keys
    else:
        metadata_key = constants.SSH_KEYS_METADATA_KEY
        updated_ssh_keys = ssh_keys
    updated_ssh_keys.append(entry)
    return metadata_utils.ConstructMetadataMessage(
        message_classes=message_classes,
        metadata={metadata_key: _PrepareSSHKeysValue(updated_ssh_keys)},
        existing_metadata=metadata)
Ejemplo n.º 19
0
    def testChangedAfterNone(self):
        self._MockRequest(self.example_metrics,
                          start_time=times.FormatDateTime(
                              times.ParseDateTime('2100-01-01 00:00:00')))
        self.Run(
            'beta dataflow metrics list %s --changed-after="2100-01-01 00:00:00"'
            % self.job_id)
        self.AssertOutputEquals("""\
""")
Ejemplo n.º 20
0
  def testSuccessPgp(self):
    ascii_armored_key = textwrap.dedent("""
        -----BEGIN PGP PUBLIC KEY BLOCK-----
        aBcDeFg
        aBcDeFg
        aBcDeFg
        -----END PGP PUBLIC KEY BLOCK-----
    """)
    fname = self.Touch(directory=self.cwd_path, contents=ascii_armored_key)

    name = 'bar'
    proj = self.Project()
    new_pub_key = self.messages.AttestorPublicKey(
        asciiArmoredPgpPublicKey=ascii_armored_key,
        comment=None,
        id='0638AttestorDD940361EA2D7F14C58C124F0E663DA097')
    # Handle multiple API versions: GA has userOwnedGrafeasNote,
    # beta has has userOwnedDrydockNote (also below).
    try:
      attestor = self.messages.Attestor(
          name='projects/{}/attestors/{}'.format(proj, name),
          updateTime=None,
          userOwnedGrafeasNote=self.messages.UserOwnedGrafeasNote(
              noteReference='projects/{}/notes/{}'.format(proj, name),
              publicKeys=[],
          ))
    except AttributeError:
      attestor = self.messages.Attestor(
          name='projects/{}/attestors/{}'.format(proj, name),
          updateTime=None,
          userOwnedDrydockNote=self.messages.UserOwnedDrydockNote(
              noteReference='projects/{}/notes/{}'.format(proj, name),
              publicKeys=[],
          ))

    updated_attestor = copy.deepcopy(attestor)
    try:
      updated_attestor.userOwnedGrafeasNote.publicKeys.append(new_pub_key)
    except AttributeError:
      updated_attestor.userOwnedDrydockNote.publicKeys.append(new_pub_key)
    updated_attestor.updateTime = times.FormatDateTime(
        datetime.datetime.utcnow())

    req = self.messages.BinaryauthorizationProjectsAttestorsGetRequest(
        name=attestor.name)

    self.mock_client.projects_attestors.Get.Expect(req, response=attestor)
    self.mock_client.projects_attestors.Update.Expect(
        attestor, response=updated_attestor)

    response = self.RunBinauthz(
        'attestors public-keys add '
        '--attestor={name} --pgp-public-key-file={fname}'.format(
            name=name, fname=fname))

    self.assertEqual(response, new_pub_key)
Ejemplo n.º 21
0
def ParseExpireTime(expiration_value):
  """Parse flag value into Datetime format for expireTime."""
  # expiration_value could be in Datetime format or Duration format.
  # backend timezone is UTC.
  datetime = (times.ParseDuration(expiration_value)
              .GetRelativeDateTime(times.Now(times.UTC)))
  parsed_datetime = times.FormatDateTime(datetime,
                                         '%Y-%m-%dT%H:%M:%S.%6f%Ez',
                                         tzinfo=times.UTC)
  return parsed_datetime
Ejemplo n.º 22
0
def ParseCredential(path, type_str, expiration_time=None, messages=None):
    messages = messages or devices.GetMessagesModule()

    type_ = _ConvertStringToFormatEnum(type_str, messages)
    contents = _ReadKeyFileFromPath(path)
    if expiration_time:
        expiration_time = times.FormatDateTime(expiration_time)

    return messages.DeviceCredential(expirationTime=expiration_time,
                                     publicKey=messages.PublicKeyCredential(
                                         format=type_, key=contents))
Ejemplo n.º 23
0
    def testChangedAfterAll(self):
        self._MockRequest(self.example_metrics,
                          start_time=times.FormatDateTime(
                              times.ParseDateTime('2000-01-01 00:00:00')))
        self.Run(
            'beta dataflow metrics list %s --changed-after="2000-01-01 00:00:00"'
            % self.job_id)
        self.AssertOutputEquals("""\
---
name:
  context: {}
  name: s5-ByteCount
  origin: dataflow/v1b3
scalar: 130194921.0
updateTime: '2015-01-15 12:31:07'
---
name:
  context: {}
  name: s05-s5-finish-msecs
  origin: dataflow/v1b3
scalar: 0.0
updateTime: '2015-01-15 12:31:07'
---
name:
  context:
    output_user_name: some/transform/Read-out0
  name: ElementCount
  origin: dataflow/v1b3
scalar: 164656.0
updateTime: '2015-01-15 12:31:07'
---
name:
  context:
    output_user_name: some/transform/Write-out0
  name: MeanByteCount
  origin: dataflow/v1b3
scalar: 164656.0
updateTime: '2015-01-15 12:31:07'
---
name:
  context:
    output_user_name: BigQueryIO.Read3-out0
  name: ElementCount
  origin: dataflow/v1b3
scalar: 164656.0
updateTime: '2015-01-15 12:31:07'
---
name:
  context: {}
  name: BigQueryIO.Read3-out0-MeanByteCount
  origin: dataflow/v1b3
scalar: 77.0
updateTime: '2015-01-15 12:31:07'
""")
Ejemplo n.º 24
0
def MakeGetAssetsHistoryHttpRequests(args, api_version=DEFAULT_API_VERSION):
    """Manually make the get assets history request."""
    http_client = http.Http()
    query_params = [('assetNames', asset_name)
                    for asset_name in args.asset_names or []]
    query_params.extend([
        ('contentType', ContentTypeTranslation(args.content_type)),
        ('readTimeWindow.startTime', times.FormatDateTime(args.start_time))
    ])
    if args.IsSpecified('end_time'):
        query_params.extend([('readTimeWindow.endTime',
                              times.FormatDateTime(args.end_time))])
    parent = asset_utils.GetParentNameForGetHistory(args.organization,
                                                    args.project)
    endpoint = apis.GetEffectiveApiEndpoint(API_NAME, api_version)
    url = '{0}{1}/{2}:{3}'.format(endpoint, api_version, parent,
                                  'batchGetAssetsHistory')
    encoded_query_params = six.moves.urllib.parse.urlencode(query_params)
    response, raw_content = http_client.request(uri=url,
                                                headers=_HEADERS,
                                                method='POST',
                                                body=encoded_query_params)

    content = core_encoding.Decode(raw_content)

    if int(response['status']) != httplib.OK:
        http_error = api_exceptions.HttpError(response, content, url)
        raise exceptions.HttpException(http_error)

    response_message_class = GetMessages(
        api_version).BatchGetAssetsHistoryResponse
    try:
        history_response = encoding.JsonToMessage(response_message_class,
                                                  content)
    except ValueError as e:
        err_msg = ('Failed receiving proper response from server, cannot'
                   'parse received assets. Error details: ' + six.text_type(e))
        raise MessageDecodeError(err_msg)

    for asset in history_response.assets:
        yield asset
Ejemplo n.º 25
0
  def testListTagsOutput(self):
    self._manifests = {
        _MakeSha('sha1'): {
            'tag': ['tag1'],
            'timeCreatedMs': _TIME_CREATED_MS
        },
        _MakeSha('sha2'): {
            'tag': ['tag2', 'tag3'],
            'timeCreatedMs': _NEWER_TIME_CREATED_MS
        },
        _MakeSha('sha3'): {
            'tag': ['tag4'],
            'timeCreatedMs': _UNDERFLOW_CREATED_MS
        }
    }

    self.ListTags()
    expected_time = times.FormatDateTime(
        times.GetDateTimeFromTimeStamp(_TIME_CREATED), '%Y-%m-%dT%H:%M:%S')
    newer_expected_time = times.FormatDateTime(
        times.GetDateTimeFromTimeStamp(
            _NEWER_TIME_CREATED), '%Y-%m-%dT%H:%M:%S')

    self.AssertOutputContains('DIGEST TAGS TIMESTAMP', normalize_space=True)
    self.AssertOutputContains(
        '{digest} {tags} {timestamp}'.format(
            digest='sha1', tags='tag1', timestamp=expected_time),
        normalize_space=True)
    self.AssertOutputContains(
        '{digest} {tags} {timestamp}'.format(
            digest='sha2', tags='tag2,tag3', timestamp=newer_expected_time),
        normalize_space=True)
    self.AssertOutputContains(
        '{digest} {tags}'.format(digest='sha3', tags='tag4'),
        normalize_space=True)

    # Verify descending order of the timestamps.
    self.assertLess(
        self.GetOutput().index('sha2'), self.GetOutput().index('sha1'))
    self.assertLess(
        self.GetOutput().index('sha1'), self.GetOutput().index('sha3'))
Ejemplo n.º 26
0
 def Export(self, args):
     """Export assets with the asset export method."""
     content_type = ContentTypeTranslation(args.content_type)
     partition_key = PartitionKeyTranslation(args.partition_key)
     partition_key = getattr(
         self.message_module.PartitionSpec.PartitionKeyValueValuesEnum,
         partition_key)
     if args.output_path or args.output_path_prefix:
         output_config = self.message_module.OutputConfig(
             gcsDestination=self.message_module.GcsDestination(
                 uri=args.output_path, uriPrefix=args.output_path_prefix))
     else:
         source_ref = args.CONCEPTS.bigquery_table.Parse()
         output_config = self.message_module.OutputConfig(
             bigqueryDestination=self.message_module.BigQueryDestination(
                 dataset='projects/' + source_ref.projectId + '/datasets/' +
                 source_ref.datasetId,
                 table=source_ref.tableId,
                 force=args.force_,
                 partitionSpec=self.message_module.PartitionSpec(
                     partitionKey=partition_key),
                 separateTablesPerAssetType=args.per_type_))
     snapshot_time = None
     if args.snapshot_time:
         snapshot_time = times.FormatDateTime(args.snapshot_time)
     if (self.api_version == V1P7BETA1_API_VERSION
             and content_type == 'RELATIONSHIP'):
         content_type = getattr(
             self.message_module.ExportAssetsRequest.
             ContentTypeValueValuesEnum, content_type)
         export_assets_request = self.message_module.ExportAssetsRequest(
             assetTypes=args.asset_types,
             contentType=content_type,
             outputConfig=output_config,
             readTime=snapshot_time,
             relationshipTypes=args.relationship_types)
     elif content_type == 'RELATIONSHIP':
         raise gcloud_exceptions.InvalidArgumentException(
             'Export relationship'
             ' not supported')
     else:
         content_type = getattr(
             self.message_module.ExportAssetsRequest.
             ContentTypeValueValuesEnum, content_type)
         export_assets_request = self.message_module.ExportAssetsRequest(
             assetTypes=args.asset_types,
             contentType=content_type,
             outputConfig=output_config,
             readTime=snapshot_time)
     request_message = self.message_module.CloudassetExportAssetsRequest(
         parent=self.parent, exportAssetsRequest=export_assets_request)
     operation = self.service.ExportAssets(request_message)
     return operation
Ejemplo n.º 27
0
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: all the arguments that were provided to this command invocation.

    Returns:
      None on success, or a string containing the error message.
    """
        job_ref = job_utils.ExtractJobRef(args)

        importance_enum = (
            apis.Messages.LIST_REQUEST.MinimumImportanceValueValuesEnum)
        importance_map = {
            'debug': importance_enum.JOB_MESSAGE_DEBUG,
            'detailed': importance_enum.JOB_MESSAGE_DETAILED,
            'error': importance_enum.JOB_MESSAGE_ERROR,
            'warning': importance_enum.JOB_MESSAGE_WARNING,
        }

        request = apis.Messages.LIST_REQUEST(
            projectId=job_ref.projectId,
            jobId=job_ref.jobId,
            location=job_ref.location,
            minimumImportance=(args.importance
                               and importance_map[args.importance]),

            # Note: It if both are present, startTime > endTime, because we will
            # return messages with actual time [endTime, startTime).
            startTime=args.after and times.FormatDateTime(args.after),
            endTime=args.before and times.FormatDateTime(args.before))

        return dataflow_util.YieldFromList(job_id=job_ref.jobId,
                                           project_id=job_ref.projectId,
                                           region_id=job_ref.location,
                                           service=apis.Messages.GetService(),
                                           request=request,
                                           batch_size=args.limit,
                                           batch_size_attribute='pageSize',
                                           field='jobMessages')
Ejemplo n.º 28
0
 def SetUp(self):
     self.name = 'bar'
     proj = self.Project()
     self.note_ref = 'projects/{}/notes/{}'.format(proj, self.name)
     try:
         self.attestor = self.messages.Attestor(
             name='projects/{}/attestors/{}'.format(proj, self.name),
             description='foo',
             updateTime=times.FormatDateTime(datetime.datetime.utcnow()),
             userOwnedGrafeasNote=self.messages.UserOwnedGrafeasNote(
                 noteReference=self.note_ref,
                 publicKeys=[],
             ))
     except AttributeError:
         self.attestor = self.messages.Attestor(
             name='projects/{}/attestors/{}'.format(proj, self.name),
             description='foo',
             updateTime=times.FormatDateTime(datetime.datetime.utcnow()),
             userOwnedDrydockNote=self.messages.UserOwnedDrydockNote(
                 noteReference=self.note_ref,
                 publicKeys=[],
             ))
Ejemplo n.º 29
0
def GenerateCertId():
    """Generate a certificate id with the date and two length 3 alphanum strings.

  E.G. YYYYMMDD-ABC-DEF.

  Returns:
    The generated certificate id string.
  """
    alphanum = string.ascii_uppercase + string.digits
    alphanum_rand1 = ''.join(random.choice(alphanum) for i in range(3))
    alphanum_rand2 = ''.join(random.choice(alphanum) for i in range(3))
    date_str = times.FormatDateTime(times.Now(), '%Y%m%d')
    return '{}-{}-{}'.format(date_str, alphanum_rand1, alphanum_rand2)
Ejemplo n.º 30
0
 def _RewriteTimes(self, key, op, operand):
     """Rewrites <*Time op operand>."""
     try:
         dt = times.ParseDateTime(operand)
     except ValueError as e:
         raise ValueError(
             '{operand}: date-time value expected for {key}: {error}'.
             format(operand=operand, key=key, error=str(e)))
     dt_string = times.FormatDateTime(dt, '%Y-%m-%dT%H:%M:%S.%3f%Ez',
                                      times.UTC)
     return '{key}{op}{dt_string}'.format(key=key,
                                          op=op,
                                          dt_string=self.Quote(dt_string,
                                                               always=True))