Skip to content

Commit 52e3aca

Browse files
Merge pull request #314 from luotianqi777/bomsw
feat: support bomsw
2 parents 1e81968 + 524b933 commit 52e3aca

File tree

7 files changed

+194
-250
lines changed

7 files changed

+194
-250
lines changed

cmd/format/bomsw.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package format
2+
3+
import (
4+
"encoding/json"
5+
"io"
6+
7+
"github.com/xmirrorsecurity/opensca-cli/v3/cmd/detail"
8+
"github.com/xmirrorsecurity/opensca-cli/v3/opensca/model"
9+
)
10+
11+
func BomSWJson(report Report, out string) {
12+
outWrite(out, func(w io.Writer) error {
13+
doc := bomSWDoc(report)
14+
encoder := json.NewEncoder(w)
15+
encoder.SetIndent("", " ")
16+
return encoder.Encode(doc)
17+
})
18+
}
19+
20+
func bomSWDoc(report Report) *model.BomSWDocument {
21+
22+
doc := model.NewBomSWDocument(report.TaskInfo.AppName, "opensca-cli")
23+
24+
report.DepDetailGraph.ForEach(func(n *detail.DepDetailGraph) bool {
25+
26+
if n.Name == "" {
27+
return true
28+
}
29+
30+
lics := []string{}
31+
for _, lic := range n.Licenses {
32+
lics = append(lics, lic.ShortName)
33+
}
34+
doc.AppendComponents(func(swc *model.BomSWComponent) {
35+
swc.ID = n.Purl()
36+
swc.Name = n.Name
37+
swc.Version = n.Version
38+
swc.License = lics
39+
})
40+
41+
children := []string{}
42+
for _, c := range n.Children {
43+
if c.Name == "" {
44+
continue
45+
}
46+
children = append(children, c.Purl())
47+
}
48+
doc.AppendDependencies(n.Purl(), children)
49+
50+
return true
51+
})
52+
53+
return doc
54+
}

cmd/format/dpsbom.go

Lines changed: 0 additions & 117 deletions
This file was deleted.

