Example #1
0
def _ParseHeaderArg(args, messages):
    if args.header:
        headers_dict = {
            http_encoding.Encode(k): http_encoding.Encode(v)
            for k, v in map(_SplitHeaderArgValue, args.header)
        }
        return encoding.DictToAdditionalPropertyMessage(
            headers_dict, messages.AppEngineHttpRequest.HeadersValue)
Example #2
0
    def testUpdate_EmptyData(self):
        self._ExpectModifyConfig(http_encoding.Encode(''))

        result = self.Run('iot devices configs update '
                          '    --device my-device '
                          '    --registry my-registry '
                          '    --region us-central1 '
                          '    --config-data ""')

        expected_config = self.messages.DeviceConfig(
            binaryData=http_encoding.Encode(''), version=1)
        self.assertEqual(result, expected_config)
Example #3
0
    def testUpdate(self):
        self._ExpectModifyConfig(http_encoding.Encode('abcd'))

        result = self.Run('iot devices configs update '
                          '    --device my-device '
                          '    --registry my-registry '
                          '    --region us-central1 '
                          '    --config-data abcd')

        expected_config = self.messages.DeviceConfig(
            binaryData=http_encoding.Encode('abcd'), version=1)
        self.assertEqual(result, expected_config)
        self.AssertLogContains('Updated configuration for device [my-device].')
Example #4
0
    def testUpdate_RelativeName(self):
        self._ExpectModifyConfig(http_encoding.Encode('abcd'))

        device_name = ('projects/{}/'
                       'locations/us-central1/'
                       'registries/my-registry/'
                       'devices/my-device').format(self.Project())
        result = self.Run('iot devices configs update '
                          '    --config-data abcd'
                          '    --device {} '.format(device_name))

        expected_config = self.messages.DeviceConfig(
            binaryData=http_encoding.Encode('abcd'), version=1)
        self.assertEqual(result, expected_config)
Example #5
0
def ParseMessageBody(message_body):
    """Parse "--message-body" flag as an argparse type.

  The flag is given as a string:

      --message-body 'some data'

  Args:
    message_body: str, the value of the --message-body flag.

  Returns:
    AdditionalProperty, a cloudscheduler additional property object with
        'data' as a key, and a JSON object (with a base64-encoded string value)
        as the value.
  """
    pubsub_messages = _GetPubsubMessages()
    scheduler_messages = _GetSchedulerMessages()

    # First, put into a PubsubMessage to make sure we've got the general format
    # right.
    pubsub_message = pubsub_messages.PubsubMessage(
        data=http_encoding.Encode(message_body))

    pubsub_message_type = scheduler_messages.PubsubTarget.PubsubMessageValue
    encoded_data = base64.urlsafe_b64encode(pubsub_message.data)
    return pubsub_message_type.AdditionalProperty(
        key='data', value=extra_types.JsonValue(string_value=encoded_data))
Example #6
0
    def Publish(self,
                topic_resource,
                message=None,
                ordering_key=None,
                attributes=None):
        """Publishes a message to the specified Pub/Sub Lite topic.

    Args:
      topic_resource: The pubsub.lite_topic resource to publish to.
      message: The string message to publish.
      ordering_key: The key for ordering delivery to subscribers.
      attributes: A dict of attributes to attach to the message.

    Raises:
      EmptyMessageException: if the message is empty.
      PublishOperationException: if the publish operation is not successful.

    Returns:
      The messageId of the published message, containing the Partition and
      Offset.
    """
        if not message and not attributes:
            raise topics.EmptyMessageException(
                'You cannot send an empty message. You must specify either a '
                'MESSAGE, one or more ATTRIBUTE, or both.')
        topic_path = self._TopicResourceToPath(topic_resource)
        try:
            return self._client.publish(topic_path,
                                        http_encoding.Encode(message),
                                        ordering_key, **(attributes
                                                         or {})).result()
        except Exception as e:
            raise topics.PublishOperationException(
                'Publish operation failed with error: {error}'.format(error=e))
