Skip to content

Commit 8e22a91

Browse files
Copilotlpcox
andauthored
extract shared marshal debug helper
Agent-Logs-Url: https://github.com/github/gh-aw-mcpg/sessions/ac7dc66f-a385-4612-8f8a-66a15160508c Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
1 parent 81b3a88 commit 8e22a91

4 files changed

Lines changed: 71 additions & 21 deletions

File tree

internal/guard/wasm.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@ import (
2020

2121
var logWasm = logger.New("guard:wasm")
2222

23-
func logMarshaledForDebug(value interface{}, onMarshalSuccess func(string), onMarshalFailure func(error)) {
24-
resultJSON, err := json.Marshal(value)
25-
if err != nil {
26-
onMarshalFailure(err)
27-
return
28-
}
29-
onMarshalSuccess(string(resultJSON))
30-
}
31-
3223
// globalCompilationCache is a process-level compilation cache shared across all
3324
// WasmGuard instances. wazero's cache is goroutine-safe and eliminates redundant
3425
// JIT compilation when multiple guards load the same WASM binary.
@@ -704,7 +695,7 @@ func (g *WasmGuard) LabelAgent(ctx context.Context, policy interface{}, backend
704695
logWasm.Printf("LabelAgent normalizePolicyPayload failed: guard=%s, error=%v", g.name, err)
705696
return nil, err
706697
}
707-
logMarshaledForDebug(
698+
logger.LogMarshaledForDebug(
708699
normalizedPolicy,
709700
func(policyJSON string) {
710701
logWasm.Printf("LabelAgent normalized policy: guard=%s, policy=%s", g.name, policyJSON)
@@ -739,7 +730,7 @@ func (g *WasmGuard) LabelAgent(ctx context.Context, policy interface{}, backend
739730
return nil, err
740731
}
741732

742-
logMarshaledForDebug(
733+
logger.LogMarshaledForDebug(
743734
result,
744735
func(responseJSON string) {
745736
logWasm.Printf("LabelAgent parsed response: guard=%s, response=%s", g.name, responseJSON)

internal/logger/marshal_debug.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package logger
2+
3+
import "encoding/json"
4+
5+
// LogMarshaledForDebug marshals value for debug logging and dispatches to the
6+
// provided callbacks for success or marshal failure paths.
7+
func LogMarshaledForDebug(value interface{}, onMarshalSuccess func(string), onMarshalFailure func(error)) {
8+
resultJSON, err := json.Marshal(value)
9+
if err != nil {
10+
onMarshalFailure(err)
11+
return
12+
}
13+
onMarshalSuccess(string(resultJSON))
14+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package logger
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestLogMarshaledForDebug_Success(t *testing.T) {
11+
assert := assert.New(t)
12+
require := require.New(t)
13+
14+
var (
15+
gotJSON string
16+
gotMarshalErr error
17+
)
18+
19+
LogMarshaledForDebug(
20+
map[string]string{"status": "ok"},
21+
func(resultJSON string) {
22+
gotJSON = resultJSON
23+
},
24+
func(err error) {
25+
gotMarshalErr = err
26+
},
27+
)
28+
29+
require.NoError(gotMarshalErr)
30+
assert.JSONEq(`{"status":"ok"}`, gotJSON)
31+
}
32+
33+
func TestLogMarshaledForDebug_MarshalFailure(t *testing.T) {
34+
assert := assert.New(t)
35+
require := require.New(t)
36+
37+
var (
38+
gotJSON string
39+
gotMarshalErr error
40+
)
41+
42+
LogMarshaledForDebug(
43+
make(chan int),
44+
func(resultJSON string) {
45+
gotJSON = resultJSON
46+
},
47+
func(err error) {
48+
gotMarshalErr = err
49+
},
50+
)
51+
52+
require.Error(gotMarshalErr)
53+
assert.Empty(gotJSON)
54+
}

internal/server/guard_init.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,6 @@ import (
1717

1818
var logGuardInit = logger.New("server:guard_init")
1919

20-
func logMarshaledForDebug(value interface{}, onMarshalSuccess func(string), onMarshalFailure func(error)) {
21-
resultJSON, err := json.Marshal(value)
22-
if err != nil {
23-
onMarshalFailure(err)
24-
return
25-
}
26-
onMarshalSuccess(string(resultJSON))
27-
}
28-
2920
// hasServerGuardPolicies reports whether any server in cfg has per-server guard policies
3021
// configured. This is used during DIFC auto-detection to enable enforcement when policies
3122
// are present even if no non-noop guard was registered (e.g., guard missing or failed to load).
@@ -360,7 +351,7 @@ func (us *UnifiedServer) ensureGuardInitialized(
360351
log.Printf("[DIFC] label_agent returned nil result: server=%s, session=%s, guard=%s", serverID, sessionID, g.Name())
361352
return defaultMode, fmt.Errorf("label_agent returned nil result")
362353
}
363-
logMarshaledForDebug(
354+
logger.LogMarshaledForDebug(
364355
labelAgentResult,
365356
func(resultJSON string) {
366357
log.Printf("[DIFC] label_agent response: server=%s, session=%s, guard=%s, response=%s", serverID, sessionID, g.Name(), resultJSON)

0 commit comments

Comments
 (0)