Skip to content

Commit

Permalink
Fix logic output for integrations, resource warnings fixed, and ruame…
Browse files Browse the repository at this point in the history
…l fix.
  • Loading branch information
yeisonvargasf committed Nov 19, 2022
1 parent ef45deb commit 6cd1ae3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
9 changes: 8 additions & 1 deletion safety/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -367,5 +373,6 @@ def validate(ctx, name, path):

cli.add_command(alert)


if __name__ == "__main__":
cli()
16 changes: 7 additions & 9 deletions safety/output_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions safety/safety.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
7 changes: 5 additions & 2 deletions safety/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6cd1ae3

Please sign in to comment.