Commit 6fa69b8
authored
feat(windows_certificate): add filters.include / filters.exclude for tag-based cert collection scoping (#52441)
### What does this PR do?
Adds `filters.include` and `filters.exclude` configuration to the `windows_certificate` check. Each filter maps a tag key (e.g. `certificate_thumbprint`, `subject_CN`, `subject_alt_name_dns`) to a Go regex pattern matched against the tag's value.
```yaml
filters:
include:
<cert_tag_key>: <regex_pattern>
exclude:
<cert_tag_key>: <regex_pattern>
```
**Semantics:**
- `include`: a certificate must satisfy **all** include rules to be reported. A cert missing a tag for an include key is excluded.
- `exclude`: a certificate is dropped if **any** exclude rule matches one of its tag values.
- Empty section = no filtering in that direction.
- Invalid regex patterns are rejected at `Configure` time with a clear error.
**A filter rule only ever matches a tag that is actually collected.** Opt-in tag keys (`subject_alt_name_dns`, `certificate_template_name`, `signature_algorithm`, etc.) require their corresponding `*_tag` flag to be enabled to be used as filter criteria. If a rule references a tag key that isn't collected under the current config (its flag is disabled, or the key — `certificate_store`/`server` — is never available as a filter key), `Configure` logs a warning and skips that rule instead of matching against a tag that can never be present.
### Motivation
Addresses FRAGENT-3382 / tracked in TEEP-7268.
Customers running Windows Certificate Store monitoring often have many certificates in a store (IIS, SQL Server, document encryption, etc.) and need to scope collection to specific ones — by thumbprint, SAN, CN, or template name — without creating separate check instances per certificate. Previously the only agent-side filter was `certificate_subjects` (CN substring match).
With this change, operators can use any tag key emitted by the check (or available from the check's opt-in extensions, once the corresponding `*_tag` flag is enabled) as a filter key. The new opt-in tags from PR #49740 (SAN, friendly name, template, EKU) are usable as filter criteria as soon as their flag is turned on.
### Describe how you validated your changes
- **Unit tests** (`windows_certificate_tags_test.go`): cover include-only, exclude-only, combined include+exclude, empty filters (pass-through), cert with no matching tag key, and `pruneUncollectedFilterKeys` dropping rules on disabled-flag tags, unsupported keys (`certificate_store`/`server`), and keeping rules whose flag is enabled.
- **Configure tests** (`windows_certificate_test.go`): valid filter compiles without error; invalid regex returns error from `Configure`; a filter on a disabled opt-in tag is pruned rather than rejecting every certificate.
- **Integration test** (`TestConfigureWithFiltersExcludesNonMatchingCerts`): runs against the live Windows `ROOT` store with an impossible thumbprint include-filter and asserts zero `Gauge` and `ServiceCheck` calls.
- **Cross-compile**: `GOOS=windows GOARCH=amd64 go build ./pkg/collector/corechecks/system/windowscertificate/` — clean.
- **Linter**: `dda inv linter.go --targets=./pkg/collector/corechecks/system/windowscertificate/...` — 0 issues.
### Additional Notes
- Filtering is applied post-collection in `enumerateStoreContents`, after `StoreName` is stamped on each cert, covering both local and remote collection paths.
- `applyTagFilters` evaluates rules against the same `Tags` a certificate emits, so a rule only matches values that are actually collected. `pruneUncollectedFilterKeys` runs once in `Configure` and drops any rule that references a tag whose `*_tag` flag is disabled (logging a warning), so such a rule simply has no effect rather than excluding every certificate.
- `certificate_store` and `server` are not available as filter keys — they are appended to metrics in `Run()` after filtering. `pruneUncollectedFilterKeys` drops rules on these keys too.
- Regex patterns are compiled once at `Configure` time (not per-run), so there is no performance overhead during collection.
Co-authored-by: ian.bucad <ian.bucad@datadoghq.com>1 parent 8bbb28c commit 6fa69b8
7 files changed
Lines changed: 505 additions & 14 deletions
File tree
- cmd/agent/dist
- assets/windows_certificate
- conf.d/windows_certificate.d
- pkg/collector/corechecks/system/windowscertificate
- releasenotes/notes
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
263 | 263 | | |
264 | 264 | | |
265 | 265 | | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
266 | 308 | | |
267 | 309 | | |
268 | 310 | | |
| |||
Lines changed: 53 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
180 | 180 | | |
181 | 181 | | |
182 | 182 | | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
183 | 236 | | |
184 | 237 | | |
185 | 238 | | |
| |||
Lines changed: 84 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
108 | 122 | | |
109 | 123 | | |
110 | 124 | | |
| |||
127 | 141 | | |
128 | 142 | | |
129 | 143 | | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
130 | 147 | | |
131 | 148 | | |
132 | 149 | | |
133 | 150 | | |
134 | 151 | | |
135 | 152 | | |
136 | 153 | | |
| 154 | + | |
137 | 155 | | |
138 | 156 | | |
139 | 157 | | |
| |||
148 | 166 | | |
149 | 167 | | |
150 | 168 | | |
151 | | - | |
| 169 | + | |
152 | 170 | | |
153 | 171 | | |
154 | 172 | | |
| |||
242 | 260 | | |
243 | 261 | | |
244 | 262 | | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
245 | 273 | | |
246 | 274 | | |
247 | 275 | | |
| |||
573 | 601 | | |
574 | 602 | | |
575 | 603 | | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
576 | 607 | | |
577 | 608 | | |
578 | 609 | | |
| |||
655 | 686 | | |
656 | 687 | | |
657 | 688 | | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
658 | 692 | | |
659 | 693 | | |
660 | 694 | | |
| |||
972 | 1006 | | |
973 | 1007 | | |
974 | 1008 | | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
Lines changed: 116 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| |||
306 | 307 | | |
307 | 308 | | |
308 | 309 | | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
0 commit comments