Example #1
0
    def test_can_resolve_ref(self):
        parameters = {"key": "value"}
        input = {"Ref": "key"}

        ref = RefAction()
        self.assertEqual(parameters["key"],
                         ref.resolve_parameter_refs(input, parameters))
Example #2
0
    def test_must_ignore_invalid_value(self):
        parameters = {"key": "value"}
        input = {"Ref": ["invalid value"]}
        expected = {"Ref": ["invalid value"]}

        ref = RefAction()
        self.assertEqual(expected,
                         ref.resolve_parameter_refs(input, parameters))
Example #3
0
    def test_unknown_ref(self):
        parameters = {"key": "value"}
        input = {"Ref": "someotherkey"}
        expected = {"Ref": "someotherkey"}

        ref = RefAction()
        self.assertEqual(expected,
                         ref.resolve_parameter_refs(input, parameters))
Example #4
0
    def test_return_value_if_cannot_handle(self, can_handle_mock):
        parameters = {"key": "value"}
        input = {"Ref": "key"}
        expected = {"Ref": "key"}

        ref = RefAction()
        can_handle_mock.return_value = False  # Simulate failure to handle the input. Result should be same as input
        self.assertEqual(expected,
                         ref.resolve_parameter_refs(input, parameters))
    def test_can_resolve_ref(self):
        parameters = {
            "key": "value"
        }
        input = {
            "Ref": "key"
        }

        ref = RefAction()
        self.assertEqual(parameters["key"], ref.resolve_parameter_refs(input, parameters))
Example #6
0
    def test_return_value_if_cannot_handle(self, can_handle_mock):
        input = {"Ref": "key"}
        expected = {"Ref": "key"}

        ref = RefAction()
        can_handle_mock.return_value = False  # Simulate failure to handle the input. Result should be same as input
        self.assertEqual(
            expected,
            ref.resolve_resource_id_refs(input,
                                         self.supported_resource_id_refs_mock))
    def test_return_value_if_cannot_handle(self, can_handle_mock):
        input = {
            "Ref": "key"
        }
        expected = {
            "Ref": "key"
        }

        ref = RefAction()
        can_handle_mock.return_value = False # Simulate failure to handle the input. Result should be same as input
        self.assertEqual(expected, ref.resolve_resource_id_refs(input, self.supported_resource_id_refs_mock))
    def test_must_ignore_invalid_value(self):
        parameters = {
            "key": "value"
        }
        input = {
            "Ref": ["invalid value"]
        }
        expected = {
            "Ref": ["invalid value"]
        }

        ref = RefAction()
        self.assertEqual(expected, ref.resolve_parameter_refs(input, parameters))
    def test_unknown_ref(self):
        parameters = {
            "key": "value"
        }
        input = {
            "Ref": "someotherkey"
        }
        expected = {
            "Ref": "someotherkey"
        }

        ref = RefAction()
        self.assertEqual(expected, ref.resolve_parameter_refs(input, parameters))
    def test_return_value_if_cannot_handle(self, can_handle_mock):
        parameters = {
            "key": "value"
        }
        input = {
            "Ref": "key"
        }
        expected = {
            "Ref": "key"
        }

        ref = RefAction()
        can_handle_mock.return_value = False # Simulate failure to handle the input. Result should be same as input
        self.assertEqual(expected, ref.resolve_parameter_refs(input, parameters))
    def to_statement(self, parameter_values):
        """
        With the given values for each parameter, this method will return a policy statement that can be used
        directly with IAM.

        :param dict parameter_values: Dict containing values for each parameter defined in the template
        :return dict: Dictionary containing policy statement
        :raises InvalidParameterValues: If parameter values is not a valid dictionary or does not contain values for all parameters
        :raises InsufficientParameterValues: If the parameter values don't have values for all required parameters
        """

        missing = self.missing_parameter_values(parameter_values)
        if len(missing) > 0:
            # str() of elements of list to prevent any `u` prefix from being displayed in user-facing error message
            raise InsufficientParameterValues(
                "Following required parameters of template '{}' don't have values: {}"
                .format(self.name, [str(m) for m in missing]))

        # Select only necessary parameter_values. this is to prevent malicious or accidental
        # injection of values for parameters not intended in the template. This is important because "Ref" resolution
        # will substitute any references for which a value is provided.
        necessary_parameter_values = {
            name: value
            for name, value in parameter_values.items()
            if name in self.parameters
        }

        # Only "Ref" is supported
        supported_intrinsics = {RefAction.intrinsic_name: RefAction()}

        resolver = IntrinsicsResolver(necessary_parameter_values,
                                      supported_intrinsics)
        definition_copy = copy.deepcopy(self.definition)

        return resolver.resolve_parameter_refs(definition_copy)
