def get(self, featExtractId): current_app.logger.debug(f'convert dot feat extract entry: {featExtractId}') existing_feat_extract = FeatExtract.query.get_or_404(int(featExtractId)) if os.environ.get('BESSPIN_CONFIGURATOR_USE_TEMP_DIR'): WORK_DIR_OBJ = tempfile.TemporaryDirectory() WORK_DIR = WORK_DIR_OBJ.name else: WORK_DIR = tempfile.gettempdir() tmp_input_file = os.path.join(WORK_DIR, existing_feat_extract.featExtractOutputFilename) with open(tmp_input_file, 'w') as f: f.write(existing_feat_extract.featExtractOutputContent) tmp_output_file = os.path.join(WORK_DIR, 'simplified.fm.json') cmd = f'besspin-feature-model-tool simplify {tmp_input_file} > {tmp_output_file}' current_app.logger.debug(f'command to execute: {cmd}') cp = run_nix_subprocess('~/tool-suite', cmd) current_app.logger.debug('simplify stdout: ' + str(cp.stdout.decode('utf8'))) current_app.logger.debug('simplify stderr: ' + str(cp.stderr.decode('utf8'))) with open(tmp_output_file, 'r') as f: output_file_content = f.read() existing_feat_extract.featExtractOutputFilenameSimplified = existing_feat_extract.featExtractOutputFilename + '-simpl' existing_feat_extract.featExtractOutputContentSimplified = output_file_content existing_feat_extract.featExtractOutputContentClaferSimplified = fmjson_to_clafer(output_file_content.encode('utf-8')) db.session.add(existing_feat_extract) db.session.commit() return existing_feat_extract
def convert_model_to_json(source): """ Convert the source of a model to json """ filename = os.path.join(WORK_DIR, 'generated_file') filename_cfr = filename + '.cfr' filename_json = filename + '.fm.json' with open(filename_cfr, 'wb') as f: f.write(source) if config['default'].USE_TOOLSUITE and config[ 'default'].USE_TOOLSUITE_CONFIGURATOR: cp = run_nix_subprocess('~/tool-suite', CMD_CLAFER.format(filename_cfr)) else: cp = subprocess.run([CLAFER, filename_cfr, '-m', 'fmjson'], capture_output=True) current_app.logger.debug('Clafer stdout: ' + str(cp.stdout)) current_app.logger.debug('Clafer stderr: ' + str(cp.stderr)) json_feat_model = load_json(filename_json) version = json_feat_model['version']['base'] if version not in FORMAT_VERSIONS: current_app.logger.debug(version) raise RuntimeError( 'unsupported json format version {}'.format(version)) return json_feat_model
def get(self, archExtractOutputId): current_app.logger.debug(f'convert dot arch extract entry: {archExtractOutputId}') existing_arch_extract_output = ArchExtractOutput.query.get_or_404(int(archExtractOutputId)) if os.environ.get('BESSPIN_CONFIGURATOR_USE_TEMP_DIR'): WORK_DIR_OBJ = tempfile.TemporaryDirectory() WORK_DIR = WORK_DIR_OBJ.name else: WORK_DIR = tempfile.gettempdir() dotfile_path = os.path.join(WORK_DIR, 'dotfile.dot') with open(dotfile_path, 'w') as f: f.write(existing_arch_extract_output.archExtractOutputContent.decode()) dot_to_png = '${f%.dot}.png' cmd_png = f'for f in {WORK_DIR}/*.dot; do dot -Tpng $f -o {dot_to_png}; done' current_app.logger.debug(f'command to execute: {cmd_png}') cp_png = run_nix_subprocess('~/tool-suite', cmd_png) current_app.logger.debug('png stdout: ' + str(cp_png.stdout.decode('utf8'))) current_app.logger.debug('png stderr: ' + str(cp_png.stderr.decode('utf8'))) with open(os.path.join(WORK_DIR, 'dotfile.png'), 'rb') as f: png_bytes = f.read() return { 'archExtractOutputId': int(archExtractOutputId), 'archExtractOutputFilename': existing_arch_extract_output.archExtractOutputFilename, 'archExtractOutputContentConverted': base64.b64encode(png_bytes).decode('ascii'), }
def post(self, archExtractId): current_app.logger.debug(f'run arch extract entry') if config['default'].USE_TOOLSUITE: if os.environ.get('BESSPIN_CONFIGURATOR_USE_TEMP_DIR'): WORK_DIR_OBJ = tempfile.TemporaryDirectory() WORK_DIR = WORK_DIR_OBJ.name else: WORK_DIR = tempfile.gettempdir() arch_extract_config_file_path = os.path.join(WORK_DIR, 'arch_extract_config_file.toml') existing_arch_extract = ArchExtract.query.get_or_404(int(archExtractId)) with open(arch_extract_config_file_path, 'w') as f: f.write(existing_arch_extract.archExtractInput) cp = run_nix_subprocess('~/tool-suite', f'besspin-arch-extract {arch_extract_config_file_path} visualize') current_app.logger.debug('besspin-arch-extract stdout: ' + str(cp.stdout.decode('utf8'))) current_app.logger.debug('besspin-arch-extract stderr: ' + str(cp.stderr.decode('utf8'))) log_output = str(cp.stdout.decode('utf8')) out_dir_path = os.path.join('/home/besspinuser/tool-suite', get_variable(existing_arch_extract.archExtractInput, 'out-dir')) current_app.logger.debug(f'out_dir_path: {str(out_dir_path)}') file_list = os.listdir(out_dir_path) current_app.logger.debug(f'file_list: {str(file_list)}') file_path_list = [] for file_item in file_list: file_item_path = os.path.join(out_dir_path, file_item) current_app.logger.debug(f'file_item_path: {file_item_path}') with open(file_item_path, 'r') as f: file_item_text = f.read() arch_extract_output_record = ArchExtractOutput( archExtractOutputFilename=file_item_path, archExtractId=int(archExtractId), archExtractOutputContent=file_item_text.encode('utf-8') ) db.session.add(arch_extract_output_record) db.session.commit() current_app.logger.debug(f'Filename: {str(file_item_path)}') current_app.logger.debug(f'OutputId: {str(arch_extract_output_record.archExtractOutputId)}') file_path_list += [{ 'archExtractOutputFilename': file_item_path, 'archExtractOutputId': arch_extract_output_record.archExtractOutputId, }] current_app.logger.debug(f'out: {str(file_path_list)}') return { 'archExtractOutputList': file_path_list } return
def make_nix_call(workflow: Workflow, vulnerability: VulnerabilityConfigurationInput, vuln_feature_model: FeatureModel): if os.environ.get('BESSPIN_CONFIGURATOR_USE_TEMP_DIR'): WORK_DIR_OBJ = tempfile.TemporaryDirectory() WORK_DIR = WORK_DIR_OBJ.name else: WORK_DIR = tempfile.gettempdir() current_app.logger.debug('WORK_DIR: ' + WORK_DIR) constraints_path = generate_test_constraints(WORK_DIR, vulnerability, vuln_feature_model) testgen_config_path = generate_test_config(WORK_DIR, constraints_path, workflow) try: testgen_path = config['default'].TESTGEN_PATH cp = run_nix_subprocess( testgen_path, f'./testgen.sh {testgen_config_path} ; ./scripts/CI/ciJobDecision.py runOnPush' ) current_app.logger.debug('Testgen stdout: ' + str(cp.stdout.decode('utf8'))) current_app.logger.debug('Testgen stderr: ' + str(cp.stderr.decode('utf8'))) log_output = str(cp.stdout.decode('utf8')) current_app.logger.debug(f'testgen_path="{testgen_path}') score_path = os.path.join( testgen_path, 'workDir', config['default'].TESTGEN_VULN_DIR_MAP[vulnerability.vulnClass], 'scores.csv') scores = fetch_scores(score_path) except TypeError as err: current_app.logger.error(f'Unexpected error running nix: {err}') except: # noqa E722 current_app.logger.error( f'Unexpected error running nix: {sys.exc_info()[0]}') log_output = 'Exception occurred runing tests. Please check server logs for more information' scores = [] return [log_output, scores]
def configuration_algo(cfr_source, feature_selection): """ Execute the configuration validation algorithm and returns a boolean indicating the success or failure of the validation. When `USE_TOOLSUITE` is disabled, it blindly returns true :param cfr_source: string :param feature_selection: :return: boolean """ if (not config['default'].USE_TOOLSUITE or not config['default'].USE_TOOLSUITE_CONFIGURATOR): return True filename = os.path.join(WORK_DIR, 'generated_configured_feature_model') filename_cfr = filename + '.cfr' filename_json = filename + '.fm.json' current_app.logger.debug('CFR source: ' + cfr_source) with open(filename_cfr, 'w') as f: f.write(cfr_source) f.write( selected_features_to_constraints(feature_selection, even_not_validated=True)) with open(filename_cfr, 'r') as f: text = f.read() current_app.logger.debug('CFR+CONSTRAINTS: \n' + text) cmd = CMD_CONFIGURATION_ALGO.format(filename_cfr, filename_json) cp = run_nix_subprocess('~/tool-suite', cmd) current_app.logger.debug('configuration algo stdout: ' + str(cp.stdout)) current_app.logger.debug('configuration algo stderr: ' + str(cp.stderr)) if cp.stderr: raise RuntimeError(cp.stderr) n = _find_answer_in_configuration_algo_output(cp.stdout.decode('utf8')) return n > 0
def post(self, featExtractId): current_app.logger.debug(f'run feat extract entry') if config['default'].USE_TOOLSUITE: if os.environ.get('BESSPIN_CONFIGURATOR_USE_TEMP_DIR'): WORK_DIR_OBJ = tempfile.TemporaryDirectory() WORK_DIR = WORK_DIR_OBJ.name else: WORK_DIR = tempfile.gettempdir() feat_extract_config_file_path = os.path.join(WORK_DIR, 'feat_extract_config_file.toml') existing_feat_extract = FeatExtract.query.get_or_404(int(featExtractId)) with open(feat_extract_config_file_path, 'w') as f: f.write(existing_feat_extract.featExtractInput) cp = run_nix_subprocess('~/tool-suite', f'besspin-feature-extract {feat_extract_config_file_path} synthesize') current_app.logger.debug('besspin-feature-extract stdout: ' + str(cp.stdout.decode('utf8'))) current_app.logger.debug('besspin-feature-extract stderr: ' + str(cp.stderr.decode('utf8'))) log_output = str(cp.stdout.decode('utf8')) output_filename = get_variable(existing_feat_extract.featExtractInput, 'out-file') output_file_path = os.path.join('/home/besspinuser/tool-suite', output_filename) with open(output_file_path, 'r') as f: output_file_content = f.read() existing_feat_extract.featExtractOutputFilename = output_filename.strip('.fm.json') existing_feat_extract.featExtractOutputContent = output_file_content existing_feat_extract.featExtractOutputContentClafer = fmjson_to_clafer(output_file_content.encode('utf-8')) db.session.add(existing_feat_extract) db.session.commit() return existing_feat_extract return
def fmjson_to_clafer(source): """ Convert a feature model from fm.json format to clafer format, when USE_TOOLSUITE is set """ if (not config['default'].USE_TOOLSUITE): return source.decode('utf8') filename = os.path.join(WORK_DIR, 'generated_file') filename_json = filename + '.fm.json' with open(filename_json, 'wb') as f: f.write(source) cmd = CMD_PRINT_CLAFER.format(filename_json) cp = run_nix_subprocess('~/tool-suite', cmd) current_app.logger.debug('besspin-feature-model-tool stdout: ' + str(cp.stdout)) current_app.logger.debug('besspin-feature-model-tool stderr: ' + str(cp.stderr)) if cp.stderr: raise RuntimeError(cp.stderr) return cp.stdout.decode('utf8')