|
| 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