|
14 | 14 |
|
15 | 15 | package extractors |
16 | 16 |
|
17 | | -// comp_with_dependencies: Valid dependency graph ? |
| 17 | +import ( |
| 18 | + "strings" |
| 19 | + |
| 20 | + "github.com/interlynk-io/sbomqs/pkg/sbom" |
| 21 | + "github.com/interlynk-io/sbomqs/pkg/scorer/v2/config" |
| 22 | + "github.com/interlynk-io/sbomqs/pkg/scorer/v2/engine" |
| 23 | + "github.com/samber/lo" |
| 24 | +) |
| 25 | + |
| 26 | +// comp_with_dependencies (component-level coverage) |
| 27 | +// SPDX: relationships (DEPENDS_ON); CDX: component.dependencies / bom.dependencies |
| 28 | +func CompWithDependencies(doc sbom.Document) config.FeatureScore { |
| 29 | + comps := doc.Components() |
| 30 | + if len(comps) == 0 { |
| 31 | + return engine.ScoreNA() |
| 32 | + } |
| 33 | + |
| 34 | + have := lo.CountBy(comps, func(c sbom.GetComponent) bool { |
| 35 | + return c.HasRelationShips() || c.CountOfDependencies() > 0 |
| 36 | + }) |
| 37 | + |
| 38 | + return config.FeatureScore{ |
| 39 | + Score: engine.PerComponentScore(have, len(comps)), |
| 40 | + Desc: engine.CompDescription(have, len(comps), "dependencies"), |
| 41 | + Ignore: false, |
| 42 | + } |
| 43 | +} |
18 | 44 |
|
19 | 45 | // comp_with_declared_completeness: Completeness declaration present |
| 46 | +func CompWithCompleteness(doc sbom.Document) config.FeatureScore { |
| 47 | + comps := doc.Components() |
| 48 | + if len(comps) == 0 { |
| 49 | + return engine.ScoreNA() |
| 50 | + } |
| 51 | + |
| 52 | + spec := doc.Spec().GetSpecType() |
| 53 | + |
| 54 | + switch spec { |
| 55 | + case string(sbom.SBOMSpecSPDX): |
| 56 | + // N/A for SPDX |
| 57 | + return config.FeatureScore{ |
| 58 | + Score: engine.BooleanScore(false), |
| 59 | + Desc: engine.NonSupportedSPDXField(), |
| 60 | + Ignore: true, |
| 61 | + } |
| 62 | + |
| 63 | + case string(sbom.SBOMSpecCDX): |
| 64 | + // TODO: to add this method in our sbom module, then only we can fetch it here |
| 65 | + // Compositions/Aggregate |
| 66 | + // have := lo.CountBy(doc.Components(), func(c sbom.GetComponent) bool { |
| 67 | + // return c.GetComposition() != "" |
| 68 | + // }) |
| 69 | + } |
| 70 | + |
| 71 | + return config.FeatureScore{ |
| 72 | + Score: engine.BooleanScore(false), |
| 73 | + Desc: engine.UnknownSpec(), |
| 74 | + Ignore: true, |
| 75 | + } |
| 76 | +} |
20 | 77 |
|
21 | 78 | // sbom_with_primary_comp: Single primary component defined |
| 79 | +func SBOMWithPrimaryComponent(doc sbom.Document) config.FeatureScore { |
| 80 | + comps := doc.Components() |
| 81 | + isPrimaryPresent := doc.PrimaryComp().IsPresent() |
| 82 | + |
| 83 | + if !isPrimaryPresent { |
| 84 | + return config.FeatureScore{ |
| 85 | + Score: engine.PerComponentScore(0, len(comps)), |
| 86 | + Desc: "absent", |
| 87 | + Ignore: true, |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return config.FeatureScore{ |
| 92 | + Score: engine.BooleanScore(isPrimaryPresent), |
| 93 | + Desc: "identified", |
| 94 | + Ignore: false, |
| 95 | + } |
| 96 | +} |
22 | 97 |
|
23 | 98 | // comps_with_source_code: Valid VCS URL |
| 99 | +func CompWithSourceCode(doc sbom.Document) config.FeatureScore { |
| 100 | + comps := doc.Components() |
| 101 | + if len(comps) == 0 { |
| 102 | + return engine.ScoreNA() |
| 103 | + } |
| 104 | + |
| 105 | + have := lo.CountBy(doc.Components(), func(c sbom.GetComponent) bool { |
| 106 | + return strings.TrimSpace(c.SourceCodeURL()) != "" |
| 107 | + }) |
| 108 | + |
| 109 | + return config.FeatureScore{ |
| 110 | + Score: engine.PerComponentScore(have, len(comps)), |
| 111 | + Desc: engine.CompDescription(have, len(comps), "source URIs"), |
| 112 | + Ignore: false, |
| 113 | + } |
| 114 | +} |
24 | 115 |
|
25 | 116 | // comp_with_supplier |
| 117 | +func CompWithSupplier(doc sbom.Document) config.FeatureScore { |
| 118 | + comps := doc.Components() |
| 119 | + if len(comps) == 0 { |
| 120 | + return engine.ScoreNA() |
| 121 | + } |
| 122 | + |
| 123 | + have := lo.CountBy(comps, func(c sbom.GetComponent) bool { |
| 124 | + s := c.Suppliers() |
| 125 | + hasName := strings.TrimSpace(s.GetName()) != "" |
| 126 | + hasContact := strings.TrimSpace(s.GetEmail()) != "" || strings.TrimSpace(s.GetURL()) != "" |
| 127 | + return c.Suppliers().IsPresent() && hasName && hasContact |
| 128 | + }) |
| 129 | + |
| 130 | + return config.FeatureScore{ |
| 131 | + Score: engine.PerComponentScore(have, len(comps)), |
| 132 | + Desc: engine.CompDescription(have, len(comps), "suppliers"), |
| 133 | + Ignore: false, |
| 134 | + } |
| 135 | +} |
26 | 136 |
|
27 | 137 | // comp_with_primary_purpose |
| 138 | +func CompWithPackagePurpose(doc sbom.Document) config.FeatureScore { |
| 139 | + comps := doc.Components() |
| 140 | + if len(comps) == 0 { |
| 141 | + return engine.ScoreNA() |
| 142 | + } |
| 143 | + |
| 144 | + have := lo.CountBy(comps, func(c sbom.GetComponent) bool { |
| 145 | + return c.PrimaryPurpose() != "" |
| 146 | + }) |
| 147 | + |
| 148 | + return config.FeatureScore{ |
| 149 | + Score: engine.PerComponentScore(have, len(comps)), |
| 150 | + Desc: engine.CompDescription(have, len(comps), "type"), |
| 151 | + Ignore: false, |
| 152 | + } |
| 153 | +} |
0 commit comments