Skip to content

Commit e04f187

Browse files
committed
make sarif constructor have functional arguments instead of accepting an ever growing list of parameters
1 parent 696053f commit e04f187

2 files changed

Lines changed: 137 additions & 28 deletions

File tree

sarif/sarif.go

Lines changed: 108 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,43 +69,88 @@ func validateDataSource(dataSource *ocsffindinginfo.DataSource) error {
6969
return nil
7070
}
7171

72-
func NewTransformer(
73-
scanResult *sarif.SchemaJson,
74-
findingsEcosystem string,
75-
clock clockwork.Clock,
76-
guidProvider StableUUIDProvider,
77-
richDescription bool,
78-
dataSource *ocsffindinginfo.DataSource,
79-
) (*SarifTransformer, error) {
80-
if scanResult == nil {
81-
return nil, errors.Errorf("method 'NewTransformer called with nil scanResult")
72+
type Config struct {
73+
ScanResult *sarif.SchemaJson
74+
FindingsEcosystem string
75+
Clock clockwork.Clock
76+
GuidProvider StableUUIDProvider
77+
RichDescription bool
78+
DataSource *ocsffindinginfo.DataSource
79+
}
80+
81+
type TransformerOption func(*Config)
82+
83+
func WithScanResult(scanResult *sarif.SchemaJson) TransformerOption {
84+
return func(cfg *Config) {
85+
cfg.ScanResult = scanResult
86+
}
87+
}
88+
89+
func WithFindingsEcosystem(findingsEcosystem string) TransformerOption {
90+
return func(cfg *Config) {
91+
cfg.FindingsEcosystem = findingsEcosystem
92+
}
93+
}
94+
95+
func WithClock(clock clockwork.Clock) TransformerOption {
96+
return func(cfg *Config) {
97+
cfg.Clock = clock
98+
}
99+
}
100+
101+
func WithGuidProvider(guidProvider StableUUIDProvider) TransformerOption {
102+
return func(cfg *Config) {
103+
cfg.GuidProvider = guidProvider
104+
}
105+
}
106+
107+
func WithRichDescription() TransformerOption {
108+
return func(cfg *Config) {
109+
cfg.RichDescription = true
110+
}
111+
}
112+
113+
func WithDataSource(dataSource *ocsffindinginfo.DataSource) TransformerOption {
114+
return func(cfg *Config) {
115+
cfg.DataSource = dataSource
116+
}
117+
}
118+
119+
func NewTransformer(options ...TransformerOption) (*SarifTransformer, error) {
120+
cfg := &Config{}
121+
for _, opt := range options {
122+
opt(cfg)
82123
}
83124

84-
if clock == nil {
85-
clock = clockwork.NewRealClock()
125+
if cfg.ScanResult == nil {
126+
return nil, errors.Errorf("method 'NewTransformerWithConfig' called with nil scanResult")
86127
}
87128

88-
if utils.IsNil(guidProvider) {
129+
if cfg.Clock == nil {
130+
cfg.Clock = clockwork.NewRealClock()
131+
}
132+
133+
if utils.IsNil(cfg.GuidProvider) {
89134
var err error
90-
guidProvider, err = NewBasicStableUUIDProvider()
135+
cfg.GuidProvider, err = NewBasicStableUUIDProvider()
91136
if err != nil {
92137
return nil, errors.Errorf("could not bootstrap stable UUID provider: %w", err)
93138
}
94139
}
95140

96-
if err := validateDataSource(dataSource); err != nil {
141+
if err := validateDataSource(cfg.DataSource); err != nil {
97142
return nil, errors.Errorf("invalid data source provider: %w", err)
98143
}
99144

100145
return &SarifTransformer{
101-
clock: clock,
102-
sarifResult: *scanResult,
103-
findingsEcosystem: findingsEcosystem,
104-
guidProvider: guidProvider,
146+
clock: cfg.Clock,
147+
sarifResult: *cfg.ScanResult,
148+
findingsEcosystem: cfg.FindingsEcosystem,
149+
guidProvider: cfg.GuidProvider,
105150
ruleToTools: make(map[string]sarif.ReportingDescriptor),
106151
taxasByCWEID: make(map[string]sarif.ReportingDescriptor),
107-
richDescription: richDescription,
108-
dataSource: dataSource,
152+
richDescription: cfg.RichDescription,
153+
dataSource: cfg.DataSource,
109154
}, nil
110155
}
111156

@@ -707,3 +752,45 @@ func (s *SarifTransformer) mergeDataSources(
707752

708753
return dataSource, nil
709754
}
755+
756+
func (s *SarifTransformer) Compare(sarif1, sarif2 *sarif.SchemaJson) map[string][]string {
757+
result := map[string][]string{
758+
"exist in first but dont exist in second": {},
759+
"exist in second but not in first": {},
760+
"exist in both": {},
761+
}
762+
763+
getFindingIDs := func(sarifDoc *sarif.SchemaJson) map[string]struct{} {
764+
ids := make(map[string]struct{})
765+
if sarifDoc == nil {
766+
return ids
767+
}
768+
for _, run := range sarifDoc.Runs {
769+
for _, res := range run.Results {
770+
if res.RuleId != nil {
771+
ids[*res.RuleId] = struct{}{}
772+
}
773+
}
774+
}
775+
return ids
776+
}
777+
778+
ids1 := getFindingIDs(sarif1)
779+
ids2 := getFindingIDs(sarif2)
780+
781+
for id := range ids1 {
782+
if _, ok := ids2[id]; ok {
783+
result["exist in both"] = append(result["exist in both"], id)
784+
} else {
785+
result["exist in first but dont exist in second"] = append(result["exist in first but dont exist in second"], id)
786+
}
787+
}
788+
789+
for id := range ids2 {
790+
if _, ok := ids1[id]; !ok {
791+
result["exist in second but not in first"] = append(result["exist in second but not in first"], id)
792+
}
793+
}
794+
795+
return result
796+
}

sarif/sarif_test.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,10 @@ func Test_ParseOut(t *testing.T) {
490490
},
491491
}
492492
transformer, err := sariftransformer.NewTransformer(
493-
&sarifOutput, "", clock, nil, true, datasource,
493+
sariftransformer.WithScanResult(&sarifOutput),
494+
sariftransformer.WithClock(clock),
495+
sariftransformer.WithRichDescription(),
496+
sariftransformer.WithDataSource(datasource),
494497
)
495498
require.NoError(t, err)
496499
actualIssues, err := transformer.ToOCSF(context.Background())
@@ -777,7 +780,11 @@ func Test_ParseOut(t *testing.T) {
777780
},
778781
}
779782
transformer, err := sariftransformer.NewTransformer(
780-
&sarifOutput, "npm", clock, nil, true, dataSource,
783+
sariftransformer.WithScanResult(&sarifOutput),
784+
sariftransformer.WithFindingsEcosystem("npm"),
785+
sariftransformer.WithClock(clock),
786+
sariftransformer.WithRichDescription(),
787+
sariftransformer.WithDataSource(dataSource),
781788
)
782789
require.NoError(t, err)
783790
actualIssues, err := transformer.ToOCSF(context.Background())
@@ -1078,7 +1085,9 @@ func Test_ParseOut(t *testing.T) {
10781085
}
10791086

10801087
transformer, err := sariftransformer.NewTransformer(
1081-
&sarifOutput, "", clock, nil, true, dataSource,
1088+
sariftransformer.WithScanResult(&sarifOutput),
1089+
sariftransformer.WithClock(clock),
1090+
sariftransformer.WithDataSource(dataSource),
10821091
)
10831092
require.NoError(t, err)
10841093

@@ -1385,7 +1394,10 @@ func Test_ParseOut(t *testing.T) {
13851394
},
13861395
}
13871396
transformer, err := sariftransformer.NewTransformer(
1388-
&sarifOutput, "", clock, nil, true, dataSource,
1397+
sariftransformer.WithScanResult(&sarifOutput),
1398+
sariftransformer.WithClock(clock),
1399+
sariftransformer.WithRichDescription(),
1400+
sariftransformer.WithDataSource(dataSource),
13891401
)
13901402
require.NoError(t, err)
13911403
actualIssues, err := transformer.ToOCSF(context.Background())
@@ -1602,7 +1614,11 @@ func Test_ParseOut(t *testing.T) {
16021614
},
16031615
}
16041616
transformer, err := sariftransformer.NewTransformer(
1605-
&sarifOutput, "docker", clock, nil, true, dataSource,
1617+
sariftransformer.WithScanResult(&sarifOutput),
1618+
sariftransformer.WithFindingsEcosystem("docker"),
1619+
sariftransformer.WithClock(clock),
1620+
sariftransformer.WithRichDescription(),
1621+
sariftransformer.WithDataSource(dataSource),
16061622
)
16071623
require.NoError(t, err)
16081624
actualIssues, err := transformer.ToOCSF(context.Background())
@@ -1944,7 +1960,10 @@ func Test_ParseOut(t *testing.T) {
19441960
},
19451961
}
19461962
transformer, err := sariftransformer.NewTransformer(
1947-
&sarifOutput, "", clock, nil, true, dataSource,
1963+
sariftransformer.WithScanResult(&sarifOutput),
1964+
sariftransformer.WithClock(clock),
1965+
sariftransformer.WithRichDescription(),
1966+
sariftransformer.WithDataSource(dataSource),
19481967
)
19491968
require.NoError(t, err)
19501969

@@ -2227,7 +2246,10 @@ func Test_ParseOut(t *testing.T) {
22272246
},
22282247
}
22292248
transformer, err := sariftransformer.NewTransformer(
2230-
&sarifOutput, "", clock, nil, true, dataSource,
2249+
sariftransformer.WithScanResult(&sarifOutput),
2250+
sariftransformer.WithClock(clock),
2251+
sariftransformer.WithRichDescription(),
2252+
sariftransformer.WithDataSource(dataSource),
22312253
)
22322254
require.NoError(t, err)
22332255
actualIssues, err := transformer.ToOCSF(context.Background())

0 commit comments

Comments
 (0)