|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/kernel-guard/bpfcompat/internal/conformance" |
| 11 | + "github.com/kernel-guard/bpfcompat/internal/runner" |
| 12 | +) |
| 13 | + |
| 14 | +func runConformance(args []string) int { |
| 15 | + if len(args) == 0 { |
| 16 | + printConformanceUsage() |
| 17 | + return runner.ExitToolError |
| 18 | + } |
| 19 | + switch args[0] { |
| 20 | + case "evaluate": |
| 21 | + return runConformanceEvaluate(args[1:]) |
| 22 | + case "-h", "--help", "help": |
| 23 | + printConformanceUsage() |
| 24 | + return runner.ExitSuccess |
| 25 | + default: |
| 26 | + fmt.Fprintf(os.Stderr, "unknown conformance subcommand: %s\n\n", args[0]) |
| 27 | + printConformanceUsage() |
| 28 | + return runner.ExitToolError |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func runConformanceEvaluate(args []string) int { |
| 33 | + fs := flag.NewFlagSet("conformance evaluate", flag.ContinueOnError) |
| 34 | + fs.SetOutput(os.Stderr) |
| 35 | + profilePath := fs.String("profile", "", "Path to a versioned conformance profile YAML") |
| 36 | + reportPath := fs.String("report", "", "Path to the bpfcompat JSON report to evaluate") |
| 37 | + outDir := fs.String("out-dir", "", "Directory for decision and in-toto statement outputs") |
| 38 | + verifierID := fs.String("verifier-id", "", "Absolute HTTPS URI identifying the verifier") |
| 39 | + reportURL := fs.String("report-url", "", "Optional absolute HTTPS URL for the originating test run") |
| 40 | + fs.Usage = func() { |
| 41 | + fmt.Fprintf(fs.Output(), "Usage:\n bpfcompat conformance evaluate --profile <file> --report <file> --out-dir <dir> --verifier-id <uri> [--report-url <url>]\n\n") |
| 42 | + fs.PrintDefaults() |
| 43 | + } |
| 44 | + if err := fs.Parse(args); err != nil { |
| 45 | + if errors.Is(err, flag.ErrHelp) { |
| 46 | + return runner.ExitSuccess |
| 47 | + } |
| 48 | + return runner.ExitToolError |
| 49 | + } |
| 50 | + if fs.NArg() != 0 { |
| 51 | + fmt.Fprintf(os.Stderr, "unexpected positional arguments: %v\n", fs.Args()) |
| 52 | + return runner.ExitToolError |
| 53 | + } |
| 54 | + for name, value := range map[string]string{ |
| 55 | + "--profile": *profilePath, |
| 56 | + "--report": *reportPath, |
| 57 | + "--out-dir": *outDir, |
| 58 | + "--verifier-id": *verifierID, |
| 59 | + } { |
| 60 | + if strings.TrimSpace(value) == "" { |
| 61 | + fmt.Fprintf(os.Stderr, "%s is required\n", name) |
| 62 | + return runner.ExitToolError |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + profile, err := conformance.LoadProfile(*profilePath) |
| 67 | + if err != nil { |
| 68 | + fmt.Fprintf(os.Stderr, "load conformance profile: %v\n", err) |
| 69 | + return runner.ExitToolError |
| 70 | + } |
| 71 | + report, err := conformance.LoadReport(*reportPath) |
| 72 | + if err != nil { |
| 73 | + fmt.Fprintf(os.Stderr, "load conformance report: %v\n", err) |
| 74 | + return runner.ExitToolError |
| 75 | + } |
| 76 | + evaluation, err := conformance.Evaluate(profile, report, conformance.EvaluateOptions{ |
| 77 | + VerifierID: strings.TrimSpace(*verifierID), |
| 78 | + ReportURL: strings.TrimSpace(*reportURL), |
| 79 | + }) |
| 80 | + if err != nil { |
| 81 | + fmt.Fprintf(os.Stderr, "evaluate conformance report: %v\n", err) |
| 82 | + return runner.ExitToolError |
| 83 | + } |
| 84 | + paths, err := conformance.WriteEvaluation(*outDir, evaluation) |
| 85 | + if err != nil { |
| 86 | + fmt.Fprintf(os.Stderr, "write conformance evidence: %v\n", err) |
| 87 | + return runner.ExitToolError |
| 88 | + } |
| 89 | + |
| 90 | + fmt.Printf("Conformance: %s\n", evaluation.Decision.Status) |
| 91 | + fmt.Printf("Profile: %s sha256:%s\n", evaluation.Decision.Profile.ID, evaluation.Decision.Profile.SHA256) |
| 92 | + fmt.Printf("Subject: %s sha256:%s\n", evaluation.Decision.Subject.Name, evaluation.Decision.Subject.Digest["sha256"]) |
| 93 | + fmt.Printf("Decision: %s\n", paths.Decision) |
| 94 | + fmt.Printf("Test Result: %s\n", paths.TestResult) |
| 95 | + if paths.VerificationResult != "" { |
| 96 | + fmt.Printf("Verification Result: %s\n", paths.VerificationResult) |
| 97 | + } else { |
| 98 | + fmt.Println("Verification Result: not issued") |
| 99 | + } |
| 100 | + |
| 101 | + switch evaluation.Decision.Status { |
| 102 | + case conformance.StatusConformant: |
| 103 | + return runner.ExitSuccess |
| 104 | + case conformance.StatusNonconformant: |
| 105 | + return runner.ExitCompatibilityFailure |
| 106 | + default: |
| 107 | + return runner.ExitToolError |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func printConformanceUsage() { |
| 112 | + fmt.Println("Usage:") |
| 113 | + fmt.Println(" bpfcompat conformance evaluate --profile <file> --report <file> --out-dir <dir> --verifier-id <uri> [--report-url <url>]") |
| 114 | +} |
0 commit comments