|
| 1 | +/* |
| 2 | +Copyright © 2025 NAME HERE <EMAIL ADDRESS> |
| 3 | +*/ |
| 4 | +package cmd |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + |
| 11 | + "github.com/slsa-framework/slsa-source-poc/sourcetool/pkg/attest" |
| 12 | + "github.com/slsa-framework/slsa-source-poc/sourcetool/pkg/gh_control" |
| 13 | + "github.com/slsa-framework/slsa-source-poc/sourcetool/pkg/policy" |
| 14 | + "github.com/spf13/cobra" |
| 15 | + "google.golang.org/protobuf/encoding/protojson" |
| 16 | +) |
| 17 | + |
| 18 | +type CheckTagArgs struct { |
| 19 | + commit string |
| 20 | + owner string |
| 21 | + repo string |
| 22 | + tagName string |
| 23 | + outputSignedBundle string |
| 24 | + useLocalPolicy string |
| 25 | +} |
| 26 | + |
| 27 | +var ( |
| 28 | + checkTagArgs CheckTagArgs |
| 29 | + // checktagCmd represents the checktag command |
| 30 | + checktagCmd = &cobra.Command{ |
| 31 | + Use: "checktag", |
| 32 | + Short: "Checks to see if the tag operation should be allowed and issues a VSA", |
| 33 | + Run: func(cmd *cobra.Command, args []string) { |
| 34 | + doCheckTag(checkTagArgs) |
| 35 | + }, |
| 36 | + } |
| 37 | +) |
| 38 | + |
| 39 | +func doCheckTag(args CheckTagArgs) { |
| 40 | + gh_connection := |
| 41 | + gh_control.NewGhConnection(args.owner, args.repo, gh_control.TagToFullRef(args.tagName)).WithAuthToken(githubToken) |
| 42 | + ctx := context.Background() |
| 43 | + verifier := getVerifier() |
| 44 | + |
| 45 | + // Create tag provenance. |
| 46 | + pa := attest.NewProvenanceAttestor(gh_connection, verifier) |
| 47 | + prov, err := pa.CreateTagProvenance(ctx, args.commit, gh_control.TagToFullRef(args.tagName)) |
| 48 | + if err != nil { |
| 49 | + log.Fatal(err) |
| 50 | + } |
| 51 | + |
| 52 | + // check p against policy |
| 53 | + pe := policy.NewPolicyEvaluator() |
| 54 | + pe.UseLocalPolicy = args.useLocalPolicy |
| 55 | + verifiedLevels, policyPath, err := pe.EvaluateTagProv(ctx, gh_connection, prov) |
| 56 | + if err != nil { |
| 57 | + log.Fatal(err) |
| 58 | + } |
| 59 | + |
| 60 | + // create vsa |
| 61 | + unsignedVsa, err := attest.CreateUnsignedSourceVsa(gh_connection.GetRepoUri(), gh_connection.GetFullRef(), args.commit, verifiedLevels, policyPath) |
| 62 | + if err != nil { |
| 63 | + log.Fatal(err) |
| 64 | + } |
| 65 | + |
| 66 | + unsignedProv, err := protojson.Marshal(prov) |
| 67 | + if err != nil { |
| 68 | + log.Fatal(err) |
| 69 | + } |
| 70 | + |
| 71 | + if args.outputSignedBundle != "" { |
| 72 | + f, err := os.OpenFile(args.outputSignedBundle, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644) |
| 73 | + if err != nil { |
| 74 | + log.Fatal(err) |
| 75 | + } |
| 76 | + defer f.Close() |
| 77 | + |
| 78 | + signedProv, err := attest.Sign(string(unsignedProv)) |
| 79 | + if err != nil { |
| 80 | + log.Fatal(err) |
| 81 | + } |
| 82 | + |
| 83 | + signedVsa, err := attest.Sign(unsignedVsa) |
| 84 | + if err != nil { |
| 85 | + log.Fatal(err) |
| 86 | + } |
| 87 | + |
| 88 | + f.WriteString(signedProv) |
| 89 | + f.WriteString("\n") |
| 90 | + f.WriteString(signedVsa) |
| 91 | + f.WriteString("\n") |
| 92 | + } else { |
| 93 | + log.Printf("unsigned prov: %s\n", unsignedProv) |
| 94 | + log.Printf("unsigned vsa: %s\n", unsignedVsa) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func init() { |
| 99 | + rootCmd.AddCommand(checktagCmd) |
| 100 | + |
| 101 | + checktagCmd.Flags().StringVar(&checkTagArgs.commit, "commit", "", "The commit to check - required.") |
| 102 | + checktagCmd.Flags().StringVar(&checkTagArgs.owner, "owner", "", "The GitHub repository owner - required.") |
| 103 | + checktagCmd.Flags().StringVar(&checkTagArgs.repo, "repo", "", "The GitHub repository name - required.") |
| 104 | + checktagCmd.Flags().StringVar(&checkTagArgs.tagName, "tag_name", "", "The name of the new tag - required.") |
| 105 | + checktagCmd.Flags().StringVar(&checkTagArgs.outputSignedBundle, "output_signed_bundle", "", "The path to write a bundle of signed attestations.") |
| 106 | + checktagCmd.Flags().StringVar(&checkTagArgs.useLocalPolicy, "use_local_policy", "", "UNSAFE: Use the policy at this local path instead of the official one.") |
| 107 | + |
| 108 | +} |
0 commit comments