Skip to content

Commit 1acf59f

Browse files
authored
feat: Add get_event by ID method to Events client v1 and v2 (#188)
* feat: Add get_event by ID method to Events client v1 and v2 * fix: Solve linting problems * fix(ci): Update custom fixture rules file
1 parent 818f7d2 commit 1acf59f

File tree

5 files changed

+71
-117
lines changed

5 files changed

+71
-117
lines changed

fixtures/custom_rules.yaml

Lines changed: 1 addition & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,6 @@
1212

1313
# Or override any rule, macro, or list from the Default Rules
1414
---
15-
- macro: "user_known_k8s_client_container"
16-
condition: "container.image.repository=\"k8s.gcr.io/fluentd-gcp-scaler\" or container.image.repository=\"\
17-
fluxcd/flux\" or container.image.repository=\"sysdig/agent\" or container.image.repository=\"\
18-
fluxcd/helm-operator\" or (container.image.repository=\"google/cloud-sdk\")"
19-
append: false
20-
21-
- macro: "user_known_write_below_root_activities"
22-
condition: "(container.image.repository startswith \"bbcdocker/go-synapse\" and\
23-
\ fd.name=\"/haproxy.conf\") or (container.image.repository=\"cassandra\" and\
24-
\ fd.name startswith \"/root/.cassandra/\") or (container.id=host and fd.name\
25-
\ startswith /root/.kube/) or (container.image.repository=\"mariadb\" and proc.name=\"\
26-
mysqld\") or (container.image.repository=\"mariadb\" and proc.name=\"mysql\")"
27-
append: false
28-
2915
- macro: "user_known_network_tool_client_container"
3016
condition: "container.image.repository=\"bbcdocker/go-synapse\" or container.image.repository=\"\
3117
strimzi/kafka\" or container.image.repository=\"landoop/fast-data-dev\""
@@ -50,12 +36,6 @@
5036
source: "syscall"
5137
append: false
5238

53-
- macro: "user_known_write_below_etc_activities"
54-
condition: "(container.image.repository=\"quay.io/thanos/thanos\" and fd.name=\"\
55-
/etc/prom/prometheus.yaml.tmp\" or (container.image.repository=\"eu.gcr.io/bbc-registry/comuto3\"\
56-
\ and fd.name startswith \"/etc/nginx/\"))"
57-
append: false
58-
5939
- rule: "The docker client is executed in a container"
6040
desc: "Detect a k8s client tool executed inside a container"
6141
condition: "spawned_process and container and not user_known_k8s_client_container\
@@ -85,39 +65,6 @@
8565
- "users"
8666
append: false
8767

88-
- macro: "user_known_write_etc_conditions"
89-
condition: "proc.name=confd or (container.image.repository=\"confluentinc/cp-schema-registry\"\
90-
\ and fd.name startswith \"/etc/schema-registry/\") or (container.image.repository=\"\
91-
eu.gcr.io/bbc-registry/communication\" and fd.name startswith \"/etc/nginx/\"\
92-
) or (container.image.repository=\"eu.gcr.io/bbc-registry/redirector\" and fd.name\
93-
\ startswith \"/etc/nginx/\") or (container.image.repository=\"eu.gcr.io/bbc-registry/webhooks\"\
94-
\ and fd.name startswith \"/etc/nginx/\")or (container.image.repository=\"thanosio/thanos\"\
95-
\ and fd.name startswith \"/etc/prom/\") or (container.image.repository=\"eu.gcr.io/bbc-registry/insurance-backoffice\"\
96-
\ and fd.name startswith \"/etc/nginx/\") or (container.id=\"host\" and proc.name=\"\
97-
exe\" and proc.pname=\"dockerd\")"
98-
append: false
99-
100-
- macro: "user_known_package_manager_in_container"
101-
condition: "(container.image.repository=\"confluentinc/cp-schema-registry\" and\
102-
\ proc.name=\"pip\") or (container.image.repository=sysdig/node-image-analyzer\
103-
\ and proc.name=rpm)"
104-
append: false
105-
106-
- macro: "user_privileged_containers"
107-
condition: "(container.image.repository endswith sysdig/agent) or (container.image.repository=weaveworks/scope)\
108-
\ or (container.image.repository=docker.io/weaveworks/scope) or (container.image.repository=gcr.io/google-containers/startup-script)\
109-
\ or (container.image.repository=gke.gcr.io/kube-proxy) or (container.image.repository=sysdig/node-image-analyzer)"
110-
append: false
111-
112-
- macro: "user_sensitive_mount_containers"
113-
condition: "(container.image.repository = docker.io/sysdig/agent) or (container.image.repository=quay.io/prometheus/node-exporter)\
114-
\ or (container.image.repository=weaveworks/scope) or (container.image.repository=datadog/agent)"
115-
append: false
116-
117-
- macro: "user_known_change_thread_namespace_activities"
118-
condition: "container.image.repository=gcr.io/google-containers/startup-script"
119-
append: false
120-
12168
- list: "user_known_hostnetwork_images"
12269
items:
12370
- "gke.gcr.io/kube-proxy"
@@ -168,23 +115,6 @@
168115
tags: []
169116
append: true
170117

