From cb2b963e0f8c9075cda0ebbaaeb1a61816441606 Mon Sep 17 00:00:00 2001 From: Filip Tubic Date: Fri, 14 Jul 2023 10:46:35 +0200 Subject: [PATCH 1/3] feat(scanning_alerts): implement add scanning alert object method --- sdcclient/secure/scanning/_alerts.py | 39 ++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/sdcclient/secure/scanning/_alerts.py b/sdcclient/secure/scanning/_alerts.py index 7c721d03..86c50f89 100644 --- a/sdcclient/secure/scanning/_alerts.py +++ b/sdcclient/secure/scanning/_alerts.py @@ -379,3 +379,42 @@ def delete_alert(self, policyid): # FIXME: policyid must be maintained for back if not self._checkResponse(res): return [False, self.lasterr] return [True, res.text] + + def add_alert_object(self, object): + ''' + Adds alert object as raw JSON object. + + Args: + object: JSON repsentation of the alert. + + Examples: + >>> client = ScanningAlertsClientV1(sdc_url=os.getenv("SDC_SECURE_URL", "https://secure.sysdig.com"), + >>> token=os.getenv("SDC_SECURE_TOKEN")) + >>> alert = { + >>> "teamId": 3203292, + >>> "alertId": "alert_2SNfqleJM9T0C8eYPtkrVCpH8jl", + >>> "enabled": false, + >>> "type": "runtime", + >>> "name": "[sdc-cli-test] runtime-scanning-alert-asdfg", + >>> "triggers": { + >>> "unscanned": true, + >>> "analysis_update": false, + >>> "vuln_update": false, + >>> "policy_eval": false, + >>> "failed": false + >>> }, + >>> "autoscan": false, + >>> "onlyPassFail": false, + >>> "skipEventSend": false, + >>> "notificationChannelIds": [] + >>> } + >>> client.add_alert_object(alert) + ''' + url = self.url + '/api/scanning/v1/alerts' + data = json.dumps(object) + res = self.http.post(url, headers=self.hdrs, data=data, verify=self.ssl_verify) + if not self._checkResponse(res): + return [False, self.lasterr] + + return [True, res.json()] + From e99586080b57fa303fce8465d6edbe0d6dbf223e Mon Sep 17 00:00:00 2001 From: Filip Tubic Date: Fri, 14 Jul 2023 10:49:49 +0200 Subject: [PATCH 2/3] feat(scanning_alerts): fix blank line and the end of file --- sdcclient/secure/scanning/_alerts.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sdcclient/secure/scanning/_alerts.py b/sdcclient/secure/scanning/_alerts.py index 86c50f89..fe395a7e 100644 --- a/sdcclient/secure/scanning/_alerts.py +++ b/sdcclient/secure/scanning/_alerts.py @@ -417,4 +417,3 @@ def add_alert_object(self, object): return [False, self.lasterr] return [True, res.json()] - From e6d671bcc9b04ce49d224e2645dccd58c6b02cb4 Mon Sep 17 00:00:00 2001 From: Filip Tubic Date: Fri, 14 Jul 2023 11:33:19 +0200 Subject: [PATCH 3/3] feat(scanning_alerts): add client test for adding scanning alert --- sdcclient/secure/scanning/_alerts.py | 22 +++++++++---------- specs/secure/scanning/alerts_spec.py | 33 ++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/sdcclient/secure/scanning/_alerts.py b/sdcclient/secure/scanning/_alerts.py index fe395a7e..c39f356d 100644 --- a/sdcclient/secure/scanning/_alerts.py +++ b/sdcclient/secure/scanning/_alerts.py @@ -391,21 +391,19 @@ def add_alert_object(self, object): >>> client = ScanningAlertsClientV1(sdc_url=os.getenv("SDC_SECURE_URL", "https://secure.sysdig.com"), >>> token=os.getenv("SDC_SECURE_TOKEN")) >>> alert = { - >>> "teamId": 3203292, - >>> "alertId": "alert_2SNfqleJM9T0C8eYPtkrVCpH8jl", - >>> "enabled": false, + >>> "enabled": True, >>> "type": "runtime", - >>> "name": "[sdc-cli-test] runtime-scanning-alert-asdfg", + >>> "name": "runtime-scanning-alert", >>> "triggers": { - >>> "unscanned": true, - >>> "analysis_update": false, - >>> "vuln_update": false, - >>> "policy_eval": false, - >>> "failed": false + >>> "unscanned": True, + >>> "analysis_update": False, + >>> "vuln_update": False, + >>> "policy_eval": False, + >>> "failed": False >>> }, - >>> "autoscan": false, - >>> "onlyPassFail": false, - >>> "skipEventSend": false, + >>> "autoscan": False, + >>> "onlyPassFail": False, + >>> "skipEventSend": False, >>> "notificationChannelIds": [] >>> } >>> client.add_alert_object(alert) diff --git a/specs/secure/scanning/alerts_spec.py b/specs/secure/scanning/alerts_spec.py index 428131a1..0f0ccf4e 100644 --- a/specs/secure/scanning/alerts_spec.py +++ b/specs/secure/scanning/alerts_spec.py @@ -1,12 +1,13 @@ import os +import uuid -from expects import be_empty, be_false, be_true, contain, contain_exactly, expect, have_keys +from expects import be_empty, be_false, be_true, contain, contain_exactly, expect, have_keys, equal from mamba import after, before, context, description, it from sdcclient import SdScanningClient from specs import be_successful_api_call -with description("Scanning Alerts") as self: +with description("Scanning Alerts", "integration") as self: with before.all: self.client = SdScanningClient(sdc_url=os.getenv("SDC_SECURE_URL", "https://secure.sysdig.com"), token=os.getenv("SDC_SECURE_TOKEN")) @@ -18,6 +19,34 @@ for alert in res["alerts"]: self.client.delete_alert(alert["alertId"]) + with it("add alert object"): + alert = { + "enabled": True, + "type": "runtime", + "name": f"runtime-scanning-alert-{uuid.uuid4()}", + "triggers": { + "unscanned": True, + "analysis_update": False, + "vuln_update": False, + "policy_eval": False, + "failed": False + }, + "autoscan": False, + "onlyPassFail": False, + "skipEventSend": False, + "notificationChannelIds": [] + } + ok, res = self.client.add_alert_object(alert) + expect((ok, res)).to(be_successful_api_call) + expect(res['enabled']).to(equal(alert['enabled'])) + expect(res['type']).to(equal(alert['type'])) + expect(res['name']).to(equal(alert['name'])) + expect(res['triggers']).to(equal(alert['triggers'])) + expect(res['autoscan']).to(equal(alert['autoscan'])) + expect(res['onlyPassFail']).to(equal(alert['onlyPassFail'])) + expect(res['skipEventSend']).to(equal(alert['skipEventSend'])) + expect(res['notificationChannelIds']).to(equal(alert['notificationChannelIds'])) + with it("lists all the scanning alerts"): ok, res = self.client.add_runtime_alert( name="A name",