class TestRefCanResolveResourceIdRefs(TestCase):

    def setUp(self):
        self.supported_resource_id_refs_mock = Mock()
        self.ref = RefAction()

    def test_must_replace_refs(self):
        resolved_value = "NewLogicalId"
        input = {
            "Ref": "LogicalId"
        }
        expected = {
            "Ref": resolved_value
        }
        self.supported_resource_id_refs_mock.get.return_value = resolved_value

        output = self.ref.resolve_resource_id_refs(input, self.supported_resource_id_refs_mock)

        self.assertEqual(expected, output)
        self.supported_resource_id_refs_mock.get.assert_called_once_with("LogicalId")

    def test_handle_unsupported_references(self):
        input = {
            "Ref": "OtherLogicalId.Property"
        }
        expected = {
            "Ref": "OtherLogicalId.Property"
        }

        self.supported_resource_id_refs_mock.get.return_value = None

        output = self.ref.resolve_resource_id_refs(input, self.supported_resource_id_refs_mock)
        self.assertEqual(expected, output)
        self.supported_resource_id_refs_mock.get.assert_not_called()

    @patch.object(RefAction, "can_handle")
    def test_return_value_if_cannot_handle(self, can_handle_mock):
        input = {
            "Ref": "key"
        }
        expected = {
            "Ref": "key"
        }

        ref = RefAction()
        can_handle_mock.return_value = False # Simulate failure to handle the input. Result should be same as input
        self.assertEqual(expected, ref.resolve_resource_id_refs(input, self.supported_resource_id_refs_mock))
Example #13
0
class TestRefCanResolveResourceIdRefs(TestCase):
    def setUp(self):
        self.supported_resource_id_refs_mock = Mock()
        self.ref = RefAction()

    def test_must_replace_refs(self):
        resolved_value = "NewLogicalId"
        input = {"Ref": "LogicalId"}
        expected = {"Ref": resolved_value}
        self.supported_resource_id_refs_mock.get.return_value = resolved_value

        output = self.ref.resolve_resource_id_refs(
            input, self.supported_resource_id_refs_mock)

        self.assertEqual(expected, output)
        self.supported_resource_id_refs_mock.get.assert_called_once_with(
            "LogicalId")

    def test_handle_unsupported_references(self):
        input = {"Ref": "OtherLogicalId.Property"}
        expected = {"Ref": "OtherLogicalId.Property"}

        self.supported_resource_id_refs_mock.get.return_value = None

        output = self.ref.resolve_resource_id_refs(
            input, self.supported_resource_id_refs_mock)
        self.assertEqual(expected, output)
        self.supported_resource_id_refs_mock.get.assert_not_called()

    @patch.object(RefAction, "can_handle")
    def test_return_value_if_cannot_handle(self, can_handle_mock):
        input = {"Ref": "key"}
        expected = {"Ref": "key"}

        ref = RefAction()
        can_handle_mock.return_value = False  # Simulate failure to handle the input. Result should be same as input
        self.assertEqual(
            expected,
            ref.resolve_resource_id_refs(input,
                                         self.supported_resource_id_refs_mock))
 def setUp(self):
     self.supported_resource_id_refs_mock = Mock()
     self.ref = RefAction()
Example #15
0
 def setUp(self):
     self.supported_resource_id_refs_mock = Mock()
     self.ref = RefAction()