(t *testing.T)
| 137 | } |
| 138 | |
| 139 | func TestFindMatchingRule(t *testing.T) { |
| 140 | rules := []PolicyRule{ |
| 141 | { |
| 142 | ID: "allow-github", |
| 143 | Order: 1, |
| 144 | Action: "allow", |
| 145 | ACLName: "allowed_domains", |
| 146 | Protocol: "both", |
| 147 | Domains: []string{".github.com"}, |
| 148 | }, |
| 149 | { |
| 150 | ID: "allow-npm", |
| 151 | Order: 2, |
| 152 | Action: "allow", |
| 153 | ACLName: "npm_domains", |
| 154 | Protocol: "both", |
| 155 | Domains: []string{"registry.npmjs.org"}, |
| 156 | }, |
| 157 | { |
| 158 | ID: "deny-all", |
| 159 | Order: 3, |
| 160 | Action: "deny", |
| 161 | ACLName: "all", |
| 162 | Protocol: "both", |
| 163 | Domains: []string{}, |
| 164 | }, |
| 165 | } |
| 166 | |
| 167 | t.Run("matches first rule - allowed HTTPS", func(t *testing.T) { |
| 168 | entry := AuditLogEntry{Host: "api.github.com:443", Method: "CONNECT", Status: 200, Decision: "TCP_TUNNEL"} |
| 169 | rule := findMatchingRule(entry, rules) |
| 170 | require.NotNil(t, rule, "Should find a matching rule") |
| 171 | assert.Equal(t, "allow-github", rule.ID, "Should match allow-github rule") |
| 172 | }) |
| 173 | |
| 174 | t.Run("matches second rule", func(t *testing.T) { |
| 175 | entry := AuditLogEntry{Host: "registry.npmjs.org:443", Method: "CONNECT", Status: 200, Decision: "TCP_TUNNEL"} |
| 176 | rule := findMatchingRule(entry, rules) |
| 177 | require.NotNil(t, rule, "Should find a matching rule") |
| 178 | assert.Equal(t, "allow-npm", rule.ID, "Should match allow-npm rule") |
| 179 | }) |
| 180 | |
| 181 | t.Run("aclName all catches unmatched denied traffic", func(t *testing.T) { |
| 182 | entry := AuditLogEntry{Host: "evil.com:443", Method: "CONNECT", Status: 403, Decision: "NONE_NONE"} |
| 183 | rule := findMatchingRule(entry, rules) |
| 184 | require.NotNil(t, rule, "Should find the catch-all deny rule") |
| 185 | assert.Equal(t, "deny-all", rule.ID, "Should match deny-all rule via aclName 'all'") |
| 186 | }) |
| 187 | |
| 188 | t.Run("aclName all skipped for allowed traffic", func(t *testing.T) { |
| 189 | // If a domain doesn't match specific rules but traffic was allowed, |
| 190 | // the deny-all rule should NOT match (action mismatch) |
| 191 | entry := AuditLogEntry{Host: "unknown.com:443", Method: "CONNECT", Status: 200, Decision: "TCP_TUNNEL"} |
| 192 | rule := findMatchingRule(entry, rules) |
| 193 | assert.Nil(t, rule, "deny-all rule should not match allowed traffic") |
| 194 | }) |
| 195 | |
| 196 | t.Run("first matching rule wins", func(t *testing.T) { |
nothing calls this directly
no test coverage detected