Skip to content

Commit b258896

Browse files
committed
feat(objectstore-plugin): trust the system roots and service DNS bundle for the issuer
The sidecar verified the actor JWT issuer with a CA file that ate-setup picked by an exact match on the default issuer, so an equivalent spelling such as https://idp.<ns>.svc:443 fell back to the system roots and stopped the sidecar. Verify the issuer against the system roots plus --actor-jwt-issuer-ca-file, which defaults to the service DNS trust bundle, and drop the ATE_API_ACTOR_JWT_ISSUER_CA_FILE ConfigMap key. Signed-off-by: Anish Ramasekar <[email protected]>
1 parent 6d4b9eb commit b258896

12 files changed

Lines changed: 213 additions & 97 deletions

File tree

‎cmd/ate-setup/internal/steps/apienv.go‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ func (e *Env) CreateAPIServerEnvVars(ctx context.Context) error {
138138
configVars["ATE_API_POSTGRES_POOL_MAX_CONNS"] = poolMaxConns
139139
}
140140
configVars["ATE_API_ACTOR_JWT_ISSUER"] = e.Cfg.ActorJWTIssuer
141-
configVars["ATE_API_ACTOR_JWT_ISSUER_CA_FILE"] = actorJWTIssuerCAFile(e.Cfg.ActorJWTIssuer, e.Namespace())
142141
// Written empty on kind, so the sidecar's argument is never the literal
143142
// $(ATE_API_SNAPSHOT_TRANSFER_SERVICE_ACCOUNT).
144143
configVars["ATE_API_SNAPSHOT_TRANSFER_SERVICE_ACCOUNT"] = e.Cfg.TransferServiceAccount()
@@ -155,19 +154,6 @@ func (e *Env) CreateAPIServerEnvVars(ctx context.Context) error {
155154
return e.applyPostgresServerCA(ctx)
156155
}
157156

158-
// actorJWTIssuerCAFile returns the CA file the objectstore-plugin sidecar
159-
// verifies issuer's serving certificate with. The default issuer is this
160-
// install's ate-idp-server, which serves a service DNS certificate. A custom
161-
// issuer gets "", which selects the system roots. The caller writes the key
162-
// either way, so the sidecar's argument is never the literal
163-
// $(ATE_API_ACTOR_JWT_ISSUER_CA_FILE).
164-
func actorJWTIssuerCAFile(issuer, namespace string) string {
165-
if issuer == installdefaults.ActorJWTIssuer(namespace) {
166-
return "/run/servicedns.podcert.ate.dev/trust-bundle.pem"
167-
}
168-
return ""
169-
}
170-
171157
// UpdateAPIServerEnvVars is CreateAPIServerEnvVars for an install that is not
172158
// being redeployed: it also stamps the new digest on the running Deployment,
173159
// which rolls ate-api-server onto the new values.

‎cmd/ate-setup/internal/steps/apienv_test.go‎

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -299,46 +299,26 @@ func TestCreateAPIServerEnvVarsPoolSize(t *testing.T) {
299299
}
300300

