forked from in-toto/in-toto-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsign.go
88 lines (73 loc) · 1.67 KB
/
sign.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package cmd
import (
"fmt"
intoto "github.com/in-toto/in-toto-golang/in_toto"
"github.com/spf13/cobra"
)
var (
outputPath string
verifyFile bool
)
var signCmd = &cobra.Command{
Use: "sign",
Short: "Provides command line interface to sign in-toto link or layout metadata",
Long: `Provides command line interface to sign in-toto link or layout metadata`,
RunE: sign,
}
func init() {
rootCmd.AddCommand(signCmd)
signCmd.Flags().StringVarP(
&outputPath,
"output",
"o",
"",
`Path to store metadata file after signing`,
)
signCmd.Flags().StringVarP(
&layoutPath,
"file",
"f",
"",
`Path to link or layout file to be signed or verified.`,
)
signCmd.Flags().StringVarP(
&keyPath,
"key",
"k",
"",
`Path to PEM formatted private key used to sign the passed
root layout's signature(s). Passing exactly one key using
'--key' is required.`,
)
signCmd.Flags().BoolVar(
&verifyFile,
"verify",
false,
"Verify signature of signed file",
)
signCmd.MarkFlagRequired("file")
signCmd.MarkFlagRequired("key")
}
func sign(cmd *cobra.Command, args []string) error {
layoutEnv, err := intoto.LoadMetadata(layoutPath)
if err != nil {
return fmt.Errorf("failed to load layout at %s: %w", layoutPath, err)
}
key = intoto.Key{}
if err := key.LoadKeyDefaults(keyPath); err != nil {
return fmt.Errorf("invalid key at %s: %w", keyPath, err)
}
if verifyFile {
if err := layoutEnv.VerifySignature(key); err != nil {
return fmt.Errorf("signature verification failed: %w", err)
}
return nil
}
if len(outputPath) == 0 {
outputPath = layoutPath
}
if err := layoutEnv.Sign(key); err != nil {
return err
}
return layoutEnv.Dump(outputPath)
}