File
Blob: acme/pebble_test.go
| 1 | package acme |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "crypto/tls" |
| 7 | "crypto/x509" |
| 8 | "fmt" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "github.com/stretchr/testify/require" |
| 15 | "github.com/testcontainers/testcontainers-go" |
| 16 | "github.com/testcontainers/testcontainers-go/wait" |
| 17 | ) |
| 18 | |
| 19 | const ( |
| 20 | pebbleImage = "ghcr.io/letsencrypt/pebble:latest" |
| 21 | acmePort = "14000/tcp" |
| 22 | mgmtPort = "15000/tcp" |
| 23 | ) |
| 24 | |
| 25 | // pebbleEnv holds the configuration for connecting to a Pebble ACME server |
| 26 | type pebbleEnv struct { |
| 27 | ACMEURL string |
| 28 | TrustedRoots *x509.CertPool |
| 29 | container testcontainers.Container |
| 30 | } |
| 31 | |
| 32 | // Stop terminates the Pebble container, if this fixture started one. |
| 33 | func (p *pebbleEnv) Stop(ctx context.Context) error { |
| 34 | if p.container != nil { |
| 35 | return testcontainers.TerminateContainer(p.container) |
| 36 | } |
| 37 | return nil |
| 38 | } |
| 39 | |
| 40 | // StartPebble connects to SPECTER_TEST_ACME_URL when set, or starts a container. |
| 41 | // Both paths trust dev/pebble/certs/cert.pem. The container also requires the |
| 42 | // following files to exist (run `make certs` first): |
| 43 | // - dev/pebble/config.json |
| 44 | // - dev/pebble/certs/cert.pem (Pebble CA) |
| 45 | // - certs/pebble.pem (Pebble server cert) |
| 46 | // - certs/pebble.key (Pebble server key) |
| 47 | func StartPebble(t *testing.T) *pebbleEnv { |
| 48 | t.Helper() |
| 49 | as := require.New(t) |
| 50 | ctx := t.Context() |
| 51 | |
| 52 | // Go tests and the native VM runner execute from the package directory. |
| 53 | devPebblePath := filepath.Join("..", "dev", "pebble") |
| 54 | certsPath := filepath.Join("..", "certs") |
| 55 | |
| 56 | // Read Pebble CA for trusted roots |
| 57 | pebbleCaPath := filepath.Join(devPebblePath, "certs", "cert.pem") |
| 58 | pebbleCa, err := os.ReadFile(pebbleCaPath) |
| 59 | as.NoError(err, "failed to read Pebble CA; run 'make certs' first") |
| 60 | |
| 61 | trustedRoots := x509.NewCertPool() |
| 62 | ok := trustedRoots.AppendCertsFromPEM(pebbleCa) |
| 63 | as.True(ok, "failed to parse Pebble CA certificate") |
| 64 | |
| 65 | if acmeURL := os.Getenv("SPECTER_TEST_ACME_URL"); acmeURL != "" { |
| 66 | t.Logf("Using external Pebble ACME server at %s", acmeURL) |
| 67 | return &pebbleEnv{ACMEURL: acmeURL, TrustedRoots: trustedRoots} |
| 68 | } |
| 69 | |
| 70 | // Read files to copy into the container |
| 71 | configPath := filepath.Join(devPebblePath, "config.json") |
| 72 | configData, err := os.ReadFile(configPath) |
| 73 | as.NoError(err, "failed to read Pebble config.json") |
| 74 | |
| 75 | pebblePemPath := filepath.Join(certsPath, "pebble.pem") |
| 76 | pebblePem, err := os.ReadFile(pebblePemPath) |
| 77 | as.NoError(err, "failed to read certs/pebble.pem; run 'make certs' first") |
| 78 | |
| 79 | pebbleKeyPath := filepath.Join(certsPath, "pebble.key") |
| 80 | pebbleKey, err := os.ReadFile(pebbleKeyPath) |
| 81 | as.NoError(err, "failed to read certs/pebble.key; run 'make certs' first") |
| 82 | |
| 83 | // Create a TLS config that trusts the Pebble CA for the wait strategy |
| 84 | tlsConfig := &tls.Config{ |
| 85 | RootCAs: trustedRoots, |
| 86 | } |
| 87 | |
| 88 | // Start Pebble container |
| 89 | ctr, err := testcontainers.Run(ctx, pebbleImage, |
| 90 | testcontainers.WithFiles( |
| 91 | testcontainers.ContainerFile{ |
| 92 | Reader: bytes.NewReader(configData), |
| 93 | ContainerFilePath: "/pebble/config.json", |
| 94 | FileMode: 0o644, |
| 95 | }, |
| 96 | testcontainers.ContainerFile{ |
| 97 | Reader: bytes.NewReader(pebblePem), |
| 98 | ContainerFilePath: "/certs/pebble.pem", |
| 99 | FileMode: 0o644, |
| 100 | }, |
| 101 | testcontainers.ContainerFile{ |
| 102 | Reader: bytes.NewReader(pebbleKey), |
| 103 | ContainerFilePath: "/certs/pebble.key", |
| 104 | FileMode: 0o600, |
| 105 | }, |
| 106 | ), |
| 107 | testcontainers.WithExposedPorts(acmePort, mgmtPort), |
| 108 | testcontainers.WithEnv(map[string]string{ |
| 109 | "PEBBLE_VA_NOSLEEP": "1", |
| 110 | // reduce test flakes due to certmagic waiting at least 1 minute before retrying |
| 111 | "PEBBLE_VA_ALWAYS_VALID": "1", |
| 112 | "PEBBLE_WFE_NONCEREJECT": "0", |
| 113 | }), |
| 114 | testcontainers.WithCmd("-config", "/pebble/config.json"), |
| 115 | testcontainers.WithWaitStrategy( |
| 116 | wait.ForHTTP("/dir"). |
| 117 | WithTLS(true, tlsConfig). |
| 118 | WithPort(acmePort). |
| 119 | WithStartupTimeout(30*time.Second), |
| 120 | ), |
| 121 | ) |
| 122 | as.NoError(err, "failed to start Pebble container") |
| 123 | |
| 124 | // Get the mapped port |
| 125 | host, err := ctr.Host(ctx) |
| 126 | as.NoError(err) |
| 127 | |
| 128 | mappedPort, err := ctr.MappedPort(ctx, acmePort) |
| 129 | as.NoError(err) |
| 130 | |
| 131 | acmeURL := fmt.Sprintf("https://%s:%s/dir", host, mappedPort.Port()) |
| 132 | t.Logf("Pebble ACME server started at %s", acmeURL) |
| 133 | |
| 134 | return &pebbleEnv{ |
| 135 | ACMEURL: acmeURL, |
| 136 | TrustedRoots: trustedRoots, |
| 137 | container: ctr, |
| 138 | } |
| 139 | } |