-
Notifications
You must be signed in to change notification settings - Fork 500
/
attach.go
117 lines (97 loc) · 3.71 KB
/
attach.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
//
// Copyright 2021 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package options
import (
"fmt"
"strings"
"github.com/google/go-containerregistry/pkg/v1/types"
"github.com/spf13/cobra"
ctypes "github.com/sigstore/cosign/pkg/types"
)
// AttachSignatureOptions is the top level wrapper for the attach signature command.
type AttachSignatureOptions struct {
Signature string
Payload string
Registry RegistryOptions
}
var _ Interface = (*AttachSignatureOptions)(nil)
// AddFlags implements Interface
func (o *AttachSignatureOptions) AddFlags(cmd *cobra.Command) {
o.Registry.AddFlags(cmd)
cmd.Flags().StringVar(&o.Signature, "signature", "",
"the signature, path to the signature, or {-} for stdin")
cmd.Flags().StringVar(&o.Payload, "payload", "",
"path to the payload covered by the signature (if using another format)")
}
// AttachSBOMOptions is the top level wrapper for the attach sbom command.
type AttachSBOMOptions struct {
SBOM string
SBOMType string
SBOMInputFormat string
Registry RegistryOptions
}
var _ Interface = (*AttachSBOMOptions)(nil)
// AddFlags implements Interface
func (o *AttachSBOMOptions) AddFlags(cmd *cobra.Command) {
o.Registry.AddFlags(cmd)
cmd.Flags().StringVar(&o.SBOM, "sbom", "",
"path to the sbom, or {-} for stdin")
cmd.Flags().StringVar(&o.SBOMType, "type", "spdx",
"type of sbom (spdx|cyclonedx|syft)")
cmd.Flags().StringVar(&o.SBOMInputFormat, "input-format", "",
"type of sbom input format (json|xml|text)")
}
func (o *AttachSBOMOptions) MediaType() (types.MediaType, error) {
var looksLikeJSON bool
if strings.HasSuffix(o.SBOM, ".json") {
looksLikeJSON = true
}
switch o.SBOMType {
case "cyclonedx":
if o.SBOMInputFormat != "" && o.SBOMInputFormat != ctypes.XMLInputFormat && o.SBOMInputFormat != ctypes.JSONInputFormat {
return "invalid", fmt.Errorf("invalid SBOM input format: %q, expected (json|xml)", o.SBOMInputFormat)
}
if o.SBOMInputFormat == ctypes.JSONInputFormat || looksLikeJSON {
return ctypes.CycloneDXJSONMediaType, nil
}
return ctypes.CycloneDXXMLMediaType, nil
case "spdx":
if o.SBOMInputFormat != "" && o.SBOMInputFormat != ctypes.TextInputFormat && o.SBOMInputFormat != ctypes.JSONInputFormat {
return "invalid", fmt.Errorf("invalid SBOM input format: %q, expected (json|text)", o.SBOMInputFormat)
}
if o.SBOMInputFormat == ctypes.JSONInputFormat || looksLikeJSON {
return ctypes.SPDXJSONMediaType, nil
}
return ctypes.SPDXMediaType, nil
case "syft":
if o.SBOMInputFormat != "" && o.SBOMInputFormat != ctypes.JSONInputFormat {
return "invalid", fmt.Errorf("invalid SBOM input format: %q, expected (json)", o.SBOMInputFormat)
}
return ctypes.SyftMediaType, nil
default:
return "unknown", fmt.Errorf("unknown SBOM type: %q, expected (spdx|cyclonedx|syft)", o.SBOMType)
}
}
// AttachAttestationOptions is the top level wrapper for the attach attestation command.
type AttachAttestationOptions struct {
Attestation string
Registry RegistryOptions
}
// AddFlags implements Interface
func (o *AttachAttestationOptions) AddFlags(cmd *cobra.Command) {
o.Registry.AddFlags(cmd)
cmd.Flags().StringVar(&o.Attestation, "attestation", "",
"path to the attestation envelope")
}