Skip to content

Commit f3ecfcf

Browse files
committed
Add test for empty federated bundle
This causes the workload to be unable to fetch X509-SVIDs, even though its own trust domain still has a valid trust bundle
1 parent fb781b6 commit f3ecfcf

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

v2/bundle/x509bundle/bundle_test.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,15 @@ func TestParse(t *testing.T) {
9696
expNumAuthorities: 1,
9797
},
9898
{
99-
name: "Parse empty bytes should fail",
100-
path: "testdata/empty.pem",
101-
expErrContains: "x509bundle: cannot parse certificate: no PEM blocks found",
99+
name: "Parse empty bytes should result in empty bundle",
100+
path: "testdata/empty.pem",
101+
expNumAuthorities: 0,
102102
},
103103
{
104104
name: "Parse non-PEM bytes should fail",
105105
path: "testdata/not-pem.pem",
106106
expErrContains: "x509bundle: cannot parse certificate: no PEM blocks found",
107107
},
108-
{
109-
name: "Parse should fail if no certificate block is is found",
110-
path: "testdata/key.pem",
111-
expErrContains: "x509bundle: no certificates found",
112-
},
113108
{
114109
name: "Parse a corrupted certificate should fail",
115110
path: "testdata/corrupted.pem",
@@ -155,9 +150,9 @@ func TestParseRaw(t *testing.T) {
155150
expNumAuthorities: 1,
156151
},
157152
{
158-
name: "Parse should fail if no certificate block is is found",
159-
path: "testdata/key.pem",
160-
expErrContains: "x509bundle: no certificates found",
153+
name: "Parse should not fail if no certificate block is is found",
154+
path: "testdata/empty.pem",
155+
expNumAuthorities: 0,
161156
},
162157
}
163158

@@ -322,6 +317,10 @@ func loadRawCertificates(t *testing.T, path string) []byte {
322317
certsBytes, err := os.ReadFile(path)
323318
require.NoError(t, err)
324319

320+
if len(certsBytes) == 0 {
321+
return []byte{}
322+
}
323+
325324
certs, err := pemutil.ParseCertificates(certsBytes)
326325
require.NoError(t, err)
327326

v2/workloadapi/client_test.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -190,16 +190,22 @@ func TestFetchX509Context(t *testing.T) {
190190
assertX509Bundle(t, x509Ctx.Bundles, td, ca.X509Bundle())
191191
assertX509Bundle(t, x509Ctx.Bundles, federatedTD, federatedCA.X509Bundle())
192192

193-
// Now set the next response without any bundles and assert that the call
194-
// fails since the bundle cannot be empty.
193+
// Now set the next response with an empty federated bundles and assert that the call
194+
// still succeeds.
195195
wl.SetX509SVIDResponse(&fakeworkloadapi.X509SVIDResponse{
196-
SVIDs: svids,
196+
Bundle: ca.X509Bundle(),
197+
SVIDs: svids,
198+
FederatedBundles: []*x509bundle.Bundle{x509bundle.FromX509Authorities(federatedCA.Bundle().TrustDomain(), nil)},
197199
})
198200

199201
x509Ctx, err = c.FetchX509Context(context.Background())
200-
201-
require.EqualError(t, err, `empty X.509 bundle for trust domain "example.org"`)
202-
require.Nil(t, x509Ctx)
202+
require.NoError(t, err)
203+
// inspect svids
204+
require.Len(t, x509Ctx.SVIDs, 4)
205+
assertX509SVID(t, x509Ctx.SVIDs[0], fooID, resp.SVIDs[0].Certificates, hintInternal)
206+
assertX509SVID(t, x509Ctx.SVIDs[1], barID, resp.SVIDs[1].Certificates, hintExternal)
207+
assertX509SVID(t, x509Ctx.SVIDs[2], emptyHintSVID1.ID, resp.SVIDs[3].Certificates, "")
208+
assertX509SVID(t, x509Ctx.SVIDs[3], emptyHintSVID2.ID, resp.SVIDs[4].Certificates, "")
203209
}
204210

205211
func TestWatchX509Context(t *testing.T) {

0 commit comments

Comments
 (0)