|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "log" |
| 6 | + "log/slog" |
| 7 | + |
| 8 | + v1 "github.com/ocurity/dracon/api/proto/v1" |
| 9 | + |
| 10 | + "github.com/ocurity/dracon/components/producers" |
| 11 | +) |
| 12 | + |
| 13 | +func main() { |
| 14 | + if err := producers.ParseFlags(); err != nil { |
| 15 | + log.Fatal(err) |
| 16 | + } |
| 17 | + |
| 18 | + inFile, err := producers.ReadInFile() |
| 19 | + if err != nil { |
| 20 | + log.Fatal(err) |
| 21 | + } |
| 22 | + |
| 23 | + var results ModelScanOut |
| 24 | + if err := json.Unmarshal(inFile, &results); err != nil { |
| 25 | + log.Fatal(err) |
| 26 | + } |
| 27 | + |
| 28 | + issues, err := parseIssues(&results) |
| 29 | + if err != nil { |
| 30 | + log.Fatal(err) |
| 31 | + } |
| 32 | + if err := producers.WriteDraconOut( |
| 33 | + "modelscan", |
| 34 | + issues, |
| 35 | + ); err != nil { |
| 36 | + log.Fatal(err) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func parseIssues(out *ModelScanOut) ([]*v1.Issue, error) { |
| 41 | + issues := make([]*v1.Issue, 0, len(out.Issues)) |
| 42 | + slog.Info("found Critical issues", slog.Int("numCrit", out.Summary.TotalIssuesBySeverity.Critical)) |
| 43 | + slog.Info("found High issues", slog.Int("numCrit", out.Summary.TotalIssuesBySeverity.High)) |
| 44 | + slog.Info("found Medium issues", slog.Int("numCrit", out.Summary.TotalIssuesBySeverity.Medium)) |
| 45 | + slog.Info("found Low issues", slog.Int("numCrit", out.Summary.TotalIssuesBySeverity.Low)) |
| 46 | + for _, issue := range out.Issues { |
| 47 | + issues = append(issues, |
| 48 | + &v1.Issue{ |
| 49 | + Target: "file:///" + issue.Source, |
| 50 | + Type: issue.Scanner, |
| 51 | + Description: issue.Description, |
| 52 | + Title: issue.Description, |
| 53 | + Severity: modelScanSeverityToDracon(issue.Severity), |
| 54 | + Confidence: v1.Confidence_CONFIDENCE_UNSPECIFIED, |
| 55 | + }) |
| 56 | + } |
| 57 | + return issues, nil |
| 58 | +} |
| 59 | + |
| 60 | +func modelScanSeverityToDracon(severity string) v1.Severity { |
| 61 | + switch severity { |
| 62 | + case "CRITICAL": |
| 63 | + return v1.Severity_SEVERITY_CRITICAL |
| 64 | + case "HIGH": |
| 65 | + return v1.Severity_SEVERITY_HIGH |
| 66 | + case "MEDIUM": |
| 67 | + return v1.Severity_SEVERITY_MEDIUM |
| 68 | + case "LOW": |
| 69 | + return v1.Severity_SEVERITY_LOW |
| 70 | + default: |
| 71 | + return v1.Severity_SEVERITY_UNSPECIFIED |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +type ModelScanOut struct { |
| 76 | + Summary ModelScanSummary `json:"summary,omitempty"` |
| 77 | + Issues []ModelScanIssue `json:"issues,omitempty"` |
| 78 | + Errors []any `json:"errors,omitempty"` |
| 79 | +} |
| 80 | + |
| 81 | +type ModelScanIssue struct { |
| 82 | + Description string `json:"description,omitempty"` |
| 83 | + Operator string `json:"operator,omitempty"` |
| 84 | + Module string `json:"module,omitempty"` |
| 85 | + Source string `json:"source,omitempty"` |
| 86 | + Scanner string `json:"scanner,omitempty"` |
| 87 | + Severity string `json:"severity,omitempty"` |
| 88 | +} |
| 89 | + |
| 90 | +type ModelScanSummary struct { |
| 91 | + TotalIssuesBySeverity TotalIssuesBySeverity `json:"total_issues_by_severity,omitempty"` |
| 92 | + TotalIssues int `json:"total_issues,omitempty"` |
| 93 | + InputPath string `json:"input_path,omitempty"` |
| 94 | + AbsolutePath string `json:"absolute_path,omitempty"` |
| 95 | + ModelscanVersion string `json:"modelscan_version,omitempty"` |
| 96 | + Timestamp string `json:"timestamp,omitempty"` |
| 97 | + Scanned Scanned `json:"scanned,omitempty"` |
| 98 | +} |
| 99 | + |
| 100 | +type TotalIssuesBySeverity struct { |
| 101 | + Low int `json:"LOW,omitempty"` |
| 102 | + Medium int `json:"MEDIUM,omitempty"` |
| 103 | + High int `json:"HIGH,omitempty"` |
| 104 | + Critical int `json:"CRITICAL,omitempty"` |
| 105 | +} |
| 106 | + |
| 107 | +type Scanned struct { |
| 108 | + TotalScanned int `json:"total_scanned,omitempty"` |
| 109 | + ScannedFiles []string `json:"scanned_files,omitempty"` |
| 110 | +} |
0 commit comments