Skip to content

Commit

Permalink
handle special case when server is not running (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
hkadakia authored Nov 16, 2021
1 parent a57e576 commit dfff411
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
7 changes: 7 additions & 0 deletions pkcs11/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ func getRemainingRequestTime(ctx context.Context, keyIdentifier string) (time.Du
}

func getSigner(ctx context.Context, requestChan chan scheduler.Request, pool sPool, keyIdentifier string, priority proto.Priority) (signer signerWithSignAlgorithm, err error) {
// Need to handle case when we directly invoke SignSSHCert or SignX509Cert for
// either generating the host certs or X509 CA certs. In that case we don't need the server
// running nor do we need to worry about priority scheduling. In that case, we immediately
// fetch the signer from the pool.
if requestChan == nil {
return pool.get(ctx)
}
remTime, err := getRemainingRequestTime(ctx, keyIdentifier)
if err != nil {
return nil, err
Expand Down
16 changes: 11 additions & 5 deletions pkcs11/signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,18 +438,24 @@ func TestSignX509ECCert(t *testing.T) {
isBadSigner bool
expectError bool
}{
"cert-ec-good-signer": {ctx, certEC, defaultIdentifier, proto.Priority_Unspecified_priority, false, false},
"cert-ec-bad-identifier": {ctx, certEC, badIdentifier, proto.Priority_Medium, false, true},
"cert-ec-bad-signer": {ctx, certEC, badIdentifier, proto.Priority_Medium, true, true},
"cert-ec-good-signer": {ctx, certEC, defaultIdentifier, proto.Priority_Unspecified_priority, false, false},
"cert-ec-bad-identifier": {ctx, certEC, badIdentifier, proto.Priority_Medium, false, true},
"cert-ec-bad-signer": {ctx, certEC, badIdentifier, proto.Priority_Medium, true, true},
"x509-ec-ca-cert-no-server": {ctx, certEC, defaultIdentifier, proto.Priority_Unspecified_priority, false, false},
}
go dummyScheduler(ctx, reqChan)
for label, tt := range testcases {
label, tt := label, tt
t.Run(label, func(t *testing.T) {
t.Parallel()
signer := initMockSigner(x509.ECDSA, caPriv, caCert, tt.isBadSigner)
data, err := signer.SignX509Cert(tt.ctx, reqChan, tt.cert, tt.identifier, tt.priority)
if err != nil != tt.expectError {
var data []byte
if label == "x509-ec-ca-cert-no-server" {
data, err = signer.SignX509Cert(tt.ctx, nil, tt.cert, tt.identifier, tt.priority)
} else {
data, err = signer.SignX509Cert(tt.ctx, reqChan, tt.cert, tt.identifier, tt.priority)
}
if (err != nil) != tt.expectError {
t.Fatalf("%s: got err: %v, expect err: %v", label, err, tt.expectError)
}
if err != nil {
Expand Down

0 comments on commit dfff411

Please sign in to comment.