Skip to content

Commit 25db0fb

Browse files
committed
tests: verify exec traffic is policy-checked in caps proxy test
Extend testBuildPolicyCapsProxy so that instead of only checking that the network proxy was enabled, the build runs a command that makes an HTTP request to a local test server. The policy denies that URL as an HTTP source, so the test now verifies that exec traffic actually flows through the proxy and is subject to source policy, including the deny message and DENY decision in the build output. Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
1 parent 4da04e3 commit 25db0fb

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

tests/policy_build.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,37 @@ var policyBuildTests = []func(t *testing.T, sb integration.Sandbox){
4444
testBuildPolicyCapsProxyUnsupported,
4545
}
4646

47-
var policyCapsProxyFile = []byte(`
47+
func policyCapsProxyFile(serverURL string) []byte {
48+
return fmt.Appendf(nil, `
4849
package docker
4950
default allow = true
51+
default deny_msg := []
5052
default caps := {}
5153
caps := {"exec.proxy": true} if input.env.capsRequest
52-
decision := {"allow": allow, "caps": caps}
53-
`)
54+
allow := false if input.http.url == "%s/file"
55+
deny_msg := ["exec proxy http source denied by policy"] if input.http.url == "%s/file"
56+
decision := {"allow": allow, "deny_msg": deny_msg, "caps": caps}
57+
`, serverURL, serverURL)
58+
}
5459

5560
func testBuildPolicyCapsProxy(t *testing.T, sb integration.Sandbox) {
5661
if buildkitTag() != "master" {
5762
skipNoCompatBuildKit(t, sb, ">= 0.31.0-0", "network proxy requires BuildKit v0.31.0+")
5863
}
64+
resp := &httpserver.Response{Content: []byte("policy-caps-proxy")}
65+
server := httpserver.NewTestServer(map[string]*httpserver.Response{
66+
"/file": resp,
67+
})
68+
defer server.Close()
69+
5970
dockerfile := []byte(`
60-
FROM scratch
61-
COPY foo /foo
71+
FROM busybox:latest
72+
RUN wget -O- ` + server.URL + `/file
6273
`)
6374
dir := tmpdir(
6475
t,
6576
fstest.CreateFile("Dockerfile", dockerfile, 0600),
66-
fstest.CreateFile("Dockerfile.rego", policyCapsProxyFile, 0600),
67-
fstest.CreateFile("foo", []byte("foo"), 0600),
77+
fstest.CreateFile("Dockerfile.rego", policyCapsProxyFile(server.URL), 0600),
6878
)
6979

7080
cmd := buildxCmd(sb, withDir(dir), withArgs(
@@ -74,8 +84,11 @@ COPY foo /foo
7484
dir,
7585
))
7686
out, err := cmd.CombinedOutput()
77-
require.NoError(t, err, string(out))
87+
require.Error(t, err, string(out))
7888
require.Contains(t, string(out), "policy enabled network proxy")
89+
require.Contains(t, string(out), "exec proxy http source denied by policy")
90+
require.Contains(t, string(out), "policy decision for source "+server.URL+"/file")
91+
require.Contains(t, string(out), "DENY")
7992
}
8093

8194
func testBuildPolicyCapsProxyUnsupported(t *testing.T, sb integration.Sandbox) {
@@ -90,7 +103,7 @@ COPY foo /foo
90103
dir := tmpdir(
91104
t,
92105
fstest.CreateFile("Dockerfile", dockerfile, 0600),
93-
fstest.CreateFile("Dockerfile.rego", policyCapsProxyFile, 0600),
106+
fstest.CreateFile("Dockerfile.rego", policyCapsProxyFile(""), 0600),
94107
fstest.CreateFile("foo", []byte("foo"), 0600),
95108
)
96109

0 commit comments

Comments
 (0)