Example #7
0
 def __init__(self, headers, payload, omit_fields):
     self._headers = headers
     if yaml.dict_like(payload):
         payload = json.dumps(payload)
     payload = payload or ''
     self._payload = http_encoding.Encode(payload)
     self._omit_fields = omit_fields
Example #8
0
def ModifyCreatePubsubJobRequest(job_ref, args, create_job_req):
  """Add the pubsubMessage field to the given request.

  Because the Cloud Scheduler API has a reference to a PubSub message, but
  represents it as a bag of properties, we need to construct the object here and
  insert it into the request.

  Args:
    job_ref: Resource reference to the job to be created (unused)
    args: argparse namespace with the parsed arguments from the command line. In
        particular, we expect args.message_body and args.attributes (optional)
        to be AdditionalProperty types.
    create_job_req: CloudschedulerProjectsLocationsJobsCreateRequest, the
        request constructed from the remaining arguments.

  Returns:
    CloudschedulerProjectsLocationsJobsCreateRequest: the given request but with
        the job.pubsubTarget.pubsubMessage field populated.
  """
  ModifyCreateJobRequest(job_ref, args, create_job_req)
  create_job_req.job.pubsubTarget.data = http_encoding.Encode(
      args.message_body or args.message_body_from_file)
  if args.attributes:
    create_job_req.job.pubsubTarget.attributes = args.attributes
  return create_job_req
Example #9
0
def ParseMessageBody(message_body):
  """Parse "--message-body" flag as an argparse type.

  The flag is given as a string:

      --message-body 'some data'

  Args:
    message_body: str, the value of the --message-body flag.

  Returns:
    AdditionalProperty, a cloudscheduler additional property object with
        'data' as a key, and a JSON object (with a base64-encoded string value)
        as the value.
  """
  pubsub_messages = _GetPubsubMessages()
  scheduler_messages = _GetSchedulerMessages()

  # First, put into a PubsubMessage to make sure we've got the general format
  # right.
  pubsub_message = pubsub_messages.PubsubMessage(
      data=http_encoding.Encode(message_body))

  pubsub_message_type = scheduler_messages.PubsubTarget.PubsubMessageValue
  encoded_data = base64.urlsafe_b64encode(pubsub_message.data)
  # Apitools will convert these messages to JSON values before issuing the
  # request. Since extra_types is used here, apitools does not handle converting
  # string_value to unicode before converting to JSON using json.dumps. That
  # means we have to convert to unicode here before the request goes into
  # apitools. Since the data is base64 encoded (which is all ascii), encoding it
  # here will not change it's value.
  encoded_data = http_encoding.Decode(encoded_data)
  return pubsub_message_type.AdditionalProperty(
      key='data',
      value=extra_types.JsonValue(string_value=encoded_data))
Example #10
0
def _ParsePayloadArgs(args):
  if args.IsSpecified('payload_file'):
    payload = console_io.ReadFromFileOrStdin(args.payload_file, binary=False)
  elif args.IsSpecified('payload_content'):
    payload = args.payload_content
  else:
    return None
  return http_encoding.Encode(payload)
Example #11
0
 def ResponseToTransportResponse(self, response):
   """Converts a Response object to the response returned by the transport."""
   resp = requests.Response()
   resp.status_code = response.status
   resp.headers = response.headers
   # pylint: disable=protected-access
   resp._content = http_encoding.Encode(response.body)
   return resp
Example #12
0
def _ParseBodyArgs(args):
    if args.IsSpecified('body_file'):
        body = console_io.ReadFromFileOrStdin(args.body_file, binary=False)
    elif args.IsSpecified('body_content'):
        body = args.body_content
    else:
        return None
    return http_encoding.Encode(body)
Example #13
0
def DecodeFingerprint(fingerprint):
    """Returns the base64 url decoded fingerprint."""
    try:
        decoded_fingerprint = base64.urlsafe_b64decode(
            http_encoding.Encode(fingerprint))
    except (TypeError, binascii.Error):
        raise calliope_exceptions.InvalidArgumentException(
            '--fingerprint', 'fingerprint cannot be decoded.')
    return decoded_fingerprint
