Skip to content

Commit

Permalink
audit-alert command: enable/disable alerts, support HTTP body. KC-784
Browse files Browse the repository at this point in the history
  • Loading branch information
sk-keeper authored and aaunario-keeper committed Jun 21, 2024
1 parent dba1435 commit 7c805f6
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion keepercommander/commands/audit_alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import datetime
import json
import logging
import os.path
import secrets
from typing import Optional, List, Tuple, Union

Expand Down Expand Up @@ -46,6 +47,9 @@
alert_recipient_edit_options.add_argument(
'--webhook', dest='webhook', metavar='URL', action='store',
help='Webhook URL. See https://docs.keeper.io/enterprise-guide/webhooks')
alert_recipient_edit_options.add_argument(
'--http-body', dest='http_body', metavar='HTTP_BODY', action='store',
help='Webhook HTTP Body')
alert_recipient_edit_options.add_argument(
'--cert-errors', dest='cert_errors', action='store', choices=['ignore', 'enforce'],
help='Webhook SSL Certificate errors')
Expand Down Expand Up @@ -97,6 +101,9 @@
alert_edit_options.add_argument(
'--shared-folder-uid', dest='shared_folder_uid', action='append', metavar='SHARED_FOLDER_UID',
help='Shared Folder UID. Can be repeated.')
alert_edit_options.add_argument(
'--active', dest='active', action='store', metavar='ACTIVE', choices=['on', 'off'],
help='Enable or disable alert')


alert_add_parser = argparse.ArgumentParser(prog='audit-alert add', parents=[alert_edit_options])
Expand Down Expand Up @@ -462,8 +469,11 @@ def execute(self, params, **kwargs):
if 'webhook' in r:
wh = r['webhook']
recipients.append(['Webhook URL', wh.get('url')])
recipients.append(['Certificate Errors', 'Ignore' if wh.get('allowUnverifiedCertificate') else 'Enforce'])
http_body = wh.get('template')
if http_body:
recipients.append(['HTTP Body', http_body])
recipients.append(['Webhook Token', wh.get('token')])
recipients.append(['Certificate Errors', 'Ignore' if wh.get('allowUnverifiedCertificate') else 'Enforce'])
email = r.get('email')
if email:
recipients.append(['Email To', email])
Expand Down Expand Up @@ -620,6 +630,23 @@ def apply_recipient(recipient, options):
}
else:
recipient['webhook']['url'] = webhook
http_body = options.get('http_body')
if http_body is not None:
if 'webhook' in recipient:
webhook = recipient['webhook']
if http_body:
if http_body[0] == '@':
file_name = http_body[1:]
file_name = os.path.expanduser(file_name)
if os.path.isfile(file_name):
with open(file_name, 'rt') as tf:
webhook_body = tf.read()
else:
raise CommandError('', f'File \"{file_name}\" not found')
webhook['template'] = webhook_body
elif 'template' in webhook:
webhook['template'] = None

cert_errors = options.get('cert_errors')
if cert_errors is not None:
if 'webhook' in recipient:
Expand Down Expand Up @@ -708,6 +735,23 @@ def execute(self, params, **kwargs):
'settings': alert,
}
api.communicate(params, rq)

active = kwargs.get('active')
if isinstance(active, str):
alert_id = alert.get('id')
ctx = AuditSettingMixin.get_alert_context(alert_id) or {'id': alert_id}
current_active = 'off' if ctx.get('disabled') is True else 'on'
if active != current_active:
rq = {
'command': 'put_enterprise_setting',
'type': 'AuditAlertContext',
'settings': {
'id': alert_id,
'disabled': active == 'off'
}
}
api.communicate(params, rq)

self.invalidate_alerts()
command = AuditAlertView()
command.execute(params, target=kwargs.get('target'))
Expand Down

0 comments on commit 7c805f6

Please sign in to comment.