-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
example_test.go
143 lines (130 loc) · 4.42 KB
/
example_test.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
// This file is part of CycloneDX Go
//
// 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.
//
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) OWASP Foundation. All Rights Reserved.
package cyclonedx_test
import (
"fmt"
"net/http"
"os"
cdx "github.com/CycloneDX/cyclonedx-go"
)
// This example demonstrates how to create and encode a BOM in CycloneDX format.
func Example_encode() {
metadata := cdx.Metadata{
// Define metadata about the main component
// (the component which the BOM will describe)
Component: &cdx.Component{
BOMRef: "pkg:golang/acme-inc/[email protected]",
Type: cdx.ComponentTypeApplication,
Name: "ACME Application",
Version: "v1.0.0",
},
// Use properties to include an internal identifier for this BOM
// https://cyclonedx.org/use-cases/#properties--name-value-store
Properties: &[]cdx.Property{
{
Name: "internal:bom-identifier",
Value: "123456789",
},
},
}
// Define the components that acme-app ships with
// https://cyclonedx.org/use-cases/#inventory
components := []cdx.Component{
{
BOMRef: "pkg:golang/github.com/CycloneDX/[email protected]",
Type: cdx.ComponentTypeLibrary,
Author: "CycloneDX",
Name: "cyclonedx-go",
Version: "v0.3.0",
PackageURL: "pkg:golang/github.com/CycloneDX/[email protected]",
},
}
// Define the dependency graph
// https://cyclonedx.org/use-cases/#dependency-graph
dependencies := []cdx.Dependency{
{
Ref: "pkg:golang/acme-inc/[email protected]",
Dependencies: &[]string{
"pkg:golang/github.com/CycloneDX/[email protected]",
},
},
{
Ref: "pkg:golang/github.com/CycloneDX/[email protected]",
},
}
// Assemble the BOM
bom := cdx.NewBOM()
bom.Metadata = &metadata
bom.Components = &components
bom.Dependencies = &dependencies
// Encode the BOM
err := cdx.NewBOMEncoder(os.Stdout, cdx.BOMFileFormatXML).
SetPretty(true).
Encode(bom)
if err != nil {
panic(err)
}
// Output:
// <?xml version="1.0" encoding="UTF-8"?>
// <bom xmlns="http://cyclonedx.org/schema/bom/1.6" version="1">
// <metadata>
// <component bom-ref="pkg:golang/acme-inc/[email protected]" type="application">
// <name>ACME Application</name>
// <version>v1.0.0</version>
// </component>
// <properties>
// <property name="internal:bom-identifier">123456789</property>
// </properties>
// </metadata>
// <components>
// <component bom-ref="pkg:golang/github.com/CycloneDX/[email protected]" type="library">
// <author>CycloneDX</author>
// <name>cyclonedx-go</name>
// <version>v0.3.0</version>
// <purl>pkg:golang/github.com/CycloneDX/[email protected]</purl>
// </component>
// </components>
// <dependencies>
// <dependency ref="pkg:golang/acme-inc/[email protected]">
// <dependency ref="pkg:golang/github.com/CycloneDX/[email protected]"></dependency>
// </dependency>
// <dependency ref="pkg:golang/github.com/CycloneDX/[email protected]"></dependency>
// </dependencies>
// </bom>
}
// This example demonstrates how to decode and work with BOMs in CycloneDX format.
func Example_decode() {
// Acquire a BOM (e.g. by downloading it)
res, err := http.Get("https://github.com/DependencyTrack/dependency-track/releases/download/4.1.0/bom.json")
if err != nil {
panic(err)
}
defer res.Body.Close()
// Decode the BOM
bom := new(cdx.BOM)
decoder := cdx.NewBOMDecoder(res.Body, cdx.BOMFileFormatJSON)
if err = decoder.Decode(bom); err != nil {
panic(err)
}
fmt.Printf("Successfully decoded BOM of %s\n", bom.Metadata.Component.PackageURL)
fmt.Printf("- Generated: %s with %s\n", bom.Metadata.Timestamp, (*bom.Metadata.Tools.Tools)[0].Name)
fmt.Printf("- Components: %d\n", len(*bom.Components))
// Output:
// Successfully decoded BOM of pkg:maven/org.dependencytrack/[email protected]
// - Generated: 2021-02-09T20:40:32Z with CycloneDX Maven plugin
// - Components: 167
}