Skip to content

Commit

Permalink
pass a callback through instance options to create storage
Browse files Browse the repository at this point in the history
  • Loading branch information
phbnf committed Aug 8, 2024
1 parent 16366c4 commit 01cd7f9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
7 changes: 2 additions & 5 deletions personalities/sctfe/ct_server_gcp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,8 @@ func setupAndRegister(ctx context.Context, deadline time.Duration, vCfg *sctfe.V

switch vCfg.Config.StorageConfig.(type) {
case *configpb.LogConfig_Gcp:
storage, err := newGCPStorage(ctx, vCfg)
if err != nil {
return nil, fmt.Errorf("failed to initialize GCP storage: %v", err)
}
opts.Storage = storage
klog.Info("Found GCP storage config, will set up GCP tessera storage")
opts.CreateStorage = newGCPStorage
default:
return nil, fmt.Errorf("unrecognized storage config")
}
Expand Down
3 changes: 2 additions & 1 deletion personalities/sctfe/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,14 @@ func newLogInfo(
validationOpts CertValidationOpts,
signer crypto.Signer,
timeSource TimeSource,
storage Storage,
) *logInfo {
vCfg := instanceOpts.Validated
cfg := vCfg.Config

li := &logInfo{
LogOrigin: cfg.Origin,
storage: instanceOpts.Storage,
storage: storage,
signer: signer,
TimeSource: timeSource,
instanceOpts: instanceOpts,
Expand Down
4 changes: 2 additions & 2 deletions personalities/sctfe/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ func setupTest(t *testing.T, pemRoots []string, signer crypto.Signer) handlerTes

cfg := &configpb.LogConfig{Origin: "example.com"}
vCfg := &ValidatedLogConfig{Config: cfg}
iOpts := InstanceOptions{Validated: vCfg, Storage: info.storage, Deadline: time.Millisecond * 500, MetricFactory: monitoring.InertMetricFactory{}, RequestLog: new(DefaultRequestLog)}
info.li = newLogInfo(iOpts, vOpts, signer, fakeTimeSource)
iOpts := InstanceOptions{Validated: vCfg, Deadline: time.Millisecond * 500, MetricFactory: monitoring.InertMetricFactory{}, RequestLog: new(DefaultRequestLog)}
info.li = newLogInfo(iOpts, vOpts, signer, fakeTimeSource, info.storage)

for _, pemRoot := range pemRoots {
if !info.roots.AppendCertsFromPEM([]byte(pemRoot)) {
Expand Down
11 changes: 8 additions & 3 deletions personalities/sctfe/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type InstanceOptions struct {
// Validated holds the original configuration options for the log, and some
// of its fields parsed as a result of validating it.
Validated *ValidatedLogConfig
// Storage is a corresponding Tessera storage implementation.
Storage Storage
// CreateStorage instantiates a Tessera storage implementation.
CreateStorage func(context.Context, *ValidatedLogConfig) (*CTStorage, error)
// Deadline is a timeout for Tessera requests.
Deadline time.Duration
// MetricFactory allows creating metrics.
Expand Down Expand Up @@ -146,7 +146,12 @@ func setUpLogInfo(ctx context.Context, opts InstanceOptions) (*logInfo, error) {
return nil, fmt.Errorf("failed to parse RejectExtensions: %v", err)
}

logInfo := newLogInfo(opts, validationOpts, signer, new(SystemTimeSource))
storage, err := opts.CreateStorage(ctx, opts.Validated)
if err != nil {
return nil, fmt.Errorf("failed to create storage backend: %v", err)
}

logInfo := newLogInfo(opts, validationOpts, signer, new(SystemTimeSource), storage)
return logInfo, nil
}

Expand Down
8 changes: 6 additions & 2 deletions personalities/sctfe/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func init() {
keys.RegisterHandler(&keyspb.PEMKeyFile{}, pem.FromProto)
}

func fakeCTStorage(_ context.Context, _ *ValidatedLogConfig) (*CTStorage, error) {
return &CTStorage{}, nil
}

func TestSetUpInstance(t *testing.T) {
ctx := context.Background()

Expand Down Expand Up @@ -147,7 +151,7 @@ func TestSetUpInstance(t *testing.T) {
if err != nil {
t.Fatalf("ValidateLogConfig(): %v", err)
}
opts := InstanceOptions{Validated: vCfg, Deadline: time.Second, MetricFactory: monitoring.InertMetricFactory{}}
opts := InstanceOptions{Validated: vCfg, Deadline: time.Second, MetricFactory: monitoring.InertMetricFactory{}, CreateStorage: fakeCTStorage}

if _, err := SetUpInstance(ctx, opts); err != nil {
if test.wantErr == "" {
Expand Down Expand Up @@ -238,7 +242,7 @@ func TestSetUpInstanceSetsValidationOpts(t *testing.T) {
if err != nil {
t.Fatalf("ValidateLogConfig(): %v", err)
}
opts := InstanceOptions{Validated: vCfg, Deadline: time.Second, MetricFactory: monitoring.InertMetricFactory{}, CacheType: cache.NOOP, CacheOption: cache.Option{}}
opts := InstanceOptions{Validated: vCfg, Deadline: time.Second, MetricFactory: monitoring.InertMetricFactory{}, CacheType: cache.NOOP, CacheOption: cache.Option{}, CreateStorage: fakeCTStorage}

inst, err := SetUpInstance(ctx, opts)
if err != nil {
Expand Down

0 comments on commit 01cd7f9

Please sign in to comment.