|
| 1 | +package model |
| 2 | + |
| 3 | +import "time" |
| 4 | + |
| 5 | +type DpSbomDocument struct { |
| 6 | + // 文档名称 |
| 7 | + DocumentName string `json:"DocumentName"` |
| 8 | + // 文档版本 |
| 9 | + DocumentVersion string `json:"DocumentVersion"` |
| 10 | + // 文档创建/更新时间 yyyy-MM-ddTHH:mm:ssTZD |
| 11 | + DocumentTime string `json:"DocumentTime"` |
| 12 | + // 文档格式 |
| 13 | + BomFormat string `json:"BomFormat"` |
| 14 | + // 生成工具 |
| 15 | + Tool string `json:"tool"` |
| 16 | + // sbom签名信息 |
| 17 | + Hashes DpSbomHashes `json:"Hashes"` |
| 18 | + // 组件列表 |
| 19 | + Packages []DpSbomPackage `json:"Packages"` |
| 20 | + // 依赖关系 |
| 21 | + Dependencies []DpSbomDependencies `json:"Dependencies"` |
| 22 | +} |
| 23 | + |
| 24 | +type DpSbomPackage struct { |
| 25 | + Name string `json:"ComponentName"` |
| 26 | + Version string `json:"ComponentVersion"` |
| 27 | + |
| 28 | + Identifier struct { |
| 29 | + Purl string `json:"PURL"` |
| 30 | + } `json:"ComponentIdentifier"` |
| 31 | + |
| 32 | + License []string `json:"License"` |
| 33 | + |
| 34 | + Author []map[string]string `json:"Author"` |
| 35 | + Provider []map[string]string `json:"Provider"` |
| 36 | + Hash DpSbomHash `json:"ComponentHash"` |
| 37 | + |
| 38 | + // 组件信息更新时间 yyyy-MM-ddTHH:mm:ssTZD |
| 39 | + Timestamp string `json:"Timestamp"` |
| 40 | +} |
| 41 | + |
| 42 | +type DpSbomDependencies struct { |
| 43 | + Ref string `json:"Ref"` |
| 44 | + DependsOn []struct { |
| 45 | + Target string `json:"Target"` |
| 46 | + } `json:"DependsOn"` |
| 47 | +} |
| 48 | + |
| 49 | +func newDependencies(ref string, dependsOn []string) DpSbomDependencies { |
| 50 | + deps := DpSbomDependencies{Ref: ref} |
| 51 | + deps.DependsOn = make([]struct { |
| 52 | + Target string "json:\"Target\"" |
| 53 | + }, len(dependsOn)) |
| 54 | + for i, d := range dependsOn { |
| 55 | + deps.DependsOn[i].Target = d |
| 56 | + } |
| 57 | + return deps |
| 58 | +} |
| 59 | + |
| 60 | +type DpSbomHashes struct { |
| 61 | + Algorithm string `json:"Algorithm"` |
| 62 | + HashFile string `json:"HashFile,omitempty"` |
| 63 | + DigitalFile string `json:"DigitalFile,omitempty"` |
| 64 | +} |
| 65 | + |
| 66 | +type DpSbomHash struct { |
| 67 | + Algorithm string `json:"Algorithm,omitempty"` |
| 68 | + Hash string `json:"Hash,omitempty"` |
| 69 | +} |
| 70 | + |
| 71 | +func NewDpSbomDocument(name, creator string) *DpSbomDocument { |
| 72 | + version := "1.0.0" |
| 73 | + timestamp := time.Now().Format("2006-01-02T15:04:05MST") |
| 74 | + return &DpSbomDocument{ |
| 75 | + DocumentName: name, |
| 76 | + DocumentVersion: version, |
| 77 | + DocumentTime: timestamp, |
| 78 | + BomFormat: "DP-SBOM-1.0", |
| 79 | + Tool: creator, |
| 80 | + Hashes: DpSbomHashes{ |
| 81 | + Algorithm: "SHA-256", |
| 82 | + HashFile: "sha256.txt", |
| 83 | + }, |
| 84 | + Dependencies: []DpSbomDependencies{}, |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func (doc *DpSbomDocument) AppendComponents(fn func(*DpSbomPackage)) { |
| 89 | + c := DpSbomPackage{} |
| 90 | + if fn != nil { |
| 91 | + fn(&c) |
| 92 | + } |
| 93 | + if c.Timestamp == "" { |
| 94 | + c.Timestamp = time.Now().Format("2006-01-02T15:04:05MST") |
| 95 | + } |
| 96 | + if c.Author == nil { |
| 97 | + c.Author = []map[string]string{} |
| 98 | + } |
| 99 | + if c.Provider == nil { |
| 100 | + c.Provider = []map[string]string{} |
| 101 | + } |
| 102 | + doc.Packages = append(doc.Packages, c) |
| 103 | +} |
| 104 | + |
| 105 | +func (doc *DpSbomDocument) AppendDependencies(parentId string, childrenIds []string) { |
| 106 | + if doc.Dependencies == nil { |
| 107 | + doc.Dependencies = []DpSbomDependencies{} |
| 108 | + } |
| 109 | + if len(childrenIds) > 0 { |
| 110 | + doc.Dependencies = append(doc.Dependencies, newDependencies(parentId, childrenIds)) |
| 111 | + } |
| 112 | +} |
0 commit comments