This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
/
create.go
234 lines (194 loc) · 6.15 KB
/
create.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
Copyright 2023 Chainguard, Inc.
SPDX-License-Identifier: Apache-2.0
*/
package cmd
import (
"errors"
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"chainguard.dev/vex/pkg/vex"
)
type createOptions struct {
vexDocOptions
vexStatementOptions
outFilePath string
}
// Validates the options in context with arguments
func (o *createOptions) Validate(args []string) error {
if o.Status != string(vex.StatusAffected) && o.ActionStatement == vex.NoActionStatementMsg {
o.ActionStatement = ""
}
if len(args) == 0 && len(o.Products) == 0 {
return errors.New("a required product id is required to generate a valid VEX statement")
}
if len(args) < 2 && o.Vulnerability == "" {
return errors.New("a vulnerability ID is required to generate a valid VEX statement")
}
if len(args) < 3 && o.Status == "" {
return fmt.Errorf("a valid impact status is required, one of %s", strings.Join(vex.Statuses(), ", "))
}
if len(args) >= 2 && o.Vulnerability != "" && args[1] != o.Vulnerability {
return errors.New("vulnerability can only be specified once")
}
if len(args) >= 3 && o.Status != "" && args[2] != o.Status {
return errors.New("status can only be specified once")
}
return nil
}
func addCreate(parentCmd *cobra.Command) {
opts := createOptions{}
createCmd := &cobra.Command{
Short: fmt.Sprintf("%s create: creates a new VEX document", appname),
Long: fmt.Sprintf(`%s create: creates a new VEX document
The create subcommand generates a single statement document
from the command line. This is intended for simple use cases
or to get a base document to get started.
You can specify multiple products and customize the metadata of
the document via the command line flags. %s will honor the
SOURCE_DATE_EPOCH environment variable and use that date for
the document (it can be formatted in UNIX time or RFC3339).
If you don't specify an ID for the document, one will be generated
using its canonicalization hash.
Examples:
# Generate a document stating that CVE-2023-12345 was fixed in the
# git package of Wolfi:
%s create "pkg:apk/wolfi/git@2.39.0-r1?arch=x86_64" CVE-2023-12345 fixed
# You can specify more than one product. %s will read one from
# the argument but you can control all parameters through command line
# flags. Here's an example with two products in the same document:
%s create --product="pkg:apk/wolfi/git@2.39.0-r1?arch=x86_64" \
--product="pkg:apk/wolfi/git@2.39.0-r1?arch=armv7" \
--vuln="CVE-2023-12345" \
--status="fixed"
# not_affected statements need a justification:
%s create --product="pkg:apk/wolfi/trivy@0.36.1-r0?arch=x86_64" \
--vuln="CVE-2023-12345" \
--status="not_affected" \
--justification="component_not_present"
`, appname, appname, appname, appname, appname, appname),
Use: "create [flags] [product_id [vuln_id [status]]]",
Example: fmt.Sprintf("%s create \"pkg:apk/wolfi/git@2.39.0-r1?arch=x86_64\" CVE-2022-39260 fixed ", appname),
SilenceUsage: false,
SilenceErrors: false,
PersistentPreRunE: initLogging,
RunE: func(cmd *cobra.Command, args []string) error {
if err := opts.Validate(args); err != nil {
return err
}
// If we have arguments, add them
for i := range args {
switch i {
case 0:
opts.Products = append(opts.Products, args[i])
case 1:
opts.Vulnerability = args[i]
case 2:
opts.Status = args[i]
}
}
newDoc := vex.New()
statement := vex.Statement{
Vulnerability: opts.Vulnerability,
Products: opts.Products,
Subcomponents: opts.Subcomponents,
Status: vex.Status(opts.Status),
StatusNotes: opts.StatusNotes,
Justification: vex.Justification(opts.Justification),
ImpactStatement: opts.ImpactStatement,
ActionStatement: opts.ActionStatement,
}
if err := statement.Validate(); err != nil {
return fmt.Errorf("invalid statement: %w", err)
}
newDoc.Statements = append(newDoc.Statements, statement)
if _, err := newDoc.GenerateCanonicalID(); err != nil {
return fmt.Errorf("generating document id: %w", err)
}
out := os.Stdout
if opts.outFilePath != "" {
f, err := os.Create(opts.outFilePath)
if err != nil {
return fmt.Errorf("opening VEX file to write document: %w", err)
}
out = f
defer f.Close()
}
if err := newDoc.ToJSON(out); err != nil {
return fmt.Errorf("writing new VEX document: %w", err)
}
if opts.outFilePath != "" {
fmt.Fprintf(os.Stderr, " > VEX document written to %s\n", opts.outFilePath)
}
return nil
},
}
createCmd.PersistentFlags().StringVar(
&opts.DocumentID,
"id",
"",
"ID for the new VEX document (default will be computed)",
)
createCmd.PersistentFlags().StringVar(
&opts.Author,
"author",
vex.DefaultAuthor,
"author to record in the new document",
)
createCmd.PersistentFlags().StringVar(
&opts.AuthorRole,
"author-role",
vex.DefaultRole,
"author role to record in the new document",
)
createCmd.PersistentFlags().StringVarP(
&opts.Vulnerability,
"vuln",
"v",
"",
"vulnerability to add to the statement (eg CVE-2023-12345)",
)
createCmd.PersistentFlags().StringSliceVarP(
&opts.Products,
"product",
"p",
[]string{},
"list of products to list in the statement, at least one is required",
)
createCmd.PersistentFlags().StringVarP(
&opts.Status,
"status",
"s",
"",
fmt.Sprintf("status of the product vs the vulnerability, see '%s show statuses' for list", appname),
)
createCmd.PersistentFlags().StringSliceVar(
&opts.Products,
"subcomponents",
[]string{},
"list of subcomponents to add to the statement",
)
createCmd.PersistentFlags().StringVarP(
&opts.Justification,
"justification",
"j",
"",
fmt.Sprintf("justification for not_affected status, see '%s show justifications' for list", appname),
)
createCmd.PersistentFlags().StringVarP(
&opts.ActionStatement,
"action-statement",
"a",
vex.NoActionStatementMsg,
"action statement for affected status",
)
createCmd.PersistentFlags().StringVar(
&opts.outFilePath,
"file",
"",
"file to write the document (default is STDOUT)",
)
parentCmd.AddCommand(createCmd)
}