Skip to content

Commit 1f0f237

Browse files
committed
policy-test: use local registry
1 parent 3e71596 commit 1f0f237

6 files changed

Lines changed: 119 additions & 8 deletions

File tree

justfile

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@ default_cli := "contrast.cli"
22
default_deploy_target := "openssl"
33
default_platform := "${default_platform}"
44
default_set := "${set}"
5+
default_registry := "${container_registry}"
56
workspace_dir := "workspace"
67
default_collateral_proxy := "http://collateral-proxy.default.svc"
78

89
# Undeploy, rebuild, deploy.
910
default target=default_deploy_target platform=default_platform cli=default_cli: soft-clean coordinator initializer openssl port-forwarder service-mesh-proxy memdump debugshell (deploy target cli platform) set-manifest verify (wait-for-workload target)
1011

1112
# Build and push a container image.
12-
push target set=default_set:
13+
push target set=default_set registry=default_registry:
1314
#!/usr/bin/env bash
1415
set -euo pipefail
1516
mkdir -p {{ workspace_dir }}
16-
echo "Pushing container $container_registry/contrast/{{ target }}"
17-
nix run -L .#{{ set }}.scripts.containers.push-{{ target }} -- "$container_registry/contrast/{{ target }}" "{{ workspace_dir }}/just.containerlookup" "{{ workspace_dir }}/layers-cache.json"
17+
echo "Pushing container {{ registry }}/contrast/{{ target }}"
18+
nix run -L .#{{ set }}.scripts.containers.push-{{ target }} -- "{{ registry }}/contrast/{{ target }}" "{{ workspace_dir }}/just.containerlookup" "{{ workspace_dir }}/layers-cache.json"
1819
1920
coordinator: (push "coordinator")
2021

@@ -554,12 +555,34 @@ lint:
554555
unit:
555556
CGO_ENABLED=1 go test -tags=contrast_unstable_api -v -race ./...
556557
558+
# Run a local registry needed for the policy test suite.
559+
policy-registry:
560+
#!/usr/bin/env bash
561+
set -euo pipefail
562+
mkdir -p ./{{ workspace_dir }}
563+
echo "Setting up registry..."
564+
crane registry serve --address localhost:5000 > ./{{ workspace_dir }}/registry.log 2>&1 &
565+
PID=$!
566+
trap 'kill $PID 2>/dev/null; wait $PID 2>/dev/null || true' EXIT
567+
just push "initializer" "{{ default_set }}" "localhost:5000"
568+
crane copy busybox localhost:5000/busybox
569+
sha=$(crane digest localhost:5000/busybox)
570+
printf "busybox@sha256:dc2d74b28e4cf8984fa52af1f39bc7c3d9c73760b41a74d629f5d11b1ab28616=localhost:5000/busybox@%s\n" "$sha" >> ./{{ workspace_dir }}/just.containerlookup
571+
crane copy ghcr.io/edgelesssys/kubernetes/pause:3.6 localhost:5000/edgelesssys/kubernetes/pause:3.6
572+
echo "Registry is ready."
573+
tail -n 0 --pid $PID -f ./{{ workspace_dir }}/registry.log
574+
557575
# Run the policy test suite.
558-
policy: initializer
576+
policy:
559577
#!/usr/bin/env bash
560578
set -euo pipefail
579+
if ! curl -sf localhost:5000/v2/ > /dev/null; then
580+
echo "Registry not ready, please run 'just policy-registry' first."
581+
exit 1
582+
fi
583+
mkdir -p ./{{ workspace_dir }}
561584
echo "Running policy tests..."
562-
nix run -L .#base.policy-test -- --image-replacements ./{{ workspace_dir }}/just.containerlookup
585+
nix run -L .#base.policy-test -- --image-replacements ./{{ workspace_dir }}/just.containerlookup --insecure-registry localhost:5000
563586
564587
# Check links.
565588
check-links config="external":