Example #14
0
 def ObjectHook(value):
   value_datetime = value.get('_datetime')
   if value_datetime is not None:
     return datetime.datetime.strptime(value_datetime, _BOTOCORE_DATE_FORMAT)
   value_streamingbody = value.get('_streamingbody')
   if value_streamingbody is not None:
     body = six.BytesIO(http_encoding.Encode(value_streamingbody))
     return botocore.response.StreamingBody(body, len(value_streamingbody))
   return value
Example #15
0
def _EncodeMessageBody(message_body):
  """HTTP encodes the given message body.

  Args:
    message_body: the message body to be encoded

  Returns:
    String containing HTTP encoded message body.
  """
  message_body_str = encoding.Decode(message_body, encoding='utf-8')
  return http_encoding.Encode(message_body_str)
 def _MakePolicy(self):
     return self.messages.Policy(
         bindings=[
             self.messages.Binding(
                 role='roles/resourcemanager.projectCreator',
                 members=['domain:foo.com']),
             self.messages.Binding(
                 role='roles/resourcemanager.organizationAdmin',
                 members=['user:[email protected]'])
         ],
         etag=http_encoding.Encode('someUniqueEtag'),
         version=iam_util.MAX_LIBRARY_IAM_SUPPORTED_VERSION)
Example #17
0
 def testGetIamPolicyFolder(self):
   test_policy = self.messages.Policy(
       bindings=[
           self.messages.Binding(
               role='roles/resourcemanager.projectCreator',
               members=['domain:foo.com']), self.messages.Binding(
                   role='roles/resourcemanager.organizationAdmin',
                   members=['user:[email protected]'])
       ],
       etag=http_encoding.Encode('someUniqueEtag'),
       version=1)
   self.mock_folders.GetIamPolicy.Expect(self.ExpectedRequest(), test_policy)
   self.assertEqual(self.DoRequest(), test_policy)
Example #18
0
def _EncodeMessageBody(message_body):
    """HTTP encodes the given message body.

  Args:
    message_body: the message body to be encoded

  Returns:
    String containing HTTP encoded message body.
  """
    message_body_str = message_body
    if not isinstance(message_body, six.string_types):
        message_body_str = str(message_body, 'utf8')
    return http_encoding.Encode(message_body_str)
Example #19
0
    def _Update(self, args, var_resource, value):
        variable_client = util.VariableClient()
        messages = util.Messages()

        result = variable_client.Update(
            messages.Variable(
                name=var_resource.RelativeName(),
                value=http_encoding.Encode(value)
                if not args.is_text else None,
                text=value if args.is_text else None,
            ))

        log.UpdatedResource(var_resource)
        return util.FormatVariable(result)
Example #20
0
def _Run(args, message_body, legacy_output=False):
    """Publishes a message to a topic."""
    client = topics.TopicsClient()

    attributes = util.ParseAttributes(args.attribute, messages=client.messages)
    topic_ref = args.CONCEPTS.topic.Parse()

    result = client.Publish(topic_ref, http_encoding.Encode(message_body),
                            attributes)

    if legacy_output:
        # We only allow to publish one message at a time, so do not return a
        # list of messageId.
        result = resource_projector.MakeSerializable(result)
        result['messageIds'] = result['messageIds'][0]
    return result
Example #21
0
 def testGetIamPolicy(self):
     test_iam_policy = self.messages.Policy(
         bindings=[
             self.messages.Binding(role='roles/billing.admin',
                                   members=['*****@*****.**']),
         ],
         etag=http_encoding.Encode('someUniqueEtag'),
     )
     self.mocked_billing.billingAccounts.GetIamPolicy.Expect(
         self.messages.CloudbillingBillingAccountsGetIamPolicyRequest(
             resource=base.BILLING_ACCOUNTS[0].name),
         test_iam_policy,
     )
     result = self.Run('billing accounts get-iam-policy ' +
                       base.BILLING_ACCOUNTS[0].name)
     self.assertEqual(result, test_iam_policy)
