Skip to content

Commit ce6923b

Browse files
committed
Methods/examples to set/get falco rules files
Add the following methods to the python client: - {get,set}_default_falco_rules_files: a wrapper around the api endpoint /api/settings/defaultRulesFiles, handling PUT and GET. - load_default_falco_rules_files: load a collection of files on disk with a documented structure, returning a dict suitable for use in set_default_falco_rules_files() - save_default_falco_rules_files: given a dict from get_default_falco_rules_files, save it to a collection of files on disk with a documented structure Also add example programs {set,get}_secure_default_falco_rules.py. get_... has the ability to either print the returned set of files directly or save them using a --save <root dir path> option. Similarly, set_ has a --load <root dir path> option to load files from disk to a dict for the PUT /api/settings/defaultRulesFiles. set_ also has some easier-to-use command line options that allow setting a single file and tag.
1 parent 78ec26e commit ce6923b

File tree

3 files changed

+409
-0
lines changed

3 files changed

+409
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env python
2+
#
3+
# Get the sysdig secure default rules files.
4+
#
5+
# The _files programs and endpoints are a replacement for the
6+
# system_file endpoints and allow for publishing multiple files
7+
# instead of a single file as well as publishing multiple variants of
8+
# a given file that are compatible with different agent versions.
9+
#
10+
11+
import os
12+
import sys
13+
import pprint
14+
import getopt
15+
import shutil
16+
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
17+
from sdcclient import SdSecureClient
18+
19+
#
20+
# Parse arguments
21+
#
22+
def usage():
23+
print 'usage: %s [-s|--save <path>] <sysdig-token>' % sys.argv[0]
24+
print '-s|--save: save the retrieved files to a set of files below <path> using save_default_rules_files().'
25+
print 'You can find your token at https://secure.sysdig.com/#/settings/user'
26+
sys.exit(1)
27+
28+
try:
29+
opts, args = getopt.getopt(sys.argv[1:],"s:",["save="])
30+
except getopt.GetoptError:
31+
usage()
32+
33+
save_dir = ""
34+
for opt, arg in opts:
35+
if opt in ("-s", "--save"):
36+
save_dir = arg
37+
38+
#
39+
# Parse arguments
40+
#
41+
if len(args) != 1:
42+
usage()
43+
44+
sdc_token = args[0]
45+
46+
#
47+
# Instantiate the SDC client
48+
#
49+
sdclient = SdSecureClient(sdc_token, 'https://secure.sysdig.com')
50+
51+
#
52+
# Get the configuration
53+
#
54+
res = sdclient.get_default_falco_rules_files()
55+
56+
#
57+
# Return the result
58+
#
59+
if res[0]:
60+
if save_dir == "":
61+
pp = pprint.PrettyPrinter(indent=4)
62+
pp.pprint(res[1])
63+
else:
64+
print "Saving falco rules files below {}...".format(save_dir)
65+
sres = sdclient.save_default_falco_rules_files(res[1], save_dir)
66+
if not sres[0]:
67+
print sres[1]
68+
else:
69+
print res[1]
70+
sys.exit(1)
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/usr/bin/env python
2+
#
3+
# Set the sysdig secure default rules files.
4+
#
5+
# The _files programs and endpoints are a replacement for the
6+
# system_file endpoints and allow for publishing multiple files
7+
# instead of a single file as well as publishing multiple variants of
8+
# a given file that are compatible with different agent versions.
9+
#
10+
11+
import os
12+
import sys
13+
import pprint
14+
import getopt
15+
import shutil
16+
import yaml
17+
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
18+
from sdcclient import SdSecureClient
19+
20+
#
21+
# Parse arguments
22+
#
23+
def usage():
24+
print 'usage: %s [-l|--load <path>] [-t|--tag <tag>] [-c|--content <content>] <sysdig-token>' % sys.argv[0]
25+
print '-l|--load: load the files to set from a set of files below <path> using load_default_rules_files().'
26+
print '-t|--tag: Set a tag for the set of files'
27+
print '-c|--content: the (single) file to set'
28+
print 'if --load is specified, neither --tag nor --content can be specified'
29+
print 'You can find your token at https://secure.sysdig.com/#/settings/user'
30+
sys.exit(1)
31+
32+
try:
33+
opts, args = getopt.getopt(sys.argv[1:],"l:t:n:c:",["load=","tag=","name=","content="])
34+
except getopt.GetoptError:
35+
usage()
36+
37+
load_dir = ""
38+
tag = ""
39+
cpath = ""
40+
for opt, arg in opts:
41+
if opt in ("-l", "--load"):
42+
load_dir = arg
43+
elif opt in ("-t", "--tag"):
44+
tag = arg
45+
elif opt in ("-c", "--content"):
46+
cpath = arg
47+
48+
if load_dir != "" and (tag != "" or cpath != ""):
49+
usage()
50+
#
51+
# Parse arguments
52+
#
53+
if len(args) != 1:
54+
usage()
55+
56+
sdc_token = args[0]
57+
58+
#
59+
# Instantiate the SDC client
60+
#
61+
sdclient = SdSecureClient(sdc_token, 'https://secure.sysdig.com')
62+
63+
files_obj = {}
64+
if load_dir != "":
65+
print "Loading falco rules files from {}...".format(load_dir)
66+
res = sdclient.load_default_falco_rules_files(load_dir)
67+
if res[0]:
68+
files_obj = res[1]
69+
else:
70+
print res[1]
71+
sys.exit(1)
72+
else:
73+
with open(cpath, 'r') as content_file:
74+
content = content_file.read()
75+
required_engine_version = 0
76+
cyaml = yaml.load(content)
77+
for obj in cyaml:
78+
if "required_engine_version" in obj:
79+
try:
80+
required_engine_version = int(obj["required_engine_version"])
81+
except ValueError:
82+
print("Required engine version \"{}\" in content {} must be a number".format(obj["required_engine_version"], cpath))
83+
sys.exit(1)
84+
files_obj = {
85+
"tag": tag,
86+
"files": [{
87+
"name": os.path.basename(cpath),
88+
"variants": {
89+
"required_engine_version": required_engine_version,
90+
"content": content
91+
}
92+
}]
93+
}
94+
95+
res = sdclient.set_default_falco_rules_files(files_obj)
96+
97+
#
98+
# Return the result
99+
#
100+
if res[0]:
101+
print 'default falco rules files set successfully'
102+
else:
103+
print res[1]
104+
sys.exit(1)

0 commit comments

Comments
 (0)