171-
- macro: "user_shell_container_exclusions"
172-
condition: "((container.image.repository=bitnami/rabbitmq and proc.pname=erl) or\
173-
\ (container.image.repository=bitnami/rabbitmq and proc.pname=\"beam.smp\"))"
174-
append: false
175-
176-
- macro: "user_known_write_root_conditions"
177-
condition: "(fd.name=/root/.bash_history) or (container.image.repository=\"cassandra\"\
178-
\ and fd.name startswith \"/root/.cassandra/\") or (container.image.repository=\"\
179-
bbcdocker/go-synapse\" and fd.name=\"/haproxy.conf\") or (container.id=\"host\"\
180-
\ and proc.name=\"exe\" and proc.pname=\"dockerd\")"
181-
append: false
182-
183-
- macro: "exe_running_docker_save"
184-
condition: "((proc.cmdline startswith \"exe /var/lib/docker\" or proc.cmdline startswith\
185-
\ \"exe / /var/lib/docker\") and proc.pname in (dockerd, docker))"
186-
append: false
187-
188118
- rule: "Update Package Repository"
189119
condition: "and not exe_running_docker_save"
190120
tags: []
@@ -201,11 +131,6 @@
201131
tags: []
202132
append: true
203133

204-
- macro: "allowed_clear_log_files"
205-
condition: "(container.image.repository=\"landoop/fast-data-dev\" and fd.name=\"\
206-
/var/log/broker.log\")"
207-
append: false
208-
209134
- list: "user_known_gke_metadata_images"
210135
items:
211136
- "gke.gcr.io/kube-proxy-amd64"
@@ -225,7 +150,7 @@
225150

226151
- macro: "mariadb_snapshots_validator"
227152
condition: "(container.image.repository=\"google/cloud-sdk\" and container.name\
228-
\ contains\"snapshot-validator\")"
153+
\ contains \"snapshot-validator\")"
229154
append: false
230155

231156
- macro: "bbc_java_app_proc"
@@ -286,10 +211,6 @@
286211
tags: []
287212
append: true
288213

289-
- macro: "user_shell_container_exclusions"
290-
condition: "(container.image.repository=\"kong\" and proc.pname=\"nginx\")"
291-
append: false
292-
293214
- list: "user_known_privilged_k8s_roles"
294215
items:
295216
- "mariadb-moderation-snapshot-validated"
@@ -319,21 +240,6 @@
319240
tags: []
320241
append: true
321242

322-
- macro: "user_known_network_tool_activities"
323-
condition: "(container.image.repository=\"mariadb\" and (proc.pname=\"wsrep_sst_maria\"\
324-
\ or proc.pname=\"timeout\") and proc.name=\"socat\")"
325-
append: false
326-
327-
- macro: "user_shell_container_exclusions"
328-
condition: "(container.image.repository=\"mariadb\" and proc.pname=\"mysqld\" and\
329-
\ proc.name=\"sh\")"
330-
append: false
331-
332-
- macro: "user_known_remote_file_copy_activities"
333-
condition: "(container.image.repository=\"eu.gcr.io/bbc-registry/command-export-russian-user\"\
334-
\ and proc.name=\"sftp\")"
335-
append: false
336-
337243
- rule: "Launch Remote File Copy Tools in Container"
338244
condition: "and not user_known_remote_file_copy_activities"
339245
tags: []
@@ -349,27 +255,6 @@
349255
tags: []
350256
append: true
351257

352-
- macro: "user_known_container_drift_activities"
353-
condition: "((container.image.repository=\"fluxcd/helm-operator\" and proc.name=\"\
354-
git\" and evt.arg.filename endswith \"/.git/config\") or (container.image.repository=\"\
355-
fluxcd/flux\" and proc.name=\"git\" and evt.arg.filename endswith \"/.git/config\"\
356-
) or (container.image.repository=\"k8s.gcr.io/fluentd-gcp-scaler\" and proc.name=\"\
357-
kubectl\" and evt.arg.filename startswith \"/root/.kube/cache/discovery/\") or\
358-
\ (container.image.repository=\"eu.gcr.io/bbc-registry/command-bnp-payout-report\"\
359-
\ and proc.name=\"gpg-agent\" and evt.arg.filename startswith \"/root/.gnupg/\"\
360-
) or (container.image.repository=\"gcr.io/stackdriver-agents/stackdriver-logging-agent\"\
361-
\ and evt.arg.filename startswith \"/var/run/google-fluentd/\") or (container.image.repository=\"\
362-
weaveworks/prom-aggregation-gateway\" and proc.name=\"prom-aggregatio\" and evt.arg.filename\
363-
\ startswith \"/var/lib/docker/\") or (container.image.repository=\"datadog/agent\"\
364-
\ and proc.name=\"system-probe\" and evt.arg.filename startswith \"/var/run/sysprobe/\"\
365-
) or (container.image.repository=\"docker.elastic.co/elasticsearch/elasticsearch\"\
366-
\ and proc.name=\"java\" and evt.arg.filename startswith \"/usr/share/elasticsearch/plugins/\"\
367-
) or (container.image.repository=\"docker.elastic.co/elasticsearch/elasticsearch\"\
368-
\ and proc.name=\"cp\" and evt.arg.filename startswith \"/mnt/elastic-internal/elasticsearch-config-local/\"\
369-
) or (container.image.repository=\"istio/proxyv2\" and proc.name=\"pilot-agent\"\
370-
\ and evt.arg.filename startswith \"/var/lib/docker/overlay2/\"))"
371-
append: false
372-
373258
- macro: "test_foo_bar"
374259
condition: "never_true"
375260
append: false

sdcclient/monitor/_events_v1.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,27 @@ def get_events(self, from_s=None, to_s=None, last_s=None):
3636
res = self.http.get(self.url + '/api/events/', headers=self.hdrs, params=params, verify=self.ssl_verify)
3737
return self._request_result(res)
3838

39+
def get_event(self, id):
40+
"""
41+
Retrieve an event using the ID
42+
Args:
43+
id(str): ID of the event to retrieve
44+
45+
Returns:
46+
A tuple where the first parameter indicates if the call was successful,
47+
and the second parameter holds either the error as string, or the event matching this ID.
48+
49+
Examples:
50+
>>> from sdcclient.monitor import EventsClientV1
51+
>>> client = EventsClientV1(token=SECURE_TOKEN)
52+
>>> ok, res = client.get_event(id='2343214984')
53+
>>> if ok:
54+
>>> print(res["event"])
55+
"""
56+
url = f'{self.url}/api/events/{id}'
57+
res = self.http.get(url, headers=self.hdrs, verify=self.ssl_verify)
58+
return self._request_result(res)
59+
3960
def post_event(self, name, description=None, severity=None, event_filter=None, tags=None):
4061
'''**Description**
4162
Send an event to Sysdig Monitor. The events you post are available in the Events tab in the Sysdig Monitor UI and can be overlied to charts.

sdcclient/monitor/_events_v2.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,28 @@ def get_events(self, name=None, category=None, direction='before', status=None,
7979
res = self.http.get(self.url + '/api/v2/events/', headers=self.hdrs, params=params, verify=self.ssl_verify)
8080
return self._request_result(res)
8181

82+
def get_event(self, id):
83+
"""
84+
Retrieve an event using the ID
85+
Args:
86+
id(str): ID of the event to retrieve
87+
88+
Returns:
89+
A tuple where the first parameter indicates if the call was successful,
90+
and the second parameter holds either the error as string, or the event matching this ID.
91+
92+
Examples:
93+
>>> from sdcclient.monitor import EventsClientV2
94+
>>> client = EventsClientV2(token=SECURE_TOKEN)
95+
>>> ok, res = client.get_event(id='2343214984')
96+
>>> if ok:
97+
>>> print(res["event"])
98+
"""
99+
100+
url = f'{self.url}/api/v2/events/{id}'
101+
res = self.http.get(url, headers=self.hdrs, verify=self.ssl_verify)
102+
return self._request_result(res)
103+
82104
def delete_event(self, event):
83105
'''**Description**
84106
Deletes an event.

specs/monitor/events_v1_spec.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import time
33

4-
from expects import expect, have_key, contain, have_keys, be_empty
4+
from expects import expect, have_key, contain, have_keys, be_empty, equal
55
from mamba import it, before, description
66

77
from sdcclient.monitor import EventsClientV1
@@ -18,6 +18,19 @@
1818
description="This event was created in a CI pipeline for the Python SDK library")
1919
expect(call).to(be_successful_api_call)
2020

21+
with it("is able to retrieve an event by ID"):
22+
ok, res = self.client.post_event(name=self.event_name,
23+
description="This event was created in a CI pipeline for the Python SDK library")
24+
expect((ok, res)).to(be_successful_api_call)
25+
26+
event = res["event"]
27+
event_id = event["id"]
28+
29+
ok, res = self.client.get_event(id=event_id)
30+
expect((ok, res)).to(be_successful_api_call)
31+
32+
expect(res["event"]).to(equal(event))
33+
2134
with it("is able to list the events happened without any filter"):
2235
time.sleep(3) # Wait for the event to appear in the feed
2336
ok, res = self.client.get_events()

specs/monitor/events_v2_spec.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@
3232
expect(res).to(have_key("events"))
3333
expect(res["events"]).to(contain(have_key("scope", equal("host.hostName = 'ci'"))))
3434

35+
with it("is able to retrieve an event by ID"):
36+
ok, res = self.client.post_event(name=self.event_name,
37+
description="This event was created in a CI pipeline for the Python SDK library")
38+
expect((ok, res)).to(be_successful_api_call)
39+
40+
event = res["event"]
41+
event_id = event["id"]
42+
43+
ok, res = self.client.get_event(id=event_id)
44+
expect((ok, res)).to(be_successful_api_call)
45+
46+
expect(res["event"]).to(equal(event))
47+
3548
with it("is able to list the events happened without any filter"):
3649
time.sleep(3) # Wait for the event to appear in the feed
3750
ok, res = self.client.get_events()

0 commit comments

Comments
 (0)