diff --git a/safety/cli.py b/safety/cli.py index 41130e1c..1d36db9e 100644 --- a/safety/cli.py +++ b/safety/cli.py @@ -46,6 +46,11 @@ def cli(ctx, debug, telemetry, disable_optional_telemetry_data): LOG.info(f'Telemetry enabled: {ctx.telemetry}') + @ctx.call_on_close + def clean_up_on_close(): + LOG.debug('Calling clean up on close function.') + safety.close_session() + @cli.command() @click.option("--key", default="", envvar="SAFETY_API_KEY", @@ -278,7 +283,8 @@ def license(ctx, key, db, output, cache, files, proxyprotocol, proxyhost, proxyp licenses_db = {} try: - licenses_db = safety.get_licenses(key, db, cache, proxy_dictionary, telemetry=ctx.parent.telemetry) + licenses_db = safety.get_licenses(key=key, db_mirror=db, cached=cache, proxy=proxy_dictionary, + telemetry=ctx.parent.telemetry) except SafetyError as e: LOG.exception('Expected SafetyError happened: %s', e) output_exception(e, exit_code_output=False) @@ -367,5 +373,6 @@ def validate(ctx, name, path): cli.add_command(alert) + if __name__ == "__main__": cli() diff --git a/safety/output_utils.py b/safety/output_utils.py index 529860ff..78e7abd4 100644 --- a/safety/output_utils.py +++ b/safety/output_utils.py @@ -495,18 +495,16 @@ def build_using_sentence(key, db): key_sentence = [{'style': True, 'value': 'an API KEY'}, {'style': False, 'value': ' and the '}] db_name = 'PyUp Commercial' - elif db and custom_integration and is_a_remote_mirror(db): - return [] + elif db: + if is_a_remote_mirror(db): + if custom_integration: + return [] + db_name = f"remote URL {db}" + else: + db_name = f"local file {db}" else: db_name = 'non-commercial' - if db: - db_type = 'local file' - if is_a_remote_mirror(db): - db_type = 'remote URL' - - db_name = f"{db_type} {db}" - database_sentence = [{'style': True, 'value': db_name + ' database'}] return [{'style': False, 'value': 'Using '}] + key_sentence + database_sentence diff --git a/safety/safety.py b/safety/safety.py index 67a2b414..b07d7666 100644 --- a/safety/safety.py +++ b/safety/safety.py @@ -208,10 +208,12 @@ def fetch_database_file(path, db_name): def fetch_database(full=False, key=False, db=False, cached=0, proxy=None, telemetry=True): - if db: + if key: + mirrors = API_MIRRORS + elif db: mirrors = [db] else: - mirrors = API_MIRRORS if key else OPEN_MIRRORS + mirrors = OPEN_MIRRORS db_name = "insecure_full.json" if full else "insecure.json" for mirror in mirrors: @@ -346,7 +348,7 @@ def check(packages, key=False, db_mirror=False, cached=0, ignore_vulns=None, ign ignore_vuln_if_needed(vuln_id, cve, ignore_vulns, ignore_severity_rules) - vulnerability = get_vulnerability_from(vuln_id, cve, data, specifier, db, name, pkg, + vulnerability = get_vulnerability_from(vuln_id, cve, data, specifier, db_full, name, pkg, ignore_vulns) should_add_vuln = not (vulnerability.is_transitive and is_env_scan) @@ -608,3 +610,8 @@ def read_vulnerabilities(fh): raise MalformedDatabase(reason=e, fetched_from=fh.name) return data + + +def close_session(): + LOG.debug('Closing requests session.') + session.close() diff --git a/safety/util.py b/safety/util.py index 5af070b3..b24b7f69 100644 --- a/safety/util.py +++ b/safety/util.py @@ -388,11 +388,13 @@ def __init__( mode: str = "r", encoding: str = None, errors: str = "strict", + pure: bool = os.environ.get('SAFETY_PURE_YAML', 'false').lower() == 'true' ) -> None: self.mode = mode self.encoding = encoding self.errors = errors self.basic_msg = '\n' + click.style('Unable to load the Safety Policy file "{name}".', fg='red') + self.pure = pure def to_info_dict(self): info_dict = super().to_info_dict() @@ -429,16 +431,17 @@ def convert(self, value, param, ctx): msg = self.basic_msg.format(name=value) + '\n' + click.style('HINT:', fg='yellow') + ' {hint}' - f, should_close = click.types.open_stream( + f, _ = click.types.open_stream( value, self.mode, self.encoding, self.errors, atomic=False ) filename = '' try: raw = f.read() - yaml = YAML(typ='safe', pure=False) + yaml = YAML(typ='safe', pure=self.pure) safety_policy = yaml.load(raw) filename = f.name + f.close() except Exception as e: show_parsed_hint = isinstance(e, MarkedYAMLError) hint = str(e)