-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Marcela Melara <[email protected]>
- Loading branch information
1 parent
e57ba1e
commit 900a062
Showing
5 changed files
with
348 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/pem" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/in-toto/scai-demos/scai-gen/pkg/fileio" | ||
|
||
sigbundle "github.com/sigstore/protobuf-specs/gen/pb-go/bundle/v1" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var parseCmd = &cobra.Command{ | ||
Use: "parse-sigstore", | ||
Short: "Parses a JSON-encoded Sigstore bundle", | ||
} | ||
|
||
var pubkeyCmd = &cobra.Command{ | ||
Use: "pubkey", | ||
Args: cobra.ExactArgs(1), | ||
Short: "Outputs the PEM-formatted public key contained in a Sigstore bundle body", | ||
RunE: getPubKey, | ||
} | ||
|
||
func init() { | ||
pubkeyCmd.Flags().StringVarP( | ||
&outFile, | ||
"out-file", | ||
"o", | ||
"", | ||
"Filename to write out the JSON-encoded object", | ||
) | ||
|
||
parseCmd.AddCommand(pubkeyCmd) | ||
} | ||
|
||
func getPubKey(_ *cobra.Command, args []string) error { | ||
// read in the sigstore bundle file | ||
bundleFile := args[0] | ||
bundle := &sigbundle.Bundle{} | ||
|
||
err := fileio.ReadPbFromFile(bundleFile, bundle) | ||
if err != nil { | ||
return fmt.Errorf("failed to read Sigstore bundle file %s: %w", bundleFile, err) | ||
} | ||
|
||
certChain := bundle.GetVerificationMaterial().GetX509CertificateChain() | ||
if certChain == nil { | ||
return fmt.Errorf("failed to retrieve x509 certificatefrom Sigstore bundle %s", bundleFile) | ||
} | ||
|
||
certs := certChain.GetCertificates() | ||
if certs == nil || len(certs[0].GetRawBytes()) == 0 { | ||
return fmt.Errorf("failed to retrieve x509 leaf certificate from Sigstore bundle %s", bundleFile) | ||
} | ||
|
||
block := &pem.Block{ | ||
Type: "CERTIFICATE", | ||
Bytes: certs[0].GetRawBytes(), | ||
} | ||
|
||
encoded := pem.EncodeToMemory(block) | ||
if encoded == nil { | ||
return fmt.Errorf("failed to PEM-encode x509 certificate for Sigstore bundle %s", bundleFile) | ||
} | ||
|
||
|
||
if len(outFile) > 0 { | ||
err = os.WriteFile(outFile, encoded, 0644) | ||
} else{ | ||
fmt.Printf("Parsed: \n\n%s", string(encoded)) | ||
err = nil | ||
} | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// rootCmd represents the base command when called without any subcommands | ||
var rootCmd = &cobra.Command{ | ||
Use: "in-toto-attest", | ||
Short: "A CLI tool for generating/verifying attestations about build environments", | ||
} | ||
|
||
var ( | ||
outFile string | ||
) | ||
|
||
// Execute adds all child commands to the root command and sets flags appropriately. | ||
// This is called by main.main(). It only needs to happen once to the rootCmd. | ||
func Execute() { | ||
err := rootCmd.Execute() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(generateCmd) | ||
rootCmd.AddCommand(parseCmd) | ||
} | ||
|
||
func main() { | ||
Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.