def header(args): """make a csv file with Just a header""" # import pdb; pdb.set_trace() types = ["elements", "properties", "relations"] if args.csvtype not in types: shlog.error("%s is not one of %s" % (args.csvtype, types)) exit(1) if args.csvtype == "elements": hdr = '"ID","Type","Name","Documentation"' elif args.csvtype == "properties": hdr = '"ID","Key","Value"' else: #relations hdr = '"ID","Type","Name","Documentation","Source","Target"' sqldbfile = open(args.prefix + args.csvtype + ".csv", 'w') sqldbfile.write(hdr + '\n') sqldbfile.close()
def check_lambda_code_bucket_exists(args, bucket): # Check that there is an appropriatly named code bucket # for each region we will deploy to. # to-do check that it's in the region it's name implies. try: args.s3.meta.client.head_bucket(Bucket=bucket) shlog.normal("prerequisite bucket {} exists".format(bucket)) return True except botocore.exceptions.ClientError as e: # If a client error is thrown, then check that it was a 404 error. # If it was a 404 error, then the bucket does not exist. error_code = int(e.response['Error']['Code']) if error_code == 403: shlog.error("cannot access pre-requisite bucket {}".format(bucket)) return False elif error_code == 404: shlog.error("pre-requisite bucket {} does not exist".format(bucket)) return False
def get_legal_elements(args): # get a list of element Types that occur within the perfect view shlog.normal("about to open %s", args.dbfile) con = sqlite3.connect(args.dbfile) curs = con.cursor() try: sql = """SELECT DISTINCT e.Type FROM FOLDER f INNER JOIN VIEWS v on v.Parent_folder_id = f.ID INNER JOIN VIEW_OBJECTS vo on vo.View_id = v.Id INNER JOIN ELEMENTS e on e.Id = vo.Object_id WHERE f.name = 'Viewpoint Definitions' and v.Name = 'F1LL3R'""".replace('F1LL3R', args.perfect_view) except TypeError: shlog.error('TypeError thrown! Check if --perfect_view had been provided.') exit(0) shlog.verbose(sql) curs.execute(sql) rows = curs.fetchall() element_list = [elem[0] for elem in rows] return element_list
def get_all_elements(args): # get all elements that appear within the views found by the search_term shlog.normal("about to open %s", args.dbfile) con = sqlite3.connect(args.dbfile) curs = con.cursor() try: sql = """SELECT DISTINCT e.Type FROM FOLDER f INNER JOIN VIEWS v on v.Parent_folder_id = f.ID INNER JOIN VIEW_OBJECTS vo on vo.View_id = v.Id INNER JOIN ELEMENTS e on e.Id = vo.Object_id WHERE f.Depth LIKE '%F1LL3R%'""".replace('F1LL3R', args.search_term) except TypeError: shlog.error('TypeError thrown! Check if --search_term had been provided.') exit(0) shlog.verbose(sql) curs.execute(sql) rows = curs.fetchall() element_list = [elem[0] for elem in rows] return element_list
def get_offending_elements(args, type): # get a list of element Types that occur within the perfect view shlog.normal("about to open %s", args.dbfile) con = sqlite3.connect(args.dbfile) curs = con.cursor() try: sql = """SELECT DISTINCT e.Name as Element, v.Name as View FROM FOLDER f INNER JOIN VIEWS v on v.Parent_folder_id = f.ID INNER JOIN VIEW_OBJECTS vo on vo.View_id = v.Id INNER JOIN ELEMENTS e on e.Id = vo.Object_id WHERE f.Depth LIKE '%F1LL3R1%' and e.Type = 'F1LL3R2'""".replace('F1LL3R1', args.search_term)\ .replace('F1LL3R2', type) except TypeError: shlog.error('TypeError thrown! Check if --search_term had been provided.') exit(0) shlog.verbose(sql) curs.execute(sql) rows = curs.fetchall() details = [list(elem) for elem in rows] return details
def get_all_relations(args): # return a relations as they appear in view found by the search_term shlog.normal("about to open %s", args.dbfile) con = sqlite3.connect(args.dbfile) curs = con.cursor() try: sql = """SELECT DISTINCT e1.Type as Source, r.Type as Relationship, e2.Type as Target FROM FOLDER f INNER JOIN VIEWS v on v.Parent_folder_id = f.ID INNER JOIN CONNECTIONS c on c.view_id = v.Id INNER JOIN RELATIONS r on r.ID = c.relationship_id INNER JOIN ELEMENTS e1 on e1.Id = r.Source INNER JOIN ELEMENTS e2 on e2.Id = r.Target WHERE f.Depth LIKE '%F1LL3R%'""".replace('F1LL3R', args.search_term) except TypeError: shlog.error('TypeError thrown! Check if --search_term had been provided.') exit(0) shlog.verbose(sql) curs.execute(sql) rows = curs.fetchall() relation_list = [list(elem) for elem in rows] return relation_list
def get_legal_relations(args): # return a relations as set by the perfect view shlog.normal("about to open %s", args.dbfile) con = sqlite3.connect(args.dbfile) curs = con.cursor() try: sql = """SELECT DISTINCT e1.Type as Source, r.Type as Relationship, e2.Type as Target FROM FOLDER f INNER JOIN VIEWS v on v.Parent_folder_id = f.ID INNER JOIN CONNECTIONS c on c.view_id = v.Id INNER JOIN RELATIONS r on r.ID = c.relationship_id INNER JOIN ELEMENTS e1 on e1.Id = r.Source INNER JOIN ELEMENTS e2 on e2.Id = r.Target WHERE f.name = 'Viewpoint Definitions' and v.Name = 'F1LL3R'""".replace('F1LL3R', args.perfect_view) except TypeError: shlog.error('TypeError thrown! Check if --perfect_view had been provided.') exit(0) shlog.verbose(sql) curs.execute(sql) rows = curs.fetchall() relation_list = [list(elem) for elem in rows] return relation_list
def main(args): "perform (or dry run) the rdk create" if os.path.isdir(args.rulename): shlog.error("Exiting: {} is an existing rule".format(args.rulename)) exit(1) # Specify the default taglist in plain python.. default_json_tag_list = ''' [ {"Key" : "Criticality","Value" : "Development"}, {"Key" : "Service" ,"Value" : "OpSec"} ] ''' # reformat to RDK's needs # - rdk demands a double encoded string. # - change to binary to remove white space, then double encode. default_json_tag_list = json.loads(default_json_tag_list) default_json_tag_list = json.dumps(default_json_tag_list) default_json_tag_list = json.dumps(default_json_tag_list) shlog.normal("setting default tags:{}".format(default_json_tag_list)) # # Collect the CI's needed for the rule # cmd = '''rdk sample-ci sas 2>&1 | tr , "\\n" | grep -i '{}' '''.format(args.cipattern) status = os.system(cmd) shlog.normal(status) cis = input("Enter comma separated CI(s)>") ci_create_option = "-r {}".format(cis) # actually create the project. ) cmd = "rdk create {} -R python3.6 --tags {} {}".format(args.rulename, default_json_tag_list, ci_create_option) execute_or_not(args, cmd) # dump a sample ci in the project directory for ci in cis.split(","): cmd = "cd {}; rdk sample-ci {} > {}.json".format(args.rulename, ci, ci) execute_or_not(args, cmd)
def get_offending_relations(args, source, relation, target): # get a list of element Types that occur within the perfect view shlog.normal("about to open %s", args.dbfile) con = sqlite3.connect(args.dbfile) curs = con.cursor() try: sql = """SELECT DISTINCT e1.Name as Source, (r.Name || ' (' || r.Type || ')') as Relation, e2.Name as Target, v.Name as View FROM FOLDER f INNER JOIN VIEWS v on v.Parent_folder_id = f.ID INNER JOIN CONNECTIONS c on c.view_id = v.Id INNER JOIN RELATIONS r on r.ID = c.relationship_id INNER JOIN ELEMENTS e1 on e1.Id = r.Source INNER JOIN ELEMENTS e2 on e2.Id = r.Target WHERE f.Depth LIKE 'F1LL3R' AND e1.Type = '%s' AND r.Type = '%s' AND e2.Type = '%s'""" % (source, relation, target) sql = sql.replace('F1LL3R', '%' + args.search_term + '%') except TypeError: shlog.error('TypeError thrown! Check if --search_term had been provided.') exit(0) shlog.verbose(sql) curs.execute(sql) rows = curs.fetchall() details = [list(elem) for elem in rows] return details
"-t", help="Add timestamp to output file name", default=False, action='store_true') args = main_parser.parse_args() # translate text arguement to log level. # least to most verbose FATAL WARN INFO DEBUG # level also printst things to the left of it. loglevel = shlog.__dict__[args.loglevel] assert type(loglevel) == type(1) shlog.basicConfig(level=shlog.__dict__[args.loglevel]) shlog.normal("Database is %s" % args.dbfile) if ".py" in args.module: shlog.error("Module names do not contain the .py suffix") exit(1) if not args.function: args.function = args.module if not args.excelfile or '.xlsx' not in args.excelfile: args.excelfile = args.module + ".xlsx" if args.timestamp: args.excelfile = args.excelfile[:-5] + datetime.now().strftime( "_%m.%d.%Y_%H.%M") + '.xlsx' # add report/validate folders to PATH sys.path.insert(0, './validate/') sys.path.insert(0, './report/') # remove the actual path from args args.module = args.module.replace('validate/', '').replace('report/', '') args.function = args.function.replace('validate/', '').replace('report/', '')
default=75) main_parser.add_argument("--file", "-f", help='csv input with file equivalencies', default='CapacityReport 2.csv') args = main_parser.parse_args() loglevel = shlog.__dict__[args.loglevel] assert type(loglevel) == type(1) shlog.basicConfig(level=shlog.__dict__[args.loglevel]) # remove old processed report try: os.remove(args.file[:-4] + '.xlsx') except OSError: shlog.error('Warning: ' + args.file[:-4] + '.xlsx' + ' does not exist, ignoring..') # fix pandas bug when writing title column names with open(args.file, 'r') as raw: data = raw.readlines() try: if data[0][0] == ',': data[0] = data[0][1:] data[1] = data[1][2:] if not data[0].startswith('Trigger'): raise IncorrectStructure with open(args.file, 'w') as raw: raw.writelines(data) except IndexError: shlog.error(args.file + ' is empty!') exit(0)