Skip to content

Commit 1fd7688

Browse files
committed
Isolate mock signer for externaljwt tests
1 parent 810e9e2 commit 1fd7688

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

pkg/controlplane/apiserver/options/options_test.go

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -345,15 +345,10 @@ func TestCompleteForServiceAccount(t *testing.T) {
345345
t.Fatalf("Failed to encode private key: %v", err)
346346
}
347347

348-
// create and start mock signer.
349-
socketPath := "@mock-external-jwt-signer.sock"
350-
mockSigner := v1alpha1testing.NewMockSigner(t, socketPath)
351-
defer mockSigner.CleanUp()
352-
353348
testCases := []struct {
354349
desc string
355350
issuers []string
356-
signingEndpoint string
351+
externalSigner bool
357352
signingKeyFiles string
358353
maxExpiration time.Duration
359354
externalMaxExpirationSec int64
@@ -366,11 +361,11 @@ func TestCompleteForServiceAccount(t *testing.T) {
366361
externalPublicKeyGetterPresent bool
367362
}{
368363
{
369-
desc: "no endpoint or key file",
364+
desc: "endpoint and key file",
370365
issuers: []string{
371366
"iss",
372367
},
373-
signingEndpoint: socketPath,
368+
externalSigner: true,
374369
signingKeyFiles: "private_key.pem",
375370
maxExpiration: time.Second * 3600,
376371

@@ -381,7 +376,7 @@ func TestCompleteForServiceAccount(t *testing.T) {
381376
issuers: []string{
382377
"iss",
383378
},
384-
signingEndpoint: socketPath,
379+
externalSigner: true,
385380
signingKeyFiles: "private_key.pem",
386381
maxExpiration: time.Second * 10,
387382

@@ -392,7 +387,7 @@ func TestCompleteForServiceAccount(t *testing.T) {
392387
issuers: []string{
393388
"iss",
394389
},
395-
signingEndpoint: "",
390+
externalSigner: false,
396391
signingKeyFiles: "private_key.pem",
397392
maxExpiration: time.Second * 3600,
398393

@@ -405,7 +400,7 @@ func TestCompleteForServiceAccount(t *testing.T) {
405400
issuers: []string{
406401
"iss",
407402
},
408-
signingEndpoint: socketPath,
403+
externalSigner: true,
409404
signingKeyFiles: "",
410405
maxExpiration: 0,
411406
externalMaxExpirationSec: 600, // 10m
@@ -419,7 +414,7 @@ func TestCompleteForServiceAccount(t *testing.T) {
419414
issuers: []string{
420415
"iss",
421416
},
422-
signingEndpoint: socketPath,
417+
externalSigner: true,
423418
signingKeyFiles: "",
424419
maxExpiration: time.Second * 3600,
425420
externalMaxExpirationSec: 600, // 10m
@@ -431,7 +426,7 @@ func TestCompleteForServiceAccount(t *testing.T) {
431426
issuers: []string{
432427
"iss",
433428
},
434-
signingEndpoint: socketPath,
429+
externalSigner: true,
435430
signingKeyFiles: "",
436431
maxExpiration: 0,
437432
externalMaxExpirationSec: 300, // 5m
@@ -443,7 +438,7 @@ func TestCompleteForServiceAccount(t *testing.T) {
443438
issuers: []string{
444439
"iss",
445440
},
446-
signingEndpoint: socketPath,
441+
externalSigner: true,
447442
signingKeyFiles: "",
448443
maxExpiration: 0,
449444
externalMaxExpirationSec: 900, // 15m
@@ -456,7 +451,7 @@ func TestCompleteForServiceAccount(t *testing.T) {
456451
issuers: []string{
457452
"iss",
458453
},
459-
signingEndpoint: socketPath,
454+
externalSigner: true,
460455
signingKeyFiles: "",
461456
maxExpiration: 0,
462457
externalMaxExpirationSec: 900, // 15m
@@ -468,8 +463,20 @@ func TestCompleteForServiceAccount(t *testing.T) {
468463

469464
for _, tc := range testCases {
470465
t.Run(tc.desc, func(t *testing.T) {
466+
471467
options := NewOptions()
472-
options.ServiceAccountSigningEndpoint = tc.signingEndpoint
468+
if tc.externalSigner {
469+
// create and start mock signer.
470+
socketPath := fmt.Sprintf("@mock-external-jwt-signer-%d.sock", time.Now().Nanosecond())
471+
mockSigner := v1alpha1testing.NewMockSigner(t, socketPath)
472+
defer mockSigner.CleanUp()
473+
474+
mockSigner.MaxTokenExpirationSeconds = tc.externalMaxExpirationSec
475+
mockSigner.MetadataError = tc.metadataError
476+
mockSigner.FetchError = tc.fetchError
477+
478+
options.ServiceAccountSigningEndpoint = socketPath
479+
}
473480
options.ServiceAccountSigningKeyFile = tc.signingKeyFiles
474481
options.Authentication = &kubeoptions.BuiltInAuthenticationOptions{
475482
ServiceAccounts: &kubeoptions.ServiceAccountAuthenticationOptions{
@@ -478,16 +485,13 @@ func TestCompleteForServiceAccount(t *testing.T) {
478485
},
479486
}
480487

481-
_ = mockSigner.Reset()
482-
mockSigner.MaxTokenExpirationSeconds = tc.externalMaxExpirationSec
483-
mockSigner.MetadataError = tc.metadataError
484-
mockSigner.FetchError = tc.fetchError
485-
486488
co := completedOptions{
487489
Options: *options,
488490
}
489491

490-
err := options.completeServiceAccountOptions(context.Background(), &co)
492+
ctx, cancel := context.WithCancel(context.Background())
493+
defer cancel()
494+
err := options.completeServiceAccountOptions(ctx, &co)
491495

492496
if tc.wantError != nil {
493497
if err == nil || tc.wantError.Error() != err.Error() {

pkg/serviceaccount/externaljwt/plugin/testing/v1alpha1/externalsigner_mock.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"crypto/x509"
2525
"encoding/base64"
2626
"encoding/json"
27+
"errors"
2728
"fmt"
2829
"net"
2930
"os"
@@ -225,7 +226,7 @@ func (m *MockSigner) start(t *testing.T) error {
225226

226227
klog.Infof("Starting Mock Signer at socketPath %s", m.socketPath)
227228
go func() {
228-
if err := m.server.Serve(m.listener); err != nil {
229+
if err := m.server.Serve(m.listener); err != nil && !errors.Is(err, grpc.ErrServerStopped) {
229230
t.Error(err)
230231
}
231232
}()
@@ -264,7 +265,7 @@ func (m *MockSigner) waitForMockServerToStart() error {
264265

265266
// CleanUp stops gRPC server and the underlying listener.
266267
func (m *MockSigner) CleanUp() {
267-
m.server.Stop()
268+
m.server.GracefulStop()
268269
_ = m.listener.Close()
269270
_ = os.Remove(m.socketPath)
270271
}

0 commit comments

Comments
 (0)