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>
This commit is contained in:
Tonis Tiigi
2026-06-10 08:58:48 -07:00
parent 4da04e3bf4
commit 25db0fb050
+22 -9
View File
@@ -44,27 +44,37 @@ var policyBuildTests = []func(t *testing.T, sb integration.Sandbox){
testBuildPolicyCapsProxyUnsupported, testBuildPolicyCapsProxyUnsupported,
} }
var policyCapsProxyFile = []byte(` func policyCapsProxyFile(serverURL string) []byte {
return fmt.Appendf(nil, `
package docker package docker
default allow = true default allow = true
default deny_msg := []
default caps := {} default caps := {}
caps := {"exec.proxy": true} if input.env.capsRequest caps := {"exec.proxy": true} if input.env.capsRequest
decision := {"allow": allow, "caps": caps} allow := false if input.http.url == "%s/file"
`) deny_msg := ["exec proxy http source denied by policy"] if input.http.url == "%s/file"
decision := {"allow": allow, "deny_msg": deny_msg, "caps": caps}
`, serverURL, serverURL)
}
func testBuildPolicyCapsProxy(t *testing.T, sb integration.Sandbox) { func testBuildPolicyCapsProxy(t *testing.T, sb integration.Sandbox) {
if buildkitTag() != "master" { if buildkitTag() != "master" {
skipNoCompatBuildKit(t, sb, ">= 0.31.0-0", "network proxy requires BuildKit v0.31.0+") skipNoCompatBuildKit(t, sb, ">= 0.31.0-0", "network proxy requires BuildKit v0.31.0+")
} }
resp := &httpserver.Response{Content: []byte("policy-caps-proxy")}
server := httpserver.NewTestServer(map[string]*httpserver.Response{
"/file": resp,
})
defer server.Close()
dockerfile := []byte(` dockerfile := []byte(`
FROM scratch FROM busybox:latest
COPY foo /foo RUN wget -O- ` + server.URL + `/file
`) `)
dir := tmpdir( dir := tmpdir(
t, t,
fstest.CreateFile("Dockerfile", dockerfile, 0600), fstest.CreateFile("Dockerfile", dockerfile, 0600),
fstest.CreateFile("Dockerfile.rego", policyCapsProxyFile, 0600), fstest.CreateFile("Dockerfile.rego", policyCapsProxyFile(server.URL), 0600),
fstest.CreateFile("foo", []byte("foo"), 0600),
) )
cmd := buildxCmd(sb, withDir(dir), withArgs( cmd := buildxCmd(sb, withDir(dir), withArgs(
@@ -74,8 +84,11 @@ COPY foo /foo
dir, dir,
)) ))
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
require.NoError(t, err, string(out)) require.Error(t, err, string(out))
require.Contains(t, string(out), "policy enabled network proxy") require.Contains(t, string(out), "policy enabled network proxy")
require.Contains(t, string(out), "exec proxy http source denied by policy")
require.Contains(t, string(out), "policy decision for source "+server.URL+"/file")
require.Contains(t, string(out), "DENY")
} }
func testBuildPolicyCapsProxyUnsupported(t *testing.T, sb integration.Sandbox) { func testBuildPolicyCapsProxyUnsupported(t *testing.T, sb integration.Sandbox) {
@@ -90,7 +103,7 @@ COPY foo /foo
dir := tmpdir( dir := tmpdir(
t, t,
fstest.CreateFile("Dockerfile", dockerfile, 0600), fstest.CreateFile("Dockerfile", dockerfile, 0600),
fstest.CreateFile("Dockerfile.rego", policyCapsProxyFile, 0600), fstest.CreateFile("Dockerfile.rego", policyCapsProxyFile(""), 0600),
fstest.CreateFile("foo", []byte("foo"), 0600), fstest.CreateFile("foo", []byte("foo"), 0600),
) )