-
Notifications
You must be signed in to change notification settings - Fork 354
Expand file tree
/
Copy pathcompiler_difc_proxy_test.go
More file actions
812 lines (747 loc) · 27.2 KB
/
compiler_difc_proxy_test.go
File metadata and controls
812 lines (747 loc) · 27.2 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
//go:build !integration
package workflow
import (
"strings"
"testing"
"github.com/github/gh-aw/pkg/constants"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestHasDIFCProxyNeeded verifies that DIFC proxy injection is triggered only
// when guard policies are configured AND pre-agent steps have GH_TOKEN.
func TestHasDIFCProxyNeeded(t *testing.T) {
tests := []struct {
name string
data *WorkflowData
expected bool
desc string
}{
{
name: "nil workflow data",
data: nil,
expected: false,
desc: "nil data should never need proxy",
},
{
name: "no github tool",
data: &WorkflowData{Tools: map[string]any{}},
expected: false,
desc: "no github tool means no guard policy, proxy not needed",
},
{
name: "github tool disabled",
data: &WorkflowData{
Tools: map[string]any{
"github": false,
},
},
expected: false,
desc: "disabled github tool should not trigger proxy",
},
{
name: "github tool without guard policy",
data: &WorkflowData{
Tools: map[string]any{
"github": map[string]any{"toolsets": []string{"default"}},
},
CustomSteps: "steps:\n - name: Fetch data\n env:\n GH_TOKEN: ${{ github.token }}\n run: gh issue list",
},
expected: false,
desc: "no guard policy (auto-lockdown only) should not trigger proxy",
},
{
name: "guard policy configured but no pre-agent steps with GH_TOKEN",
data: &WorkflowData{
Tools: map[string]any{
"github": map[string]any{
"min-integrity": "approved",
},
},
},
expected: false,
desc: "guard policy without GH_TOKEN pre-agent steps should not trigger proxy",
},
{
name: "guard policy + custom steps with GH_TOKEN but integrity-proxy disabled",
data: &WorkflowData{
Tools: map[string]any{
"github": map[string]any{
"min-integrity": "approved",
"integrity-proxy": false,
},
},
CustomSteps: "steps:\n - name: Fetch issues\n env:\n GH_TOKEN: ${{ github.token }}\n run: gh issue list",
},
expected: false,
desc: "integrity-proxy: false → proxy not triggered even when guard policy and GH_TOKEN present",
},
{
name: "guard policy + custom steps with GH_TOKEN (proxy enabled by default)",
data: &WorkflowData{
Tools: map[string]any{
"github": map[string]any{
"min-integrity": "approved",
},
},
CustomSteps: "steps:\n - name: Fetch issues\n env:\n GH_TOKEN: ${{ github.token }}\n run: gh issue list",
},
expected: true,
desc: "guard policy + custom steps with GH_TOKEN should trigger proxy by default (no feature flag needed)",
},
{
name: "guard policy + custom steps with GH_TOKEN + integrity-proxy explicitly true",
data: &WorkflowData{
Tools: map[string]any{
"github": map[string]any{
"min-integrity": "approved",
"integrity-proxy": true,
},
},
CustomSteps: "steps:\n - name: Fetch issues\n env:\n GH_TOKEN: ${{ github.token }}\n run: gh issue list",
},
expected: true,
desc: "integrity-proxy: true explicitly set should trigger proxy",
},
{
name: "guard policy + repo-memory configured",
data: &WorkflowData{
Tools: map[string]any{
"github": map[string]any{
"min-integrity": "approved",
"repos": "all",
},
},
RepoMemoryConfig: &RepoMemoryConfig{
Memories: []RepoMemoryEntry{{ID: "memory"}},
},
},
expected: false,
desc: "guard policy + repo-memory should NOT trigger proxy: repo-memory clones use direct git URLs, not GH_HOST",
},
{
name: "guard policy with allowed-repos + custom steps with GH_TOKEN (default enabled)",
data: &WorkflowData{
Tools: map[string]any{
"github": map[string]any{
"min-integrity": "merged",
"allowed-repos": []string{"owner/repo"},
},
},
CustomSteps: "steps:\n - name: Fetch PRs\n env:\n GH_TOKEN: ${{ secrets.MY_TOKEN }}\n run: gh pr list",
},
expected: true,
desc: "allowed-repos + min-integrity + GH_TOKEN custom steps should trigger proxy by default",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := hasDIFCProxyNeeded(tt.data)
assert.Equal(t, tt.expected, got, "hasDIFCProxyNeeded: %s", tt.desc)
})
}
}
// TestHasPreAgentStepsWithGHToken verifies detection of pre-agent steps with GH_TOKEN.
func TestHasPreAgentStepsWithGHToken(t *testing.T) {
tests := []struct {
name string
data *WorkflowData
expected bool
}{
{
name: "nil data",
data: nil,
expected: false,
},
{
name: "empty data",
data: &WorkflowData{},
expected: false,
},
{
name: "custom steps without GH_TOKEN",
data: &WorkflowData{
CustomSteps: "steps:\n - name: Build\n run: make build\n",
},
expected: false,
},
{
name: "custom steps with GH_TOKEN",
data: &WorkflowData{
CustomSteps: "steps:\n - name: Fetch\n env:\n GH_TOKEN: ${{ github.token }}\n run: gh issue list\n",
},
expected: true,
},
{
name: "repo-memory configured",
data: &WorkflowData{
RepoMemoryConfig: &RepoMemoryConfig{
Memories: []RepoMemoryEntry{{ID: "memory"}},
},
},
expected: false,
// repo-memory clone steps use direct "git clone https://x-access-token:${GH_TOKEN}@..."
// URLs derived from GITHUB_SERVER_URL, not GH_HOST, so the proxy does not intercept them.
},
{
name: "repo-memory with empty memories (no clone steps generated)",
data: &WorkflowData{
RepoMemoryConfig: &RepoMemoryConfig{},
},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := hasPreAgentStepsWithGHToken(tt.data)
assert.Equal(t, tt.expected, got, "test: %s", tt.name)
})
}
}
// TestGetDIFCProxyPolicyJSON verifies that the proxy policy JSON contains
// only the static fields (min-integrity and repos) without dynamic expressions.
func TestGetDIFCProxyPolicyJSON(t *testing.T) {
tests := []struct {
name string
githubTool any
expectedContains []string
expectedAbsent []string
expectEmpty bool
}{
{
name: "nil tool",
githubTool: nil,
expectEmpty: true,
},
{
name: "non-map tool",
githubTool: false,
expectEmpty: true,
},
{
name: "min-integrity only",
githubTool: map[string]any{
"min-integrity": "approved",
},
expectedContains: []string{`"allow-only"`, `"min-integrity":"approved"`, `"repos":"all"`},
expectedAbsent: []string{"blocked-users", "approval-labels", "steps.parse-guard-vars", "__GH_AW_GUARD_EXPR"},
},
{
name: "min-integrity and repos",
githubTool: map[string]any{
"min-integrity": "merged",
"repos": "all",
},
expectedContains: []string{`"allow-only"`, `"min-integrity":"merged"`, `"repos":"all"`},
expectedAbsent: []string{"blocked-users", "approval-labels"},
},
{
name: "allowed-repos (preferred field name)",
githubTool: map[string]any{
"min-integrity": "unapproved",
"allowed-repos": "owner/*",
},
expectedContains: []string{`"min-integrity":"unapproved"`, `"repos":"owner/*"`},
expectedAbsent: []string{"blocked-users", "approval-labels"},
},
{
name: "tool without guard policy fields",
githubTool: map[string]any{
"toolsets": []string{"default"},
},
expectEmpty: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := getDIFCProxyPolicyJSON(tt.githubTool, nil, nil)
if tt.expectEmpty {
assert.Empty(t, got, "policy JSON should be empty for: %s", tt.name)
return
}
require.NotEmpty(t, got, "policy JSON should not be empty for: %s", tt.name)
for _, s := range tt.expectedContains {
assert.Contains(t, got, s, "policy JSON should contain %q for: %s", s, tt.name)
}
for _, s := range tt.expectedAbsent {
assert.NotContains(t, got, s, "policy JSON should NOT contain %q for: %s", s, tt.name)
}
})
}
}
// TestGenerateStartDIFCProxyStep verifies the YAML generated for the proxy start step.
func TestGenerateStartDIFCProxyStep(t *testing.T) {
c := &Compiler{}
t.Run("no proxy when guard policy not configured", func(t *testing.T) {
var yaml strings.Builder
data := &WorkflowData{
Tools: map[string]any{
"github": map[string]any{"toolsets": []string{"default"}},
},
CustomSteps: "steps:\n - name: Fetch\n env:\n GH_TOKEN: ${{ github.token }}\n run: gh issue list",
SandboxConfig: &SandboxConfig{},
}
c.generateStartDIFCProxyStep(&yaml, data)
assert.Empty(t, yaml.String(), "should not generate proxy step without guard policy")
})
t.Run("no proxy when no GH_TOKEN pre-agent steps", func(t *testing.T) {
var yaml strings.Builder
data := &WorkflowData{
Tools: map[string]any{
"github": map[string]any{"min-integrity": "approved"},
},
SandboxConfig: &SandboxConfig{},
}
c.generateStartDIFCProxyStep(&yaml, data)
assert.Empty(t, yaml.String(), "should not generate proxy step without pre-agent GH_TOKEN steps")
})
t.Run("generates start step with guard policy and custom steps", func(t *testing.T) {
var yaml strings.Builder
data := &WorkflowData{
Tools: map[string]any{
"github": map[string]any{
"min-integrity": "approved",
},
},
CustomSteps: "steps:\n - name: Fetch\n env:\n GH_TOKEN: ${{ github.token }}\n run: gh issue list",
SandboxConfig: &SandboxConfig{},
}
ensureDefaultMCPGatewayConfig(data)
c.generateStartDIFCProxyStep(&yaml, data)
result := yaml.String()
require.NotEmpty(t, result, "should generate proxy start step")
assert.Contains(t, result, "Start DIFC proxy for pre-agent gh calls", "step name should be present")
assert.Contains(t, result, "GH_TOKEN:", "step should include GH_TOKEN env var")
assert.Contains(t, result, "GITHUB_SERVER_URL:", "step should include GITHUB_SERVER_URL env var")
assert.Contains(t, result, "DIFC_PROXY_POLICY:", "step should include policy as env var")
assert.Contains(t, result, "DIFC_PROXY_IMAGE:", "step should include image as env var")
assert.Contains(t, result, "start_difc_proxy.sh", "step should call the proxy script")
assert.Contains(t, result, `"allow-only"`, "step should include guard policy JSON in env var")
assert.Contains(t, result, `"min-integrity":"approved"`, "step should include min-integrity in policy env var")
assert.Contains(t, result, "ghcr.io/github/gh-aw-mcpg", "step should include container image in env var")
assert.NotContains(t, result, "blocked-users", "proxy policy should not include dynamic blocked-users")
assert.NotContains(t, result, "approval-labels", "proxy policy should not include dynamic approval-labels")
})
}
// TestGenerateStopDIFCProxyStep verifies the YAML generated for the proxy stop step.
func TestGenerateStopDIFCProxyStep(t *testing.T) {
c := &Compiler{}
t.Run("no stop step when proxy not needed", func(t *testing.T) {
var yaml strings.Builder
data := &WorkflowData{
Tools: map[string]any{
"github": map[string]any{"toolsets": []string{"default"}},
},
SandboxConfig: &SandboxConfig{},
}
c.generateStopDIFCProxyStep(&yaml, data)
assert.Empty(t, yaml.String(), "should not generate stop step when proxy not needed")
})
t.Run("generates stop step when proxy is needed", func(t *testing.T) {
var yaml strings.Builder
data := &WorkflowData{
Tools: map[string]any{
"github": map[string]any{"min-integrity": "approved"},
},
CustomSteps: "steps:\n - name: Fetch\n env:\n GH_TOKEN: ${{ github.token }}\n run: gh issue list",
SandboxConfig: &SandboxConfig{},
}
c.generateStopDIFCProxyStep(&yaml, data)
result := yaml.String()
require.NotEmpty(t, result, "should generate proxy stop step")
assert.Contains(t, result, "Stop DIFC proxy", "step name should be present")
assert.Contains(t, result, "stop_difc_proxy.sh", "step should call the stop script")
})
}
// TestDIFCProxyLogPaths verifies the artifact paths returned for DIFC proxy logs.
func TestDIFCProxyLogPaths(t *testing.T) {
t.Run("no log paths when proxy not needed", func(t *testing.T) {
data := &WorkflowData{
Tools: map[string]any{
"github": map[string]any{"toolsets": []string{"default"}},
},
}
paths := difcProxyLogPaths(data)
assert.Empty(t, paths, "should return no log paths when proxy not needed")
})
t.Run("returns proxy-logs path when proxy is needed", func(t *testing.T) {
data := &WorkflowData{
Tools: map[string]any{
"github": map[string]any{"min-integrity": "approved"},
},
CustomSteps: "steps:\n - name: Fetch\n env:\n GH_TOKEN: ${{ github.token }}\n run: gh issue list",
}
paths := difcProxyLogPaths(data)
require.Len(t, paths, 2, "should return include path and exclusion path")
assert.Contains(t, paths[0], "proxy-logs", "first path should include proxy-logs directory")
assert.Contains(t, paths[1], "proxy-tls", "second path should exclude proxy-tls directory")
assert.True(t, strings.HasPrefix(paths[1], "!"), "exclusion path should start with !")
})
}
// TestDIFCProxyStepOrderInCompiledWorkflow verifies that proxy steps are injected
// at the correct positions in the generated workflow YAML.
func TestDIFCProxyStepOrderInCompiledWorkflow(t *testing.T) {
workflow := `---
on: issues
engine: copilot
tools:
github:
mode: local
toolsets: [default]
min-integrity: approved
steps:
- name: Fetch repo data
env:
GH_TOKEN: ${{ secrets.GH_AW_GITHUB_MCP_SERVER_TOKEN || secrets.GITHUB_TOKEN }}
run: |
gh issue list -R $GITHUB_REPOSITORY --state open --limit 500 \
--json number,labels > /tmp/gh-aw/issues.json 2>/dev/null \
|| echo '[]' > /tmp/gh-aw/issues.json
---
# Test Workflow
Test that DIFC proxy is injected by default when min-integrity is set with custom steps using GH_TOKEN.
`
compiler := NewCompiler()
data, err := compiler.ParseWorkflowString(workflow, "test-workflow.md")
require.NoError(t, err, "parsing should succeed")
result, err := compiler.CompileToYAML(data, "test-workflow.md")
require.NoError(t, err, "compilation should succeed")
// Verify proxy start step is present
assert.Contains(t, result, "Start DIFC proxy for pre-agent gh calls",
"compiled workflow should contain proxy start step")
// Verify proxy stop step is present
assert.Contains(t, result, "Stop DIFC proxy",
"compiled workflow should contain proxy stop step")
// Verify the "Set GH_REPO" step is present
assert.Contains(t, result, "Set GH_REPO for proxied steps",
"compiled workflow should contain Set GH_REPO step")
// Verify step ordering: Start proxy must come before Stop proxy
startIdx := strings.Index(result, "Start DIFC proxy for pre-agent gh calls")
setRepoIdx := strings.Index(result, "Set GH_REPO for proxied steps")
stopIdx := strings.Index(result, "Stop DIFC proxy")
require.Greater(t, startIdx, -1, "start proxy step should be in output")
require.Greater(t, setRepoIdx, -1, "set GH_REPO step should be in output")
require.Greater(t, stopIdx, -1, "stop proxy step should be in output")
assert.Less(t, startIdx, setRepoIdx, "Start DIFC proxy must come before Set GH_REPO")
assert.Less(t, startIdx, stopIdx, "Start DIFC proxy must come before Stop DIFC proxy")
// Verify "Set GH_REPO" step is before custom step ("Fetch repo data")
customStepIdx := strings.Index(result, "Fetch repo data")
require.Greater(t, customStepIdx, -1, "custom step should be in output")
assert.Less(t, startIdx, customStepIdx, "Start DIFC proxy must come before custom step")
assert.Less(t, setRepoIdx, customStepIdx, "Set GH_REPO must come before custom step")
// Verify proxy stop is before MCP gateway start
gatewayIdx := strings.Index(result, "Start MCP Gateway")
require.Greater(t, gatewayIdx, -1, "gateway start step should be in output")
assert.Less(t, stopIdx, gatewayIdx, "Stop DIFC proxy must come before Start MCP Gateway")
// Verify start_difc_proxy.sh and stop_difc_proxy.sh are referenced
assert.Contains(t, result, "start_difc_proxy.sh", "should reference start script")
assert.Contains(t, result, "stop_difc_proxy.sh", "should reference stop script")
// Verify the policy JSON in the proxy start step does NOT contain dynamic fields.
// Note: the MCP gateway config may include approval-labels/blocked-users, but the proxy policy must not.
// The policy is stored in the DIFC_PROXY_POLICY env var line.
proxyPolicyLine := ""
for line := range strings.SplitSeq(result, "\n") {
if strings.Contains(line, "DIFC_PROXY_POLICY") {
proxyPolicyLine = line
break
}
}
require.NotEmpty(t, proxyPolicyLine, "should find the DIFC_PROXY_POLICY env var line")
assert.NotContains(t, proxyPolicyLine, "blocked-users", "proxy policy should not include blocked-users")
assert.NotContains(t, proxyPolicyLine, "approval-labels", "proxy policy should not include approval-labels")
}
// TestDIFCProxyNotInjectedWithoutGuardPolicy verifies no proxy injection without guard policy.
func TestDIFCProxyNotInjectedWithoutGuardPolicy(t *testing.T) {
workflow := `---
on: issues
engine: copilot
tools:
github:
mode: local
toolsets: [default]
steps:
- name: Fetch repo data
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: gh issue list
---
# Test Workflow
Test that DIFC proxy is NOT injected when min-integrity is not set.
`
compiler := NewCompiler()
data, err := compiler.ParseWorkflowString(workflow, "test-workflow.md")
require.NoError(t, err, "parsing should succeed")
result, err := compiler.CompileToYAML(data, "test-workflow.md")
require.NoError(t, err, "compilation should succeed")
assert.NotContains(t, result, "Start DIFC proxy",
"compiled workflow should NOT contain proxy start step without guard policy")
assert.NotContains(t, result, "Stop DIFC proxy",
"compiled workflow should NOT contain proxy stop step without guard policy")
}
// TestDIFCProxyNotInjectedWhenIntegrityProxyFalse verifies no proxy injection when
// guard policies are configured but tools.github.integrity-proxy: false is set.
func TestDIFCProxyNotInjectedWhenIntegrityProxyFalse(t *testing.T) {
workflow := `---
on: issues
engine: copilot
tools:
github:
mode: local
toolsets: [default]
min-integrity: approved
integrity-proxy: false
steps:
- name: Fetch repo data
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: gh issue list
---
# Test Workflow
Test that DIFC proxy is NOT injected when integrity-proxy: false is set.
`
compiler := NewCompiler()
data, err := compiler.ParseWorkflowString(workflow, "test-workflow.md")
require.NoError(t, err, "parsing should succeed")
result, err := compiler.CompileToYAML(data, "test-workflow.md")
require.NoError(t, err, "compilation should succeed")
assert.NotContains(t, result, "Start DIFC proxy",
"compiled workflow should NOT contain proxy start step without guard policy")
assert.NotContains(t, result, "Stop DIFC proxy",
"compiled workflow should NOT contain proxy stop step without guard policy")
}
// TestHasDIFCGuardsConfigured verifies the base guard policy check.
func TestHasDIFCGuardsConfigured(t *testing.T) {
tests := []struct {
name string
data *WorkflowData
expected bool
}{
{
name: "nil data",
data: nil,
expected: false,
},
{
name: "no github tool",
data: &WorkflowData{Tools: map[string]any{}},
expected: false,
},
{
name: "github tool without guard policy",
data: &WorkflowData{
Tools: map[string]any{
"github": map[string]any{"toolsets": []string{"default"}},
},
},
expected: false,
},
{
name: "github tool with min-integrity (enabled by default)",
data: &WorkflowData{
Tools: map[string]any{
"github": map[string]any{"min-integrity": "approved"},
},
},
expected: true,
},
{
name: "github tool with min-integrity and integrity-proxy: true",
data: &WorkflowData{
Tools: map[string]any{
"github": map[string]any{
"min-integrity": "approved",
"integrity-proxy": true,
},
},
},
expected: true,
},
{
name: "github tool with min-integrity and integrity-proxy: false",
data: &WorkflowData{
Tools: map[string]any{
"github": map[string]any{
"min-integrity": "approved",
"integrity-proxy": false,
},
},
},
expected: false,
},
{
name: "github tool with allowed-repos and min-integrity (enabled by default)",
data: &WorkflowData{
Tools: map[string]any{
"github": map[string]any{
"allowed-repos": "all",
"min-integrity": "merged",
},
},
},
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := hasDIFCGuardsConfigured(tt.data)
assert.Equal(t, tt.expected, got, "hasDIFCGuardsConfigured: %s", tt.name)
})
}
}
// TestBuildSetGHRepoStepYAML verifies the YAML generated for the "Set GH_REPO" step.
func TestBuildSetGHRepoStepYAML(t *testing.T) {
result := buildSetGHRepoStepYAML()
assert.Contains(t, result, "Set GH_REPO for proxied steps", "step name should be present")
assert.Contains(t, result, "GH_REPO=${GITHUB_REPOSITORY}", "should set GH_REPO from GITHUB_REPOSITORY")
assert.Contains(t, result, "GITHUB_ENV", "should write GH_REPO to GITHUB_ENV")
assert.NotContains(t, result, "GH_HOST", "should not modify GH_HOST (proxy must keep routing)")
}
// TestGenerateSetGHRepoAfterDIFCProxyStep verifies that the step is emitted only when
// the DIFC proxy is needed (guard policies configured + pre-agent GH_TOKEN steps).
func TestGenerateSetGHRepoAfterDIFCProxyStep(t *testing.T) {
c := &Compiler{}
t.Run("no step when guard policy not configured", func(t *testing.T) {
var yaml strings.Builder
data := &WorkflowData{
Tools: map[string]any{
"github": map[string]any{"toolsets": []string{"default"}},
},
CustomSteps: "steps:\n - name: Fetch\n env:\n GH_TOKEN: ${{ github.token }}\n run: gh issue list",
SandboxConfig: &SandboxConfig{},
}
c.generateSetGHRepoAfterDIFCProxyStep(&yaml, data)
assert.Empty(t, yaml.String(), "should not generate step without guard policy")
})
t.Run("no step when no GH_TOKEN pre-agent steps", func(t *testing.T) {
var yaml strings.Builder
data := &WorkflowData{
Tools: map[string]any{
"github": map[string]any{"min-integrity": "approved"},
},
SandboxConfig: &SandboxConfig{},
}
c.generateSetGHRepoAfterDIFCProxyStep(&yaml, data)
assert.Empty(t, yaml.String(), "should not generate step without pre-agent GH_TOKEN steps")
})
t.Run("generates set GH_REPO step when guard policy and custom steps with GH_TOKEN", func(t *testing.T) {
var yaml strings.Builder
data := &WorkflowData{
Tools: map[string]any{
"github": map[string]any{"min-integrity": "approved"},
},
CustomSteps: "steps:\n - name: Fetch\n env:\n GH_TOKEN: ${{ github.token }}\n run: gh issue list",
SandboxConfig: &SandboxConfig{},
}
c.generateSetGHRepoAfterDIFCProxyStep(&yaml, data)
result := yaml.String()
require.NotEmpty(t, result, "should generate set GH_REPO step")
assert.Contains(t, result, "Set GH_REPO for proxied steps", "step name should be present")
assert.Contains(t, result, "GH_REPO=${GITHUB_REPOSITORY}", "should set GH_REPO from GITHUB_REPOSITORY")
assert.Contains(t, result, "GITHUB_ENV", "should write to GITHUB_ENV")
assert.NotContains(t, result, "GH_HOST", "should not touch GH_HOST")
})
}
// TestBuildStartCliProxyStepYAML verifies that the CLI proxy step always emits
// CLI_PROXY_POLICY, using the default permissive policy when no guard policy is
// configured in the frontmatter.
func TestBuildStartCliProxyStepYAML(t *testing.T) {
c := &Compiler{}
t.Run("emits default policy when no guard policy is configured", func(t *testing.T) {
data := &WorkflowData{
Tools: map[string]any{
"github": map[string]any{"toolsets": []string{"default"}},
},
}
result := c.buildStartCliProxyStepYAML(data)
require.NotEmpty(t, result, "should emit CLI proxy step even without guard policy")
assert.Contains(t, result, "CLI_PROXY_POLICY", "should always emit CLI_PROXY_POLICY")
assert.Contains(t, result, `"allow-only"`, "default policy should contain allow-only")
assert.Contains(t, result, `"repos":"all"`, "default policy should allow all repos")
assert.Contains(t, result, `"min-integrity":"none"`, "default policy should have min-integrity none")
})
t.Run("emits default policy when github tool is nil", func(t *testing.T) {
data := &WorkflowData{
Tools: map[string]any{},
}
result := c.buildStartCliProxyStepYAML(data)
require.NotEmpty(t, result, "should emit CLI proxy step even without github tool")
assert.Contains(t, result, "CLI_PROXY_POLICY", "should always emit CLI_PROXY_POLICY")
assert.Contains(t, result, `"min-integrity":"none"`, "should use default min-integrity")
})
t.Run("uses configured guard policy when present", func(t *testing.T) {
data := &WorkflowData{
Tools: map[string]any{
"github": map[string]any{
"min-integrity": "approved",
"allowed-repos": "owner/*",
},
},
}
result := c.buildStartCliProxyStepYAML(data)
require.NotEmpty(t, result, "should emit CLI proxy step")
assert.Contains(t, result, "CLI_PROXY_POLICY", "should emit CLI_PROXY_POLICY")
assert.Contains(t, result, `"min-integrity":"approved"`, "should use configured min-integrity")
assert.Contains(t, result, `"repos":"owner/*"`, "should use configured repos")
})
t.Run("emits correct step structure", func(t *testing.T) {
data := &WorkflowData{
Tools: map[string]any{
"github": map[string]any{"toolsets": []string{"default"}},
},
}
result := c.buildStartCliProxyStepYAML(data)
assert.Contains(t, result, "name: Start CLI proxy", "should have correct step name")
assert.Contains(t, result, "GH_TOKEN:", "should include GH_TOKEN")
assert.Contains(t, result, "GITHUB_SERVER_URL:", "should include GITHUB_SERVER_URL")
assert.Contains(t, result, "CLI_PROXY_IMAGE:", "should include CLI_PROXY_IMAGE")
assert.Contains(t, result, "start_cli_proxy.sh", "should reference the start script")
})
}
// TestResolveProxyContainerImage verifies that the helper builds the correct container
// image reference from the gateway config, falling back to the default version when
// no version is specified.
func TestResolveProxyContainerImage(t *testing.T) {
tests := []struct {
name string
config *MCPGatewayRuntimeConfig
expected string
}{
{
name: "uses default version when version is empty",
config: &MCPGatewayRuntimeConfig{
Container: constants.DefaultMCPGatewayContainer,
Version: "",
},
expected: constants.DefaultMCPGatewayContainer + ":" + string(constants.DefaultMCPGatewayVersion),
},
{
name: "uses explicit version when set",
config: &MCPGatewayRuntimeConfig{
Container: constants.DefaultMCPGatewayContainer,
Version: "v1.2.3",
},
expected: constants.DefaultMCPGatewayContainer + ":v1.2.3",
},
{
name: "custom container with default version fallback",
config: &MCPGatewayRuntimeConfig{
Container: "ghcr.io/myorg/my-proxy",
Version: "",
},
expected: "ghcr.io/myorg/my-proxy:" + string(constants.DefaultMCPGatewayVersion),
},
{
name: "custom container with explicit version",
config: &MCPGatewayRuntimeConfig{
Container: "ghcr.io/myorg/my-proxy",
Version: "latest",
},
expected: "ghcr.io/myorg/my-proxy:latest",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := resolveProxyContainerImage(tt.config)
assert.Equal(t, tt.expected, got, "resolveProxyContainerImage(%+v)", tt.config)
})
}
}