Skip to content

Commit 70d190a

Browse files
committed
Cover for "decrypt_kv" in "default" value of the schema
PR #4709
1 parent d496c0f commit 70d190a

File tree

5 files changed

+62
-9
lines changed

5 files changed

+62
-9
lines changed

st2common/st2common/util/pack.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,6 @@ def validate_config_against_schema(config_schema, config_object, config_path,
104104

105105
pack_name = pack_name or 'unknown'
106106

107-
for key in config_object:
108-
if (jinja_utils.is_jinja_expression(value=config_object.get(key)) and
109-
"decrypt_kv" in config_object.get(key) and config_schema.get(key).get('secret')):
110-
raise ValueValidationException('Values specified as "secret: True" in config schema '
111-
'are automatically decrypted by default. Use of '
112-
'"decrypt_kv" jinja filter is not allowed for such '
113-
'values.')
114-
115107
schema = util_schema.get_schema_for_resource_parameters(parameters_schema=config_schema,
116108
allow_additional_properties=True)
117109
instance = config_object
@@ -120,6 +112,14 @@ def validate_config_against_schema(config_schema, config_object, config_path,
120112
cleaned = util_schema.validate(instance=instance, schema=schema,
121113
cls=util_schema.CustomValidator, use_default=True,
122114
allow_default_none=True)
115+
for key in cleaned:
116+
if (jinja_utils.is_jinja_expression(value=cleaned.get(key)) and
117+
"decrypt_kv" in cleaned.get(key) and config_schema.get(key).get('secret')):
118+
raise ValueValidationException('Values specified as "secret: True" in config '
119+
'schema are automatically decrypted by default. Use '
120+
'of "decrypt_kv" jinja filter is not allowed for '
121+
'such values. Please check the specified values in '
122+
'the config or the default values in the schema.')
123123
except jsonschema.ValidationError as e:
124124
attribute = getattr(e, 'path', [])
125125

st2common/tests/unit/test_configs_registrar.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
PACK_6_PATH = os.path.join(fixturesloader.get_fixtures_packs_base_path(), 'dummy_pack_6')
3838
PACK_19_PATH = os.path.join(fixturesloader.get_fixtures_packs_base_path(), 'dummy_pack_19')
3939
PACK_11_PATH = os.path.join(fixturesloader.get_fixtures_packs_base_path(), 'dummy_pack_11')
40+
PACK_22_PATH = os.path.join(fixturesloader.get_fixtures_packs_base_path(), 'dummy_pack_22')
4041

4142

4243
class ConfigsRegistrarTestCase(CleanDbTestCase):
@@ -151,6 +152,9 @@ def test_register_all_configs_with_config_schema_validation_validation_failure_2
151152
base_dirs=packs_base_paths)
152153

153154
def test_register_all_configs_with_config_schema_validation_validation_failure_3(self):
155+
# This test checks for values containing "decrypt_kv" jinja filter in the config
156+
# object where keys have "secret: True" set in the schema.
157+
154158
# Verify DB is empty
155159
pack_dbs = Pack.get_all()
156160
config_dbs = Config.get_all()
@@ -170,7 +174,38 @@ def test_register_all_configs_with_config_schema_validation_validation_failure_3
170174

171175
expected_msg = ('Values specified as "secret: True" in config schema are automatically '
172176
'decrypted by default. Use of "decrypt_kv" jinja filter is not allowed '
173-
'for such values.')
177+
'for such values. Please check the specified values in the config or '
178+
'the default values in the schema.')
179+
180+
self.assertRaisesRegexp(ValueError, expected_msg,
181+
registrar.register_from_packs,
182+
base_dirs=packs_base_paths)
183+
184+
def test_register_all_configs_with_config_schema_validation_validation_failure_4(self):
185+
# This test checks for default values containing "decrypt_kv" jinja filter for
186+
# keys which have "secret: True" set.
187+
188+
# Verify DB is empty
189+
pack_dbs = Pack.get_all()
190+
config_dbs = Config.get_all()
191+
192+
self.assertEqual(len(pack_dbs), 0)
193+
self.assertEqual(len(config_dbs), 0)
194+
195+
registrar = ConfigsRegistrar(use_pack_cache=False, fail_on_failure=True,
196+
validate_configs=True)
197+
registrar._pack_loader.get_packs = mock.Mock()
198+
registrar._pack_loader.get_packs.return_value = {'dummy_pack_22': PACK_22_PATH}
199+
200+
# Register ConfigSchema for pack
201+
registrar._register_pack_db = mock.Mock()
202+
registrar._register_pack(pack_name='dummy_pack_22', pack_dir=PACK_22_PATH)
203+
packs_base_paths = content_utils.get_packs_base_paths()
204+
205+
expected_msg = ('Values specified as "secret: True" in config schema are automatically '
206+
'decrypted by default. Use of "decrypt_kv" jinja filter is not allowed '
207+
'for such values. Please check the specified values in the config or '
208+
'the default values in the schema.')
174209

175210
self.assertRaisesRegexp(ValueError, expected_msg,
176211
registrar.register_from_packs,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
key_with_no_secret_and_no_default: "Any Value"
3+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
wrong_key_with_invalid_default_value:
3+
type: "string"
4+
secret: true
5+
required: true
6+
default: "{{st2kv.user.api_key | decrypt_kv}}"
7+
key_with_no_secret_and_no_default:
8+
type: "string"
9+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
name : dummy_pack_22
3+
description : dummy pack
4+
version : 0.1.0
5+
author : st2-dev
6+

0 commit comments

Comments
 (0)