From 01cd7f96aac76620f2f73592ae66b460f79c6bc7 Mon Sep 17 00:00:00 2001 From: Philippe Boneff Date: Thu, 8 Aug 2024 12:07:19 +0000 Subject: [PATCH] pass a callback through instance options to create storage --- personalities/sctfe/ct_server_gcp/main.go | 7 ++----- personalities/sctfe/handlers.go | 3 ++- personalities/sctfe/handlers_test.go | 4 ++-- personalities/sctfe/instance.go | 11 ++++++++--- personalities/sctfe/instance_test.go | 8 ++++++-- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/personalities/sctfe/ct_server_gcp/main.go b/personalities/sctfe/ct_server_gcp/main.go index 0762c8706..bbd2dcc09 100644 --- a/personalities/sctfe/ct_server_gcp/main.go +++ b/personalities/sctfe/ct_server_gcp/main.go @@ -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") } diff --git a/personalities/sctfe/handlers.go b/personalities/sctfe/handlers.go index 5eaae36a5..903fa7b35 100644 --- a/personalities/sctfe/handlers.go +++ b/personalities/sctfe/handlers.go @@ -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, diff --git a/personalities/sctfe/handlers_test.go b/personalities/sctfe/handlers_test.go index 220135d9b..be063ba12 100644 --- a/personalities/sctfe/handlers_test.go +++ b/personalities/sctfe/handlers_test.go @@ -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)) { diff --git a/personalities/sctfe/instance.go b/personalities/sctfe/instance.go index eede95bb7..d83912fc4 100644 --- a/personalities/sctfe/instance.go +++ b/personalities/sctfe/instance.go @@ -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. @@ -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 } diff --git a/personalities/sctfe/instance_test.go b/personalities/sctfe/instance_test.go index 09b32b030..5371342bd 100644 --- a/personalities/sctfe/instance_test.go +++ b/personalities/sctfe/instance_test.go @@ -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() @@ -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 == "" { @@ -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 {