|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Find critical Snyk findings linked to resolved issues where the issue |
| 4 | +was resolved (updatedOn) within 15 days of being opened (createdOn). |
| 5 | +
|
| 6 | +J1QL cannot perform property-to-property arithmetic in WHERE clauses, |
| 7 | +so we retrieve both timestamps and filter in Python. |
| 8 | +
|
| 9 | +Usage: |
| 10 | + export JUPITERONE_ACCOUNT_ID="<your-account-id>" |
| 11 | + export JUPITERONE_API_TOKEN="<your-api-token>" |
| 12 | + python snyk_findings_resolved_within_15_days.py [--csv output.csv] |
| 13 | +""" |
| 14 | + |
| 15 | +import os |
| 16 | +import sys |
| 17 | +import csv |
| 18 | +import argparse |
| 19 | +from datetime import datetime, timezone |
| 20 | + |
| 21 | +from jupiterone import JupiterOneClient |
| 22 | + |
| 23 | +FIFTEEN_DAYS_MS = 15 * 24 * 60 * 60 * 1000 |
| 24 | + |
| 25 | +J1QL_QUERY = """\ |
| 26 | +FIND snyk_finding WITH severity = 'critical' AS finding |
| 27 | + THAT RELATES TO snyk_issue WITH status = 'resolved' AS issue |
| 28 | +RETURN |
| 29 | + finding.displayName AS findingName, |
| 30 | + finding._key AS findingKey, |
| 31 | + finding.severity AS severity, |
| 32 | + issue.displayName AS issueName, |
| 33 | + issue._key AS issueKey, |
| 34 | + issue.status AS issueStatus, |
| 35 | + issue.createdOn AS createdOn, |
| 36 | + issue.updatedOn AS updatedOn |
| 37 | +LIMIT 250\ |
| 38 | +""" |
| 39 | + |
| 40 | + |
| 41 | +def ms_to_iso(epoch_ms): |
| 42 | + """Convert epoch milliseconds to a human-readable ISO-8601 string.""" |
| 43 | + if epoch_ms is None: |
| 44 | + return "N/A" |
| 45 | + try: |
| 46 | + return datetime.fromtimestamp(epoch_ms / 1000, tz=timezone.utc).strftime( |
| 47 | + "%Y-%m-%d %H:%M:%S UTC" |
| 48 | + ) |
| 49 | + except (TypeError, ValueError, OSError): |
| 50 | + return str(epoch_ms) |
| 51 | + |
| 52 | + |
| 53 | +def main(): |
| 54 | + parser = argparse.ArgumentParser( |
| 55 | + description="Find critical Snyk findings resolved within 15 days." |
| 56 | + ) |
| 57 | + parser.add_argument( |
| 58 | + "--csv", |
| 59 | + metavar="FILE", |
| 60 | + help="Write results to a CSV file instead of stdout.", |
| 61 | + ) |
| 62 | + args = parser.parse_args() |
| 63 | + |
| 64 | + account = os.getenv("JUPITERONE_ACCOUNT_ID") |
| 65 | + token = os.getenv("JUPITERONE_API_TOKEN") |
| 66 | + if not account or not token: |
| 67 | + sys.exit( |
| 68 | + "Error: JUPITERONE_ACCOUNT_ID and JUPITERONE_API_TOKEN " |
| 69 | + "environment variables are required." |
| 70 | + ) |
| 71 | + |
| 72 | + j1 = JupiterOneClient( |
| 73 | + account=account, |
| 74 | + token=token, |
| 75 | + url=os.getenv("JUPITERONE_URL", "https://graphql.us.jupiterone.io"), |
| 76 | + sync_url=os.getenv("JUPITERONE_SYNC_URL", "https://api.us.jupiterone.io"), |
| 77 | + ) |
| 78 | + |
| 79 | + print(f"Executing J1QL query ...\n{J1QL_QUERY}\n") |
| 80 | + result = j1.query_v1(query=J1QL_QUERY) |
| 81 | + rows = result.get("data", []) |
| 82 | + print(f"Total rows returned: {len(rows)}") |
| 83 | + |
| 84 | + filtered = [] |
| 85 | + skipped_missing_dates = 0 |
| 86 | + |
| 87 | + for row in rows: |
| 88 | + props = row.get("properties", row) |
| 89 | + created_on = props.get("createdOn") |
| 90 | + updated_on = props.get("updatedOn") |
| 91 | + |
| 92 | + if created_on is None or updated_on is None: |
| 93 | + skipped_missing_dates += 1 |
| 94 | + continue |
| 95 | + |
| 96 | + delta_ms = updated_on - created_on |
| 97 | + if delta_ms <= FIFTEEN_DAYS_MS: |
| 98 | + filtered.append( |
| 99 | + { |
| 100 | + "findingName": props.get("findingName", ""), |
| 101 | + "findingKey": props.get("findingKey", ""), |
| 102 | + "severity": props.get("severity", ""), |
| 103 | + "issueName": props.get("issueName", ""), |
| 104 | + "issueKey": props.get("issueKey", ""), |
| 105 | + "issueStatus": props.get("issueStatus", ""), |
| 106 | + "createdOn": created_on, |
| 107 | + "updatedOn": updated_on, |
| 108 | + "createdOnHuman": ms_to_iso(created_on), |
| 109 | + "updatedOnHuman": ms_to_iso(updated_on), |
| 110 | + "daysToResolve": round(delta_ms / (24 * 60 * 60 * 1000), 2), |
| 111 | + } |
| 112 | + ) |
| 113 | + |
| 114 | + print(f"Rows matching <=15-day window: {len(filtered)}") |
| 115 | + if skipped_missing_dates: |
| 116 | + print(f"Rows skipped (missing createdOn/updatedOn): {skipped_missing_dates}") |
| 117 | + |
| 118 | + if not filtered: |
| 119 | + print("No matching results.") |
| 120 | + return |
| 121 | + |
| 122 | + if args.csv: |
| 123 | + fieldnames = list(filtered[0].keys()) |
| 124 | + with open(args.csv, "w", newline="") as f: |
| 125 | + writer = csv.DictWriter(f, fieldnames=fieldnames) |
| 126 | + writer.writeheader() |
| 127 | + writer.writerows(filtered) |
| 128 | + print(f"\nResults written to {args.csv}") |
| 129 | + else: |
| 130 | + print(f"\n{'Finding':<40} {'Issue':<40} {'Created':<24} {'Updated':<24} {'Days':>6}") |
| 131 | + print("-" * 138) |
| 132 | + for r in filtered: |
| 133 | + print( |
| 134 | + f"{r['findingName']:<40} " |
| 135 | + f"{r['issueName']:<40} " |
| 136 | + f"{r['createdOnHuman']:<24} " |
| 137 | + f"{r['updatedOnHuman']:<24} " |
| 138 | + f"{r['daysToResolve']:>6}" |
| 139 | + ) |
| 140 | + |
| 141 | + |
| 142 | +if __name__ == "__main__": |
| 143 | + main() |
0 commit comments