-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
106 lines (88 loc) · 2.62 KB
/
util.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
// This file contains code of CycloneDX GoMod
// Copyright 2024 The Heimdall authors, OWASP Foundation
//
// 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
//
// https://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.
//go:build !no_cyclonedx
package cyclonedx
import (
"fmt"
"os"
"time"
cdx "github.com/CycloneDX/cyclonedx-go"
"github.com/google/uuid"
"github.com/rs/zerolog"
)
func addCommonMetadata(logger zerolog.Logger, bom *cdx.BOM) error {
if bom.Metadata == nil {
bom.Metadata = &cdx.Metadata{}
}
tool, err := buildToolMetadata(logger)
if err != nil {
return fmt.Errorf("failed to build tool metadata: %w", err)
}
bom.Metadata.Timestamp = time.Now().Format(time.RFC3339)
bom.Metadata.Tools = &[]cdx.Tool{*tool}
return nil
}
// setSerialNumber sets the serial number of a given BOM according to the provided SBOMOptions.
func setSerialNumber(bom *cdx.BOM, cfg goModCfg) error {
if cfg.noserial {
return nil
}
if cfg.serial == "" {
bom.SerialNumber = uuid.New().URN()
} else {
serial, err := uuid.Parse(cfg.serial)
if err != nil {
return err
}
bom.SerialNumber = serial.URN()
}
return nil
}
// writeBOM writes the given bom according to the provided OutputOptions.
func writeBOM(bom *cdx.BOM, cfg goModCfg) error {
var outputFormat cdx.BOMFileFormat
if cfg.json {
outputFormat = cdx.BOMFileFormatJSON
} else {
outputFormat = cdx.BOMFileFormatXML
}
outputVersion, err := parseSpecVersion(cfg.outputVersion)
if err != nil {
return fmt.Errorf("failed to parse output version: %w", err)
}
encoder := cdx.NewBOMEncoder(os.Stdout, outputFormat)
encoder.SetPretty(true)
if err = encoder.EncodeVersion(bom, outputVersion); err != nil {
return fmt.Errorf("failed to encode sbom: %w", err)
}
return err
}
func parseSpecVersion(specVersion string) (sv cdx.SpecVersion, err error) {
switch specVersion {
case cdx.SpecVersion1_0.String():
sv = cdx.SpecVersion1_0
case cdx.SpecVersion1_1.String():
sv = cdx.SpecVersion1_1
case cdx.SpecVersion1_2.String():
sv = cdx.SpecVersion1_2
case cdx.SpecVersion1_3.String():
sv = cdx.SpecVersion1_3
case cdx.SpecVersion1_4.String():
sv = cdx.SpecVersion1_4
default:
err = cdx.ErrInvalidSpecVersion
}
return
}