Skip to content

Commit e2e4e26

Browse files
authored
Merge pull request #5594 from dmork123/security_audit
Add security_audit feature
2 parents 3cc23fe + f8282a2 commit e2e4e26

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ Added
157157

158158
Contributed by @Kami.
159159

160+
* Add new audit message when a user has decrypted a key whether manually in the container (st2 key get [] --decrypt)
161+
or through a workflow with a defined config. #5594
162+
Contributed by @dmork123
163+
160164
* Added garbage collection for rule_enforcement and trace models #5596/5602
161165
Contributed by Amanda McGuinness (@amanda11 intive)
162166

st2api/st2api/controllers/v1/keyvalue.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ def get_one(self, name, requester_user, scope=None, user=None, decrypt=False):
120120
kvp_api = self._get_one_by_scope_and_name(
121121
name=key_ref, scope=scope, from_model_kwargs=from_model_kwargs
122122
)
123+
if decrypt and kvp_api.secret:
124+
LOG.audit(
125+
"User %s decrypted the value %s ",
126+
user,
127+
name,
128+
extra={
129+
"user": user,
130+
"scope": scope,
131+
"key_name": name,
132+
"operation": "decrypt",
133+
},
134+
)
123135

124136
return kvp_api
125137

@@ -212,6 +224,7 @@ def get_all(
212224
kvp_apis_user = []
213225

214226
if scope in [ALL_SCOPE, SYSTEM_SCOPE, FULL_SYSTEM_SCOPE]:
227+
decrypted_keys = []
215228
# If user has system role, then retrieve all system scoped items
216229
if has_system_role:
217230
raw_filters["scope"] = FULL_SYSTEM_SCOPE
@@ -227,6 +240,10 @@ def get_all(
227240
)
228241

229242
kvp_apis_system.extend(items.json or [])
243+
if decrypt and items.json:
244+
decrypted_keys.extend(
245+
kv_api["name"] for kv_api in items.json if kv_api["secret"]
246+
)
230247
else:
231248
# Otherwise if user is not an admin, then get the list of
232249
# system scoped items that user is granted permission to.
@@ -241,6 +258,21 @@ def get_all(
241258
kvp_apis_system.append(item)
242259
except Exception as e:
243260
LOG.error("Unable to get key %s: %s", key, str(e))
261+
continue
262+
if decrypt and item.secret:
263+
decrypted_keys.append(key)
264+
if decrypted_keys:
265+
LOG.audit(
266+
"User %s decrypted the values %s ",
267+
user,
268+
decrypted_keys,
269+
extra={
270+
"User": user,
271+
"scope": FULL_SYSTEM_SCOPE,
272+
"key_name": decrypted_keys,
273+
"operation": "decrypt",
274+
},
275+
)
244276

245277
if scope in [ALL_SCOPE, USER_SCOPE, FULL_USER_SCOPE]:
246278
# Retrieves all the user scoped items that the current user owns.
@@ -257,6 +289,22 @@ def get_all(
257289
)
258290

259291
kvp_apis_user.extend(items.json)
292+
if decrypt and items.json:
293+
decrypted_keys = [
294+
kvp_api["name"] for kvp_api in items.json if kvp_api["secret"]
295+
]
296+
if decrypted_keys:
297+
LOG.audit(
298+
"User %s decrypted the values %s ",
299+
user,
300+
decrypted_keys,
301+
extra={
302+
"User": user,
303+
"scope": FULL_USER_SCOPE,
304+
"key_name": decrypted_keys,
305+
"operation": "decrypt",
306+
},
307+
)
260308

261309
return kvp_apis_system + kvp_apis_user
262310

st2common/st2common/util/config_loader.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,19 @@ def _get_datastore_value_for_expression(self, key, value, config_schema_item=Non
235235

236236
config_schema_item = config_schema_item or {}
237237
secret = config_schema_item.get("secret", False)
238-
238+
if secret or "decrypt_kv" in value:
239+
LOG.audit(
240+
"User %s is decrypting the value for key %s from the config within pack %s",
241+
self.user,
242+
key,
243+
self.pack_name,
244+
extra={
245+
"user": self.user,
246+
"key_name": key,
247+
"pack_name": self.pack_name,
248+
"operation": "pack_config_value_decrypt",
249+
},
250+
)
239251
try:
240252
value = render_template_with_system_and_user_context(
241253
value=value, user=self.user

0 commit comments

Comments
 (0)