cmd/format/save.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@ func Save(report Report, output string) {
3939
switch filepath.Ext(out) {
4040
case ".html":
4141
Html(genReport(report), out)
42-
case ".zip":
43-
if strings.HasSuffix(out, ".dpsbom.zip") {
44-
DpSbomZip(report, out)
45-
} else {
46-
Json(genReport(report), out)
47-
}
4842
case ".json":
4943
if strings.HasSuffix(out, ".spdx.json") {
5044
SpdxJson(report, out)
@@ -54,13 +48,13 @@ func Save(report Report, output string) {
5448
CycloneDXJson(report, out)
5549
} else if strings.HasSuffix(out, ".swid.json") {
5650
SwidJson(report, out)
57-
} else if strings.HasSuffix(out, ".dpsbom.json") {
58-
DpSbomZip(report, out)
51+
} else if strings.HasSuffix(out, ".bomsw.json") {
52+
BomSWJson(report, out)
5953
} else {
6054
Json(genReport(report), out)
6155
}
62-
case ".dpsbom":
63-
DpSbomZip(report, out)
56+
case ".sw", ".bom-sw", ".bomsw":
57+
BomSWJson(report, out)
6458
case ".dsdx":
6559
Dsdx(report, out)
6660
case ".spdx":

opensca/model/bomsw.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package model
2+
3+
import (
4+
"time"
5+
)
6+
7+
type BomSWDocument struct {
8+
Basic swBasicInfo `json:"documentBasicInfo"`
9+
Software swSoftwareCompositionInfo `json:"softwareCompositionInfo"`
10+
}
11+
12+
type swBasicInfo struct {
13+
// 文档名称
14+
DocumentName string `json:"documentName"`
15+
// 文档版本
16+
DocumentVersion string `json:"documentVersion"`
17+
// 文档创建/更新时间 yyyy-MM-ddTHH:mm:ssTZD
18+
DocumentTime string `json:"timestamp"`
19+
// 文档格式
20+
SbomFormat string `json:"sbomFormat"`
21+
// 生成工具
22+
ToolInfo string `json:"toolInfo"`
23+
// bom作者
24+
SbomAuthor string `json:"sbomAuthor"`
25+
// 文档作者注释
26+
SbomAuthorComments string `json:"sbomAuthorComments"`
27+
// 文档注释
28+
SbomComments string `json:"sbomComments"`
29+
// 文档类型
30+
SbomType string `json:"sbomType"`
31+
}
32+
33+
type swSoftwareCompositionInfo struct {
34+
// 组件列表
35+
Components []BomSWComponent `json:"components"`
36+
// 依赖关系
37+
Dependencies []swDependencies `json:"dependencies"`
38+
}
39+
40+
type BomSWComponent struct {
41+
Author map[string]string `json:"componentAuthor"`
42+
Provider map[string]string `json:"componentProvider"`
43+
Name string `json:"componentName"`
44+
Version string `json:"componentVersion"`
45+
// map[hash算法]hash值
46+
HashValue []swChecksumValue `json:"componentHashValue"`
47+
ID string `json:"componentId"`
48+
License []string `json:"license"`
49+
// 组件信息更新时间 yyyy-MM-ddTHH:mm:ssTZD
50+
Timestamp string `json:"componentTimestamp"`
51+
}
52+
53+
type swChecksumValue struct {
54+
Algorithm string `json:"algorithm"`
55+
Value string `json:"hashValue"`
56+
}
57+
58+
type swDependencies struct {
59+
Ref string `json:"ref"`
60+
DependsOn []struct {
61+
Ref string `json:"ref"`
62+
} `json:"dependsOn"`
63+
}
64+
65+
func newDependencies(ref string, dependsOn []string) swDependencies {
66+
deps := swDependencies{Ref: ref}
67+
deps.DependsOn = make([]struct {
68+
Ref string `json:"ref"`
69+
}, len(dependsOn))
70+
for i, d := range dependsOn {
71+
deps.DependsOn[i].Ref = d
72+
}
73+
return deps
74+
}
75+
76+
func NewBomSWDocument(name, creator string) *BomSWDocument {
77+
version := "1.0.0"
78+
timestamp := time.Now().Format("2006-01-02T15:04:05MST")
79+
return &BomSWDocument{
80+
Basic: swBasicInfo{
81+
DocumentName: name,
82+
DocumentVersion: version,
83+
DocumentTime: timestamp,
84+
SbomFormat: "BOM-SW 1.0",
85+
ToolInfo: creator,
86+
SbomAuthor: "",
87+
SbomAuthorComments: "",
88+
SbomComments: "",
89+
SbomType: "analyzed",
90+
},
91+
Software: swSoftwareCompositionInfo{
92+
Dependencies: []swDependencies{},
93+
},
94+
}
95+
}
96+
97+
func (doc *BomSWDocument) AppendComponents(fn func(*BomSWComponent)) {
98+
c := BomSWComponent{
99+
Author: map[string]string{
100+
"name": "NONE",
101+
},
102+
Provider: map[string]string{
103+
"shortName": "NONE",
104+
"fullName": "NONE",
105+
},
106+
HashValue: []swChecksumValue{},
107+
License: []string{},
108+
}
109+
if fn != nil {
110+
fn(&c)
111+
}
112+
if c.Timestamp == "" {
113+
c.Timestamp = time.Now().Format("2006-01-02T15:04:05MST")
114+
}
115+
doc.Software.Components = append(doc.Software.Components, c)
116+
}
117+
118+
func (doc *BomSWDocument) AppendDependencies(parentId string, childrenIds []string) {
119+
if doc.Software.Dependencies == nil {
120+
doc.Software.Dependencies = []swDependencies{}
121+
}
122+
if len(childrenIds) > 0 {
123+
doc.Software.Dependencies = append(doc.Software.Dependencies, newDependencies(parentId, childrenIds))
124+
}
125+
}

0 commit comments

Comments
 (0)