Skip to content

Commit

Permalink
feat: print deps option
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-es committed Mar 1, 2023
1 parent 94f5d6c commit a74a25e
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 12 deletions.
4 changes: 2 additions & 2 deletions defs.bzl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
""" Entrypoint for Snyk macros """

load("//snyk/maven:rules.bzl", _snyk_maven = "snyk_maven")
#load("//snyk/gomod:rules.bzl", _snyk_gomod = "snyk_gomod")
load("//snyk/gomod:rules.bzl", _snyk_gomod = "snyk_gomod")
#load("//snyk/pip:rules.bzl", _snyk_pip = "snyk_pip")
load("//snyk/tester:rules.bzl", _snyk_python_tester = "snyk_python_tester")

snyk_maven = _snyk_maven
# snyk_gomod = _snyk_gomod
snyk_gomod = _snyk_gomod
# snyk_pip = _snyk_pip
snyk_python_tester = _snyk_python_tester
15 changes: 14 additions & 1 deletion snyk/maven/rules.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
load(":aspect.bzl", "maven_deps_aspect")
load(":depgraph.bzl", _depgraph = "snyk_maven_depgraph")
load("//snyk:rules.bzl", _monitor = "snyk_depgraph_monitor_deps", _test = "snyk_depgraph_test_deps")
load(
"//snyk:rules.bzl",
_test = "snyk_depgraph_test_deps",
_monitor = "snyk_depgraph_monitor_deps",
_print_deps = "snyk_depgraph_print_deps"
)

def snyk_maven(
name,
Expand Down Expand Up @@ -33,6 +38,14 @@ def snyk_maven(
# nocolor = nocolor,
)

_print_deps(
name = name + "_print_deps",
package_source = package_source,
depgraph = depgraph_rule_name,
json = json,
# nocolor = nocolor,
)

_depgraph(
name = depgraph_rule_name,
target = target,
Expand Down
66 changes: 66 additions & 0 deletions snyk/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

def _snyk_depgraph_test_deps_impl(ctx):
depgraph_file = ctx.attr.depgraph.files.to_list()[0]
# gomod_list_file = ctx.attr.depgraph.files.to_list()[1]

args = [
"--depgraph-file",
depgraph_file.short_path,
Expand Down Expand Up @@ -59,6 +61,37 @@ def _snyk_depgraph_monitor_deps_impl(ctx):
runfiles = ctx.runfiles(files = [ctx.executable._snyk_cli_zip, depgraph_file])
return [DefaultInfo(runfiles = runfiles)]

def _snyk_depgraph_print_deps_impl(ctx):
depgraph_file = ctx.attr.depgraph.files.to_list()[0]
# gomod_list_file = ctx.attr.depgraph.files.to_list()[1]

args = [
"--depgraph-file",
depgraph_file.short_path,
"--package-source",
ctx.attr.package_source,
"print-deps",
]

if ctx.attr.json:
args.append("--json")
#if ctx.attr.nocolor:
# args.append("-nocolor")

ctx.actions.write(
output = ctx.outputs.executable,
content = "\n".join([
"#!/bin/bash",
"exec python3 %s %s" % (ctx.executable._snyk_cli_zip.short_path, " ".join(args))
]),
is_executable = True,
)

runfiles = ctx.runfiles(files = [ctx.executable._snyk_cli_zip, depgraph_file])
return [DefaultInfo(
runfiles = runfiles
)]

snyk_depgraph_test_deps = rule(
attrs = {
"_snyk_cli": attr.label(
Expand Down Expand Up @@ -132,6 +165,39 @@ snyk_depgraph_monitor_deps = rule(
executable = True
)

snyk_depgraph_print_deps = rule(
attrs = {
"_snyk_cli": attr.label(
default = "//snyk/scripts/cli:main",
cfg = "host",
executable = True,
),
"_snyk_cli_zip": attr.label(
default = "//snyk/scripts/cli:main_zip",
cfg = "host",
executable = True
),
"package_source": attr.string(
doc = "The package source type",
#default = "maven",
mandatory = True
),
"depgraph": attr.label(
mandatory = True
),
"json": attr.bool(
doc = "Dump full JSON output",
default = False
),
"nocolor": attr.bool(
doc = "Don't display colors",
default = False
)
},
implementation = _snyk_depgraph_print_deps_impl,
executable = True
)

def _snyk_python_tester_impl(ctx):
args = []
print("ctx: " + str(dir(ctx.toolchains)))
Expand Down
32 changes: 23 additions & 9 deletions snyk/scripts/cli/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
import json
import json as json_lib
import os
import typer
import snyk
Expand Down Expand Up @@ -72,9 +72,9 @@ def main(ctx: typer.Context,
logger.setLevel(logging.DEBUG)

f = open(depgraph_file)
g['depgraph_json'] = json.load(f)
g['depgraph_json'] = json_lib.load(f)

#print(json.dumps(g['depgraph_json']))
#print(json_lib.dumps(g['depgraph_json']))

@app.command()
def test(
Expand All @@ -97,7 +97,7 @@ def test(
json: bool = typer.Option(
False,
"--json",
help="return the JSON output from the test API results"
help="Return the JSON output from the test API results"
),
):

Expand All @@ -117,7 +117,7 @@ def test(
# )

if json:
print(json.dumps(json_response, indent=4))
print(json_lib.dumps(json_response, indent=4))
sys.exit(0)

# create a list of dictionaries with the key of the package name
Expand Down Expand Up @@ -214,18 +214,32 @@ def monitor(
response: requests.Response = monitor_depgraph(snyk_client, g['depgraph_json'], snyk_org_id)

json_response = response.json()
print(json.dumps(json_response, indent=4))
print(json_lib.dumps(json_response, indent=4))

if str(json_response['ok']) == "False":
typer.echo("\n" + textColor.light_red + "security issues found, exiting with code 1 ...\n", file=sys.stderr)
sys.exit(1)

# Utility functions
@app.command()
def print_deps(
json: bool = typer.Option(
False,
"--json",
help="Print the dependency tree in JSON format"
),
):
"""
Print the dependency tree
"""
print(json_lib.dumps(g['depgraph_json'], indent=4))


# depgraph functions
def test_depgraph(snyk_client, depgraph: str, org_id: UUID) -> requests.Response:
return snyk_client.post(f"{DEPGRAPH_BASE_TEST_URL}{org_id}", body=depgraph)
return snyk_client.post(f"{DEPGRAPH_BASE_TEST_URL}{org_id}", body=depgraph)

def monitor_depgraph(snyk_client, depgraph: str, org_id: UUID) -> requests.Response:
return snyk_client.post(f"{DEPGRAPH_BASE_MONITOR_URL}{org_id}", body=depgraph)
return snyk_client.post(f"{DEPGRAPH_BASE_MONITOR_URL}{org_id}", body=depgraph)


if __name__ == "__main__":
Expand Down

0 comments on commit a74a25e

Please sign in to comment.