Skip to content

Commit 22f9e21

Browse files
committed
refactor: extract cache wrapper types into pkg/cache sub-packages
Move AttestationBundleCache and policy eval bundle cache types into their own packages (pkg/cache/attestationbundle and pkg/cache/policyevalbundle) so they can be initialized outside of the controlplane. Each package provides a Cache wrapper type, a New constructor, and default constants for TTL, bucket name, and description. Signed-off-by: Miguel Martinez Trivino <migmartri@gmail.com> Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
1 parent a699e8e commit 22f9e21

8 files changed

Lines changed: 180 additions & 72 deletions

File tree

app/controlplane/cmd/wire.go

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import (
3838
"github.com/chainloop-dev/chainloop/app/controlplane/plugins/sdk/v1"
3939
"github.com/chainloop-dev/chainloop/pkg/blobmanager/loader"
4040
"github.com/chainloop-dev/chainloop/pkg/cache"
41+
"github.com/chainloop-dev/chainloop/pkg/cache/attestationbundle"
42+
"github.com/chainloop-dev/chainloop/pkg/cache/policyevalbundle"
4143
"github.com/chainloop-dev/chainloop/pkg/credentials"
4244
"github.com/chainloop-dev/chainloop/pkg/natsconn"
4345
"github.com/go-kratos/kratos/v2/log"
@@ -170,34 +172,12 @@ func newMembershipsCache(ctx context.Context, rc *natsconn.ReloadableConnection,
170172
return cache.New[*entities.Membership](opts...)
171173
}
172174

173-
func newPolicyEvalBundleCache(ctx context.Context, rc *natsconn.ReloadableConnection, logger log.Logger) (cache.Cache[[]byte], error) {
174-
l := log.NewHelper(logger)
175-
backend := "memory"
176-
opts := []cache.Option{cache.WithTTL(24 * time.Hour), cache.WithLogger(&kratosLogAdapter{h: l}), cache.WithDescription("Cache for policy evaluation bundles from CAS")}
177-
if rc != nil {
178-
backend = "nats"
179-
opts = append(opts, cache.WithNATS(rc.Conn, "chainloop-policy-eval-bundles"))
180-
opts = append(opts, cache.WithReconnect(rc.Subscribe(ctx)))
181-
}
182-
l.Infow("msg", "cache initialized", "bucket", "chainloop-policy-eval-bundles", "backend", backend, "ttl", "24h")
183-
return cache.New[[]byte](opts...)
175+
func newPolicyEvalBundleCache(ctx context.Context, rc *natsconn.ReloadableConnection, logger log.Logger) (*policyevalbundle.Cache, error) {
176+
return policyevalbundle.New(ctx, rc, logger)
184177
}
185178

186-
func newAttestationBundleCache(ctx context.Context, rc *natsconn.ReloadableConnection, logger log.Logger) (*biz.AttestationBundleCache, error) {
187-
l := log.NewHelper(logger)
188-
backend := "memory"
189-
opts := []cache.Option{cache.WithTTL(5 * 24 * time.Hour), cache.WithLogger(&kratosLogAdapter{h: l}), cache.WithDescription("Cache for attestation bundles")}
190-
if rc != nil {
191-
backend = "nats"
192-
opts = append(opts, cache.WithNATS(rc.Conn, "chainloop-attestation-bundles"))
193-
opts = append(opts, cache.WithReconnect(rc.Subscribe(ctx)))
194-
}
195-
l.Infow("msg", "cache initialized", "bucket", "chainloop-attestation-bundles", "backend", backend, "ttl", "120h")
196-
c, err := cache.New[[]byte](opts...)
197-
if err != nil {
198-
return nil, err
199-
}
200-
return &biz.AttestationBundleCache{Cache: c}, nil
179+
func newAttestationBundleCache(ctx context.Context, rc *natsconn.ReloadableConnection, logger log.Logger) (*attestationbundle.Cache, error) {
180+
return attestationbundle.New(ctx, rc, logger)
201181
}
202182

203183
// kratosLogAdapter adapts kratos log.Helper (Debugw(...interface{})) to cache.Logger (Debugw(string, ...any)).

app/controlplane/cmd/wire_gen.go

Lines changed: 10 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/controlplane/internal/service/workflowrun.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"github.com/chainloop-dev/chainloop/app/controlplane/pkg/biz"
2828
"github.com/chainloop-dev/chainloop/app/controlplane/pkg/pagination"
2929
chainloop "github.com/chainloop-dev/chainloop/pkg/attestation/renderer/chainloop"
30-
"github.com/chainloop-dev/chainloop/pkg/cache"
30+
"github.com/chainloop-dev/chainloop/pkg/cache/policyevalbundle"
3131
"github.com/chainloop-dev/chainloop/pkg/credentials"
3232
errors "github.com/go-kratos/kratos/v2/errors"
3333
"github.com/google/uuid"
@@ -46,7 +46,7 @@ type WorkflowRunService struct {
4646
credsReader credentials.Reader
4747
casClient biz.CASClient
4848
casMappingUC *biz.CASMappingUseCase
49-
policyEvalCache cache.Cache[[]byte]
49+
policyEvalCache *policyevalbundle.Cache
5050
}
5151

5252
type NewWorkflowRunServiceOpts struct {
@@ -57,7 +57,7 @@ type NewWorkflowRunServiceOpts struct {
5757
CredsReader credentials.Reader
5858
CASClient biz.CASClient
5959
CASMappingUC *biz.CASMappingUseCase
60-
PolicyEvalCache cache.Cache[[]byte]
60+
PolicyEvalCache *policyevalbundle.Cache
6161
Opts []NewOpt
6262
}
6363

app/controlplane/pkg/biz/testhelpers/wire.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/chainloop-dev/chainloop/app/controlplane/plugins/sdk/v1"
3636
robotaccount "github.com/chainloop-dev/chainloop/internal/robotaccount/cas"
3737
backends "github.com/chainloop-dev/chainloop/pkg/blobmanager"
38+
"github.com/chainloop-dev/chainloop/pkg/cache/attestationbundle"
3839
"github.com/chainloop-dev/chainloop/pkg/credentials"
3940
"github.com/chainloop-dev/chainloop/pkg/natsconn"
4041
"github.com/go-kratos/kratos/v2/log"
@@ -71,7 +72,7 @@ func WireTestData(context.Context, *TestDatabase, *testing.T, log.Logger, creden
7172
)
7273
}
7374

74-
func newAttestationBundleCache() *biz.AttestationBundleCache {
75+
func newAttestationBundleCache() *attestationbundle.Cache {
7576
return nil
7677
}
7778

app/controlplane/pkg/biz/testhelpers/wire_gen.go

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/controlplane/pkg/biz/workflowrun.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
"github.com/chainloop-dev/chainloop/pkg/attestation"
3131
"github.com/chainloop-dev/chainloop/pkg/attestation/renderer/chainloop"
3232
"github.com/chainloop-dev/chainloop/pkg/attestation/verifier"
33-
"github.com/chainloop-dev/chainloop/pkg/cache"
33+
"github.com/chainloop-dev/chainloop/pkg/cache/attestationbundle"
3434
"github.com/secure-systems-lab/go-securesystemslib/dsse"
3535
protobundle "github.com/sigstore/protobuf-specs/gen/pb-go/bundle/v1"
3636
"github.com/sigstore/sigstore/pkg/cryptoutils"
@@ -104,20 +104,14 @@ type WorkflowRunRepo interface {
104104
Expire(ctx context.Context, id uuid.UUID) error
105105
}
106106

107-
// AttestationBundleCache wraps cache.Cache[[]byte] to disambiguate from other
108-
// []byte caches (e.g. policy evaluation bundles) in the wire dependency graph.
109-
type AttestationBundleCache struct {
110-
cache.Cache[[]byte]
111-
}
112-
113107
type WorkflowRunUseCase struct {
114108
wfRunRepo WorkflowRunRepo
115109
wfRepo WorkflowRepo
116110
logger *log.Helper
117111
auditorUC *AuditorUseCase
118112

119113
signingUseCase *SigningUseCase
120-
bundleCache *AttestationBundleCache
114+
bundleCache *attestationbundle.Cache
121115
casClient CASClient
122116
casMappingUC *CASMappingUseCase
123117
}
@@ -128,7 +122,7 @@ type WorkflowRunUseCaseOpts struct {
128122
SigningUC *SigningUseCase
129123
AuditorUC *AuditorUseCase
130124
Logger log.Logger
131-
BundleCache *AttestationBundleCache
125+
BundleCache *attestationbundle.Cache
132126
CASClient CASClient
133127
CASMappingUC *CASMappingUseCase
134128
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//
2+
// Copyright 2026 The Chainloop Authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
// Package attestationbundle provides a typed cache for attestation bundles.
17+
package attestationbundle
18+
19+
import (
20+
"context"
21+
"time"
22+
23+
"github.com/chainloop-dev/chainloop/pkg/cache"
24+
"github.com/chainloop-dev/chainloop/pkg/natsconn"
25+
"github.com/go-kratos/kratos/v2/log"
26+
)
27+
28+
const (
29+
ttl = 5 * 24 * time.Hour
30+
bucket = "chainloop-attestation-bundles"
31+
description = "Cache for attestation bundles"
32+
)
33+
34+
// Cache wraps cache.Cache[[]byte] to provide a distinct type for wire disambiguation.
35+
type Cache struct {
36+
cache.Cache[[]byte]
37+
}
38+
39+
// New creates an attestation bundle cache with built-in TTL, bucket, and description.
40+
func New(ctx context.Context, rc *natsconn.ReloadableConnection, logger log.Logger) (*Cache, error) {
41+
opts := []cache.Option{
42+
cache.WithTTL(ttl),
43+
cache.WithDescription(description),
44+
}
45+
46+
if logger != nil {
47+
opts = append(opts, cache.WithLogger(&kratosLogAdapter{h: log.NewHelper(logger)}))
48+
}
49+
50+
if rc != nil {
51+
opts = append(opts, cache.WithNATS(rc.Conn, bucket))
52+
opts = append(opts, cache.WithReconnect(rc.Subscribe(ctx)))
53+
}
54+
55+
c, err := cache.New[[]byte](opts...)
56+
if err != nil {
57+
return nil, err
58+
}
59+
return &Cache{Cache: c}, nil
60+
}
61+
62+
// kratosLogAdapter adapts kratos log.Helper to cache.Logger.
63+
type kratosLogAdapter struct{ h *log.Helper }
64+
65+
func (a *kratosLogAdapter) Debugw(msg string, keyvals ...any) {
66+
a.h.Debugw(append([]any{"msg", msg}, keyvals...)...)
67+
}
68+
func (a *kratosLogAdapter) Infow(msg string, keyvals ...any) {
69+
a.h.Infow(append([]any{"msg", msg}, keyvals...)...)
70+
}
71+
func (a *kratosLogAdapter) Warnw(msg string, keyvals ...any) {
72+
a.h.Warnw(append([]any{"msg", msg}, keyvals...)...)
73+
}
74+
func (a *kratosLogAdapter) Errorw(msg string, keyvals ...any) {
75+
a.h.Errorw(append([]any{"msg", msg}, keyvals...)...)
76+
}

0 commit comments

Comments
 (0)