Skip to content

Commit

Permalink
Clean up agent rotator tests (#2604)
Browse files Browse the repository at this point in the history
* Clean up agent rotator tests

- Removes the agent client mock
- Restructures as simple table-driven tests
- Replaces Catalog with SVID Key Manager

Signed-off-by: Andrew Harding <aharding@vmware.com>
  • Loading branch information
azdagron authored Oct 29, 2021
1 parent d27ee9d commit fbffcf0
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 321 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ service-protos := \
# mock-destination-pkg,src-go-pkg,interface[,additional interfaces]
mockgen_mocks = \
test/mock/server/aws,github.com/spiffe/spire/pkg/server/plugin/nodeattestor/aws,Client \
test/mock/agent/client,github.com/spiffe/spire/pkg/agent/client,Client \
test/mock/common/plugin/k8s/apiserver,github.com/spiffe/spire/pkg/common/plugin/k8s/apiserver,Client \

# The following vars are used in rule construction
Expand Down
20 changes: 10 additions & 10 deletions pkg/agent/manager/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ func newManager(c *Config) *manager {
cache := cache.New(c.Log.WithField(telemetry.SubsystemName, telemetry.CacheManager), c.TrustDomain, c.Bundle, c.Metrics)

rotCfg := &svid.RotatorConfig{
Catalog: c.Catalog,
Log: c.Log,
Metrics: c.Metrics,
SVID: c.SVID,
SVIDKey: c.SVIDKey,
BundleStream: cache.SubscribeToBundleChanges(),
ServerAddr: c.ServerAddr,
TrustDomain: c.TrustDomain,
Interval: c.RotationInterval,
Clk: c.Clk,
SVIDKeyManager: keymanager.ForSVID(c.Catalog.GetKeyManager()),
Log: c.Log,
Metrics: c.Metrics,
SVID: c.SVID,
SVIDKey: c.SVIDKey,
BundleStream: cache.SubscribeToBundleChanges(),
ServerAddr: c.ServerAddr,
TrustDomain: c.TrustDomain,
Interval: c.RotationInterval,
Clk: c.Clk,
}
svidRotator, client := svid.NewRotator(rotCfg)

Expand Down
11 changes: 7 additions & 4 deletions pkg/agent/svid/rotator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ type Rotator interface {
SetRotationFinishedHook(func())
}

type Client interface {
RenewSVID(ctx context.Context, csr []byte) (*client.X509SVID, error)
Release()
}

type rotator struct {
c *RotatorConfig
client client.Client
client Client

state observer.Property
clk clock.Clock
Expand Down Expand Up @@ -139,14 +144,12 @@ func (r *rotator) rotateSVID(ctx context.Context) (err error) {
defer r.rotMtx.Unlock()
r.c.Log.Debug("Rotating agent SVID")

svidKM := keymanager.ForSVID(r.c.Catalog.GetKeyManager())

var existingKey keymanager.Key
if state, ok := r.state.Value().(State); ok && state.Key != nil {
existingKey, _ = state.Key.(keymanager.Key)
}

key, err := svidKM.GenerateKey(ctx, existingKey)
key, err := r.c.SVIDKeyManager.GenerateKey(ctx, existingKey)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/agent/svid/rotator_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/imkira/go-observer"
"github.com/sirupsen/logrus"
"github.com/spiffe/go-spiffe/v2/spiffeid"
"github.com/spiffe/spire/pkg/agent/catalog"
"github.com/spiffe/spire/pkg/agent/client"
"github.com/spiffe/spire/pkg/agent/common/backoff"
"github.com/spiffe/spire/pkg/agent/manager/cache"
Expand All @@ -21,11 +20,12 @@ import (
const DefaultRotatorInterval = 5 * time.Second

type RotatorConfig struct {
Catalog catalog.Catalog
Log logrus.FieldLogger
Metrics telemetry.Metrics
TrustDomain spiffeid.TrustDomain
ServerAddr string
SVIDKeyManager keymanager.SVIDKeyManager
Log logrus.FieldLogger
Metrics telemetry.Metrics
TrustDomain spiffeid.TrustDomain
ServerAddr string

// Initial SVID and key
SVID []*x509.Certificate
SVIDKey keymanager.Key
Expand Down
Loading

0 comments on commit fbffcf0

Please sign in to comment.