Ejemplo n.º 1
0
    def Run(self, args):
        """Issues the request to add Signed URL key to the backend bucket."""
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        api_client = holder.client.apitools_client
        messages = holder.client.messages
        service = api_client.backendServices

        backend_service_ref = flags.GLOBAL_BACKEND_SERVICE_ARG.ResolveAsResource(
            args,
            holder.resources,
            scope_lister=compute_flags.GetDefaultScopeLister(holder.client))
        key_value = file_utils.ReadFile(args.key_file, 'key').rstrip()
        request = messages.ComputeBackendServicesAddSignedUrlKeyRequest(
            project=backend_service_ref.project,
            backendService=backend_service_ref.Name(),
            signedUrlKey=messages.SignedUrlKey(keyName=args.key_name,
                                               keyValue=key_value))

        operation = service.AddSignedUrlKey(request)
        operation_ref = holder.resources.Parse(
            operation.selfLink, collection='compute.globalOperations')

        operation_poller = poller.Poller(service)
        return waiter.WaitFor(
            operation_poller, operation_ref,
            'Adding Cloud CDN Signed URL key to [{0}]'.format(
                backend_service_ref.Name()))
Ejemplo n.º 2
0
  def CreateRequests(self, args):
    """Returns the request necessary for adding the SSL certificate."""

    ssl_certificate_ref = self.SSL_CERTIFICATE_ARG.ResolveAsResource(
        args, self.resources)
    certificate = file_utils.ReadFile(args.certificate, 'certificate')
    private_key = file_utils.ReadFile(args.private_key, 'private key')

    request = self.messages.ComputeSslCertificatesInsertRequest(
        sslCertificate=self.messages.SslCertificate(
            name=ssl_certificate_ref.Name(),
            certificate=certificate,
            privateKey=private_key,
            description=args.description),
        project=self.project)

    return [request]
Ejemplo n.º 3
0
def _GetContainerManifest(name, container_manifest, docker_image,
                          port_mappings, run_command, run_as_privileged):
    """Loads container manifest from file or creates a new one."""
    if container_manifest:
        return file_utils.ReadFile(container_manifest, 'container manifest')
    else:
        return CreateContainerManifest(name, docker_image, port_mappings,
                                       run_command, run_as_privileged)
Ejemplo n.º 4
0
  def CreateRequests(self, args):
    """Returns the request necessary for adding the SSL certificate."""

    ssl_certificate_ref = self.CreateGlobalReference(
        args.name, resource_type='sslCertificates')
    certificate = file_utils.ReadFile(args.certificate, 'certificate')
    private_key = file_utils.ReadFile(args.private_key, 'private key')

    request = self.messages.ComputeSslCertificatesInsertRequest(
        sslCertificate=self.messages.SslCertificate(
            name=ssl_certificate_ref.Name(),
            certificate=certificate,
            privateKey=private_key,
            description=args.description),
        project=self.project)

    return [request]
Ejemplo n.º 5
0
    def Run(self, args):
        holder = base_classes.ComputeUserAccountsApiHolder(self.ReleaseTrack())
        client = holder.client

        name = args.name
        if not name:
            name = gaia.GetDefaultAccountName(client.http)

        user_ref = holder.resources.Parse(
            name,
            params={'project': properties.VALUES.core.project.GetOrFail},
            collection='clouduseraccounts.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_util.CalculateExpiration(args.expire)

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

            request = (client.MESSAGES_MODULE.
                       ClouduseraccountsUsersAddPublicKeyRequest(
                           project=user_ref.project,
                           publicKey=public_key_message,
                           user=user_ref.Name()))
            requests.append((client.users, 'AddPublicKey', request))

        errors = []
        responses = list(
            request_helper.MakeRequests(
                requests=requests,
                http=client.http,
                batch_url='https://www.googleapis.com/batch/',
                errors=errors))
        if errors:
            utils.RaiseToolException(errors,
                                     error_message='Could not fetch resource:')
        return responses
Ejemplo n.º 6
0
    def Run(self, args):
        """Issues the request necessary for adding the SSL certificate."""
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client

        ssl_certificate_ref = self.SSL_CERTIFICATE_ARG.ResolveAsResource(
            args, holder.resources)
        certificate = file_utils.ReadFile(args.certificate, 'certificate')
        private_key = file_utils.ReadFile(args.private_key, 'private key')

        request = client.messages.ComputeSslCertificatesInsertRequest(
            sslCertificate=client.messages.SslCertificate(
                name=ssl_certificate_ref.Name(),
                certificate=certificate,
                privateKey=private_key,
                description=args.description),
            project=ssl_certificate_ref.project)

        return client.MakeRequests([(client.apitools_client.sslCertificates,
                                     'Insert', request)])
Ejemplo n.º 7
0
def ConstructMetadataMessage(message_classes,
                             metadata=None,
                             metadata_from_file=None,
                             existing_metadata=None):
  """Creates a Metadata message from the given dicts of metadata.

  Args:
    message_classes: An object containing API message classes.
    metadata: A dict mapping metadata keys to metadata values or None.
    metadata_from_file: A dict mapping metadata keys to file names
      containing the keys' values or None.
    existing_metadata: If not None, the given metadata values are
      combined with this Metadata message.

  Raises:
    ToolException: If metadata and metadata_from_file contain duplicate
      keys or if there is a problem reading the contents of a file in
      metadata_from_file.

  Returns:
    A Metadata protobuf.
  """
  metadata = metadata or {}
  metadata_from_file = metadata_from_file or {}

  new_metadata_dict = copy.deepcopy(metadata)
  for key, file_path in six.iteritems(metadata_from_file):
    if key in new_metadata_dict:
      raise exceptions.ToolException(
          'Encountered duplicate metadata key [{0}].'.format(key))

    new_metadata_dict[key] = file_utils.ReadFile(
        file_path, 'metadata key [{0}]'.format(key))

  existing_metadata_dict = _MetadataMessageToDict(existing_metadata)
  existing_metadata_dict.update(new_metadata_dict)
  try:
    _ValidateSshKeys(existing_metadata_dict)
  except InvalidSshKeyException as e:
    log.warning(e)

  new_metadata_message = _DictToMetadataMessage(message_classes,
                                                existing_metadata_dict)

  if existing_metadata:
    new_metadata_message.fingerprint = existing_metadata.fingerprint

  return new_metadata_message
Ejemplo n.º 8
0
    def CreateRequests(self, args):

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

        user_ref = self.clouduseraccounts_resources.Parse(
            name,
            params={'project': properties.VALUES.core.project.GetOrFail},
            collection='clouduseraccounts.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_util.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.ClouduseraccountsUsersAddPublicKeyRequest(
                project=self.project,
                publicKey=public_key_message,
                user=user_ref.Name())
            requests.append(request)

        return requests
Ejemplo n.º 9
0
  def Run(self, args):
    """Signs the specified URL and optionally also validates it.

    Args:
      args: The arguments passed to this command.

    Returns:
      Returns a dictionary. The 'signedUrl' key in the dictionary maps to the
      Signed URL. The 'validationResponseCode' key in the dictionary maps to
      the response code obtained for the HEAD request issued to the Signed URL.
    """
    key_value = file_utils.ReadFile(args.key_file, 'key').rstrip()
    result = {}
    result['signedUrl'] = sign_url_utils.SignUrl(args.url, args.key_name,
                                                 key_value, args.expires_in)

    if args.validate:
      result['validationResponseCode'] = sign_url_utils.ValidateSignedUrl(
          result['signedUrl'])

    return result