forked from TrueCloudLab/certificates
Avoid closing pkcs#11 context twice.
This commit is contained in:
parent
f289d1ee1f
commit
ebaeae9008
3 changed files with 27 additions and 4 deletions
|
@ -11,6 +11,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/ThalesIgnite/crypto11"
|
"github.com/ThalesIgnite/crypto11"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
@ -44,6 +45,7 @@ var p11Configure = func(config *crypto11.Config) (P11, error) {
|
||||||
// PKCS11 is the implementation of a KMS using the PKCS #11 standard.
|
// PKCS11 is the implementation of a KMS using the PKCS #11 standard.
|
||||||
type PKCS11 struct {
|
type PKCS11 struct {
|
||||||
p11 P11
|
p11 P11
|
||||||
|
closed sync.Once
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a new PKCS11 KMS.
|
// New returns a new PKCS11 KMS.
|
||||||
|
@ -232,8 +234,11 @@ func (k *PKCS11) DeleteCertificate(uri string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close releases the connection to the PKCS#11 module.
|
// Close releases the connection to the PKCS#11 module.
|
||||||
func (k *PKCS11) Close() error {
|
func (k *PKCS11) Close() (err error) {
|
||||||
return errors.Wrap(k.p11.Close(), "error closing pkcs#11 context")
|
k.closed.Do(func() {
|
||||||
|
err = errors.Wrap(k.p11.Close(), "error closing pkcs#11 context")
|
||||||
|
})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func toByte(s string) []byte {
|
func toByte(s string) []byte {
|
||||||
|
|
|
@ -709,3 +709,21 @@ func TestPKCS11_DeleteCertificate(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPKCS11_Close(t *testing.T) {
|
||||||
|
k := mustPKCS11(t)
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{"ok", false},
|
||||||
|
{"second", false},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if err := k.Close(); (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("PKCS11.Close() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -37,7 +37,7 @@ func mustPKCS11(t TBTesting) *PKCS11 {
|
||||||
Pin: "0001password",
|
Pin: "0001password",
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to configure yubiHSM2 on %s: %v", runtime.GOOS, err)
|
t.Fatalf("failed to configure YubiHSM2 on %s: %v", runtime.GOOS, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
k := &PKCS11{
|
k := &PKCS11{
|
||||||
|
|
Loading…
Reference in a new issue