Skip to content

Commit

Permalink
keystore config prep (#43153)
Browse files Browse the repository at this point in the history
  • Loading branch information
nklaassen authored Jun 18, 2024
1 parent 6c74121 commit 7c722b3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
11 changes: 3 additions & 8 deletions lib/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3768,15 +3768,10 @@ func TestCAGeneration(t *testing.T) {
privKey, pubKey, err := testauthority.New().GenerateKeyPair()
require.NoError(t, err)

ksConfig := keystore.Config{
Software: keystore.SoftwareConfig{
RSAKeyPairSource: func() (priv []byte, pub []byte, err error) {
return privKey, pubKey, nil
},
},
rsaKeyPairSource := func() (priv []byte, pub []byte, err error) {
return privKey, pubKey, nil
}
keyStore, err := keystore.NewManager(ctx, ksConfig)
require.NoError(t, err)
keyStore := keystore.NewSoftwareKeystoreForTests(t, keystore.WithRSAKeyPairSource(rsaKeyPairSource))

for _, caType := range types.CertAuthTypes {
t.Run(string(caType), func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion lib/auth/keystore/keystore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ func newTestPack(ctx context.Context, t *testing.T) *testPack {
softwareConfig := Config{Software: SoftwareConfig{
RSAKeyPairSource: native.GenerateKeyPair,
}}
softwareBackend := newSoftwareKeyStore(&softwareConfig.Software, logger)
softwareBackend := newSoftwareKeyStore(&softwareConfig.Software)
backends = append(backends, &backendDesc{
name: "software",
config: softwareConfig,
Expand Down
2 changes: 1 addition & 1 deletion lib/auth/keystore/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func NewManager(ctx context.Context, cfg Config) (*Manager, error) {
return nil, trace.Wrap(err)
}

softwareBackend := newSoftwareKeyStore(&cfg.Software, cfg.Logger)
softwareBackend := newSoftwareKeyStore(&cfg.Software)

if (cfg.PKCS11 != PKCS11Config{}) {
pkcs11Backend, err := newPKCS11KeyStore(&cfg.PKCS11, cfg.Logger)
Expand Down
3 changes: 1 addition & 2 deletions lib/auth/keystore/software.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"crypto"

"github.com/gravitational/trace"
"github.com/sirupsen/logrus"

"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/auth/native"
Expand All @@ -48,7 +47,7 @@ func (cfg *SoftwareConfig) CheckAndSetDefaults() error {
return nil
}

func newSoftwareKeyStore(config *SoftwareConfig, logger logrus.FieldLogger) *softwareKeyStore {
func newSoftwareKeyStore(config *SoftwareConfig) *softwareKeyStore {
return &softwareKeyStore{
rsaKeyPairSource: config.RSAKeyPairSource,
}
Expand Down
26 changes: 26 additions & 0 deletions lib/auth/keystore/testhelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,29 @@ func softHSMTestConfig(t *testing.T) (servicecfg.KeystoreConfig, bool) {
}
return *cachedSoftHSMConfig, true
}

type testKeystoreOptions struct {
rsaKeyPairSource RSAKeyPairSource
}

type TestKeystoreOption func(*testKeystoreOptions)

func WithRSAKeyPairSource(rsaKeyPairSource RSAKeyPairSource) TestKeystoreOption {
return func(opts *testKeystoreOptions) {
opts.rsaKeyPairSource = rsaKeyPairSource
}
}

// NewSoftwareKeystoreForTests returns a new *Manager that is valid for tests not specifically testing the
// keystore functionality.
func NewSoftwareKeystoreForTests(_ *testing.T, opts ...TestKeystoreOption) *Manager {
var options testKeystoreOptions
for _, opt := range opts {
opt(&options)
}
softwareBackend := newSoftwareKeyStore(&SoftwareConfig{RSAKeyPairSource: options.rsaKeyPairSource})
return &Manager{
backendForNewKeys: softwareBackend,
usableSigningBackends: []backend{softwareBackend},
}
}

0 comments on commit 7c722b3

Please sign in to comment.