| 1 |
package sarif |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
) |
| 6 |
|
| 7 |
// RunOption ... |
| 8 |
type RunOption int |
| 9 |
|
| 10 |
// IncludeEmptyResults ... |
| 11 |
const IncludeEmptyResults RunOption = iota |
| 12 |
|
| 13 |
// Run type represents a run of a tool |
| 14 |
type Run struct { |
| 15 |
Tool Tool `json:"tool"` |
| 16 |
Artifacts []*Artifact `json:"artifacts,omitempty"` |
| 17 |
Invocations []*Invocation `json:"invocations,omitempty"` |
| 18 |
LogicalLocations []*LogicalLocation `json:"logicalLocations,omitempty"` |
| 19 |
Results []*Result `json:"results"` |
| 20 |
Addresses []*Address `json:"addresses,omitempty"` |
| 21 |
AutomationDetails *RunAutomationDetails `json:"automationDetails,omitempty"` |
| 22 |
BaselineGUID *string `json:"baselineGuid,omitempty"` |
| 23 |
ColumnKind interface{} `json:"columnKind,omitempty"` |
| 24 |
Conversion *Conversion `json:"conversion,omitempty"` |
| 25 |
DefaultEncoding *string `json:"defaultEncoding,omitempty"` |
| 26 |
DefaultSourceLanguage *string `json:"defaultSourceLanguage,omitempty"` |
| 27 |
ExternalPropertyFileReferences *ExternalPropertyFileReferences `json:"externalPropertyFileReferences,omitempty"` |
| 28 |
Graphs []*Graph `json:"graphs,omitempty"` |
| 29 |
Language *string `json:"language,omitempty"` |
| 30 |
NewlineSequences []string `json:"newlineSequences,omitempty"` |
| 31 |
OriginalUriBaseIDs map[string]*ArtifactLocation `json:"originalUriBaseIds,omitempty"` |
| 32 |
Policies []*ToolComponent `json:"policies,omitempty"` |
| 33 |
RedactionTokens []string `json:"redactionTokens,omitempty"` |
| 34 |
RunAggregates []*RunAutomationDetails `json:"runAggregates,omitempty"` |
| 35 |
SpecialLocations *SpecialLocations `json:"specialLocations,omitempty"` |
| 36 |
Taxonomies []*ToolComponent `json:"taxonomies,omitempty"` |
| 37 |
ThreadFlowLocations []*ThreadFlowLocation `json:"threadFlowLocations,omitempty"` |
| 38 |
Translations []*ToolComponent `json:"translations,omitempty"` |
| 39 |
VersionControlProvenance []*VersionControlDetails `json:"versionControlProvenance,omitempty"` |
| 40 |
WebRequests []*WebRequest `json:"webRequests,omitempty"` |
| 41 |
WebResponses []*WebResponse `json:"webResponses,omitempty"` |
| 42 |
PropertyBag |
| 43 |
} |
| 44 |
|
| 45 |
// NewRun creates a new Run and returns a pointer to it |
| 46 |
func NewRun(tool Tool) *Run { |
| 47 |
return &Run{ |
| 48 |
Tool: tool, |
| 49 |
Results: []*Result{}, |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// NewRunWithInformationURI creates a new Run and returns a pointer to it |
| 54 |
func NewRunWithInformationURI(toolName, informationURI string) *Run { |
| 55 |
run := &Run{ |
| 56 |
Tool: Tool{ |
| 57 |
Driver: &ToolComponent{ |
| 58 |
Name: toolName, |
| 59 |
InformationURI: &informationURI, |
| 60 |
Rules: []*ReportingDescriptor{}, |
| 61 |
}, |
| 62 |
}, |
| 63 |
Results: []*Result{}, |
| 64 |
} |
| 65 |
|
| 66 |
return run |
| 67 |
} |
| 68 |
|
| 69 |
// WithResults sets the Results |
| 70 |
func (run *Run) WithResults(results []*Result) *Run { |
| 71 |
for _, result := range results { |
| 72 |
run.AddResult(result) |
| 73 |
} |
| 74 |
return run |
| 75 |
} |
| 76 |
|
| 77 |
// AddResult ... |
| 78 |
func (run *Run) AddResult(result *Result) { |
| 79 |
result = result.WithRuleIndex(run.Tool.Driver.getRuleIndex(result.RuleID)) |
| 80 |
run.Results = append(run.Results, result) |
| 81 |
} |
| 82 |
|
| 83 |
// WithAddresses sets the Addresses |
| 84 |
func (run *Run) WithAddresses(addresses []*Address) *Run { |
| 85 |
run.Addresses = addresses |
| 86 |
return run |
| 87 |
} |
| 88 |
|
| 89 |
// WithArtifacts sets the Artifacts |
| 90 |
func (run *Run) WithArtifacts(artifacts []*Artifact) *Run { |
| 91 |
run.Artifacts = artifacts |
| 92 |
return run |
| 93 |
} |
| 94 |
|
| 95 |
// WithAutomationDetails sets the AutomationDetails |
| 96 |
func (run *Run) WithAutomationDetails(automationDetails *RunAutomationDetails) *Run { |
| 97 |
run.AutomationDetails = automationDetails |
| 98 |
return run |
| 99 |
} |
| 100 |
|
| 101 |
// WithBaselineGUID sets the BaselineGUID |
| 102 |
func (run *Run) WithBaselineGUID(baselineGUID string) *Run { |
| 103 |
run.BaselineGUID = &baselineGUID |
| 104 |
return run |
| 105 |
} |
| 106 |
|
| 107 |
// WithColumnKind sets the ColumnKind |
| 108 |
func (run *Run) WithColumnKind(columnKind interface{}) *Run { |
| 109 |
run.ColumnKind = columnKind |
| 110 |
return run |
| 111 |
} |
| 112 |
|
| 113 |
// WithConversion sets the Conversion |
| 114 |
func (run *Run) WithConversion(conversion *Conversion) *Run { |
| 115 |
run.Conversion = conversion |
| 116 |
return run |
| 117 |
} |
| 118 |
|
| 119 |
// WithDefaultEncoding sets the DefaultEncoding |
| 120 |
func (run *Run) WithDefaultEncoding(defaultEncoding string) *Run { |
| 121 |
run.DefaultEncoding = &defaultEncoding |
| 122 |
return run |
| 123 |
} |
| 124 |
|
| 125 |
// WithDefaultSourceLanguage sets the DefaultSourceLanguage |
| 126 |
func (run *Run) WithDefaultSourceLanguage(defaultSourceLangauge string) *Run { |
| 127 |
run.DefaultSourceLanguage = &defaultSourceLangauge |
| 128 |
return run |
| 129 |
} |
| 130 |
|
| 131 |
// WithExternalPropertyFileReferences sets the ExternalPropertyFileReferences |
| 132 |
func (run *Run) WithExternalPropertyFileReferences(references *ExternalPropertyFileReferences) *Run { |
| 133 |
run.ExternalPropertyFileReferences = references |
| 134 |
return run |
| 135 |
} |
| 136 |
|
| 137 |
// WithGraphs sets the Graphs |
| 138 |
func (run *Run) WithGraphs(graphs []*Graph) *Run { |
| 139 |
run.Graphs = graphs |
| 140 |
return run |
| 141 |
} |
| 142 |
|
| 143 |
// AddGraph ... |
| 144 |
func (run *Run) AddGraph(graph *Graph) { |
| 145 |
run.Graphs = append(run.Graphs, graph) |
| 146 |
} |
| 147 |
|
| 148 |
// WithInvocations sets the Invocations |
| 149 |
func (run *Run) WithInvocations(invocations []*Invocation) *Run { |
| 150 |
run.Invocations = invocations |
| 151 |
return run |
| 152 |
} |
| 153 |
|
| 154 |
// AddInvocations ... |
| 155 |
func (run *Run) AddInvocations(invocation *Invocation) { |
| 156 |
run.Invocations = append(run.Invocations, invocation) |
| 157 |
} |
| 158 |
|
| 159 |
// WithLanguage sets the Language |
| 160 |
func (run *Run) WithLanguage(language string) *Run { |
| 161 |
run.Language = &language |
| 162 |
return run |
| 163 |
} |
| 164 |
|
| 165 |
// WithLogicalLocations sets the LogicalLocations |
| 166 |
func (run *Run) WithLogicalLocations(locations []*LogicalLocation) *Run { |
| 167 |
run.LogicalLocations = locations |
| 168 |
return run |
| 169 |
} |
| 170 |
|
| 171 |
// AddILogicalLocation ... |
| 172 |
func (run *Run) AddILogicalLocation(logicalLocation *LogicalLocation) { |
| 173 |
run.LogicalLocations = append(run.LogicalLocations, logicalLocation) |
| 174 |
} |
| 175 |
|
| 176 |
// WithNewlineSequences sets the NewlineSequences |
| 177 |
func (run *Run) WithNewlineSequences(newLines []string) *Run { |
| 178 |
run.NewlineSequences = newLines |
| 179 |
return run |
| 180 |
} |
| 181 |
|
| 182 |
// WithOriginalUriBaseIds sets the OriginalUriBaseIds |
| 183 |
func (run *Run) WithOriginalUriBaseIds(originalUriBaseIDs map[string]*ArtifactLocation) *Run { |
| 184 |
run.OriginalUriBaseIDs = originalUriBaseIDs |
| 185 |
return run |
| 186 |
} |
| 187 |
|
| 188 |
// WithPolicies sets the Policies |
| 189 |
func (run *Run) WithPolicies(policies []*ToolComponent) *Run { |
| 190 |
run.Policies = policies |
| 191 |
return run |
| 192 |
} |
| 193 |
|
| 194 |
// AddPolicy ... |
| 195 |
func (run *Run) AddPolicy(policy *ToolComponent) { |
| 196 |
run.Policies = append(run.Policies, policy) |
| 197 |
} |
| 198 |
|
| 199 |
// WithRedactionTokens sets the RedactionTokens |
| 200 |
func (run *Run) WithRedactionTokens(redactedTokens []string) *Run { |
| 201 |
run.RedactionTokens = redactedTokens |
| 202 |
return run |
| 203 |
} |
| 204 |
|
| 205 |
// WithRunAggregates sets the RunAggregates |
| 206 |
func (run *Run) WithRunAggregates(runAggregates []*RunAutomationDetails) *Run { |
| 207 |
run.RunAggregates = runAggregates |
| 208 |
return run |
| 209 |
} |
| 210 |
|
| 211 |
// AddRunAggregate ... |
| 212 |
func (run *Run) AddRunAggregate(runAggregate *RunAutomationDetails) { |
| 213 |
run.RunAggregates = append(run.RunAggregates, runAggregate) |
| 214 |
} |
| 215 |
|
| 216 |
// WithSpecialLocations sets the SpecialLocations |
| 217 |
func (run *Run) WithSpecialLocations(specialLocation *SpecialLocations) *Run { |
| 218 |
run.SpecialLocations = specialLocation |
| 219 |
return run |
| 220 |
} |
| 221 |
|
| 222 |
// WithTaxonomies sets the Taxonomies |
| 223 |
func (run *Run) WithTaxonomies(taxonomies []*ToolComponent) *Run { |
| 224 |
run.Taxonomies = taxonomies |
| 225 |
return run |
| 226 |
} |
| 227 |
|
| 228 |
// AddTaxonomy ... |
| 229 |
func (run *Run) AddTaxonomy(taxonomy *ToolComponent) { |
| 230 |
run.Taxonomies = append(run.Taxonomies, taxonomy) |
| 231 |
} |
| 232 |
|
| 233 |
// WithThreadFlowLocations sets the ThreadFlowLocations |
| 234 |
func (run *Run) WithThreadFlowLocations(threadFlowLocations []*ThreadFlowLocation) *Run { |
| 235 |
run.ThreadFlowLocations = threadFlowLocations |
| 236 |
return run |
| 237 |
} |
| 238 |
|
| 239 |
// AddThreadFlowLocation ... |
| 240 |
func (run *Run) AddThreadFlowLocation(threadFlowLocation *ThreadFlowLocation) { |
| 241 |
run.ThreadFlowLocations = append(run.ThreadFlowLocations, threadFlowLocation) |
| 242 |
} |
| 243 |
|
| 244 |
// WithTranslations sets the Translations |
| 245 |
func (run *Run) WithTranslations(translations []*ToolComponent) *Run { |
| 246 |
run.Translations = translations |
| 247 |
return run |
| 248 |
} |
| 249 |
|
| 250 |
// AddTranslation ... |
| 251 |
func (run *Run) AddTranslation(translation *ToolComponent) { |
| 252 |
run.Translations = append(run.Translations, translation) |
| 253 |
} |
| 254 |
|
| 255 |
// WithVersionControlProvenances sets the VersionControlProvenances |
| 256 |
func (run *Run) WithVersionControlProvenances(vcProvenance []*VersionControlDetails) *Run { |
| 257 |
run.VersionControlProvenance = vcProvenance |
| 258 |
return run |
| 259 |
} |
| 260 |
|
| 261 |
// AddVersionControlProvenance ... |
| 262 |
func (run *Run) AddVersionControlProvenance(vcProvenance *VersionControlDetails) { |
| 263 |
run.VersionControlProvenance = append(run.VersionControlProvenance, vcProvenance) |
| 264 |
} |
| 265 |
|
| 266 |
// WithWebRequests sets the WebRequests |
| 267 |
func (run *Run) WithWebRequests(webRequests []*WebRequest) *Run { |
| 268 |
run.WebRequests = webRequests |
| 269 |
return run |
| 270 |
} |
| 271 |
|
| 272 |
// AddWebRequest ... |
| 273 |
func (run *Run) AddWebRequest(webRequest *WebRequest) { |
| 274 |
run.WebRequests = append(run.WebRequests, webRequest) |
| 275 |
} |
| 276 |
|
| 277 |
// WithWebResponses sets the WebResponses |
| 278 |
func (run *Run) WithWebResponses(webResponses []*WebResponse) *Run { |
| 279 |
run.WebResponses = webResponses |
| 280 |
return run |
| 281 |
} |
| 282 |
|
| 283 |
// AddWebResponse ... |
| 284 |
func (run *Run) AddWebResponse(webResponse *WebResponse) { |
| 285 |
run.WebResponses = append(run.WebResponses, webResponse) |
| 286 |
} |
| 287 |
|
| 288 |
// AddInvocation adds an invocation to the run and returns a pointer to it |
| 289 |
func (run *Run) AddInvocation(executionSuccessful bool) *Invocation { |
| 290 |
i := &Invocation{ |
| 291 |
ExecutionSuccessful: &executionSuccessful, |
| 292 |
} |
| 293 |
run.Invocations = append(run.Invocations, i) |
| 294 |
return i |
| 295 |
} |
| 296 |
|
| 297 |
// AddArtifact adds an artifact to the run and returns a pointer to it |
| 298 |
func (run *Run) AddArtifact() *Artifact { |
| 299 |
a := &Artifact{ |
| 300 |
Length: -1, |
| 301 |
} |
| 302 |
run.Artifacts = append(run.Artifacts, a) |
| 303 |
return a |
| 304 |
} |
| 305 |
|
| 306 |
// AddDistinctArtifact will handle deduplication of simple artifact additions |
| 307 |
func (run *Run) AddDistinctArtifact(uri string) *Artifact { |
| 308 |
for _, artifact := range run.Artifacts { |
| 309 |
if *artifact.Location.URI == uri { |
| 310 |
return artifact |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
a := &Artifact{ |
| 315 |
Length: -1, |
| 316 |
} |
| 317 |
a.WithLocation(NewSimpleArtifactLocation(uri)) |
| 318 |
|
| 319 |
run.Artifacts = append(run.Artifacts, a) |
| 320 |
return a |
| 321 |
} |
| 322 |
|
| 323 |
// AddRule returns an existing ReportingDescriptor for the ruleID or creates a new ReportingDescriptor and returns a pointer to it |
| 324 |
func (run *Run) AddRule(ruleID string) *ReportingDescriptor { |
| 325 |
for _, rule := range run.Tool.Driver.Rules { |
| 326 |
if rule.ID == ruleID { |
| 327 |
return rule |
| 328 |
} |
| 329 |
} |
| 330 |
rule := NewRule(ruleID) |
| 331 |
run.Tool.Driver.Rules = append(run.Tool.Driver.Rules, rule) |
| 332 |
return rule |
| 333 |
} |
| 334 |
|
| 335 |
// CreateResultForRule returns an existing Result or creates a new one and returns a pointer to it |
| 336 |
func (run *Run) CreateResultForRule(ruleID string) *Result { |
| 337 |
result := NewRuleResult(ruleID) |
| 338 |
run.AddResult(result) |
| 339 |
return result |
| 340 |
} |
| 341 |
|
| 342 |
// GetRuleById finds a rule by a given rule ID and returns a pointer to it |
| 343 |
func (run *Run) GetRuleById(ruleId string) (*ReportingDescriptor, error) { |
| 344 |
if run.Tool.Driver != nil { |
| 345 |
for _, rule := range run.Tool.Driver.Rules { |
| 346 |
if rule.ID == ruleId { |
| 347 |
return rule, nil |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
return nil, fmt.Errorf("couldn't find rule %s", ruleId) |
| 352 |
} |
| 353 |
|
| 354 |
// GetResultByRuleId finds the result for a ruleId and returns a pointer to it |
| 355 |
func (run *Run) GetResultByRuleId(ruleId string) (*Result, error) { |
| 356 |
for _, result := range run.Results { |
| 357 |
if *result.RuleID == ruleId { |
| 358 |
return result, nil |
| 359 |
} |
| 360 |
} |
| 361 |
return nil, fmt.Errorf("couldn't find a result for rule %s", ruleId) |
| 362 |
} |
| 363 |
|
| 364 |
// DedupeArtifacts ... |
| 365 |
func (run *Run) DedupeArtifacts() error { |
| 366 |
dupes := map[*Artifact]bool{} |
| 367 |
deduped := []*Artifact{} |
| 368 |
|
| 369 |
for _, a := range run.Artifacts { |
| 370 |
if _, ok := dupes[a]; !ok { |
| 371 |
dupes[a] = true |
| 372 |
deduped = append(deduped, a) |
| 373 |
} |
| 374 |
} |
| 375 |
run.Artifacts = deduped |
| 376 |
return nil |
| 377 |
} |