/
main.go
287 lines (246 loc) · 8.07 KB
/
main.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package main
import (
"crypto/tls"
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/samber/lo"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/khulnasoft/tunnel/pkg/log"
)
type Insecure bool
func (i Insecure) NameOptions() []name.Option {
return lo.Ternary(bool(i), []name.Option{name.Insecure}, nil)
}
func (i Insecure) RemoteOptions() []remote.Option {
tr := http.DefaultTransport.(*http.Transport).Clone()
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: bool(i)}
return []remote.Option{remote.WithTransport(tr)}
}
type putOptions struct {
Insecure
Annotations map[string]string
Subject string
}
type getOptions struct {
Insecure
Type string
Reference string
Digest string
}
type listOptions struct {
Insecure
Type string
Subject string
Format string
FilterAnnotations map[string]string
}
type treeOptions struct {
Insecure
Subject string
Full bool
}
func keyValueSliceToMap(s []string) (map[string]string, error) {
m := make(map[string]string, len(s))
for _, kv := range s {
parts := strings.Split(kv, "=")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid key-value pair: %s", kv)
}
m[parts[0]] = parts[1]
}
return m, nil
}
func main() {
viper.SetEnvPrefix("tunnel")
viper.AutomaticEnv()
rootCmd := &cobra.Command{
Short: "A Tunnel plugin for oci referrers",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
debug := viper.GetBool("debug")
quiet := viper.GetBool("quiet")
if err := log.InitLogger(debug, quiet); err != nil {
return err
}
return nil
},
}
rootCmd.PersistentFlags().BoolP("debug", "d", false, "debug mode")
viper.BindPFlag("debug", rootCmd.PersistentFlags().Lookup("debug"))
rootCmd.PersistentFlags().BoolP("quiet", "q", false, "suppress log output")
viper.BindPFlag("quiet", rootCmd.PersistentFlags().Lookup("quiet"))
rootCmd.PersistentFlags().Bool("insecure", false, "allow insecure server connections")
viper.BindPFlag("insecure", rootCmd.PersistentFlags().Lookup("insecure"))
putCmd := &cobra.Command{
Use: "put",
Short: "put a referrer to the oci registry",
Example: ` tunnel image -q -f cyclonedx YOUR_IMAGE | tunnel referrer put
# Put SBOM attestation
tunnel referrer put -f sbom.json`,
RunE: func(cmd *cobra.Command, args []string) error {
path := viper.GetString("file")
var reader io.Reader
if path != "" {
fp, err := os.Open(path)
if err != nil {
return fmt.Errorf("error opening file: %w", err)
}
defer fp.Close()
reader = fp
} else {
reader = os.Stdin
}
annList := viper.GetStringSlice("annotation")
ann, err := keyValueSliceToMap(annList)
if err != nil {
return fmt.Errorf("error parsing annotations: %w", err)
}
err = putReferrer(reader, putOptions{
Annotations: ann,
Subject: viper.GetString("subject"),
Insecure: Insecure(viper.GetBool("insecure")),
})
if err != nil {
return fmt.Errorf("error putting referrer: %w", err)
}
return nil
},
}
putCmd.Flags().StringP("file", "f", "", "file path. If a file path is not specified, it will accept input from the standard input.")
viper.BindPFlag("file", putCmd.Flags().Lookup("file"))
putCmd.Flags().StringSliceP("annotation", "", nil, "annotations associated with the artifact (can specify multiple or separate values with commas: key1=path1,key2=path2)")
viper.BindPFlag("annotation", putCmd.Flags().Lookup("annotation"))
putCmd.Flags().StringP("subject", "", "", "set the subject to a reference (If the value is not set, it will attempt to detect it automatically from the input)")
viper.BindPFlag("subject", putCmd.Flags().Lookup("subject"))
rootCmd.AddCommand(putCmd)
getCmd := &cobra.Command{
Use: "get YOUR_IMAGE",
Short: "get the referrer's artifact",
Args: cobra.ExactArgs(1),
Example: ` $ tunnel referrer get --type cyclonedx YOUR_IMAGE
$ tunnel referrer get --digest DIGEST YOUR_IMAGE`,
RunE: func(cmd *cobra.Command, args []string) error {
output := viper.GetString("output")
var writer io.Writer
if output == "" {
writer = os.Stdout
} else {
fp, err := os.Create(output)
if err != nil {
return fmt.Errorf("error creating file: %w", err)
}
defer fp.Close()
writer = fp
}
t := viper.GetString("type")
digest := viper.GetString("digest")
err := getReferrer(writer, getOptions{
Insecure: Insecure(viper.GetBool("insecure")),
Type: t,
Digest: digest,
Reference: args[0],
})
if err != nil {
return fmt.Errorf("error getting referrer: %w", err)
}
return nil
},
}
getCmd.Flags().StringP("type", "", "", "artifact type (cyclonedx, spdx-json, sarif, cosign-vuln)")
viper.BindPFlag("type", getCmd.Flags().Lookup("type"))
getCmd.Flags().StringP("digest", "", "", "referrer digest. If the length of the digest is only partial, search for artifacts with matching prefixes")
viper.BindPFlag("digest", getCmd.Flags().Lookup("digest"))
getCmd.Flags().StringP("output", "o", "", "output file name")
viper.BindPFlag("output", getCmd.Flags().Lookup("output"))
rootCmd.AddCommand(getCmd)
listCmd := &cobra.Command{
Use: "list",
Short: "list referrers",
Example: ` $ tunnel referrer list YOUR_IMAGE`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
output := viper.GetString("output")
var writer io.Writer
if output == "" {
writer = os.Stdout
} else {
fp, err := os.Create(output)
if err != nil {
return fmt.Errorf("error creating file: %w", err)
}
defer fp.Close()
writer = fp
}
t := viper.GetString("type")
format := viper.GetString("format")
annList := viper.GetStringSlice("filter-annotation")
ann, err := keyValueSliceToMap(annList)
if err != nil {
return fmt.Errorf("invalid annotation: %w", err)
}
err = listReferrers(writer, listOptions{
Insecure: Insecure(viper.GetBool("insecure")),
Type: t,
Subject: args[0],
Format: format,
FilterAnnotations: ann,
})
if err != nil {
return fmt.Errorf("error getting referrer: %w", err)
}
return nil
},
}
listCmd.Flags().StringSliceP("filter-annotation", "", nil, "filter annotations associated with the artifact (can specify multiple or separate values with commas: key1=path1,key2=path2)")
viper.BindPFlag("filter-annotation", listCmd.Flags().Lookup("filter-annotation"))
listCmd.Flags().StringP("format", "", "", "format (json, oneline, table)")
viper.BindPFlag("format", listCmd.Flags().Lookup("format"))
listCmd.Flags().StringP("type", "", "", "artifact type (cyclonedx, spdx-json, sarif, cosign-vuln)")
viper.BindPFlag("type", listCmd.Flags().Lookup("type"))
listCmd.Flags().StringP("output", "o", "", "output file name")
viper.BindPFlag("output", listCmd.Flags().Lookup("output"))
rootCmd.AddCommand(listCmd)
treeCmd := &cobra.Command{
Use: "tree",
Short: "recurse into referrers and print the result as a tree",
Example: ` $ tunnel referrer tree YOUR_IMAGE`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
output := viper.GetString("output")
var writer io.Writer
if output == "" {
writer = os.Stdout
} else {
fp, err := os.Create(output)
if err != nil {
return fmt.Errorf("error creating file: %w", err)
}
defer fp.Close()
writer = fp
}
full := viper.GetBool("full")
err := treeReferrers(writer, treeOptions{
Insecure: Insecure(viper.GetBool("insecure")),
Subject: args[0],
Full: full,
})
if err != nil {
return fmt.Errorf("error getting referrer: %w", err)
}
return nil
},
}
treeCmd.Flags().BoolP("full", "", false, "output the full digests")
viper.BindPFlag("full", treeCmd.Flags().Lookup("full"))
treeCmd.Flags().StringP("output", "o", "", "output file name")
viper.BindPFlag("output", treeCmd.Flags().Lookup("output"))
rootCmd.AddCommand(treeCmd)
if err := rootCmd.Execute(); err != nil {
log.Logger.Fatal(err)
}
}