Skip to content

Commit cc704e3

Browse files
test: add certificates to container
1 parent 5b8c7ad commit cc704e3

File tree

2 files changed

+102
-10
lines changed

2 files changed

+102
-10
lines changed

api/internal/oci/pusher_test.go

Lines changed: 100 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,24 @@ package oci
55

66
import (
77
"bufio"
8+
"crypto/rand"
9+
"crypto/rsa"
10+
"crypto/x509"
11+
"crypto/x509/pkix"
12+
"encoding/pem"
813
"fmt"
14+
"math/big"
915
"net"
1016
"os/exec"
1117
"strconv"
1218
"strings"
1319
"syscall"
1420
"testing"
21+
"time"
1522

1623
"github.com/stretchr/testify/require"
24+
25+
loctest "sigs.k8s.io/kustomize/api/testutils/localizertest"
1726
)
1827

1928
func skipIfNoDocker(t *testing.T) {
@@ -23,30 +32,56 @@ func skipIfNoDocker(t *testing.T) {
2332
}
2433
}
2534

35+
// run calls Cmd.Run and wraps the error to include the output to make debugging
36+
// easier. Not safe for real code, but fine for tests.
37+
func run(cmd *exec.Cmd) error {
38+
if out, err := cmd.CombinedOutput(); err != nil {
39+
return fmt.Errorf("%w\n--- COMMAND OUTPUT ---\n%s", err, string(out))
40+
}
41+
return nil
42+
}
43+
2644
// Set up the registry.
27-
func registry(t *testing.T) (*exec.Cmd, int, error) {
45+
func registry(t *testing.T, certificate_path string, key_path string) (*exec.Cmd, int, error) {
2846
skipIfNoDocker(t)
47+
t.Helper()
48+
49+
const container_cert_path = "/certs/cert.pem"
50+
const container_key_path = "/certs/key.pem"
2951

30-
container_name := fmt.Sprintf("%s_%d", t.Name(), 15)
52+
container_name := t.Name()
3153
internal_port := 5000
3254

33-
registry := exec.Command("docker", "run",
55+
create := exec.Command("docker", "create",
3456
"--rm",
35-
"-p", fmt.Sprintf("0:%d", internal_port),
3657
"--name", container_name,
58+
"--publish", fmt.Sprintf("0:%d", internal_port),
59+
"--env", "REGISTRY_HTTP_TLS_CERTIFICATE="+container_cert_path,
60+
"--env", "REGISTRY_HTTP_TLS_KEY="+container_key_path,
3761
"docker.io/library/registry:3.0.0",
3862
)
63+
require.NoError(t, run(create))
64+
65+
cert_upload := exec.Command("docker", "cp", certificate_path, container_name+":"+container_cert_path)
66+
require.NoError(t, run(cert_upload))
67+
key_upload := exec.Command("docker", "cp", key_path, container_name+":"+container_key_path)
68+
require.NoError(t, run(key_upload))
3969

40-
stdout, err := registry.StderrPipe()
70+
start := exec.Command("docker", "start",
71+
"--attach", "--interactive",
72+
container_name,
73+
)
74+
75+
stdout, err := start.StderrPipe()
4176
if err != nil {
4277
t.Fatal(err)
4378
}
4479

45-
if err := registry.Start(); err != nil {
80+
if err := start.Start(); err != nil {
4681
t.Fatal(err)
4782
}
4883

49-
t.Cleanup(func() { registry.Process.Signal(syscall.SIGTERM) })
84+
t.Cleanup(func() { start.Process.Signal(syscall.SIGTERM) })
5085

5186
scanner := bufio.NewScanner(stdout)
5287
for scanner.Scan() {
@@ -75,16 +110,71 @@ func registry(t *testing.T) (*exec.Cmd, int, error) {
75110
t.Fatal(err)
76111
}
77112

78-
return registry, port, nil
113+
return start, port, nil
114+
}
115+
116+
func generateSelfSignedCert(t *testing.T) (certificate string, key string) {
117+
t.Helper()
118+
119+
// Generate private key
120+
privateKey, err := rsa.GenerateKey(rand.Reader, 1024)
121+
if err != nil {
122+
t.Fatalf("failed to generate private key: %v", err)
123+
}
124+
125+
// Certificate template
126+
tmpl := x509.Certificate{
127+
SerialNumber: big.NewInt(1),
128+
Subject: pkix.Name{
129+
CommonName: "localhost",
130+
},
131+
NotBefore: time.Now(),
132+
NotAfter: time.Now().Add(24 * time.Hour),
133+
134+
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
135+
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
136+
BasicConstraintsValid: true,
137+
}
138+
139+
// Self-sign the certificate
140+
derBytes, err := x509.CreateCertificate(rand.Reader, &tmpl, &tmpl, &privateKey.PublicKey, privateKey)
141+
if err != nil {
142+
t.Fatalf("failed to create certificate: %v", err)
143+
}
144+
145+
return string(pem.EncodeToMemory(&pem.Block{
146+
Type: "CERTIFICATE",
147+
Bytes: derBytes,
148+
})),
149+
string(pem.EncodeToMemory(&pem.Block{
150+
Type: "RSA PRIVATE KEY",
151+
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
152+
}))
79153
}
80154

81155
func TestFnContainerTransformerWithConfig(t *testing.T) {
82-
registry, port, err := registry(t)
156+
certificate, key := generateSelfSignedCert(t)
157+
cert_path := "certs/cert.pem"
158+
key_path := "certs/key.pem"
159+
160+
kustomization := map[string]string{
161+
"src/README.md": `# NO VALID FILE
162+
`,
163+
cert_path: certificate,
164+
key_path: key,
165+
}
166+
// clock := NewFakePassiveClock(time.Date(int(2025), time.July, int(28), int(20), int(56), int(0), int(0), time.UTC))
167+
168+
_, _, target := loctest.PrepareFs(t, []string{"src", "certs"}, kustomization)
169+
loctest.SetWorkingDir(t, target.Join("src"))
170+
171+
registry, port, err := registry(t, target.Join(cert_path), target.Join(key_path))
83172
require.NoError(t, err)
173+
84174
// t.Cleanup(func() {registry.})
85175
require.NotNil(t, registry)
86176
t.Setenv("asdfsd", "asdfadsf")
87177

88-
require.Equal(t, port, 5)
178+
require.Equal(t, port, 7)
89179

90180
}

go.work.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,6 +1407,8 @@ github.com/stretchr/testify v0.0.0-20170130113145-4d4bfba8f1d1/go.mod h1:a8OnRci
14071407
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
14081408
github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
14091409
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
1410+
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
1411+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
14101412
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
14111413
github.com/subosito/gotenv v1.4.0/go.mod h1:mZd6rFysKEcUhUHXJk0C/08wAgyDBFuwEYL7vWWGaGo=
14121414
github.com/t-yuki/gocover-cobertura v0.0.0-20180217150009-aaee18c8195c/go.mod h1:SbErYREK7xXdsRiigaQiQkI9McGRzYMvlKYaP3Nimdk=

0 commit comments

Comments
 (0)