Example #1
0
    def test_render_template_with_system_and_user_context(self):
        # 1. No reference to the user inside the template
        template = "{{st2kv.system.key1}}"
        user = "******"

        result = render_template_with_system_and_user_context(value=template, user=user)
        self.assertEqual(result, "valuea")

        template = "{{st2kv.system.key2}}"
        user = "******"

        result = render_template_with_system_and_user_context(value=template, user=user)
        self.assertEqual(result, "valueb")

        # 2. Reference to the user inside the template
        template = "{{st2kv.user.key1}}"
        user = "******"

        result = render_template_with_system_and_user_context(value=template, user=user)
        self.assertEqual(result, "valuestanley1")

        template = "{{st2kv.user.key1}}"
        user = "******"

        result = render_template_with_system_and_user_context(value=template, user=user)
        self.assertEqual(result, "valuejoe1")
Example #2
0
    def test_render_template_with_system_and_user_context(self):
        # 1. No reference to the user inside the template
        template = '{{system.key1}}'
        user = '******'

        result = render_template_with_system_and_user_context(value=template, user=user)
        self.assertEqual(result, 'valuea')

        template = '{{system.key2}}'
        user = '******'

        result = render_template_with_system_and_user_context(value=template, user=user)
        self.assertEqual(result, 'valueb')

        # 2. Reference to the user inside the template
        template = '{{user.key1}}'
        user = '******'

        result = render_template_with_system_and_user_context(value=template, user=user)
        self.assertEqual(result, 'valuestanley1')

        template = '{{user.key1}}'
        user = '******'

        result = render_template_with_system_and_user_context(value=template, user=user)
        self.assertEqual(result, 'valuejoe1')
Example #3
0
    def test_render_template_with_system_and_user_context(self):
        # 1. No reference to the user inside the template
        template = '{{st2kv.system.key1}}'
        user = '******'

        result = render_template_with_system_and_user_context(value=template,
                                                              user=user)
        self.assertEqual(result, 'valuea')

        template = '{{st2kv.system.key2}}'
        user = '******'

        result = render_template_with_system_and_user_context(value=template,
                                                              user=user)
        self.assertEqual(result, 'valueb')

        # 2. Reference to the user inside the template
        template = '{{st2kv.user.key1}}'
        user = '******'

        result = render_template_with_system_and_user_context(value=template,
                                                              user=user)
        self.assertEqual(result, 'valuestanley1')

        template = '{{st2kv.user.key1}}'
        user = '******'

        result = render_template_with_system_and_user_context(value=template,
                                                              user=user)
        self.assertEqual(result, 'valuejoe1')
Example #4
0
    def _get_datastore_value_for_expression(self, key, value, config_schema_item=None):
        """
        Retrieve datastore value by first resolving the datastore expression and then retrieving
        the value from the datastore.

        :param key: Full path to the config item key (e.g. "token" / "auth.settings.token", etc.)
        """
        from st2common.services.config import deserialize_key_value

        config_schema_item = config_schema_item or {}
        secret = config_schema_item.get("secret", False)

        try:
            value = render_template_with_system_and_user_context(
                value=value, user=self.user
            )
        except Exception as e:
            # Throw a more user-friendly exception on failed render
            exc_class = type(e)
            original_msg = six.text_type(e)
            msg = (
                'Failed to render dynamic configuration value for key "%s" with value '
                '"%s" for pack "%s" config: %s %s '
                % (key, value, self.pack_name, exc_class, original_msg)
            )
            raise RuntimeError(msg)

        if value:
            # Deserialize the value
            value = deserialize_key_value(value=value, secret=secret)
        else:
            value = None

        return value
Example #5
0
    def _get_datastore_value_for_expression(self, key, value, config_schema_item=None):
        """
        Retrieve datastore value by first resolving the datastore expression and then retrieving
        the value from the datastore.

        :param key: Full path to the config item key (e.g. "token" / "auth.settings.token", etc.)
        """
        from st2common.services.config import deserialize_key_value

        config_schema_item = config_schema_item or {}
        secret = config_schema_item.get('secret', False)

        try:
            value = render_template_with_system_and_user_context(value=value,
                                                                 user=self.user)
        except Exception as e:
            # Throw a more user-friendly exception on failed render
            exc_class = type(e)
            original_msg = str(e)
            msg = ('Failed to render dynamic configuration value for key "%s" with value '
                   '"%s" for pack "%s" config: %s ' % (key, value, self.pack_name, original_msg))
            raise exc_class(msg)

        if value:
            # Deserialize the value
            value = deserialize_key_value(value=value, secret=secret)
        else:
            value = None

        return value
Example #6
0
    def _get_datastore_value_for_expression(self, value, config_schema_item=None):
        """
        Retrieve datastore value by first resolving the datastore expression and then retrieving
        the value from the datastore.
        """
        config_schema_item = config_schema_item or {}
        secret = config_schema_item.get('secret', False)

        # TODO: Get key name so we can throw a more friendly exception
        value = render_template_with_system_and_user_context(value=value,
                                                             user=self.user)

        if value:
            # Deserialize the value
            value = deserialize_key_value(value=value, secret=secret)
        else:
            value = None

        return value
Example #7
0
    def _get_datastore_value_for_expression(self,
                                            value,
                                            config_schema_item=None):
        """
        Retrieve datastore value by first resolving the datastore expression and then retrieving
        the value from the datastore.
        """
        config_schema_item = config_schema_item or {}
        secret = config_schema_item.get('secret', False)

        # TODO: Get key name so we can throw a more friendly exception
        value = render_template_with_system_and_user_context(value=value,
                                                             user=self.user)

        if value:
            # Deserialize the value
            value = deserialize_key_value(value=value, secret=secret)
        else:
            value = None

        return value