def _call(self, payload):
        request_without_write_properties = prune_properties(
            payload["requestData"]["resourceProperties"],
            self.write_only_paths)

        previous_request_without_write_properties = None
        if payload["requestData"]["previousResourceProperties"]:
            previous_request_without_write_properties = prune_properties(
                payload["requestData"]["previousResourceProperties"],
                self.write_only_paths,
            )
        payload_to_log = {
            "callbackContext": payload["callbackContext"],
            "action": payload["action"],
            "requestData": {
                "resourceProperties": request_without_write_properties,
                "previousResourceProperties":
                previous_request_without_write_properties,
                "logicalResourceId":
                payload["requestData"]["logicalResourceId"],
            },
            "region": payload["region"],
            "awsAccountId": payload["awsAccountId"],
            "bearerToken": payload["bearerToken"],
        }
        LOG.debug(
            "Sending request\n%s",
            json.dumps(payload_to_log, ensure_ascii=False, indent=2),
        )
        payload = json.dumps(payload, ensure_ascii=False, indent=2)
        if self._docker_image:
            if not self._executable_entrypoint:
                raise InvalidProjectError(
                    "executableEntrypoint not set in .rpdk-config. "
                    "Have you run cfn generate?")
            result = (self._docker_client.containers.run(
                self._docker_image,
                self._executable_entrypoint + " '" + payload + "'",
            ).decode().strip())
            LOG.debug("=== Handler execution logs ===")
            LOG.debug(result)
            # pylint: disable=W1401
            regex = "__CFN_RESOURCE_START_RESPONSE__([\s\S]*)__CFN_RESOURCE_END_RESPONSE__"  # noqa: W605 # pylint: disable=C0301
            payload = json.loads(re.search(regex, result).group(1))
        else:
            result = self._client.invoke(FunctionName=self._function_name,
                                         Payload=payload.encode("utf-8"))

            try:
                payload = json.load(result["Payload"])
            except json.decoder.JSONDecodeError as json_error:
                LOG.debug("Received invalid response\n%s", result["Payload"])
                raise ValueError("Handler Output is not a valid JSON document"
                                 ) from json_error
        LOG.debug("Received response\n%s", payload)
        return payload
 def get_hook_configuration():
     type_configuration = TypeConfiguration.get_type_configuration()
     if type_configuration:
         try:
             return type_configuration.get(
                 "CloudFormationConfiguration",
                 {})["HookConfiguration"]["Properties"]
         except KeyError as e:
             LOG.warning("Hook configuration is invalid")
             raise InvalidProjectError(
                 "Hook configuration is invalid") from e
     return type_configuration
Ejemplo n.º 3
0
    def _call(self, payload):
        payload_to_log = {
            "hookTypeName": payload["hookTypeName"],
            "actionInvocationPoint": payload["actionInvocationPoint"],
            "requestData": {
                "targetName": payload["requestData"]["targetName"],
                "targetLogicalId": payload["requestData"]["targetLogicalId"],
                "targetModel": payload["requestData"]["targetModel"],
            },
            "awsAccountId": payload["awsAccountId"],
            "clientRequestToken": payload["clientRequestToken"],
        }

        LOG.debug(
            "Sending request\n%s",
            json.dumps(payload_to_log, ensure_ascii=False, indent=2),
        )
        payload = json.dumps(payload, ensure_ascii=False, indent=2)
        if self._docker_image:
            if not self._executable_entrypoint:
                raise InvalidProjectError(
                    "executableEntrypoint not set in .rpdk-config. "
                    "Have you run cfn generate?")
            result = (self._docker_client.containers.run(
                self._docker_image,
                self._executable_entrypoint + " '" + payload + "'",
                environment={
                    "AWS_REGION": self.region
                },
            ).decode().strip())
            LOG.debug("=== Handler execution logs ===")
            LOG.debug(result)
            # pylint: disable=W1401
            regex = "__CFN_HOOK_START_RESPONSE__([\s\S]*)__CFN_HOOK_END_RESPONSE__"  # noqa: W605,B950 # pylint: disable=C0301
            payload = json.loads(re.search(regex, result).group(1))
        else:
            result = self._client.invoke(FunctionName=self._function_name,
                                         Payload=payload.encode("utf-8"))

            try:
                payload = json.load(result["Payload"])
            except json.decoder.JSONDecodeError as json_error:
                LOG.debug("Received invalid response\n%s", result["Payload"])
                raise ValueError("Handler Output is not a valid JSON document"
                                 ) from json_error

        LOG.debug("Received response\n%s", json.dumps(payload, indent=2))
        return payload
 def get_type_configuration():
     LOG.debug(
         "Loading type configuration setting file at '~/.cfn-cli/typeConfiguration.json'"
     )
     if TypeConfiguration.TYPE_CONFIGURATION is None:
         try:
             with open(os.path.expanduser(TYPE_CONFIGURATION_FILE_PATH),
                       encoding="utf-8") as f:
                 TypeConfiguration.TYPE_CONFIGURATION = json.load(f)
         except json.JSONDecodeError as json_decode_error:
             LOG.debug(
                 "Type configuration file '%s' is invalid",
                 TYPE_CONFIGURATION_FILE_PATH,
             )
             raise InvalidProjectError(
                 "Type configuration file '%s' is invalid" %
                 TYPE_CONFIGURATION_FILE_PATH) from json_decode_error
         except FileNotFoundError:
             LOG.debug(
                 "Type configuration file '%s' not Found, do nothing",
                 TYPE_CONFIGURATION_FILE_PATH,
             )
     return TypeConfiguration.TYPE_CONFIGURATION