301301
func TestCreateAPIServerEnvVarsActorJWTIssuer(t *testing.T) {
302-
const bundle = "/run/servicedns.podcert.ate.dev/trust-bundle.pem"
303-
for _, tc := range []struct {
304-
name string
305-
namespace string
306-
issuer string
307-
wantCAFile string
308-
}{
309-
{name: "default issuer", namespace: NamespaceAteSystem, issuer: "https://idp.ate-system.svc", wantCAFile: bundle},
310-
{name: "default issuer of a relocated install", namespace: "ate-dev", issuer: "https://idp.ate-dev.svc", wantCAFile: bundle},
311-
{name: "custom issuer", namespace: NamespaceAteSystem, issuer: "https://idp.example.com/prod"},
312-
} {
313-
t.Run(tc.name, func(t *testing.T) {
314-
cfg := config.Config{
315-
TransferGSAName: testTransferGSAName, ProjectID: testProjectID,
316-
PostgresReadWriteConnectionString: "postgres://runtime@postgres/atepg",
317-
ActorJWTIssuer: tc.issuer,
318-
}
319-
cfg.Namespace = tc.namespace
320-
cfg.SnapshotLocation = "gs://ate-snapshots/"
321-
e := &Env{Cfg: &cfg, Kube: fakeKube(t,
322-
&corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: tc.namespace}},
323-
&corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: ConfigMapAPIEnvVars, Namespace: tc.namespace}},
324-
&corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: SecretAPIEnvVars, Namespace: tc.namespace}},
325-
)}
326-
if err := e.CreateAPIServerEnvVars(t.Context()); err != nil {
327-
t.Fatal(err)
328-
}
329-
cm, err := e.Kube.GetConfigMap(t.Context(), tc.namespace, ConfigMapAPIEnvVars)
330-
if err != nil {
331-
t.Fatal(err)
332-
}
333-
if got := cm.Data["ATE_API_ACTOR_JWT_ISSUER"]; got != tc.issuer {
334-
t.Errorf("ATE_API_ACTOR_JWT_ISSUER = %q, want %q", got, tc.issuer)
335-
}
336-
// The key must be present even when empty, or the sidecar gets
337-
// the literal $(ATE_API_ACTOR_JWT_ISSUER_CA_FILE).
338-
if got, ok := cm.Data["ATE_API_ACTOR_JWT_ISSUER_CA_FILE"]; !ok || got != tc.wantCAFile {
339-
t.Errorf("ATE_API_ACTOR_JWT_ISSUER_CA_FILE = %q (present: %v), want %q", got, ok, tc.wantCAFile)
340-
}
341-
})
302+
cfg := config.Config{
303+
TransferGSAName: testTransferGSAName, ProjectID: testProjectID,
304+
PostgresReadWriteConnectionString: "postgres://runtime@postgres/atepg",
305+
ActorJWTIssuer: "https://idp.example.com/prod",
306+
}
307+
cfg.SnapshotLocation = "gs://ate-snapshots/"
308+
e := &Env{Cfg: &cfg, Kube: fakeKube(t,
309+
&corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: NamespaceAteSystem}},
310+
&corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: ConfigMapAPIEnvVars, Namespace: NamespaceAteSystem}},
311+
&corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: SecretAPIEnvVars, Namespace: NamespaceAteSystem}},
312+
)}
313+
if err := e.CreateAPIServerEnvVars(t.Context()); err != nil {
314+
t.Fatal(err)
315+
}
316+
cm, err := e.Kube.GetConfigMap(t.Context(), NamespaceAteSystem, ConfigMapAPIEnvVars)
317+
if err != nil {
318+
t.Fatal(err)
319+
}
320+
if got := cm.Data["ATE_API_ACTOR_JWT_ISSUER"]; got != cfg.ActorJWTIssuer {
321+
t.Errorf("ATE_API_ACTOR_JWT_ISSUER = %q, want %q", got, cfg.ActorJWTIssuer)
342322
}
343323
}
344324

‎cmd/objectstore-plugin/README.md‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ token. The exchange fetches the issuer's public keys through OpenID Connect
3636
discovery at `<issuer>/.well-known/openid-configuration`, at sidecar start
3737
and then as `oidcjwt.Verifier` refreshes them. An issuer it cannot reach
3838
fails the exchange with `UNAVAILABLE`.
39-
`--actor-jwt-issuer-ca-file` names the CA bundle that verifies the issuer, or
40-
the system roots when empty. The sidecar re-reads it when it changes, so a
41-
rotated bundle takes effect without a restart. If a re-read fails, it keeps
42-
the last bundle and logs a warning. If the issuer's certificate
43-
does not verify at start, the sidecar exits. Any other error at start is
44-
logged, and the sidecar serves and fetches again on the next exchange.
39+
The issuer's certificate must verify against the system roots or the CA
40+
bundle `--actor-jwt-issuer-ca-file` names, by default the service DNS trust
41+
bundle. The sidecar re-reads the bundle when it changes, so a rotated bundle
42+
takes effect without a restart. If a re-read fails, it keeps the last bundle
43+
and logs a warning. If the issuer's certificate does not verify at start, the
44+
sidecar exits. Any other error at start is logged, and the sidecar serves and
45+
fetches again on the next exchange.
4546

4647
Four packages behind the exchange know no storage provider and are what a
4748
provider's own plugin binary reuses. The rest is this binary's.

