Skip to content

Commit 988a44e

Browse files
authored
Add policy priorities (#45)
* Add API wrappers to set/get policy priorities. New methods get_policy_priorities/set_policy_priorites map to GET /api/policies/priorities, PUT /api/policies/priorities, respsectively. They work on raw json objects. Some minimal massaging of the objects will be handled in the example programs. * Update usage string. It doesn't read the policy name from the cmdline, and reads the policy description from stdin. * Add priorities order to list_policies Add the ability to return the order of policies only instead of the full list of priorities. With -o/--order-only, only the list of ids is returned. Also ensure that the list of priorities is always sorted in priorities order. * Wrapper program to set policy order. Example program to set the order of secure policies. Takes a simple list of policy ids as returned by list_policies --order-only, and wraps it in the necessary version/date object before using set_policy_priorities. * Add tests for new policy priorities apis Use list_policies -o to get the current policies order, reverse the order using "jq reverse", and set the order. Use grep -b to find where in the policies list the policy "Write Below Binary Dir" is, before and after setting the order. You should find that the position of the policy moves to farther in the output e.g. the end. Also use tee when fetching the falco rules file so if there's an error we will see it. * Update monitor tests to use python-sdc-client acct Move all of the monitor unit tests from .travis.yml into a script test_monitor_apis.sh, and get rid of the (commented-out) tests for the pip version of sdcclient. In test_monitor_apis.sh, instead of relying on a hard-coded hostname, start an agent on the fly using docker and use the current hostname for get_data_advanced/create_sysdig_capture. Also make changes to let multiple copies of test script run at once: Many of the test programs relied on hard coded names of alerts, dashboards, notification channels, etc. This caused problems when running two copies of the test script at once, for example for the travisci push and pr tests. Fix this by making the various alert/dashboard/channel names configurable in each example script, and using unique names in the wrapper script that calls the individual scripts.
1 parent 19543ed commit 988a44e

16 files changed

+404
-108
lines changed

.travis.yml

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,62 +7,8 @@ install:
77
- sudo apt-get install linux-headers-$(uname -r) dkms gcc-multilib g++-multilib
88
- pip install pyyaml requests
99
script:
10-
- echo "Testing source version"
11-
- examples/create_alert.py $DEMO_KUBE_API_TOKEN
12-
- examples/update_alert.py $DEMO_KUBE_API_TOKEN
13-
- examples/delete_alert.py $DEMO_KUBE_API_TOKEN
14-
- examples/dashboard.py $DEMO_KUBE_API_TOKEN
15-
- examples/create_dashboard.py $DEMO_KUBE_API_TOKEN
16-
- examples/delete_dashboard.py $DEMO_KUBE_API_TOKEN
17-
- examples/get_data_advanced.py $DEMO_KUBE_API_TOKEN ip-10-0-1-140.ec2.internal
18-
- examples/get_data_datasource.py $DEMO_KUBE_API_TOKEN
19-
- examples/get_data_simple.py $DEMO_KUBE_API_TOKEN
20-
- examples/list_alerts.py $DEMO_KUBE_API_TOKEN
21-
- examples/list_alert_notifications.py $DEMO_KUBE_API_TOKEN
22-
- examples/resolve_alert_notifications.py $DEMO_KUBE_API_TOKEN 1
23-
- examples/list_dashboards.py $DEMO_KUBE_API_TOKEN
24-
- examples/list_hosts.py $DEMO_KUBE_API_TOKEN
25-
- examples/list_metrics.py $DEMO_KUBE_API_TOKEN
26-
- examples/post_event.py $DEMO_KUBE_API_TOKEN "test_event_name" -d "test event description"
27-
- examples/post_event_simple.py $DEMO_KUBE_API_TOKEN "test_event_name" "test event description"
28-
- examples/list_events.py $DEMO_KUBE_API_TOKEN
29-
- examples/delete_event.py $DEMO_KUBE_API_TOKEN
30-
- examples/print_data_retention_info.py $DEMO_KUBE_API_TOKEN
31-
- examples/print_explore_grouping.py $DEMO_KUBE_API_TOKEN
32-
- examples/print_user_info.py $DEMO_KUBE_API_TOKEN
33-
- examples/list_users.py $DEMO_KUBE_API_TOKEN
34-
- examples/list_sysdig_captures.py $DEMO_KUBE_API_TOKEN
35-
- examples/create_sysdig_capture.py $DEMO_KUBE_API_TOKEN ip-10-0-1-115.ec2.internal apicapture 10
36-
- examples/notification_channels.py $DEMO_KUBE_API_TOKEN
37-
- examples/user_team_mgmt.py $DEMO_KUBE_API_TOKEN example-team [email protected]
38-
- unset SDC_TOKEN
10+
- bash test/test_monitor_apis.sh
3911
- bash test/test_secure_apis.sh
40-
- echo "Testing pip version"
41-
- rm -rf sdcclient
42-
- pip install sdcclient
43-
# - examples/create_alert.py $DEMO_KUBE_API_TOKEN
44-
# - examples/delete_alert.py $DEMO_KUBE_API_TOKEN
45-
# - examples/dashboard.py $DEMO_KUBE_API_TOKEN
46-
# - examples/create_dashboard.py $DEMO_KUBE_API_TOKEN
47-
# - examples/delete_dashboard.py $DEMO_KUBE_API_TOKEN
48-
# - examples/get_data_advanced.py $DEMO_KUBE_API_TOKEN ip-10-0-2-180.ec2.internal
49-
# - examples/get_data_datasource.py $DEMO_KUBE_API_TOKEN
50-
# - examples/get_data_simple.py $DEMO_KUBE_API_TOKEN
51-
# - examples/list_alerts.py $DEMO_KUBE_API_TOKEN
52-
# - examples/list_alert_notifications.py $DEMO_KUBE_API_TOKEN
53-
# - examples/resolve_alert_notifications.py $DEMO_KUBE_API_TOKEN 1
54-
# - examples/list_dashboards.py $DEMO_KUBE_API_TOKEN
55-
# - examples/list_hosts.py $DEMO_KUBE_API_TOKEN
56-
# - examples/list_metrics.py $DEMO_KUBE_API_TOKEN
57-
# - examples/post_event.py $DEMO_KUBE_API_TOKEN "test event name" -d "test event description"
58-
# - examples/post_event_simple.py $DEMO_KUBE_API_TOKEN "test event name" "test event description"
59-
# - examples/list_events.py $DEMO_KUBE_API_TOKEN
60-
# - examples/delete_event.py $DEMO_KUBE_API_TOKEN
61-
# - examples/print_data_retention_info.py $DEMO_KUBE_API_TOKEN
62-
# - examples/print_explore_grouping.py $DEMO_KUBE_API_TOKEN
63-
# - examples/print_user_info.py $DEMO_KUBE_API_TOKEN
64-
# - examples/list_sysdig_captures.py $DEMO_KUBE_API_TOKEN
65-
# - examples/create_sysdig_capture.py $DEMO_KUBE_API_TOKEN ip-10-0-2-202.ec2.internal apicapture 10
6612
notifications:
6713
slack:
6814
secure: GJ0H2wAaW67t3+x+cCjOVLw/b+YAZjH9rebAfluCFJklJS9u+V6/qqIyiNDAkMPIcgilFyhiyOQCZYXapgthQ1ieFbZZo//mrRtuGo2wuxCAwcOx2mXAZpZ4I5b3+Q/znVqSuqkwFRid1d138z4TW7sYSIburouDX3QUNoUOy+g7VJxCFQCcqlN8LYxGJHQYdOZa9zIGCtKrOtZ/B8C1TLgXmDMwAAVNO2WzL4GiBTLCGuMsQWMTLw2Qmv1ayZPztmeDWo1C9oa7HIH8Bg2YVjssR87el28X+EqEO533mgYjPmW2/hii30WVFOUE5hMdvKeQLBvy5N3/OCch1np0RQBd8eYEtaPv38rc5L2wAnUq9G5Zzr252z7vnwSLi6lap9jWU8tOerSTEPU+jG05PnuCnufVDXVNPyiPsH6BDP4qxHmLjooNpxfe63Df7NNyUi2I3QoroLj/UzI7zZVQjJEqsTrr5BbsH4z6NTGY91+ZqobBn62+hV3ESAam0ivQgC7s2AKko0qkKyIUGjj7ozU8ebo1UpagNvKC/J9szMqtdXJgKtG8BeonyLMeN6MEEyEvcMJbB4dCcfet+1Sb9AZWnGvYVdajhVLb1HE6OrbzZyhC3KqCe06J9O5BCY9ncy+l16i7MyIfcKTibHQlxPU+Id/VijD97JSRXxnd2i4=

examples/create_alert.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#
77
#
88

9+
import getopt
910
import os
1011
import sys
1112
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
@@ -14,12 +15,26 @@
1415
#
1516
# Parse arguments
1617
#
17-
if len(sys.argv) != 2:
18-
print 'usage: %s <sysdig-token>' % sys.argv[0]
18+
def usage():
19+
print 'usage: %s [-a|--alert <name>] <sysdig-token>' % sys.argv[0]
20+
print '-a|--alert: Set name of alert to create'
1921
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
2022
sys.exit(1)
2123

22-
sdc_token = sys.argv[1]
24+
try:
25+
opts, args = getopt.getopt(sys.argv[1:],"a:",["alert="])
26+
except getopt.GetoptError:
27+
usage()
28+
29+
alert_name = "tomcat cpu > 80% on any host"
30+
for opt, arg in opts:
31+
if opt in ("-a", "--alert"):
32+
alert_name = arg
33+
34+
if len(args) != 1:
35+
usage()
36+
37+
sdc_token = args[0]
2338

2439
#
2540
# Instantiate the SDC client
@@ -29,8 +44,8 @@
2944
#
3045
# Find notification channels (you need IDs to create an alert).
3146
#
32-
notify_channels = [ {'type': 'SLACK', 'channel': 'sysdig-demo2-alerts'},
33-
{'type': 'EMAIL', 'emailRecipients': ['demo-kube@draios.com', '[email protected]']},
47+
notify_channels = [ {'type': 'SLACK', 'channel': '#python-sdc-test-alert'},
48+
{'type': 'EMAIL', 'emailRecipients': ['python-sdc-testing@draios.com', '[email protected]']},
3449
{'type': 'SNS', 'snsTopicARNs': ['arn:aws:sns:us-east-1:273107874544:alarms-stg']}
3550
]
3651

@@ -44,7 +59,7 @@
4459
#
4560
# Create the alert.
4661
#
47-
res = sdclient.create_alert('tomcat cpu > 80% on any host', # Alert name.
62+
res = sdclient.create_alert(alert_name, # Alert name.
4863
'this alert was automatically created using the python Sysdig Cloud library', # Alert description.
4964
6, # Syslog-encoded severity. 6 means 'info'.
5065
60, # The alert will fire if the condition is met for at least 60 seconds.

examples/create_dashboard.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# will monitor.
77
#
88

9+
import getopt
910
import os
1011
import sys
1112
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
@@ -14,12 +15,27 @@
1415
#
1516
# Parse arguments
1617
#
17-
if len(sys.argv) != 2:
18-
print 'usage: %s <sysdig-token>' % sys.argv[0]
18+
def usage():
19+
print 'usage: %s [-d|--dashboard <name>] <sysdig-token>' % sys.argv[0]
20+
print '-d|--dashboard: Set name of dashboard to create'
1921
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
2022
sys.exit(1)
2123

22-
sdc_token = sys.argv[1]
24+
try:
25+
opts, args = getopt.getopt(sys.argv[1:],"d:",["dashboard="])
26+
except getopt.GetoptError:
27+
usage()
28+
29+
# Name for the dashboard to create
30+
dashboardName = "API test - cassandra in prod"
31+
for opt, arg in opts:
32+
if opt in ("-d", "--dashboard"):
33+
dashboardName = arg
34+
35+
if len(args) != 1:
36+
usage()
37+
38+
sdc_token = args[0]
2339

2440
#
2541
# Instantiate the SDC client
@@ -30,8 +46,6 @@
3046
# Create the new dashboard, applying to cassandra in production
3147
#
3248

33-
# Name for the dashboard to create
34-
dashboardName = "API test - cassandra in prod"
3549
# Name of the view to copy
3650
viewName = "Overview by Process"
3751
# Filter to apply to the new dashboard.
@@ -56,14 +70,12 @@
5670
# the dev namespace
5771
#
5872

59-
# Name for the dashboard to create
60-
dashboardName = "API test - cassandra in dev"
6173
# Name of the dashboard to copy
62-
dashboardToCopy = "API test - cassandra in prod"
74+
dashboardCopy = "Copy Of {}".format(dashboardName)
6375
# Filter to apply to the new dashboard. Same as above.
6476
dashboardFilter = "kubernetes.namespace.name = dev and proc.name = cassandra"
6577

66-
res = sdclient.create_dashboard_from_dashboard(dashboardName, dashboardToCopy, dashboardFilter)
78+
res = sdclient.create_dashboard_from_dashboard(dashboardCopy, dashboardName, dashboardFilter)
6779

6880
#
6981
# Check the result

examples/dashboard.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# edit the content, and then delete it.
55
#
66

7+
import getopt
78
import os
89
import sys
910
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
@@ -12,12 +13,26 @@
1213
#
1314
# Parse arguments
1415
#
15-
if len(sys.argv) != 2:
16-
print 'usage: %s <sysdig-token>' % sys.argv[0]
16+
def usage():
17+
print 'usage: %s [-d|--dashboard <name>] <sysdig-token>' % sys.argv[0]
18+
print '-d|--dashboard: Set name of dashboard to create'
1719
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
1820
sys.exit(1)
1921

20-
sdc_token = sys.argv[1]
22+
try:
23+
opts, args = getopt.getopt(sys.argv[1:],"d:",["dashboard="])
24+
except getopt.GetoptError:
25+
usage()
26+
27+
dashboard_name = "My Dashboard"
28+
for opt, arg in opts:
29+
if opt in ("-d", "--dashboard"):
30+
dashboard_name = arg
31+
32+
if len(args) != 1:
33+
usage()
34+
35+
sdc_token = args[0]
2136

2237
#
2338
# Instantiate the SDC client
@@ -28,7 +43,6 @@
2843
#
2944
# Create an empty dashboard
3045
#
31-
dashboard_name = 'My Dashboard'
3246
dashboard_configuration = None
3347
res = sdclient.create_dashboard(dashboard_name)
3448

@@ -142,4 +156,4 @@
142156
print 'Dashboard deleted successfully'
143157
else:
144158
print res[1]
145-
sys.exit(1)
159+
sys.exit(1)

examples/delete_alert.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# This example shows how to delete an alert
44
#
55

6+
import getopt
67
import os
78
import sys
89
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
@@ -11,12 +12,26 @@
1112
#
1213
# Parse arguments
1314
#
14-
if len(sys.argv) != 2:
15-
print 'usage: %s <sysdig-token>' % sys.argv[0]
15+
def usage():
16+
print 'usage: %s [-a|--alert <name>] <sysdig-token>' % sys.argv[0]
17+
print '-a|--alert: Set name of alert to delete'
1618
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
1719
sys.exit(1)
1820

19-
sdc_token = sys.argv[1]
21+
try:
22+
opts, args = getopt.getopt(sys.argv[1:],"a:",["alert="])
23+
except getopt.GetoptError:
24+
usage()
25+
26+
alert_name = "tomcat cpu > 80% on any host"
27+
for opt, arg in opts:
28+
if opt in ("-a", "--alert"):
29+
alert_name = arg
30+
31+
if len(args) != 1:
32+
usage()
33+
34+
sdc_token = args[0]
2035

2136
#
2237
# Instantiate the SDC client
@@ -29,7 +44,7 @@
2944
sys.exit(1)
3045

3146
for alert in res[1]['alerts']:
32-
if alert['name'] == "tomcat cpu > 80% on any host":
47+
if alert['name'] == alert_name:
3348
print "Deleting alert"
3449
res = sdclient.delete_alert(alert)
3550
if not res[0]:

examples/delete_dashboard.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# This example shows how to delete a dashboard
44
#
55

6+
import getopt
67
import os
78
import sys
89
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
@@ -11,12 +12,26 @@
1112
#
1213
# Parse arguments
1314
#
14-
if len(sys.argv) != 2:
15-
print 'usage: %s <sysdig-token>' % sys.argv[0]
15+
def usage():
16+
print 'usage: %s [-p|--pattern <name>] <sysdig-token>' % sys.argv[0]
17+
print '-p|--pattern: Delete all dashboards containing the provided pattern'
1618
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
1719
sys.exit(1)
1820

19-
sdc_token = sys.argv[1]
21+
try:
22+
opts, args = getopt.getopt(sys.argv[1:],"p:",["pattern="])
23+
except getopt.GetoptError:
24+
usage()
25+
26+
pattern = "API Test"
27+
for opt, arg in opts:
28+
if opt in ("-p", "--pattern"):
29+
pattern = arg
30+
31+
if len(args) != 1:
32+
usage()
33+
34+
sdc_token = args[0]
2035

2136
#
2237
# Instantiate the SDC client
@@ -32,10 +47,10 @@
3247
sys.exit(1)
3348

3449
#
35-
# Delete all the dashboards containing "API test"
50+
# Delete all the dashboards containing pattern
3651
#
3752
for dashboard in res[1]['dashboards']:
38-
if 'API test' in dashboard['name']:
53+
if pattern in dashboard['name']:
3954
print "Deleting " + dashboard['name']
4055
res = sdclient.delete_dashboard(dashboard)
4156
if not res[0]:

examples/delete_event.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Delete user events from Sysdig Cloud
44
#
55

6+
import getopt
67
import json
78
import os
89
import sys
@@ -12,12 +13,26 @@
1213
#
1314
# Parse arguments
1415
#
15-
if len(sys.argv) != 2:
16-
print 'usage: %s <sysdig-token>' % sys.argv[0]
16+
def usage():
17+
print 'usage: %s [-e|--event <name>] <sysdig-token>' % sys.argv[0]
18+
print '-e|--event: Name of event to delete'
1719
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
1820
sys.exit(1)
1921

20-
sdc_token = sys.argv[1]
22+
try:
23+
opts, args = getopt.getopt(sys.argv[1:],"e:",["event="])
24+
except getopt.GetoptError:
25+
usage()
26+
27+
event_name = "test_event_name"
28+
for opt, arg in opts:
29+
if opt in ("-e", "--event"):
30+
event_name = arg
31+
32+
if len(args) != 1:
33+
usage()
34+
35+
sdc_token = args[0]
2136

2237
#
2338
# Instantiate the SDC client
@@ -27,7 +42,7 @@
2742
#
2843
# Get the events that match a name
2944
#
30-
res = sdclient.get_events(name='test_event_name')
45+
res = sdclient.get_events(name=event_name)
3146

3247
if not res[0]:
3348
print res[1]

0 commit comments

Comments
 (0)