Skip to content

Commit 7e037ec

Browse files
authored
RD-6368 Add secret schema to secret create (#1450)
* RD-6368 Add secret schema to secret create
1 parent 47381ce commit 7e037ec

4 files changed

Lines changed: 78 additions & 4 deletions

File tree

cloudify_cli/cli/cfy.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,33 @@ def __init__(self):
12831283
required=False,
12841284
help=helptexts.SECRET_STRING)
12851285

1286+
self.secret_schema = click.option(
1287+
'--schema',
1288+
'secret_schema',
1289+
required=False,
1290+
default=None,
1291+
cls=MutuallyExclusiveOption,
1292+
mutually_exclusive=['dict', 'list'],
1293+
help=helptexts.SECRET_SCHEMA)
1294+
1295+
self.secret_flag_dict = click.option(
1296+
'--dict',
1297+
'secret_flag_dict',
1298+
is_flag=True,
1299+
default=False,
1300+
cls=MutuallyExclusiveOption,
1301+
mutually_exclusive=['schema', 'list'],
1302+
help=helptexts.SECRET_FLAG_DICT)
1303+
1304+
self.secret_flag_list = click.option(
1305+
'--list',
1306+
'secret_flag_list',
1307+
is_flag=True,
1308+
default=False,
1309+
cls=MutuallyExclusiveOption,
1310+
mutually_exclusive=['schema', 'dict'],
1311+
help=helptexts.SECRET_FLAG_LIST)
1312+
12861313
self.secret_update_if_exists = click.option(
12871314
'-u',
12881315
'--update-if-exists',

cloudify_cli/cli/helptexts.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@
335335
PROFILE_NAME = 'Name of the profile to use'
336336
SECRET_VALUE = "The secret's value to be set"
337337
SECRET_STRING = "The string to use as the secret's value"
338+
SECRET_FLAG_DICT = "Whether the secret is to be treated as a dict"
339+
SECRET_FLAG_LIST = "Whether the secret is to be treated as a lists"
340+
SECRET_SCHEMA = "A JSON schema against which the secret will be validated [" \
341+
"default: '{\"type\": \"string\"}']"
338342
SECRET_FILE = "The file with the contents of the secret"
339343
SECRET_UPDATE_IF_EXISTS = 'Update secret value if secret key already ' \
340344
'exists. [This option is deprecated; use cfy ' \

cloudify_cli/commands/secrets.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ def providers():
7171
@cfy.options.secret_update_if_exists
7272
@cfy.options.visibility(mutually_exclusive_required=False)
7373
@cfy.options.hidden_value
74+
@cfy.options.secret_schema
75+
@cfy.options.secret_flag_dict
76+
@cfy.options.secret_flag_list
7477
@cfy.options.tenant_name(required=False, resource_name_for_help='secret')
7578
@cfy.options.common_options
7679
@cfy.assert_manager_active()
@@ -81,6 +84,9 @@ def create(key,
8184
secret_file,
8285
update_if_exists,
8386
hidden_value,
87+
secret_schema,
88+
secret_flag_dict,
89+
secret_flag_list,
8490
visibility,
8591
tenant_name,
8692
logger,
@@ -91,16 +97,42 @@ def create(key,
9197
"""
9298
utils.explicit_tenant_name_message(tenant_name, logger)
9399
validate_visibility(visibility)
94-
secret_string = _get_secret_string(secret_file, secret_string)
95-
if not secret_string:
100+
value = _get_secret_string(secret_file, secret_string)
101+
if not value:
96102
raise CloudifyCliError('Failed to create secret key. '
97103
'Missing option '
98104
'--secret-string or secret-file.')
105+
106+
if secret_schema:
107+
try:
108+
secret_schema = json.loads(secret_schema)
109+
except json.decoder.JSONDecodeError as e:
110+
raise CloudifyCliError(
111+
f'Error decoding JSON schema "{secret_schema}": {e}')
112+
if not isinstance(secret_schema, dict) or \
113+
not secret_schema.get('type'):
114+
raise CloudifyCliError(
115+
'Invalid JSON schema. Expected a dict with a "type" key')
116+
117+
if secret_flag_dict:
118+
secret_schema = {"type": "object"}
119+
if secret_flag_list:
120+
secret_schema = {"type": "array"}
121+
122+
if secret_schema:
123+
try:
124+
value = json.loads(value)
125+
except json.decoder.JSONDecodeError:
126+
raise CloudifyCliError(
127+
f'Error decoding secret value: \'{value}\' is not of '
128+
f'type \'{secret_schema.get("type")}\'')
129+
99130
client.secrets.create(key,
100-
secret_string,
131+
value,
101132
update_if_exists,
102133
hidden_value,
103-
visibility)
134+
visibility,
135+
secret_schema)
104136

105137
logger.info('Secret `{0}` created'.format(key))
106138

cloudify_cli/tests/commands/test_secrets.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ def test_secrets_create_mutually_exclusive_arguments(self):
9292
)
9393
self.assertIn('mutually exclusive with arguments:', outcome.output)
9494

95+
def test_secrets_create_invalid_schema(self):
96+
self.invoke("cfy secrets create s1 -s hi --schema bye",
97+
err_str_segment="Error decoding JSON schema",
98+
exception=CloudifyCliError)
99+
100+
def test_secrets_create_invalid_json_value(self):
101+
self.invoke("cfy secrets create s1 -s hi --dict",
102+
err_str_segment="Error decoding secret value: 'hi' is "
103+
"not of type 'object'",
104+
exception=CloudifyCliError)
105+
95106
def test_secrets_export_invalid_password_length(self):
96107
self.invoke('cfy secrets export -p 1234567',
97108
err_str_segment='ERROR: Passphrase must contain at least '

0 commit comments

Comments
 (0)