Skip to content

Commit 5904ef5

Browse files
muntzerrDavide Schiera
authored andcommitted
Add support for policy API version 2 (#99)
1 parent cfe47d0 commit 5904ef5

16 files changed

+1098
-125
lines changed

examples/add_policy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def usage():
3232
#
3333
sdclient = SdSecureClient(sdc_token, 'https://secure.sysdig.com')
3434

35-
ok, res = sdclient.add_policy(policy_json)
35+
ok, res = sdclient.add_policy_json(policy_json)
3636

3737
#
3838
# Return the result

examples/add_policy_v1.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
#
3+
# Add a new policy
4+
#
5+
6+
import os
7+
import sys
8+
import json
9+
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
10+
from sdcclient import SdSecureClientV1
11+
12+
13+
def usage():
14+
print('usage: %s <sysdig-token>' % sys.argv[0])
15+
print('Reads policy json from standard input')
16+
print('You can find your token at https://secure.sysdig.com/#/settings/user')
17+
sys.exit(1)
18+
19+
20+
#
21+
# Parse arguments
22+
#
23+
if len(sys.argv) != 2:
24+
usage()
25+
26+
sdc_token = sys.argv[1]
27+
28+
policy_json = sys.stdin.read()
29+
30+
#
31+
# Instantiate the SDC client
32+
#
33+
sdclient = SdSecureClientV1(sdc_token, 'https://secure.sysdig.com')
34+
35+
ok, res = sdclient.add_policy(policy_json)
36+
37+
#
38+
# Return the result
39+
#
40+
if ok:
41+
print(json.dumps(res, indent=2))
42+
else:
43+
print(res)
44+
sys.exit(1)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
#
3+
# Create the default set of policies given the falco rules file.
4+
# Existing policies with the same name are unchanged. New policies
5+
# as needed will be added. Returns JSON representing the new
6+
# policies created.
7+
#
8+
9+
import os
10+
import sys
11+
import json
12+
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
13+
from sdcclient import SdSecureClientV1
14+
15+
16+
def usage():
17+
print('usage: %s <sysdig-token>' % sys.argv[0])
18+
print('You can find your token at https://secure.sysdig.com/#/settings/user')
19+
sys.exit(1)
20+
21+
22+
#
23+
# Parse arguments
24+
#
25+
if len(sys.argv) != 2:
26+
usage()
27+
28+
sdc_token = sys.argv[1]
29+
30+
#
31+
# Instantiate the SDC client
32+
#
33+
sdclient = SdSecureClientV1(sdc_token, 'https://secure.sysdig.com')
34+
35+
ok, res = sdclient.create_default_policies()
36+
37+
#
38+
# Return the result
39+
#
40+
if ok:
41+
print(json.dumps(res, indent=2))
42+
else:
43+
print(res)
44+
sys.exit(1)

examples/delete_all_policies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def usage():
3737
print(res)
3838
sys.exit(1)
3939
else:
40-
policies = res['policies']
40+
policies = res
4141

4242
for policy in policies:
4343
print("deleting policy: " + str(policy['id']))

examples/delete_all_policies_v1.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python
2+
#
3+
# Delete all secure policies.
4+
#
5+
6+
import os
7+
import sys
8+
import json
9+
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
10+
from sdcclient import SdSecureClientV1
11+
12+
13+
def usage():
14+
print('usage: %s <sysdig-token>' % sys.argv[0])
15+
print('You can find your token at https://secure.sysdig.com/#/settings/user')
16+
sys.exit(1)
17+
18+
19+
#
20+
# Parse arguments
21+
#
22+
if len(sys.argv) != 2:
23+
usage()
24+
25+
sdc_token = sys.argv[1]
26+
27+
#
28+
# Instantiate the SDC client
29+
#
30+
sdclient = SdSecureClientV1(sdc_token, 'https://secure.sysdig.com')
31+
32+
# Get a list of policyIds
33+
ok, res = sdclient.list_policies()
34+
policies = []
35+
36+
if not ok:
37+
print(res)
38+
sys.exit(1)
39+
else:
40+
policies = res['policies']
41+
42+
for policy in policies:
43+
print("deleting policy: " + str(policy['id']))
44+
ok, res = sdclient.delete_policy_id(policy['id'])
45+
if not ok:
46+
print(res)
47+
sys.exit(1)

examples/delete_policy_v1.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env python
2+
#
3+
# Delete a policy, by either id or name.
4+
#
5+
6+
import os
7+
import sys
8+
import json
9+
import getopt
10+
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
11+
from sdcclient import SdSecureClientV1
12+
13+
14+
def usage():
15+
print('usage: %s [-i|--id <id>] [-n|--name <name>] <sysdig-token>' % sys.argv[0])
16+
print('-i|--id: the id of the policy to delete')
17+
print('-n|--name: the name of the policy to delete')
18+
print('You can find your token at https://secure.sysdig.com/#/settings/user')
19+
sys.exit(1)
20+
21+
22+
#
23+
# Parse arguments
24+
#
25+
try:
26+
opts, args = getopt.getopt(sys.argv[1:], "i:n:", ["id=", "name="])
27+
except getopt.GetoptError:
28+
usage()
29+
30+
id = ""
31+
name = ""
32+
for opt, arg in opts:
33+
if opt in ("-i", "--id"):
34+
id = arg
35+
elif opt in ("-n", "--name"):
36+
name = arg
37+
38+
if len(id) + len(name) == 0:
39+
usage()
40+
41+
if len(args) < 1:
42+
usage()
43+
44+
sdc_token = args[0]
45+
46+
#
47+
# Instantiate the SDC client
48+
#
49+
sdclient = SdSecureClientV1(sdc_token, 'https://secure.sysdig.com')
50+
51+
if len(id) > 0:
52+
ok, res = sdclient.delete_policy_id(id)
53+
else:
54+
ok, res = sdclient.delete_policy_name(name)
55+
56+
#
57+
# Return the result
58+
#
59+
if ok:
60+
print(json.dumps(res, indent=2))
61+
else:
62+
print(res)
63+
sys.exit(1)

examples/get_policy_v1.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python
2+
#
3+
# Get a specific policy
4+
#
5+
6+
import os
7+
import sys
8+
import json
9+
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
10+
from sdcclient import SdSecureClientV1
11+
12+
13+
def usage():
14+
print('usage: %s <sysdig-token> <policy name>' % sys.argv[0])
15+
print('You can find your token at https://secure.sysdig.com/#/settings/user')
16+
sys.exit(1)
17+
18+
19+
#
20+
# Parse arguments
21+
#
22+
if len(sys.argv) != 3:
23+
usage()
24+
25+
sdc_token = sys.argv[1]
26+
name = sys.argv[2]
27+
28+
#
29+
# Instantiate the SDC client
30+
#
31+
sdclient = SdSecureClientV1(sdc_token, 'https://secure.sysdig.com')
32+
33+
ok, res = sdclient.get_policy(name)
34+
35+
#
36+
# Return the result
37+
#
38+
if ok:
39+
print(json.dumps(res, indent=2))
40+
else:
41+
print(res)
42+
sys.exit(1)

examples/list_policies.py

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,50 +12,30 @@
1212

1313

1414
def usage():
15-
print('usage: %s [-o|--order-only] <sysdig-token>' % sys.argv[0])
16-
print('-o|--order-only: Only display the list of policy ids in evaluation order. Suitable for use by set_policy_order.py')
15+
print('usage: %s <sysdig-token>' % sys.argv[0])
1716
print('You can find your token at https://secure.sysdig.com/#/settings/user')
1817
sys.exit(1)
1918

2019

21-
try:
22-
opts, args = getopt.getopt(sys.argv[1:], "o", ["order-only"])
23-
except getopt.GetoptError:
24-
usage()
25-
26-
order_only = False
27-
for opt, arg in opts:
28-
if opt in ("-o", "--order-only"):
29-
order_only = True
30-
3120
#
3221
# Parse arguments
3322
#
34-
if len(args) < 1:
23+
if len(sys.argv) != 2:
3524
usage()
3625

37-
sdc_token = args[0]
26+
sdc_token = sys.argv[1]
3827

3928
#
4029
# Instantiate the SDC client
4130
#
4231
sdclient = SdSecureClient(sdc_token, 'https://secure.sysdig.com')
4332

44-
ok, res = sdclient.get_policy_priorities()
33+
ok, res = sdclient.list_policies()
4534

4635
if not ok:
4736
print(res)
4837
sys.exit(1)
4938

50-
# Strip the surrounding json to only keep the list of policy ids
51-
res = res['priorities']['policyIds']
52-
53-
if not order_only:
54-
priorities = res
55-
ok, res = sdclient.list_policies()
56-
if ok:
57-
res['policies'].sort(key=lambda p: priorities.index(p['id']))
58-
5939
#
6040
# Return the result
6141
#

examples/list_policies_v1.py

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

0 commit comments

Comments
 (0)