‎cmd/objectstore-plugin/exchange.go‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ func newExchangeService(ctx context.Context, cfg exchangeConfig) (exchangepb.Cre
6767
if cfg.transferAccount == "" {
6868
return nil, nil, errors.New("--transfer-service-account is required to serve the exchange on GCS")
6969
}
70+
if cfg.issuerCAFile == "" {
71+
return nil, nil, errors.New("--actor-jwt-issuer-ca-file is required to serve the exchange on GCS")
72+
}
7073
verifier, err := newActorVerifier(ctx, cfg.issuer, cfg.issuerCAFile)
7174
if err != nil {
7275
return nil, nil, err
@@ -84,14 +87,15 @@ func newExchangeService(ctx context.Context, cfg exchangeConfig) (exchangepb.Cre
8487
}
8588

8689
// newActorVerifier returns the exchange's actor JWT verifier, with the
87-
// issuer's keys already fetched by checkIssuerCertificate.
90+
// issuer's keys already fetched by checkIssuerCertificate. The verifier
91+
// trusts the system roots and caFile, re-read when it changes.
8892
func newActorVerifier(ctx context.Context, issuer, caFile string) (*oidcjwt.Verifier, error) {
89-
client, err := oidcjwt.NewHTTPClient(issuer, caFile, "")
93+
client, err := oidcjwt.NewHTTPClientWithRoots(credbundle.SystemPoolLoader(caFile))
9094
if err != nil {
9195
return nil, fmt.Errorf("while setting up the actor JWT issuer client: %w", err)
9296
}
9397
verifier := actorauthz.NewVerifier(issuer, client)
94-
if err := checkIssuerCertificate(ctx, issuer, verifier); err != nil {
98+
if err := checkIssuerCertificate(ctx, issuer, caFile, verifier); err != nil {
9599
return nil, err
96100
}
97101
return verifier, nil
@@ -104,12 +108,12 @@ const issuerCheckTimeout = 10 * time.Second
104108
// certificate the client does not trust fails the start instead of every
105109
// exchange. Other errors may be transient, such as an issuer that is not up
106110
// yet, so they are logged and the exchange serves.
107-
func checkIssuerCertificate(ctx context.Context, issuer string, verifier *oidcjwt.Verifier) error {
111+
func checkIssuerCertificate(ctx context.Context, issuer, caFile string, verifier *oidcjwt.Verifier) error {
108112
ctx, cancel := context.WithTimeout(ctx, issuerCheckTimeout)
109113
defer cancel()
110114
err := verifier.Refresh(ctx, time.Now())
111115
if certErr := (*tls.CertificateVerificationError)(nil); errors.As(err, &certErr) {
112-
return fmt.Errorf("actor JWT issuer serves a certificate that does not verify against --actor-jwt-issuer-ca-file, or the system roots when it is empty: %w", err)
116+
return fmt.Errorf("actor JWT issuer serves a certificate that does not verify against the system roots or %s: %w", caFile, err)
113117
}
114118
if err != nil {
115119
slog.WarnContext(ctx, "Could not fetch actor JWT signing keys at start; exchanges retry the fetch", slog.String("issuer", issuer), slog.Any("err", err))

‎cmd/objectstore-plugin/exchange_test.go‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"go.ate.dev/substrate/internal/localjwtauthority"
3232
"go.ate.dev/substrate/internal/objectstoreplugin/actorauthz"
3333
exchangepb "go.ate.dev/substrate/internal/proto/objectstoreexchangepb"
34+
"go.ate.dev/substrate/internal/testcert"
3435
)
3536

3637
func TestNewExchangeService(t *testing.T) {
@@ -56,6 +57,10 @@ func TestNewExchangeService(t *testing.T) {
5657
tlsIssuer := httptest.NewTLSServer(http.NotFoundHandler())
5758
t.Cleanup(tlsIssuer.Close)
5859
tlsIssuerCA := writeServerCA(t, tlsIssuer)
60+
otherCA := filepath.Join(t.TempDir(), "other-ca.pem")
61+
if err := os.WriteFile(otherCA, testcert.NewServerTLS(t, net.ParseIP("127.0.0.1")).RootCA, 0o600); err != nil {
62+
t.Fatal(err)
63+
}
5964
// The httptest certificate is for 127.0.0.1, not localhost.
6065
_, tlsIssuerPort, err := net.SplitHostPort(tlsIssuer.Listener.Addr().String())
6166
if err != nil {
@@ -70,8 +75,9 @@ func TestNewExchangeService(t *testing.T) {
7075
{"GCS needs the transfer account", exchangeConfig{issuer: issuer}, "--transfer-service-account"},
7176
{"GCS needs a readable issuer CA file", exchangeConfig{issuer: issuer, issuerCAFile: filepath.Join(t.TempDir(), "absent.pem"), transferAccount: xfer}, "absent.pem"},
7277
{"GCS needs certificates in the issuer CA file", exchangeConfig{issuer: issuer, issuerCAFile: emptyCA, transferAccount: xfer}, "contains no certificates"},
73-
{"GCS needs an issuer certificate the CA file trusts", exchangeConfig{issuer: tlsIssuer.URL, transferAccount: xfer}, "--actor-jwt-issuer-ca-file"},
74-
{"GCS needs an issuer certificate for the issuer's host", exchangeConfig{issuer: "https://localhost:" + tlsIssuerPort, issuerCAFile: tlsIssuerCA, transferAccount: xfer}, "--actor-jwt-issuer-ca-file"},
78+
{"GCS needs the issuer CA file", exchangeConfig{issuer: issuer, transferAccount: xfer}, "--actor-jwt-issuer-ca-file"},
79+
{"GCS needs an issuer certificate the CA file or system roots trust", exchangeConfig{issuer: tlsIssuer.URL, issuerCAFile: otherCA, transferAccount: xfer}, "the system roots or " + otherCA},
80+
{"GCS needs an issuer certificate for the issuer's host", exchangeConfig{issuer: "https://localhost:" + tlsIssuerPort, issuerCAFile: tlsIssuerCA, transferAccount: xfer}, "the system roots or " + tlsIssuerCA},
7581
} {
7682
t.Run(tc.name, func(t *testing.T) {
7783
t.Setenv("ATE_STORAGE_BACKEND", "gcs")
@@ -99,7 +105,7 @@ func TestCheckIssuerCertificate(t *testing.T) {
99105
{"issuer without discovery", trusted.URL, trusted.Client()},
100106
} {
101107
t.Run(tc.name, func(t *testing.T) {
102-
if err := checkIssuerCertificate(t.Context(), tc.issuer, actorauthz.NewVerifier(tc.issuer, tc.client)); err != nil {
108+
if err := checkIssuerCertificate(t.Context(), tc.issuer, "ca.pem", actorauthz.NewVerifier(tc.issuer, tc.client)); err != nil {
103109
t.Fatalf("checkIssuerCertificate() = %v, want nil", err)
104110
}
105111
})

‎cmd/objectstore-plugin/main.go‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ import (
5454

5555
const usage = `usage: objectstore-plugin <node|control|healthcheck> [flags]`
5656

57+
// serviceDNSTrustBundle is the CA bundle of the install's service DNS
58+
// certificates, which the exchange, ate-api-server and ate-idp-server serve.
59+
const serviceDNSTrustBundle = "/run/servicedns.podcert.ate.dev/trust-bundle.pem"
60+
5761
func main() {
5862
if len(os.Args) < 2 {
5963
fmt.Fprintln(os.Stderr, usage)
@@ -70,16 +74,16 @@ func main() {
7074
apiHost := installdefaults.APIServiceHost(installdefaults.NamespaceFromPodEnv())
7175
var ncfg exchangeclient.Config
7276
flags.StringVar(&ncfg.Address, "exchange-address", net.JoinHostPort(apiHost, strconv.Itoa(exchange.DefaultPort)), "node mode on GCS: address of the credential exchange the control sidecar serves, where each transfer's actor JWT is traded for a token scoped to the actor")
73-
flags.StringVar(&ncfg.CAFile, "exchange-ca-file", "/run/servicedns.podcert.ate.dev/trust-bundle.pem", "node mode on GCS: CA bundle used to verify the exchange")
77+
flags.StringVar(&ncfg.CAFile, "exchange-ca-file", serviceDNSTrustBundle, "node mode on GCS: CA bundle used to verify the exchange")
7478
flags.StringVar(&ncfg.ServerName, "exchange-server-name", apiHost, "node mode on GCS: DNS name expected on the exchange's certificate, the API Service in the pod's namespace")
7579
var xcfg exchangeConfig
7680
flags.StringVar(&xcfg.listen, "exchange-listen", "", "control mode: TCP address to serve the credential exchange on over TLS, for example :8445. Empty leaves it unserved.")
7781
flags.StringVar(&xcfg.credBundle, "exchange-cred-bundle", "/run/servicedns.podcert.ate.dev/credential-bundle.pem", "control mode: PEM credential bundle the exchange serves with, the pod's service DNS certificate")
7882
flags.StringVar(&xcfg.issuer, "actor-jwt-issuer", "", "control mode: issuer URL of the actor JWTs the exchange accepts, the value ate-api-server runs with")
79-
flags.StringVar(&xcfg.issuerCAFile, "actor-jwt-issuer-ca-file", "", "control mode: CA bundle used to verify --actor-jwt-issuer when fetching its signing keys. Empty uses the system roots")
83+
flags.StringVar(&xcfg.issuerCAFile, "actor-jwt-issuer-ca-file", serviceDNSTrustBundle, "control mode: CA bundle trusted in addition to the system roots when verifying --actor-jwt-issuer")
8084
flags.StringVar(&xcfg.transferAccount, "transfer-service-account", "", "control mode on GCS: email of the Google service account snapshot transfers run as. The exchange impersonates it and downscopes its token per actor")
8185
flags.StringVar(&xcfg.ateapi.Address, "ateapi-address", "127.0.0.1:443", "control mode: ate-api-server gRPC address, the loopback in the shared pod")
82-
flags.StringVar(&xcfg.ateapi.CAFile, "ateapi-ca-file", "/run/servicedns.podcert.ate.dev/trust-bundle.pem", "control mode: CA bundle used to verify ate-api-server")
86+
flags.StringVar(&xcfg.ateapi.CAFile, "ateapi-ca-file", serviceDNSTrustBundle, "control mode: CA bundle used to verify ate-api-server")
8387
flags.StringVar(&xcfg.ateapi.ServerName, "ateapi-server-name", installdefaults.APIServiceHost(installdefaults.NamespaceFromPodEnv()), "control mode: DNS name expected on the ate-api-server certificate, the API Service in the pod's namespace")
8488
flags.StringVar(&xcfg.ateapi.ClientCredBundle, "ateapi-client-cred-bundle", "/run/podidentity.podcert.ate.dev/credential-bundle.pem", "control mode: PEM credential bundle presented to ate-api-server, the pod identity certificate")
8589
previewFlags := flags.StringSlice("preview", nil, "Preview gates to enable.")

‎docs/guides/operator-install.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,8 @@ The exchange verifies the actor JWT with keys it fetches from `actorJWT.issuer`
356356
through OpenID Connect discovery. The default issuer is the install's
357357
ate-idp-server. A custom issuer must route to that ate-idp-server, which
358358
serves the keys the install signs actor JWTs with. It must also be reachable
359-
from the ate-api-server pod and serve a certificate the system roots trust.
359+
from the ate-api-server pod and serve a certificate that the system roots or
360+
the install's service DNS CA trust.
360361
The exchange fetches the keys once at start, and exits if the issuer's
361362
certificate does not verify. Then ate-api-server does not start either.
362363

‎internal/credbundle/credbundle.go‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,19 @@ func ClientLoader(path string) func(*tls.CertificateRequestInfo) (*tls.Certifica
6464
// when it changes, mirroring Loader, so a rotation is picked up on the next
6565
// handshake without paying the read and parse cost when nothing changed.
6666
func PoolLoader(path string) func() (*x509.CertPool, error) {
67-
c := &poolCache{path: path}
67+
c := &poolCache{path: path, newPool: newEmptyPool}
6868
return c.get
6969
}
7070

71+
// SystemPoolLoader is PoolLoader for a pool that holds the system roots as
72+
// well as the trust bundle at path.
73+
func SystemPoolLoader(path string) func() (*x509.CertPool, error) {
74+
c := &poolCache{path: path, newPool: x509.SystemCertPool}
75+
return c.get
76+
}
77+
78+
func newEmptyPool() (*x509.CertPool, error) { return x509.NewCertPool(), nil }
79+
7180
// certCache holds the parse of a credential bundle file together with the stat
7281
// of the file it was parsed from, so unchanged files are not re-parsed on
7382
// every TLS handshake.
@@ -124,6 +133,8 @@ func (c *certCache) get() (*tls.Certificate, error) {
124133
// concurrency reasoning.
125134
type poolCache struct {
126135
path string
136+
// newPool returns the pool the bundle's certificates are added to.
137+
newPool func() (*x509.CertPool, error)
127138

128139
mu sync.Mutex
129140
fi os.FileInfo
@@ -145,8 +156,11 @@ func (c *poolCache) get() (*x509.CertPool, error) {
145156
return c.pool, nil
146157
}
147158

148-
pool, err := ParsePool(c.path)
159+
pool, err := c.newPool()
149160
if err != nil {
161+
return nil, fmt.Errorf("while loading the base pool for trust bundle %q: %w", c.path, err)
162+
}
163+
if err := appendPool(pool, c.path); err != nil {
150164
return nil, err
151165
}
152166
c.fi, c.pool = fi, pool
@@ -210,13 +224,22 @@ func Parse(bundlePath string) (*tls.Certificate, error) {
210224
// error if the file holds no certificates: an empty trust pool would silently
211225
// reject every peer, which is never what a workload should receive.
212226
func ParsePool(path string) (*x509.CertPool, error) {
227+
pool := x509.NewCertPool()
228+
if err := appendPool(pool, path); err != nil {
229+
return nil, err
230+
}
231+
return pool, nil
232+
}
233+
234+
// appendPool adds the certificates in the PEM trust-bundle file at path to
235+
// pool. It returns an error if the file holds none.
236+
func appendPool(pool *x509.CertPool, path string) error {
213237
pemBytes, err := os.ReadFile(path)
214238
if err != nil {
215-
return nil, fmt.Errorf("while reading trust bundle: %w", err)
239+
return fmt.Errorf("while reading trust bundle: %w", err)
216240
}
217-
pool := x509.NewCertPool()
218241
if !pool.AppendCertsFromPEM(pemBytes) {
219-
return nil, fmt.Errorf("trust bundle %q contains no certificates", path)
242+
return fmt.Errorf("trust bundle %q contains no certificates", path)
220243
}
221-
return pool, nil
244+
return nil
222245
}

‎internal/credbundle/credbundle_test.go‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,46 @@ func TestPoolLoaderErrorWhenBundleMissing(t *testing.T) {
286286
}
287287
}
288288

289+
// TestSystemPoolLoader checks that the pool holds the system roots and the
290+
// bundle, and is cached and reloaded like PoolLoader's. Holding the system
291+
// roots is what lets an issuer with a publicly trusted certificate verify.
292+
func TestSystemPoolLoader(t *testing.T) {
293+
path := writeProjectedBundle(t, makeTrustBundle(t, 1))
294+
getPool := SystemPoolLoader(path)
295+
first, err := getPool()
296+
if err != nil {
297+
t.Fatalf("SystemPoolLoader() first call error = %v", err)
298+
}
299+
want := func() *x509.CertPool {
300+
t.Helper()
301+
pool, err := x509.SystemCertPool()
302+
if err != nil {
303+
t.Fatal(err)
304+
}
305+
if err := appendPool(pool, path); err != nil {
306+
t.Fatal(err)
307+
}
308+
return pool
309+
}
310+
if !first.Equal(want()) {
311+
t.Fatalf("SystemPoolLoader() pool is not the system roots plus the bundle")
312+
}
313+
if second, err := getPool(); err != nil || second != first {
314+
t.Fatalf("SystemPoolLoader() with the file unchanged = %p, %v, want the cached %p", second, err, first)
315+
}
316+
317+
if err := rotateProjectedBundle(path, makeTrustBundle(t, 2)); err != nil {
318+
t.Fatalf("rotate bundle: %v", err)
319+
}
320+
rotated, err := getPool()
321+
if err != nil {
322+
t.Fatalf("SystemPoolLoader() after rotation error = %v", err)
323+
}
324+
if rotated.Equal(first) || !rotated.Equal(want()) {
325+
t.Fatalf("SystemPoolLoader() did not pick up the rotated trust bundle")
326+
}
327+
}
328+
289329
func TestParsePoolRejectsBundleWithoutCertificates(t *testing.T) {
290330
path := writeBundle(t, []byte("not a certificate"))
291331
if _, err := ParsePool(path); err == nil {

0 commit comments

Comments
 (0)