|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + vf "github.com/smithy-security/smithy/sdk/component/vulnerability-finding" |
| 12 | + ocsf "github.com/smithy-security/smithy/sdk/gen/ocsf_schema/v1" |
| 13 | +) |
| 14 | + |
| 15 | +func TestSeverityFilter_Filter(t *testing.T) { |
| 16 | + tests := []struct { |
| 17 | + name string |
| 18 | + envSeverity string |
| 19 | + findings []*vf.VulnerabilityFinding |
| 20 | + expectedFiltered int |
| 21 | + expectError bool |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "Valid configuration with HIGH severity", |
| 25 | + envSeverity: "HIGH", |
| 26 | + findings: []*vf.VulnerabilityFinding{ |
| 27 | + { |
| 28 | + Finding: &ocsf.VulnerabilityFinding{ |
| 29 | + SeverityId: ocsf.VulnerabilityFinding_SEVERITY_ID_CRITICAL, |
| 30 | + }, |
| 31 | + }, |
| 32 | + { |
| 33 | + Finding: &ocsf.VulnerabilityFinding{ |
| 34 | + SeverityId: ocsf.VulnerabilityFinding_SEVERITY_ID_HIGH, |
| 35 | + }, |
| 36 | + }, |
| 37 | + { |
| 38 | + Finding: &ocsf.VulnerabilityFinding{ |
| 39 | + SeverityId: ocsf.VulnerabilityFinding_SEVERITY_ID_MEDIUM, |
| 40 | + }, |
| 41 | + }, |
| 42 | + { |
| 43 | + Finding: &ocsf.VulnerabilityFinding{ |
| 44 | + SeverityId: ocsf.VulnerabilityFinding_SEVERITY_ID_LOW, |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + Finding: &ocsf.VulnerabilityFinding{ |
| 49 | + SeverityId: ocsf.VulnerabilityFinding_SEVERITY_ID_INFORMATIONAL, |
| 50 | + }, |
| 51 | + }, |
| 52 | + { |
| 53 | + Finding: &ocsf.VulnerabilityFinding{ |
| 54 | + SeverityId: ocsf.VulnerabilityFinding_SEVERITY_ID_UNKNOWN, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + expectedFiltered: 2, |
| 59 | + expectError: false, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "Valid configuration with MEDIUM severity", |
| 63 | + envSeverity: "MEDIUM", |
| 64 | + findings: []*vf.VulnerabilityFinding{ |
| 65 | + { |
| 66 | + Finding: &ocsf.VulnerabilityFinding{ |
| 67 | + SeverityId: ocsf.VulnerabilityFinding_SEVERITY_ID_LOW, |
| 68 | + }, |
| 69 | + }, |
| 70 | + { |
| 71 | + Finding: &ocsf.VulnerabilityFinding{ |
| 72 | + SeverityId: ocsf.VulnerabilityFinding_SEVERITY_ID_MEDIUM, |
| 73 | + }, |
| 74 | + }, |
| 75 | + { |
| 76 | + Finding: &ocsf.VulnerabilityFinding{ |
| 77 | + SeverityId: ocsf.VulnerabilityFinding_SEVERITY_ID_HIGH, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + expectedFiltered: 2, |
| 82 | + expectError: false, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "Invalid configuration with unknown severity", |
| 86 | + envSeverity: "INVALID", |
| 87 | + findings: []*vf.VulnerabilityFinding{ |
| 88 | + { |
| 89 | + Finding: &ocsf.VulnerabilityFinding{ |
| 90 | + SeverityId: ocsf.VulnerabilityFinding_SEVERITY_ID_CRITICAL, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + expectedFiltered: 0, |
| 95 | + expectError: true, |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "Empty findings list", |
| 99 | + envSeverity: "HIGH", |
| 100 | + findings: []*vf.VulnerabilityFinding{}, |
| 101 | + expectedFiltered: 0, |
| 102 | + expectError: false, |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "Valid configuration with CRITICAL severity", |
| 106 | + envSeverity: "CRITICAL", |
| 107 | + findings: []*vf.VulnerabilityFinding{ |
| 108 | + { |
| 109 | + Finding: &ocsf.VulnerabilityFinding{ |
| 110 | + SeverityId: ocsf.VulnerabilityFinding_SEVERITY_ID_CRITICAL, |
| 111 | + }, |
| 112 | + }, |
| 113 | + { |
| 114 | + Finding: &ocsf.VulnerabilityFinding{ |
| 115 | + SeverityId: ocsf.VulnerabilityFinding_SEVERITY_ID_HIGH, |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + expectedFiltered: 1, |
| 120 | + expectError: false, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + for _, tt := range tests { |
| 125 | + t.Run(tt.name, func(t *testing.T) { |
| 126 | + os.Setenv("MINIMUM_SEVERITY", tt.envSeverity) |
| 127 | + defer os.Unsetenv("MINIMUM_SEVERITY") |
| 128 | + |
| 129 | + filter, err := NewSeverityFilter() |
| 130 | + if tt.expectError { |
| 131 | + require.Error(t, err) |
| 132 | + return |
| 133 | + } |
| 134 | + require.NoError(t, err) |
| 135 | + |
| 136 | + ctx := context.Background() |
| 137 | + filteredFindings, filtered, err := filter.Filter(ctx, tt.findings) |
| 138 | + require.NoError(t, err) |
| 139 | + filteredFindingsCount := 0 |
| 140 | + for _, finding := range filteredFindings { |
| 141 | + for _, enrichment := range finding.Finding.Enrichments { |
| 142 | + if enrichment.Name == providerName { |
| 143 | + assert.Equal(t, providerName, *enrichment.Provider) |
| 144 | + assert.Equal(t, providerName, enrichment.Name) |
| 145 | + assert.NotEmpty(t, enrichment.Value) |
| 146 | + filteredFindingsCount++ |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + assert.Equal(t, tt.expectedFiltered, filteredFindingsCount) |
| 151 | + assert.Equal(t, tt.expectedFiltered > 0, filtered) |
| 152 | + }) |
| 153 | + } |
| 154 | +} |
0 commit comments