def _validate_args(self): CliCommand._validate_args(self) if self.args.output_json: self.req_headers = {'Accept': 'application/json'} if self.args.output_csv: self.req_headers = {'Accept': 'text/csv'} try: validate_write_file(self.args.path, 'output-file') except ValueError as error: print(error) sys.exit(1) # Lookup scan job id response = request(parser=self.parser, method=GET, path='%s%s' % (scan.SCAN_URI, self.args.scan_id), payload=None) if response.status_code == codes.ok: # pylint: disable=no-member json_data = response.json() self.fact_collection_id = json_data.get('fact_collection_id') if self.fact_collection_id: self.req_path = '%s?fact_collection_id=%s' % ( self.req_path, self.fact_collection_id) else: print(_(messages.REPORT_NO_REPORT_FOR_SJ % self.args.scan_id)) sys.exit(1) else: print(_(messages.REPORT_SJ_DOES_NOT_EXIST % self.args.scan_id)) sys.exit(1)
def _validate_args(self): self.req_headers = {'Accept': 'application/gzip'} if self.args.mask: self.req_params = {'mask': True} try: validate_write_file(self.args.path, 'output-file') except ValueError as error: print(error) sys.exit(1) check_extension('tar.gz', self.args.path) if self.args.report_id is None: # Lookup scan job id response = request(parser=self.parser, method=GET, path='%s%s' % (scan.SCAN_JOB_URI, self.args.scan_job_id), payload=None) if response.status_code == codes.ok: # pylint: disable=no-member json_data = response.json() self.report_id = json_data.get('report_id') if self.report_id: self.req_path = '%s%s' % (self.req_path, self.report_id) else: print( _(messages.DOWNLOAD_NO_REPORT_FOR_SJ % self.args.scan_job_id)) sys.exit(1) else: print( _(messages.DOWNLOAD_SJ_DOES_NOT_EXIST % self.args.scan_job_id)) sys.exit(1) else: self.report_id = self.args.report_id self.req_path = '%s%s' % (self.req_path, self.report_id)
def _get_report_ids(self): """Grab the report ids from the scan job if it exists. :returns Boolean regarding the existence of scan jobs & the report ids """ not_found = False report_ids = [] job_not_found = [] report_not_found = [] for scan_job_id in set(self.args.scan_job_ids): # check for existence of scan_job path = SCAN_JOB_URI + str(scan_job_id) + '/' response = request(parser=self.parser, method=GET, path=path, params=None, payload=None) if response.status_code == codes.ok: # pylint: disable=no-member json_data = response.json() report_id = json_data.get('report_id', None) if report_id: report_ids.append(report_id) else: # there is not a report id associated with this scan job report_not_found.append(scan_job_id) not_found = True else: job_not_found.append(scan_job_id) not_found = True return not_found, report_ids, job_not_found, report_not_found
def get_scan_object_id(parser, name): """Grab the scan id from the scan object if it exists. :returns Boolean regarding the existence of the object & the scan object id """ found = False scan_object_id = None response = request(parser=parser, method=GET, path=scan.SCAN_URI, params={'name': name}, payload=None) if response.status_code == codes.ok: # pylint: disable=no-member json_data = response.json() count = json_data.get('count', 0) results = json_data.get('results', []) if count >= 1: for result in results: if result['name'] == name: scan_object_id = str(result['id']) + '/' found = True if not found or count == 0: print(_(messages.SCAN_DOES_NOT_EXIST % name)) else: print(_(messages.SCAN_DOES_NOT_EXIST % name)) return found, scan_object_id
def get_source_ids(parser, source_names): """Grab the source ids from the source if it exists. :returns Boolean regarding the existence of source & the source ids """ not_found = False source_ids = [] for source_name in set(source_names): # check for existence of source response = request(parser=parser, method=GET, path=source.SOURCE_URI, params={'name': source_name}, payload=None) if response.status_code == codes.ok: # pylint: disable=no-member json_data = response.json() count = json_data.get('count', 0) results = json_data.get('results', []) if count == 1: source_entry = results[0] source_ids.append(source_entry['id']) else: print(_(messages.SOURCE_DOES_NOT_EXIST % source_name)) not_found = True else: print(_(messages.SOURCE_DOES_NOT_EXIST % source_name)) not_found = True return not_found, source_ids
def _validate_args(self): CliCommand._validate_args(self) if not (self.args.username or self.args.password or self.args.sudo_password or self.args.filename or self.args.ssh_passphrase): print(_(messages.CRED_EDIT_NO_ARGS % (self.args.name))) self.parser.print_help() sys.exit(1) if 'filename' in self.args and self.args.filename: # check for file existence on system self.args.filename = validate_sshkeyfile(self.args.filename, self.parser) # check for existence of credential response = request(parser=self.parser, method=GET, path=credential.CREDENTIAL_URI, params={'name': self.args.name}, payload=None) if response.status_code == codes.ok: # pylint: disable=no-member json_data = response.json() if len(json_data) == 1: cred_entry = json_data[0] self.cred_type = cred_entry['cred_type'] self.req_path = self.req_path + str(cred_entry['id']) + '/' else: print(_(messages.CRED_DOES_NOT_EXIST % self.args.name)) sys.exit(1) else: print(_(messages.CRED_DOES_NOT_EXIST % self.args.name)) sys.exit(1)
def _validate_args(self): CliCommand._validate_args(self) if not (self.args.username or self.args.password or self.args.filename or self.args.ssh_passphrase or self.args.become_method or self.args.become_user or self.args.become_password): print(_(messages.CRED_EDIT_NO_ARGS % (self.args.name))) self.parser.print_help() sys.exit(1) # check for existence of credential response = request(parser=self.parser, method=GET, path=credential.CREDENTIAL_URI, params={'name': self.args.name}, payload=None) if response.status_code == codes.ok: # pylint: disable=no-member json_data = response.json() count = json_data.get('count', 0) if count == 1: cred_entry = json_data.get('results')[0] self.cred_type = cred_entry['cred_type'] self.req_path = self.req_path + str(cred_entry['id']) + '/' else: print(_(messages.CRED_DOES_NOT_EXIST % self.args.name)) sys.exit(1) else: print(_(messages.CRED_DOES_NOT_EXIST % self.args.name)) sys.exit(1)
def _validate_args(self): CliCommand._validate_args(self) if ('hosts' in self.args and self.args.hosts and len(self.args.hosts) == 1): # check if a file and read in values try: self.args.hosts = read_in_file(self.args.hosts[0]) except ValueError: pass if ('exclude_hosts' in self.args and self.args.exclude_hosts and len(self.args.exclude_hosts) == 1): # check if a file and read in values try: self.args.exclude_hosts = \ read_in_file(self.args.exclude_hosts[0]) except ValueError: pass # check for valid cred values cred_list = ','.join(self.args.cred) response = request(parser=self.parser, method=GET, path=cred.CREDENTIAL_URI, params={'name': cred_list}, payload=None) if response.status_code == codes.ok: # pylint: disable=no-member json_data = response.json() count = json_data.get('count', 0) results = json_data.get('results', []) if count == len(self.args.cred): self.args.credentials = [] results_by_name_dict = {cred['name']: cred for cred in results} for cred_name in self.args.cred: self.args.credentials.append( results_by_name_dict[cred_name]['id']) else: for cred_entry in results: cred_name = cred_entry['name'] self.args.cred.remove(cred_name) not_found_str = ','.join(self.args.cred) print( _(messages.SOURCE_ADD_CREDS_NOT_FOUND % (not_found_str, self.args.name))) sys.exit(1) else: print(_(messages.SOURCE_ADD_CRED_PROCESS_ERR % self.args.name)) sys.exit(1)
def _validate_args(self): """Validate the edit arguments.""" CliCommand._validate_args(self) # Check to see if args were provided if not (self.args.sources or self.args.max_concurrency or (self.args.disabled_optional_products or self.args.disabled_optional_products == []) or (self.args.enabled_ext_product_search or self.args.enabled_ext_product_search == []) or (self.args.ext_product_search_dirs or self.args.ext_product_search_dirs == [])): print(_(messages.SCAN_EDIT_NO_ARGS % (self.args.name))) self.parser.print_help() sys.exit(1) # check for existence of scan exists = False response = request(parser=self.parser, method=GET, path=scan.SCAN_URI, params={'name': self.args.name}, payload=None) if response.status_code == codes.ok: # pylint: disable=no-member json_data = response.json() count = json_data.get('count', 0) results = json_data.get('results', []) if count >= 1: for result in results: if result['name'] == self.args.name: scan_entry = result self.req_path \ = self.req_path + str(scan_entry['id']) + '/' exists = True if not exists or count == 0: print(_(messages.SCAN_DOES_NOT_EXIST % self.args.name)) sys.exit(1) else: print(_(messages.SCAN_DOES_NOT_EXIST % self.args.name)) sys.exit(1) # check for valid source values source_ids = [] if self.args.sources: # check for existence of sources not_found, source_ids = get_source_ids(self.parser, self.args.sources) if not_found is True: sys.exit(1) self.source_ids = source_ids
def _delete_entry(self, source_entry, print_out=True): deleted = False delete_uri = source.SOURCE_URI + str(source_entry['id']) + '/' response = request(DELETE, delete_uri, parser=self.parser) name = source_entry['name'] # pylint: disable=no-member if response.status_code == codes.no_content: deleted = True if print_out: print(_(messages.SOURCE_REMOVED % name)) else: handle_error_response(response) if print_out: print(_(messages.SOURCE_FAILED_TO_REMOVE % name)) return deleted
def _validate_args(self): CliCommand._validate_args(self) extension = None if self.args.output_json: extension = '.json' self.req_headers = {'Accept': 'application/json+gzip'} if self.args.output_csv: extension = '.csv' self.req_headers = {'Accept': 'text/csv'} if self.args.mask: self.req_params = {'mask': True} if extension: check_extension(extension, self.args.path) try: validate_write_file(self.args.path, 'output-file') except ValueError as error: print(error) sys.exit(1) if self.args.report_id is None: # Lookup scan job id response = request(parser=self.parser, method=GET, path='%s%s' % (scan.SCAN_JOB_URI, self.args.scan_job_id), payload=None) if response.status_code == codes.ok: # pylint: disable=no-member json_data = response.json() self.report_id = json_data.get('report_id') if self.report_id: self.req_path = '%s%s%s' % ( self.req_path, self.report_id, report.DEPLOYMENTS_PATH_SUFFIX) else: print(_(messages.REPORT_NO_DEPLOYMENTS_REPORT_FOR_SJ % self.args.scan_job_id)) sys.exit(1) else: print(_(messages.REPORT_SJ_DOES_NOT_EXIST % self.args.scan_job_id)) sys.exit(1) else: self.report_id = self.args.report_id self.req_path = '%s%s%s' % ( self.req_path, self.report_id, report.DEPLOYMENTS_PATH_SUFFIX)
def _do_command(self): """Execute command flow. Sub-commands define this method to perform the required action once all options have been verified. """ self._build_req_params() self._build_data() self.response = request(method=self.req_method, path=self.req_path, params=self.req_params, payload=self.req_payload) # pylint: disable=no-member if self.response.status_code not in self.success_codes: # handle error cases self._handle_response_error() else: self._handle_response_success()
def _validate_args(self): CliCommand._validate_args(self) found = False response = request(parser=self.parser, method=GET, path=scan.SCAN_URI, params={'name': self.args.name}, payload=None) if response.status_code == codes.ok: # pylint: disable=no-member json_data = response.json() count = json_data.get('count', 0) results = json_data.get('results', []) if count >= 1: for result in results: if result['name'] == self.args.name: self.req_path = self.req_path + str(result['id']) + '/' found = True if not found or count == 0: print(_(messages.SCAN_DOES_NOT_EXIST % self.args.name)) sys.exit(1)
def _get_source_ids(self, source_names): not_found = False source_ids = [] for source_name in set(source_names): # check for existence of source response = request(parser=self.parser, method=GET, path=source.SOURCE_URI, params={'name': source_name}, payload=None) if response.status_code == codes.ok: # pylint: disable=no-member json_data = response.json() if len(json_data) == 1: source_entry = json_data[0] source_ids.append(source_entry['id']) else: print(_(messages.SOURCE_DOES_NOT_EXIST % source_name)) not_found = True else: print(_(messages.SOURCE_DOES_NOT_EXIST % source_name)) not_found = True return not_found, source_ids
def _validate_args(self): CliCommand._validate_args(self) if not (self.args.hosts or self.args.exclude_hosts or self.args.cred or self.args.port or self.args.use_paramiko or self.args.ssl_cert_verify or self.args.disable_ssl or self.args.ssl_protocol): print(_(messages.SOURCE_EDIT_NO_ARGS % (self.args.name))) self.parser.print_help() sys.exit(1) if ('hosts' in self.args and self.args.hosts and len(self.args.hosts) == 1): # check if a file and read in values try: self.args.hosts = read_in_file(self.args.hosts[0]) except ValueError: pass if ('exclude_hosts' in self.args and self.args.exclude_hosts and len(self.args.exclude_hosts) == 1): # check if a file and read in values try: self.args.exclude_hosts = \ read_in_file(self.args.exclude_hosts[0]) except ValueError: pass # check for existence of source response = request(parser=self.parser, method=GET, path=source.SOURCE_URI, params={'name': self.args.name}, payload=None) if response.status_code == codes.ok: # pylint: disable=no-member json_data = response.json() count = json_data.get('count', 0) results = json_data.get('results', []) if count == 1: source_entry = results[0] self.req_path = self.req_path + str(source_entry['id']) + '/' else: print(_(messages.SOURCE_DOES_NOT_EXIST % self.args.name)) sys.exit(1) else: print(_(messages.SOURCE_DOES_NOT_EXIST % self.args.name)) sys.exit(1) # check for valid cred values if len(self.args.cred) > 0: # pylint: disable=len-as-condition cred_list = ','.join(self.args.cred) response = request(parser=self.parser, method=GET, path=cred.CREDENTIAL_URI, params={'name': cred_list}, payload=None) if response.status_code == codes.ok: # pylint: disable=no-member json_data = response.json() count = json_data.get('count', 0) results = json_data.get('results', []) if count == len(self.args.cred): self.args.credentials = [] for cred_entry in results: self.args.credentials.append(cred_entry['id']) else: for cred_entry in results: cred_name = cred_entry['name'] self.args.cred.remove(cred_name) not_found_str = ','.join(self.args.cred) print( _(messages.SOURCE_EDIT_CREDS_NOT_FOUND % (not_found_str, self.args.name))) sys.exit(1) else: print(_(messages.SOURCE_EDIT_CRED_PROCESS_ERR % self.args.name)) sys.exit(1)