Example #22
0
    def _Create(self, args, var_resource, value):
        variable_client = util.VariableClient()
        messages = util.Messages()

        project = var_resource.projectsId
        config = var_resource.configsId

        result = variable_client.Create(
            messages.RuntimeconfigProjectsConfigsVariablesCreateRequest(
                parent=util.ConfigPath(project, config),
                variable=messages.Variable(
                    name=var_resource.RelativeName(),
                    value=http_encoding.Encode(value)
                    if not args.is_text else None,
                    text=value if args.is_text else None,
                )))

        log.CreatedResource(var_resource)
        return util.FormatVariable(result)
Example #23
0
 def testGetIamPolicyFolderListCommandFilter(self):
   test_policy = self.messages.Policy(
       bindings=[
           self.messages.Binding(
               role='roles/resourcemanager.projectCreator',
               members=['domain:foo.com']), self.messages.Binding(
                   role='roles/resourcemanager.organizationAdmin',
                   members=['user:[email protected]'])
       ],
       etag=http_encoding.Encode('someUniqueEtag'),
       version=1)
   self.mock_folders.GetIamPolicy.Expect(self.ExpectedRequest(), test_policy)
   args = [
       '--flatten=bindings[].members',
       '--filter=bindings.role:roles/resourcemanager.organizationAdmin',
       '--format=table[no-heading](bindings.members:sort=1)',
   ]
   self.DoRequest(args)
   self.AssertOutputEquals('user:[email protected]\n')
Example #24
0
  def MakeConfigFileMessage(self, file_contents, filename, file_type):
    """Constructs a ConfigFile message from a config file.

    Args:
      file_contents: The contents of the config file.
      filename: The full path to the config file.
      file_type: FileTypeValueValuesEnum describing the type of config file.

    Returns:
      The constructed ConfigFile message.
    """

    messages = services_util.GetMessagesModule()

    file_types = messages.ConfigFile.FileTypeValueValuesEnum
    if file_type != file_types.FILE_DESCRIPTOR_SET_PROTO:
      # File is human-readable text, not binary; needs to be encoded.
      file_contents = http_encoding.Encode(file_contents)
    return messages.ConfigFile(
        fileContents=file_contents,
        filePath=os.path.basename(filename),
        fileType=file_type,)
Example #25
0
    def __MakeApigatewayApiConfigFileMessage(self,
                                             file_contents,
                                             filename,
                                             is_binary=False):
        """Constructs a ConfigFile message from a config file.

    Args:
      file_contents: The contents of the config file.
      filename: The path to the config file.
      is_binary: If set to true, the file_contents won't be encoded.

    Returns:
      The constructed ApigatewayApiConfigFile message.
    """

        messages = apigateway_base.GetMessagesModule()
        if not is_binary:
            # File is human-readable text, not binary; needs to be encoded.
            file_contents = http_encoding.Encode(file_contents)
        return messages.ApigatewayApiConfigFile(
            contents=file_contents,
            path=os.path.basename(filename),
        )
Example #26
0
def ReadConfigData(args):
    """Read configuration data from the parsed arguments.

  See command_lib.iot.flags for the flag definitions.

  Args:
    args: a parsed argparse Namespace object containing config_data and
      config_file.

  Returns:
    str, the binary configuration data

  Raises:
    ValueError: unless exactly one of --config-data, --config-file given
  """
    if args.IsSpecified('config_data') and args.IsSpecified('config_file'):
        raise ValueError('Both --config-data and --config-file given.')
    if args.IsSpecified('config_data'):
        return http_encoding.Encode(args.config_data)
    elif args.IsSpecified('config_file'):
        return files.ReadBinaryFileContents(args.config_file)
    else:
        raise ValueError('Neither --config-data nor --config-file given.')
 def testSetIamPolicy(self):
     test_iam_policy = self.messages.Policy(
         bindings=[
             self.messages.Binding(role='roles/billing.admin',
                                   members=['*****@*****.**']),
         ],
         etag=http_encoding.Encode('someUniqueEtag'),
     )
     temp_file = self.Touch(
         self.temp_path, contents=encoding.MessageToJson(test_iam_policy))
     self.mocked_billing.billingAccounts.SetIamPolicy.Expect(
         self.messages.CloudbillingBillingAccountsSetIamPolicyRequest(
             resource=base.BILLING_ACCOUNTS[0].name,
             setIamPolicyRequest=self.messages.SetIamPolicyRequest(
                 policy=test_iam_policy,
                 updateMask='bindings,etag',
             ),
         ),
         test_iam_policy,
     )
     result = self.Run('billing accounts set-iam-policy {} {}'.format(
         base.BILLING_ACCOUNTS[0].name, temp_file))
     self.assertEqual(result, test_iam_policy)
