-
-
Notifications
You must be signed in to change notification settings - Fork 765
Fix decrypting pack config keys under additionalProperties #5225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
bffccc5
46a892c
6e864f3
44e0436
2354014
9d7c1d5
3543ad1
e1ef44b
46ce281
8bf7d2a
a2644c2
b7672ed
26741cd
e69fbae
a483888
d67b3c9
4d3be4a
574034f
d0d9924
6bfa390
532e15f
1d613bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,6 +98,23 @@ def _get_values_for_config(self, config_schema_db, config_db): | |
config = self._assign_default_values(schema=schema_values, config=config) | ||
return config | ||
|
||
@staticmethod | ||
def _get_object_property_schema(object_schema, additional_properties_keys=None): | ||
""" | ||
Create a schema for an object property using both additionalProperties and properties. | ||
|
||
:rtype: ``dict`` | ||
""" | ||
property_schema = {} | ||
additional_properties = object_schema.get("additionalProperties", {}) | ||
# additionalProperties can be a boolean or a dict | ||
if additional_properties and isinstance(additional_properties, dict): | ||
# ensure that these keys are present in the object | ||
for key in additional_properties_keys: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, if that works it should be much more straight forward to understand the code now - well, at least for me :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And re - using copy - I actually verified, if we didn't use copy with And since I believe we indeed never manipulate those values, only read / access them, copy is probably not needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. We modify the |
||
property_schema[key] = additional_properties | ||
property_schema.update(object_schema.get("properties", {})) | ||
return property_schema | ||
|
||
def _assign_dynamic_config_values(self, schema, config, parent_keys=None): | ||
""" | ||
Assign dynamic config value for a particular config item if the ite utilizes a Jinja | ||
|
@@ -127,21 +144,26 @@ def _assign_dynamic_config_values(self, schema, config, parent_keys=None): | |
is_dictionary = isinstance(config_item_value, dict) | ||
is_list = isinstance(config_item_value, list) | ||
|
||
# pass a copy of parent_keys so the loop doesn't add sibling keys | ||
current_keys = parent_keys + [str(config_item_key)] | ||
|
||
# Inspect nested object properties | ||
if is_dictionary: | ||
parent_keys += [str(config_item_key)] | ||
property_schema = self._get_object_property_schema( | ||
schema_item, | ||
additional_properties_keys=config_item_value.keys(), | ||
) | ||
self._assign_dynamic_config_values( | ||
schema=schema_item.get("properties", {}), | ||
schema=property_schema, | ||
config=config[config_item_key], | ||
parent_keys=parent_keys, | ||
parent_keys=current_keys, | ||
) | ||
# Inspect nested list items | ||
elif is_list: | ||
parent_keys += [str(config_item_key)] | ||
self._assign_dynamic_config_values( | ||
schema=schema_item.get("items", {}), | ||
config=config[config_item_key], | ||
parent_keys=parent_keys, | ||
parent_keys=current_keys, | ||
) | ||
else: | ||
is_jinja_expression = jinja_utils.is_jinja_expression( | ||
|
@@ -150,9 +172,7 @@ def _assign_dynamic_config_values(self, schema, config, parent_keys=None): | |
|
||
if is_jinja_expression: | ||
# Resolve / render the Jinja template expression | ||
full_config_item_key = ".".join( | ||
parent_keys + [str(config_item_key)] | ||
) | ||
full_config_item_key = ".".join(current_keys) | ||
value = self._get_datastore_value_for_expression( | ||
key=full_config_item_key, | ||
value=config_item_value, | ||
|
@@ -182,18 +202,24 @@ def _assign_default_values(self, schema, config): | |
default_value = schema_item.get("default", None) | ||
is_object = schema_item.get("type", None) == "object" | ||
has_properties = schema_item.get("properties", None) | ||
has_additional_properties = schema_item.get("additionalProperties", None) | ||
|
||
if has_default_value and not has_config_value: | ||
# Config value is not provided, but default value is, use a default value | ||
config[schema_item_key] = default_value | ||
|
||
# Inspect nested object properties | ||
if is_object and has_properties: | ||
if is_object and (has_properties or has_additional_properties): | ||
if not config.get(schema_item_key, None): | ||
config[schema_item_key] = {} | ||
|
||
property_schema = self._get_object_property_schema( | ||
schema_item, | ||
additional_properties_keys=config[schema_item_key].keys(), | ||
) | ||
|
||
self._assign_default_values( | ||
schema=schema_item["properties"], config=config[schema_item_key] | ||
schema=property_schema, config=config[schema_item_key] | ||
) | ||
|
||
return config | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,8 +19,10 @@ | |
from st2common.models.db.keyvalue import KeyValuePairDB | ||
from st2common.exceptions.db import StackStormDBObjectNotFoundError | ||
from st2common.persistence.keyvalue import KeyValuePair | ||
from st2common.models.api.keyvalue import KeyValuePairAPI | ||
from st2common.services.config import set_datastore_value_for_config_key | ||
from st2common.util.config_loader import ContentPackConfigLoader | ||
from st2common.util import crypto | ||
|
||
from st2tests.base import CleanDbTestCase | ||
|
||
|
@@ -43,7 +45,7 @@ def test_ensure_local_pack_config_feature_removed(self): | |
|
||
def test_get_config_some_values_overriden_in_datastore(self): | ||
# Test a scenario where some values are overriden in datastore via pack | ||
# flobal config | ||
# global config | ||
kvp_db = set_datastore_value_for_config_key( | ||
pack_name="dummy_pack_5", | ||
key_name="api_secret", | ||
|
@@ -518,6 +520,61 @@ def test_get_config_dynamic_config_item_nested_list(self): | |
|
||
config_db.delete() | ||
|
||
def test_get_config_dynamic_config_item_under_additional_properties(self): | ||
pack_name = "dummy_pack_schema_with_additional_properties_1" | ||
loader = ContentPackConfigLoader(pack_name=pack_name) | ||
|
||
encrypted_value = crypto.symmetric_encrypt( | ||
KeyValuePairAPI.crypto_key, "v1_encrypted" | ||
) | ||
KeyValuePair.add_or_update( | ||
KeyValuePairDB(name="k1_encrypted", value=encrypted_value, secret=True) | ||
) | ||
|
||
#################### | ||
# values in objects under an object with additionalProperties | ||
values = { | ||
"profiles": { | ||
"dev": { | ||
# no host or port to test default value | ||
"token": "hard-coded-secret", | ||
}, | ||
"prod": { | ||
"host": "127.1.2.7", | ||
"port": 8282, | ||
# encrypted in datastore | ||
"token": "{{st2kv.system.k1_encrypted}}", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If user wants that value in the config to actually be decrypted, they still need to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. Config is automatically decrypted when the schema lists the key as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for clarification - I was just somewhat confused by the test case aka something didn't add up. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I pushed a fix for this test - 574034f. That's the part which confused me a bit. I assumed we don't do decryption because the value which was stored and the one which was retrieved was supposed to be the same (but the KVP model layer doesn't perform any encryption and we need to encrypt the value ourselves when storing it). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah! That's why it seemed to be working before, but wasn't. Thanks for the fix! |
||
# schema declares `secret: true` which triggers auto-decryption. | ||
# If this were not encrypted, it would try to decrypt it and fail. | ||
}, | ||
} | ||
} | ||
config_db = ConfigDB(pack=pack_name, values=values) | ||
config_db = Config.add_or_update(config_db) | ||
|
||
config_rendered = loader.get_config() | ||
|
||
self.assertEqual( | ||
config_rendered, | ||
{ | ||
"region": "us-east-1", | ||
"profiles": { | ||
"dev": { | ||
"host": "127.0.0.3", | ||
"port": 8080, | ||
"token": "hard-coded-secret", | ||
}, | ||
"prod": { | ||
"host": "127.1.2.7", | ||
"port": 8282, | ||
"token": "v1_encrypted", | ||
}, | ||
}, | ||
}, | ||
) | ||
|
||
config_db.delete() | ||
|
||
def test_empty_config_object_in_the_database(self): | ||
pack_name = "dummy_pack_empty_config" | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
region: | ||
type: "string" | ||
required: false | ||
default: "us-east-1" | ||
profiles: | ||
type: "object" | ||
required: false | ||
additionalProperties: | ||
type: object | ||
additionalProperties: false | ||
properties: | ||
host: | ||
type: "string" | ||
required: false | ||
default: "127.0.0.3" | ||
port: | ||
type: "integer" | ||
required: false | ||
default: 8080 | ||
token: | ||
type: "string" | ||
required: true | ||
secret: true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
name : dummy_pack_schema_with_additional_properties_1 | ||
description : dummy pack with nested objects under additionalProperties | ||
version : 0.1.0 | ||
author : st2-dev | ||
email : [email protected] |
Uh oh!
There was an error while loading. Please reload this page.