forked from sysdiglabs/sysdig-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_policies.py
More file actions
executable file
·66 lines (52 loc) · 1.38 KB
/
list_policies.py
File metadata and controls
executable file
·66 lines (52 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
#
# List the current set of secure policies.
#
import os
import sys
import json
import getopt
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
from sdcclient import SdSecureClient
def usage():
print 'usage: %s [-o|--order-only] <sysdig-token>' % sys.argv[0]
print '-o|--order-only: Only display the list of policy ids in evaluation order. Suitable for use by set_policy_order.py'
print 'You can find your token at https://secure.sysdig.com/#/settings/user'
sys.exit(1)
try:
opts, args = getopt.getopt(sys.argv[1:],"o",["order-only"])
except getopt.GetoptError:
usage()
order_only = False
for opt, arg in opts:
if opt in ("-o", "--order-only"):
order_only = True
#
# Parse arguments
#
if len(args) < 1:
usage()
sdc_token = args[0]
#
# Instantiate the SDC client
#
sdclient = SdSecureClient(sdc_token, 'https://secure.sysdig.com')
res = sdclient.get_policy_priorities()
if not res[0]:
print res[1]
sys.exit(1)
# Strip the surrounding json to only keep the list of policy ids
res[1] = res[1]['priorities']['policyIds']
if not order_only:
priorities = res[1]
res = sdclient.list_policies()
if res[0]:
res[1]['policies'].sort(key=lambda p: priorities.index(p['id']))
#
# Return the result
#
if res[0]:
print json.dumps(res[1], indent=2)
else:
print res[1]
sys.exit(1)