Example #28
0
  def _GetTestIamPolicy(self, clear_fields=None):
    """Creates a test IAM policy.

    Args:
        clear_fields: list of policy fields to clear.
    Returns:
        IAM policy.
    """
    policy = self.messages.Policy(
        auditConfigs=[
            self.messages.AuditConfig(
                auditLogConfigs=[
                    self.messages.AuditLogConfig(
                        logType=self.messages.AuditLogConfig.
                        LogTypeValueValuesEnum.ADMIN_READ,),
                ],
                service='allServices',)
        ],
        bindings=[
            self.messages.Binding(
                role='roles/resourcemanager.projectCreator',
                members=['domain:foo.com'],),
            self.messages.Binding(
                role='roles/resourcemanager.organizationAdmin',
                members=['user:[email protected]'],),
        ],
        etag=http_encoding.Encode('someUniqueEtag'),
        version=iam_util.MAX_LIBRARY_IAM_SUPPORTED_VERSION,)

    if clear_fields is None:
      clear_fields = []

    for field in clear_fields:
      policy.reset(field)

    return policy
Example #29
0
 def ResponseToTransportResponse(self, response):
   """Converts a Response object to the response returned by the transport."""
   headers = response.headers.copy()
   headers['status'] = response.status
   return (httplib2.Response(headers), http_encoding.Encode(response.body))
Example #30
0
  def MakeRequest(url, command_path):
    """Gets the request object for the given URL.

    If the URL is for cloud storage and we get a 403, this will try to load the
    active credentials and use them to authenticate the download.

    Args:
      url: str, The URL to download.
      command_path: the command path to include in the User-Agent header if the
        URL is HTTP

    Raises:
      AuthenticationError: If this download requires authentication and there
        are no credentials or the credentials do not have access.

    Returns:
      urllib2.Request, The request.
    """
    headers = {
        b'Cache-Control': b'no-cache',
        b'User-Agent': http_encoding.Encode(
            http.MakeUserAgentString(command_path))
    }
    timeout = TIMEOUT_IN_SEC
    if command_path == UPDATE_MANAGER_COMMAND_PATH:
      timeout = UPDATE_MANAGER_TIMEOUT_IN_SEC
    try:
      if url.startswith(ComponentInstaller.GCS_BROWSER_DL_URL):
        url = url.replace(ComponentInstaller.GCS_BROWSER_DL_URL,
                          ComponentInstaller.GCS_API_DL_URL,
                          1)
      req = urllib.request.Request(url, headers=headers)
      return ComponentInstaller._RawRequest(req, timeout=timeout)
    except urllib.error.HTTPError as e:
      if e.code != 403 or not url.startswith(ComponentInstaller.GCS_API_DL_URL):
        raise e
      try:
        creds = store.Load()
        store.Refresh(creds)
        creds.apply(headers)
      except store.Error as e:
        # If we fail here, it is because there are no active credentials or the
        # credentials are bad.
        raise AuthenticationError(
            'This component requires valid credentials to install.', e)
      try:
        # Retry the download using the credentials.
        req = urllib.request.Request(url, headers=headers)
        return ComponentInstaller._RawRequest(req, timeout=timeout)
      except urllib.error.HTTPError as e:
        if e.code != 403:
          raise e
        # If we fail again with a 403, that means we used the credentials, but
        # they didn't have access to the resource.
        raise AuthenticationError("""\
Account [{account}] does not have permission to install this component.  Please
ensure that this account should have access or run:

  $ gcloud config set account `ACCOUNT`

to choose another account.""".format(
    account=properties.VALUES.core.account.Get()), e)