-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtypes.go
89 lines (76 loc) · 2.46 KB
/
types.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
package main
import "fmt"
const (
// ref. https://github.com/opencontainers/image-spec/blob/dd7fd714f5406d39db5fd0602a0e6090929dc85e/annotations.md#pre-defined-annotation-keys
annotationKeyCreated = "org.opencontainers.artifact.created"
annotationKeyDescription = "org.opencontainers.artifact.description"
annotationKeyImageCreated = "org.opencontainers.image.created"
annotationKeyImageDescription = "org.opencontainers.image.description"
customAnnotationKeyDescription = "created-by"
// Use a Media Type registered with IANA.
// ref. https://github.com/opencontainers/image-spec/blob/dd7fd714f5406d39db5fd0602a0e6090929dc85e/artifact.md#artifact-manifest-property-descriptions
// ref. https://www.iana.org/assignments/media-types/media-types.xhtml
mediaKeyCycloneDX = "application/vnd.cyclonedx+json"
mediaKeySPDX = "application/spdx+json"
mediaKeySARIF = "application/sarif+json"
// 2023/4/4: Since there is no MediaType specialized for vulnerability information registered with IANA, we use the json type.
mediaKeyCosignVuln = "application/json"
)
type ArtifactType string
const (
CycloneDX ArtifactType = "cyclonedx"
SPDXJSON ArtifactType = "spdx-json"
SARIF ArtifactType = "sarif"
CosignVuln ArtifactType = "cosign-vuln"
)
func IsCreatedKey(key string) bool {
return key == annotationKeyCreated || key == annotationKeyImageCreated
}
func IsDescriptionKey(key string) bool {
return key == annotationKeyDescription || key == annotationKeyImageDescription
}
func (at ArtifactType) String() string {
return string(at)
}
func (at ArtifactType) MediaType() string {
switch at {
case CycloneDX:
return mediaKeyCycloneDX
case SPDXJSON:
return mediaKeySPDX
case SARIF:
return mediaKeySARIF
case CosignVuln:
return mediaKeyCosignVuln
default:
return ""
}
}
func artifactTypeFromName(name string) (ArtifactType, error) {
switch name {
case CycloneDX.String():
return CycloneDX, nil
case SPDXJSON.String():
return SPDXJSON, nil
case SARIF.String():
return SARIF, nil
case CosignVuln.String():
return CosignVuln, nil
default:
return "", fmt.Errorf("unknown artifact name: " + name)
}
}
func artifactTypeFromMediaType(mediaType string) (ArtifactType, error) {
switch mediaType {
case mediaKeyCycloneDX:
return CycloneDX, nil
case mediaKeySPDX:
return SPDXJSON, nil
case mediaKeySARIF:
return SARIF, nil
case mediaKeyCosignVuln:
return CosignVuln, nil
default:
return "", fmt.Errorf("unknown media type: " + mediaType)
}
}