Commit 291b130
authored
Deduplicate guard policy JSON roundtrip and enforce logger wrapper level parity (#4155)
Duplicate-code analysis flagged two remaining patterns: repeated policy
`Marshal`→`Unmarshal` map conversion in guard/proxy paths, and repeated
log-level wrapper sets across logger variants. This PR centralizes the
policy conversion path and adds a guardrail test for logger wrapper
completeness.
- **Policy conversion deduplication (guard + proxy)**
- Added `PolicyToMap(policy interface{}) (map[string]interface{},
error)` in `internal/guard/policy_helpers.go`.
- Replaced duplicated roundtrip blocks in:
- `internal/guard/wasm.go` (`buildStrictLabelAgentPayload`,
`BuildLabelAgentPayload`)
- `internal/proxy/proxy.go` (`initGuardPolicy`, including `allow-only`
extraction path)
- Standardized error surface for invalid/non-object policies while
preserving caller-specific context.
- **Logger wrapper drift prevention**
- Added `internal/logger/log_level_wrappers_test.go`.
- Test asserts file/markdown/server wrapper sets all cover the same
levels as `logFuncs` to prevent partial additions when introducing new
log levels.
- **Focused helper coverage**
- Added `internal/guard/policy_helpers_test.go` with coverage for:
- nil policy
- non-object policy
- unmarshalable policy
- deep-copy behavior for nested map mutation safety
```go
// shared helper now used by both guard and proxy policy paths
payload, err := guard.PolicyToMap(policy)
if err != nil {
return fmt.Errorf("policy must be a JSON object: %w", err)
}
```
> [!WARNING]
>
> <details>
> <summary>Firewall rules blocked me from connecting to one or more
addresses (expand for details)</summary>
>
> #### I tried to connect to the following addresses, but was blocked by
firewall rules:
>
> - `example.com`
> - Triggering command: `/tmp/go-build73390749/b509/launcher.test
/tmp/go-build73390749/b509/launcher.test
-test.testlogfile=/tmp/go-build73390749/b509/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true
1.80.0/internal/resolver/delegatingresolver/delegatingresolver.go
xrGu/Ij3Uman6G7fpe4BrxrGu x_amd64/vet --gdwarf-5 envconfig -o
x_amd64/vet 7745�� .a ache/go/1.25.8/x-ifaceassert x_amd64/vet
--gdwarf-5 --64 -o x_amd64/vet` (dns block)
> - `nonexistent.local`
> - Triggering command: `/tmp/go-build73390749/b509/launcher.test
/tmp/go-build73390749/b509/launcher.test
-test.testlogfile=/tmp/go-build73390749/b509/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true
1.80.0/internal/resolver/delegatingresolver/delegatingresolver.go
xrGu/Ij3Uman6G7fpe4BrxrGu x_amd64/vet --gdwarf-5 envconfig -o
x_amd64/vet 7745�� .a ache/go/1.25.8/x-ifaceassert x_amd64/vet
--gdwarf-5 --64 -o x_amd64/vet` (dns block)
> - `slow.example.com`
> - Triggering command: `/tmp/go-build73390749/b509/launcher.test
/tmp/go-build73390749/b509/launcher.test
-test.testlogfile=/tmp/go-build73390749/b509/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true
1.80.0/internal/resolver/delegatingresolver/delegatingresolver.go
xrGu/Ij3Uman6G7fpe4BrxrGu x_amd64/vet --gdwarf-5 envconfig -o
x_amd64/vet 7745�� .a ache/go/1.25.8/x-ifaceassert x_amd64/vet
--gdwarf-5 --64 -o x_amd64/vet` (dns block)
> - `this-host-does-not-exist-12345.com`
> - Triggering command: `/tmp/go-build73390749/b518/mcp.test
/tmp/go-build73390749/b518/mcp.test
-test.testlogfile=/tmp/go-build73390749/b518/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true
fflib@v1.0.0/difflib/difflib.go otection x_amd64/vet --gdwarf-5 --64
lcache/go/1.25.8-bool x_amd64/vet -E 60I2XHAs9
8177450/b287//_c-ifaceassert x_amd64/vet -I . 77450/b287/ x_amd64/vet`
(dns block)
>
> If you need me to access, download, or install something from one of
these locations, you can either:
>
> - Configure [Actions setup
steps](https://gh.io/copilot/actions-setup-steps) to set up my
environment, which run before the firewall is enabled
> - Add the appropriate URLs or hosts to the custom allowlist in this
repository's [Copilot coding agent
settings](https://github.com/github/gh-aw-mcpg/settings/copilot/coding_agent)
(admins only)
>
> </details>File tree
5 files changed
+147
-19
lines changed- internal
- guard
- logger
- proxy
5 files changed
+147
-19
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
364 | 364 | | |
365 | 365 | | |
366 | 366 | | |
367 | | - | |
| 367 | + | |
368 | 368 | | |
369 | | - | |
370 | | - | |
371 | | - | |
372 | | - | |
373 | | - | |
374 | 369 | | |
375 | 370 | | |
376 | 371 | | |
| |||
549 | 544 | | |
550 | 545 | | |
551 | 546 | | |
552 | | - | |
553 | | - | |
554 | | - | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
555 | 551 | | |
556 | | - | |
| 552 | + | |
557 | 553 | | |
558 | 554 | | |
559 | 555 | | |
560 | 556 | | |
561 | | - | |
562 | | - | |
563 | | - | |
564 | | - | |
565 | | - | |
566 | 557 | | |
567 | 558 | | |
568 | 559 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
156 | 156 | | |
157 | 157 | | |
158 | 158 | | |
159 | | - | |
160 | | - | |
161 | | - | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
162 | 162 | | |
163 | 163 | | |
164 | 164 | | |
165 | 165 | | |
166 | | - | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
167 | 174 | | |
168 | 175 | | |
169 | 176 | | |
| |||
0 commit comments