Skip to content

Commit 2ebb5ea

Browse files
committed
refactor: use biz layer Revoke in stale token revoker
Rename repo RevokeInactive to FindInactive (query-only) and have the stale revoker call APITokenUseCase.Revoke per token so audit events are properly dispatched. Signed-off-by: Miguel Martinez <miguel@chainloop.dev>
1 parent ff3255b commit 2ebb5ea

6 files changed

Lines changed: 96 additions & 108 deletions

File tree

app/controlplane/cmd/wire_gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/controlplane/pkg/biz/apitoken.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,9 @@ type APITokenRepo interface {
6262
Create(ctx context.Context, name string, description *string, expiresAt *time.Time, organizationID *uuid.UUID, projectID *uuid.UUID, policies []*authz.Policy) (*APIToken, error)
6363
List(ctx context.Context, orgID *uuid.UUID, filters *APITokenListFilters) ([]*APIToken, error)
6464
Revoke(ctx context.Context, orgID *uuid.UUID, ID uuid.UUID) error
65-
// RevokeInactive bulk-revokes tokens in an organization that have been inactive since the given cutoff time.
65+
// FindInactive returns tokens in an organization that have been inactive since the given cutoff time.
6666
// A token is considered inactive if its last_used_at (or created_at, if never used) is before inactiveSince.
67-
// Returns the list of tokens that were revoked.
68-
RevokeInactive(ctx context.Context, orgID uuid.UUID, inactiveSince time.Time) ([]*APIToken, error)
67+
FindInactive(ctx context.Context, orgID uuid.UUID, inactiveSince time.Time) ([]*APIToken, error)
6968
UpdateExpiration(ctx context.Context, ID uuid.UUID, expiresAt time.Time) error
7069
UpdateLastUsedAt(ctx context.Context, ID uuid.UUID, lastUsedAt time.Time) error
7170
FindByID(ctx context.Context, ID uuid.UUID) (*APIToken, error)

app/controlplane/pkg/biz/apitoken_stale_revoker.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type APITokenStaleRevoker struct {
3232
logger *log.Helper
3333
orgRepo OrganizationRepo
3434
tokenRepo APITokenRepo
35+
tokenUC *APITokenUseCase
3536
}
3637

3738
// APITokenStaleRevokerOpts configures the sweeper's behavior.
@@ -43,11 +44,12 @@ type APITokenStaleRevokerOpts struct {
4344
}
4445

4546
// NewAPITokenStaleRevoker creates a new stale token revoker.
46-
func NewAPITokenStaleRevoker(orgRepo OrganizationRepo, tokenRepo APITokenRepo, logger log.Logger) *APITokenStaleRevoker {
47+
func NewAPITokenStaleRevoker(orgRepo OrganizationRepo, tokenRepo APITokenRepo, tokenUC *APITokenUseCase, logger log.Logger) *APITokenStaleRevoker {
4748
return &APITokenStaleRevoker{
4849
logger: log.NewHelper(log.With(logger, "component", "biz/APITokenStaleRevoker")),
4950
orgRepo: orgRepo,
5051
tokenRepo: tokenRepo,
52+
tokenUC: tokenUC,
5153
}
5254
}
5355

@@ -122,16 +124,25 @@ func (r *APITokenStaleRevoker) Sweep(ctx context.Context) error {
122124
}
123125

124126
cutoff := now.Add(-time.Duration(*org.APITokenInactivityThresholdDays) * 24 * time.Hour)
125-
revoked, err := r.tokenRepo.RevokeInactive(ctx, orgID, cutoff)
127+
staleTokens, err := r.tokenRepo.FindInactive(ctx, orgID, cutoff)
126128
if err != nil {
127-
r.logger.Errorf("revoking stale tokens for org %s (%s): %v", org.Name, org.ID, err)
129+
r.logger.Errorf("finding stale tokens for org %s (%s): %v", org.Name, org.ID, err)
128130
continue
129131
}
130132

131-
if len(revoked) > 0 {
133+
var revokedCount int
134+
for _, token := range staleTokens {
135+
if err := r.tokenUC.Revoke(ctx, org.ID, token.ID.String()); err != nil {
136+
r.logger.Errorf("revoking token %s in org %s: %v", token.ID, org.ID, err)
137+
continue
138+
}
139+
revokedCount++
140+
}
141+
142+
if revokedCount > 0 {
132143
r.logger.Infow("msg", "revoked stale API tokens",
133144
"org", org.Name, "orgID", org.ID,
134-
"count", len(revoked),
145+
"count", revokedCount,
135146
"thresholdDays", *org.APITokenInactivityThresholdDays,
136147
)
137148
}

app/controlplane/pkg/biz/apitoken_stale_revoker_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (s *staleRevokerTestSuite) SetupTest() {
4646
ctx := context.Background()
4747

4848
s.TestingUseCases = testhelpers.NewTestingUseCases(t)
49-
s.revoker = biz.NewAPITokenStaleRevoker(s.Repos.OrganizationRepo, s.Repos.APITokenRepo, s.L)
49+
s.revoker = biz.NewAPITokenStaleRevoker(s.Repos.OrganizationRepo, s.Repos.APITokenRepo, s.APIToken, s.L)
5050

5151
var err error
5252
s.user, err = s.User.UpsertByEmail(ctx, "revoker-test@test.com", nil)

app/controlplane/pkg/biz/mocks/APITokenRepo.go

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

app/controlplane/pkg/data/apitoken.go

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,8 @@ func (r *APITokenRepo) Revoke(ctx context.Context, orgID *uuid.UUID, id uuid.UUI
163163
return nil
164164
}
165165

166-
// RevokeInactive bulk-revokes tokens that have been inactive since the given cutoff.
167-
func (r *APITokenRepo) RevokeInactive(ctx context.Context, orgID uuid.UUID, inactiveSince time.Time) ([]*biz.APIToken, error) {
168-
now := time.Now()
169-
170-
// Find matching tokens: not revoked, and inactive since the cutoff
166+
// FindInactive returns tokens that have been inactive since the given cutoff.
167+
func (r *APITokenRepo) FindInactive(ctx context.Context, orgID uuid.UUID, inactiveSince time.Time) ([]*biz.APIToken, error) {
171168
// A token is inactive if last_used_at < inactiveSince (when used before),
172169
// or created_at < inactiveSince (when never used).
173170
tokens, err := r.data.DB.APIToken.Query().
@@ -186,25 +183,6 @@ func (r *APITokenRepo) RevokeInactive(ctx context.Context, orgID uuid.UUID, inac
186183
return nil, fmt.Errorf("querying inactive tokens: %w", err)
187184
}
188185

189-
if len(tokens) == 0 {
190-
return nil, nil
191-
}
192-
193-
// Collect IDs for bulk update
194-
ids := make([]uuid.UUID, 0, len(tokens))
195-
for _, t := range tokens {
196-
ids = append(ids, t.ID)
197-
}
198-
199-
// Bulk revoke
200-
if err := r.data.DB.APIToken.Update().
201-
Where(apitoken.IDIn(ids...)).
202-
SetRevokedAt(now).
203-
Exec(ctx); err != nil {
204-
return nil, fmt.Errorf("bulk revoking inactive tokens: %w", err)
205-
}
206-
207-
// Return the revoked tokens
208186
result := make([]*biz.APIToken, 0, len(tokens))
209187
for _, t := range tokens {
210188
result = append(result, entAPITokenToBiz(t))

0 commit comments

Comments
 (0)