packages/by-name/policy-test/package.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ buildGoModule (_finalAttrs: {
3535
};
3636

3737
proxyVendor = true;
38-
vendorHash = "sha256-Cob7OieQcJBSc4L4j9X2vi0AYaPU4JdB9Jt8QhIJSk4=";
38+
vendorHash = "sha256-g52+TFJlFZXsFhZweXEQQX63UBBxvoLxOdX0Px594PM=";
3939

4040
# The preConfigure and postConfigure hooks from the cli package work on the repository root, so setting
4141
# either sourceRoot or modRoot would break the build. Instead, cd into the subdir here.

policy-test/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ replace github.com/google/go-sev-guest => github.com/edgelesssys/go-sev-guest v0
1010

1111
require (
1212
github.com/edgelesssys/contrast v1.22.0
13+
github.com/elazarl/goproxy v1.8.2
1314
github.com/evanphx/json-patch/v5 v5.9.11
1415
github.com/open-policy-agent/opa v1.18.2
1516
github.com/spf13/cobra v1.10.2

policy-test/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ github.com/bytecodealliance/wasmtime-go/v44 v44.0.0 h1:WRZXnLPIer/TWs5aYPaMlmVcO
1010
github.com/bytecodealliance/wasmtime-go/v44 v44.0.0/go.mod h1:GP93piU+39CoFVCQ5xfHrPOUtL0APlMnkbblJ2d3YY0=
1111
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
1212
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
13+
github.com/coder/websocket v1.8.14 h1:9L0p0iKiNOibykf283eHkKUHHrpG7f65OE3BhhO7v9g=
14+
github.com/coder/websocket v1.8.14/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg=
1315
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
1416
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1517
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -31,6 +33,8 @@ github.com/edgelesssys/go-sev-guest v0.0.0-20260729130516-c98bf131aac5 h1:X8Zl2f
3133
github.com/edgelesssys/go-sev-guest v0.0.0-20260729130516-c98bf131aac5/go.mod h1:SK9vW+uyfuzYdVN0m8BShL3OQCtXZe/JPF7ZkpD3760=
3234
github.com/edgelesssys/go-tdx-guest v0.0.0-20260625102850-ea481d3db249 h1:Tx0olMH9+rqQgXfX8dIzIDN8UadjSJeTev6t4Qcx+J8=
3335
github.com/edgelesssys/go-tdx-guest v0.0.0-20260625102850-ea481d3db249/go.mod h1:uHy3VaNXNXhl0fiPxKqTxieeouqQmW6A0EfLcaeCYBk=
36+
github.com/elazarl/goproxy v1.8.2 h1:keGt9KHFAnrXFEctQuOF9NRxKFCXtd5cQg5PrBdeVW4=
37+
github.com/elazarl/goproxy v1.8.2/go.mod h1:b5xm6W48AUHNpRTCvlnd0YVh+JafCCtsLsJZvvNTz+E=
3438
github.com/emicklei/go-restful/v3 v3.13.0 h1:C4Bl2xDndpU6nJ4bc1jXd+uTmYPVUwkD6bFY/oTyCes=
3539
github.com/emicklei/go-restful/v3 v3.13.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
3640
github.com/evanphx/json-patch/v5 v5.9.11 h1:/8HVnzMq13/3x9TPvjG08wUGqBTmZBsCWzjTM0wiaDU=

policy-test/main.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func main() {
3131
RunE: execute,
3232
}
3333
cmd.Flags().String("image-replacements", "", "path to the image replacements file")
34+
cmd.Flags().String("insecure-registry", "", "insecure registry to use for generating policies")
3435

3536
cmd.SilenceUsage = true
3637

@@ -55,6 +56,16 @@ func execute(c *cobra.Command, _ []string) error {
5556
return err
5657
}
5758

59+
cleanup, err := setupProxy(c.Context(), flags.insecureRegistry)
60+
if err != nil {
61+
return fmt.Errorf("setup proxy: %w", err)
62+
}
63+
defer func() {
64+
if err := cleanup(); err != nil {
65+
log.Printf("Error cleaning up proxy: %v", err)
66+
}
67+
}()
68+
5869
policy, err := generatePolicy(c.Context(), workDir, flags)
5970
if err != nil {
6071
return fmt.Errorf("generate policy: %w", err)
@@ -100,9 +111,17 @@ func generatePolicy(ctx context.Context, workDir string, flags *flags) (string,
100111
"--genpolicy-cache-path=" + filepath.Join(workDir, "layers-cache.json"),
101112
"--image-replacements=" + flags.imageReplacementsFile,
102113
"--output=" + filepath.Join(workDir, "out.yml"),
103-
filepath.Join(workDir, "pod.yml"),
104114
}
105-
generateCmd.SetArgs(args)
115+
if flags.insecureRegistry != "" {
116+
// This assumes that the insecure registry contains a mirror of ghcr.io/edgelesssys/kubernetes/pause
117+
// which is used by genpolicy to fetch the pause image. See ./proxy.go.
118+
args = append(
119+
args,
120+
"--insecure-registry="+flags.insecureRegistry,
121+
"--insecure-registry=ghcr.io",
122+
)
123+
}
124+
generateCmd.SetArgs(append(args, filepath.Join(workDir, "pod.yml")))
106125
generateCmd.SetOut(io.Discard)
107126
errBuf := &bytes.Buffer{}
108127
generateCmd.SetErr(errBuf)
@@ -149,14 +168,20 @@ func extractPolicy(yaml []byte) (string, error) {
149168

150169
type flags struct {
151170
imageReplacementsFile string
171+
insecureRegistry string
152172
}
153173

154174
func parseFlags(cmd *cobra.Command) (*flags, error) {
155175
imageReplacementsFile, err := cmd.Flags().GetString("image-replacements")
156176
if err != nil {
157177
return nil, err
158178
}
179+
insecureRegistry, err := cmd.Flags().GetString("insecure-registry")
180+
if err != nil {
181+
return nil, err
182+
}
159183
return &flags{
160184
imageReplacementsFile: imageReplacementsFile,
185+
insecureRegistry: insecureRegistry,
161186
}, nil
162187
}

policy-test/proxy.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2026 Edgeless Systems GmbH
2+
// SPDX-License-Identifier: BUSL-1.1
3+
4+
package main
5+
6+
import (
7+
"context"
8+
"errors"
9+
"net"
10+
"net/http"
11+
"os"
12+
13+
"github.com/elazarl/goproxy"
14+
)
15+
16+
const ghcrHost = "ghcr.io"
17+
18+
func setupProxy(ctx context.Context, registry string) (func() error, error) {
19+
proxy := goproxy.NewProxyHttpServer()
20+
server := http.Server{Handler: proxy}
21+
errCh := make(chan error)
22+
23+
proxy.OnRequest().DoFunc(func(req *http.Request, _ *goproxy.ProxyCtx) (*http.Request, *http.Response) {
24+
// Genpolicy fetches the pause image from ghcr.io/edgelesssys/kubernetes/pause.
25+
// The image replacements file does not catch this, as it is not in any deployment YAML.
26+
// Reroute these request to the insecure registry to speed up offline testing.
27+
// This requires that the insecure registry contains a mirror of ghcr.io/edgelesssys/kubernetes/pause.
28+
if req.Host == ghcrHost && registry != "" {
29+
req.Host = registry
30+
req.URL.Host = registry
31+
}
32+
return req, nil
33+
})
34+
35+
cleanup := func() error {
36+
if err := server.Close(); err != nil {
37+
return err
38+
}
39+
err := <-errCh
40+
if !errors.Is(err, http.ErrServerClosed) {
41+
return err
42+
}
43+
return nil
44+
}
45+
46+
proxyListener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", "127.0.0.1:")
47+
if err != nil {
48+
return cleanup, err
49+
}
50+
51+
go func() {
52+
errCh <- server.Serve(proxyListener)
53+
}()
54+
55+
os.Setenv("http_proxy", proxyListener.Addr().String())
56+
57+
return cleanup, nil
58+
}

0 commit comments

Comments
 (0)