|
| 1 | +/* |
| 2 | +Copyright © 2025 NAME HERE <EMAIL ADDRESS> |
| 3 | +*/ |
| 4 | +package cmd |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "log" |
| 11 | + |
| 12 | + "github.com/spf13/cobra" |
| 13 | + |
| 14 | + "github.com/slsa-framework/slsa-source-poc/sourcetool/pkg/attest" |
| 15 | + "github.com/slsa-framework/slsa-source-poc/sourcetool/pkg/audit" |
| 16 | + "github.com/slsa-framework/slsa-source-poc/sourcetool/pkg/ghcontrol" |
| 17 | +) |
| 18 | + |
| 19 | +type AuditMode int |
| 20 | + |
| 21 | +const ( |
| 22 | + AuditModeBasic AuditMode = 1 |
| 23 | + AuditModeFull AuditMode = 2 |
| 24 | +) |
| 25 | + |
| 26 | +// Enable audit mode enum |
| 27 | +// String is used both by fmt.Print and by Cobra in help text |
| 28 | +func (e *AuditMode) String() string { |
| 29 | + switch *e { |
| 30 | + case AuditModeBasic: |
| 31 | + return "basic" |
| 32 | + case AuditModeFull: |
| 33 | + return "full" |
| 34 | + } |
| 35 | + return "error" |
| 36 | +} |
| 37 | + |
| 38 | +// Set must have pointer receiver so it doesn't change the value of a copy |
| 39 | +func (e *AuditMode) Set(v string) error { |
| 40 | + switch v { |
| 41 | + case "basic": |
| 42 | + *e = AuditModeBasic |
| 43 | + return nil |
| 44 | + case "full": |
| 45 | + *e = AuditModeFull |
| 46 | + return nil |
| 47 | + default: |
| 48 | + return errors.New(`must be one of "foo", "bar", or "moo"`) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// Type is only used in help text |
| 53 | +func (e *AuditMode) Type() string { |
| 54 | + return "AuditMode" |
| 55 | +} |
| 56 | + |
| 57 | +type AuditArgs struct { |
| 58 | + owner string |
| 59 | + repo string |
| 60 | + branch string |
| 61 | + auditDepth int |
| 62 | + endingCommit string |
| 63 | + auditMode AuditMode |
| 64 | +} |
| 65 | + |
| 66 | +func (aa *AuditArgs) Validate() error { |
| 67 | + if aa.owner == "" || aa.repo == "" || aa.branch == "" { |
| 68 | + return errors.New("must set owner, repo, and branch flags") |
| 69 | + } |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +var ( |
| 74 | + auditArgs = &AuditArgs{} |
| 75 | + |
| 76 | + auditCmd = &cobra.Command{ |
| 77 | + Use: "audit", |
| 78 | + Short: "Audits the SLSA properties and controls of a repository", |
| 79 | + Long: `Checks the revisions on the specified branch within the repository. |
| 80 | +
|
| 81 | +Revisions 'pass'and audit if they have: |
| 82 | +1. A corresponding VSA |
| 83 | +2. Corresponding source provenance |
| 84 | +3. The revision (commit) listed in the provenance matches the revision reported by GitHub |
| 85 | +
|
| 86 | +Future: |
| 87 | +* Check the provenance to validate the verifiedLevels in the VSA match expectations |
| 88 | + (i.e. that the VSA was issued correctly) |
| 89 | +`, |
| 90 | + Run: func(cmd *cobra.Command, args []string) { |
| 91 | + err := doAudit(auditArgs) |
| 92 | + if err != nil { |
| 93 | + log.Fatal(err) |
| 94 | + } |
| 95 | + }, |
| 96 | + } |
| 97 | +) |
| 98 | + |
| 99 | +func printResult(ghc *ghcontrol.GitHubConnection, ar *audit.AuditCommitResult, mode AuditMode) { |
| 100 | + good := ar.IsGood() |
| 101 | + status := "passed" |
| 102 | + if !good { |
| 103 | + status = "failed" |
| 104 | + } |
| 105 | + fmt.Printf("commit: %s - %v\n", ar.Commit, status) |
| 106 | + |
| 107 | + if good && AuditModeBasic == mode { |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + if ar.VsaPred != nil { |
| 112 | + fmt.Printf("\tvsa: %v\n", ar.VsaPred.GetVerifiedLevels()) |
| 113 | + } else { |
| 114 | + fmt.Printf("\tvsa: none\n") |
| 115 | + } |
| 116 | + if ar.ProvPred != nil { |
| 117 | + fmt.Print("\tprov:\n") |
| 118 | + fmt.Printf("\t\tcontrols: %v\n", ar.ProvPred.Controls) |
| 119 | + if ar.ProvPred.PrevCommit == ar.GhPriorCommit { |
| 120 | + fmt.Printf("\t\tPrevCommit matches GH commit: true\n") |
| 121 | + } else { |
| 122 | + fmt.Printf("\t\tPrevCommit matches GH commit: false: %s != %s\n", ar.ProvPred.PrevCommit, ar.GhPriorCommit) |
| 123 | + } |
| 124 | + } else { |
| 125 | + fmt.Printf("\tprov: none\n") |
| 126 | + } |
| 127 | + if ar.GhControlStatus != nil { |
| 128 | + fmt.Printf("\tgh controls: %v\n", ar.GhControlStatus.Controls) |
| 129 | + } |
| 130 | + |
| 131 | + fmt.Printf("\tlink: https://github.com/%s/%s/commit/%s\n", ghc.Owner(), ghc.Repo(), ar.GhPriorCommit) |
| 132 | +} |
| 133 | + |
| 134 | +func doAudit(auditArgs *AuditArgs) error { |
| 135 | + err := auditArgs.Validate() |
| 136 | + if err != nil { |
| 137 | + return err |
| 138 | + } |
| 139 | + |
| 140 | + ghc := ghcontrol.NewGhConnection(auditArgs.owner, auditArgs.repo, ghcontrol.BranchToFullRef(auditArgs.branch)).WithAuthToken(githubToken) |
| 141 | + ctx := context.Background() |
| 142 | + verifier := getVerifier() |
| 143 | + pa := attest.NewProvenanceAttestor(ghc, verifier) |
| 144 | + |
| 145 | + auditor := audit.NewAuditor(ghc, pa, verifier) |
| 146 | + |
| 147 | + latestCommit, err := ghc.GetLatestCommit(ctx, auditArgs.branch) |
| 148 | + if err != nil { |
| 149 | + return fmt.Errorf("could not get latest commit for %s", auditArgs.branch) |
| 150 | + } |
| 151 | + |
| 152 | + fmt.Printf("Auditing branch %s starting from revision %s\n", auditArgs.branch, latestCommit) |
| 153 | + |
| 154 | + count := 0 |
| 155 | + for ar, err := range auditor.AuditBranch(ctx, auditArgs.branch) { |
| 156 | + if ar == nil { |
| 157 | + return err |
| 158 | + } |
| 159 | + if err != nil { |
| 160 | + fmt.Printf("\terror: %v\n", err) |
| 161 | + } |
| 162 | + printResult(ghc, ar, auditArgs.auditMode) |
| 163 | + if auditArgs.endingCommit != "" && auditArgs.endingCommit == ar.Commit { |
| 164 | + fmt.Printf("Found ending commit %s\n", auditArgs.endingCommit) |
| 165 | + return nil |
| 166 | + } |
| 167 | + if auditArgs.auditDepth > 0 && count >= auditArgs.auditDepth { |
| 168 | + fmt.Printf("Reached depth limit %d\n", auditArgs.auditDepth) |
| 169 | + return nil |
| 170 | + } |
| 171 | + count++ |
| 172 | + } |
| 173 | + |
| 174 | + return nil |
| 175 | +} |
| 176 | + |
| 177 | +func init() { |
| 178 | + rootCmd.AddCommand(auditCmd) |
| 179 | + |
| 180 | + auditCmd.Flags().StringVar(&auditArgs.owner, "owner", "", "The GitHub repository owner - required.") |
| 181 | + auditCmd.Flags().StringVar(&auditArgs.repo, "repo", "", "The GitHub repository name - required.") |
| 182 | + auditCmd.Flags().StringVar(&auditArgs.branch, "branch", "", "The branch within the repository - required.") |
| 183 | + auditCmd.Flags().IntVar(&auditArgs.auditDepth, "depth", 0, "The max number of revisions to audit (depth <= audit all revisions).") |
| 184 | + auditCmd.Flags().StringVar(&auditArgs.endingCommit, "ending-commit", "", "The commit to stop auditing at.") |
| 185 | + auditArgs.auditMode = AuditModeBasic |
| 186 | + auditCmd.Flags().Var(&auditArgs.auditMode, "audit-mode", "'basic' for limited details (default), 'full' for all details") |
| 187 | +} |
0 commit comments