Skip to content

Commit ed2e74b

Browse files
committed
clear goroutines
Signed-off-by: Miguel Martinez <miguel@chainloop.dev>
1 parent 73d16fe commit ed2e74b

31 files changed

+2122
-679
lines changed

app/controlplane/cmd/main.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,6 @@ func main() {
152152
app.runsExpirer.Run(ctx, &biz.WorkflowRunExpirerOpts{CheckInterval: 1 * time.Minute, ExpirationWindow: 1 * time.Hour})
153153

154154
// Since policies management is not enabled yet but instead is based on a hardcoded list of permissions
155-
// We'll perform a reconciliation of the policies with the tokens stored in the database on startup
156-
// This will allow us to add more policies in the future and keep backwards compatibility with existing tokens
157-
go func() {
158-
if err := app.tokenAuthSyncer.SyncPolicies(); err != nil {
159-
_ = logger.Log(log.LevelError, "msg", "syncing policies", "error", err)
160-
}
161-
}()
162-
163155
// Sync user access
164156
go func() {
165157
if err := app.userAccessSyncer.SyncUserAccess(ctx); err != nil {

app/controlplane/cmd/wire.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func wireApp(*conf.Bootstrap, credentials.ReaderWriter, log.Logger, sdk.Availabl
5353
wire.FieldsOf(new(*conf.Bootstrap), "Server", "Auth", "Data", "CasServer", "ReferrerSharedIndex", "Onboarding", "PrometheusIntegration", "PolicyProviders", "NatsServer", "FederatedAuthentication"),
5454
wire.FieldsOf(new(*conf.Data), "Database"),
5555
dispatcher.New,
56-
authz.NewDatabaseEnforcer,
56+
authz.NewInMemoryEnforcer,
5757
policies.NewRegistry,
5858
newApp,
5959
newProtoValidator,

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/internal/usercontext/apitoken_middleware.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ func setCurrentOrgAndAPIToken(ctx context.Context, apiTokenUC *biz.APITokenUseCa
202202
Token: token.JWT,
203203
ProjectID: token.ProjectID,
204204
ProjectName: token.ProjectName,
205+
Policies: token.Policies,
205206
})
206207

207208
// Set the authorization subject that will be used to check the policies

app/controlplane/internal/usercontext/apitoken_middleware_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright 2024 The Chainloop Authors.
2+
// Copyright 2024-2025 The Chainloop Authors.
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -24,7 +24,7 @@ import (
2424

2525
"github.com/chainloop-dev/chainloop/app/controlplane/internal/usercontext/entities"
2626
"github.com/chainloop-dev/chainloop/app/controlplane/pkg/biz"
27-
bizMocks "github.com/chainloop-dev/chainloop/app/controlplane/pkg/biz/mocks"
27+
"github.com/chainloop-dev/chainloop/app/controlplane/pkg/biz/mocks"
2828
"github.com/chainloop-dev/chainloop/app/controlplane/pkg/jwt/apitoken"
2929
"github.com/go-kratos/kratos/v2/log"
3030
jwtmiddleware "github.com/go-kratos/kratos/v2/middleware/auth/jwt"
@@ -97,8 +97,8 @@ func TestWithCurrentAPITokenAndOrgMiddleware(t *testing.T) {
9797
wantToken := &biz.APIToken{ID: uuid.New(), OrganizationID: wantOrgID}
9898

9999
t.Run(tc.name, func(t *testing.T) {
100-
apiTokenRepo := bizMocks.NewAPITokenRepo(t)
101-
orgRepo := bizMocks.NewOrganizationRepo(t)
100+
apiTokenRepo := mocks.NewAPITokenRepo(t)
101+
orgRepo := mocks.NewOrganizationRepo(t)
102102
apiTokenUC, err := biz.NewAPITokenUseCase(apiTokenRepo, &biz.APITokenJWTConfig{SymmetricHmacKey: "test"}, nil, nil, nil, nil)
103103
require.NoError(t, err)
104104
orgUC := biz.NewOrganizationUseCase(orgRepo, nil, nil, nil, nil, nil, nil)

app/controlplane/internal/usercontext/entities/apitoken.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"context"
2020
"time"
2121

22+
"github.com/chainloop-dev/chainloop/app/controlplane/pkg/authz"
2223
"github.com/google/uuid"
2324
)
2425

@@ -30,6 +31,8 @@ type APIToken struct {
3031
Token string
3132
ProjectID *uuid.UUID
3233
ProjectName *string
34+
// ACL policies for this token. Used for authorization checks.
35+
Policies []*authz.Policy
3336
}
3437

3538
func WithCurrentAPIToken(ctx context.Context, token *APIToken) context.Context {

app/controlplane/pkg/authz/authz_integration_test.go

Lines changed: 0 additions & 91 deletions
This file was deleted.

app/controlplane/pkg/authz/authz_test.go

Lines changed: 1 addition & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright 2024 The Chainloop Authors.
2+
// Copyright 2024-2025 The Chainloop Authors.
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -16,92 +16,14 @@
1616
package authz
1717

1818
import (
19-
"fmt"
2019
"io"
2120
"os"
2221
"testing"
2322

24-
"github.com/google/uuid"
2523
"github.com/stretchr/testify/assert"
2624
"github.com/stretchr/testify/require"
2725
)
2826

29-
func TestAddPolicies(t *testing.T) {
30-
testcases := []struct {
31-
name string
32-
subject *SubjectAPIToken
33-
policies []*Policy
34-
wantErr bool
35-
wantNumberPolicies int
36-
}{
37-
{
38-
name: "empty policies and subject",
39-
wantErr: true,
40-
},
41-
{
42-
name: "no subject",
43-
policies: []*Policy{
44-
PolicyWorkflowContractList,
45-
},
46-
wantErr: true,
47-
},
48-
{
49-
name: "no policies",
50-
subject: &SubjectAPIToken{ID: uuid.NewString()},
51-
wantErr: true,
52-
},
53-
{
54-
name: "adds two policies",
55-
subject: &SubjectAPIToken{ID: uuid.NewString()},
56-
policies: []*Policy{
57-
PolicyWorkflowContractList,
58-
PolicyReferrerRead,
59-
},
60-
wantNumberPolicies: 2,
61-
},
62-
{
63-
name: "handles duplicated policies",
64-
subject: &SubjectAPIToken{
65-
ID: uuid.NewString(),
66-
},
67-
policies: []*Policy{
68-
PolicyWorkflowContractList,
69-
PolicyWorkflowContractRead,
70-
PolicyWorkflowContractUpdate,
71-
PolicyWorkflowContractList,
72-
PolicyArtifactDownload,
73-
PolicyWorkflowContractUpdate,
74-
PolicyArtifactDownload,
75-
},
76-
wantNumberPolicies: 4,
77-
},
78-
}
79-
80-
for _, tc := range testcases {
81-
t.Run(tc.name, func(t *testing.T) {
82-
enforcer, closer := testEnforcer(t)
83-
closer.Close()
84-
85-
err := enforcer.AddPolicies(tc.subject, tc.policies...)
86-
if tc.wantErr {
87-
assert.Error(t, err)
88-
return
89-
}
90-
91-
require.NoError(t, err)
92-
93-
for _, p := range tc.policies {
94-
ok, err := enforcer.HasPolicy(tc.subject.String(), p.Resource, p.Action)
95-
assert.NoError(t, err)
96-
assert.True(t, ok, fmt.Sprintf("policy %s:%s not found", p.Resource, p.Action))
97-
}
98-
99-
gotLength, err := enforcer.GetFilteredPolicy(0, tc.subject.String())
100-
assert.NoError(t, err)
101-
assert.Len(t, gotLength, tc.wantNumberPolicies)
102-
})
103-
}
104-
}
10527

10628
// simulate 2 enforcers on the same database (by acting on the same file enforcer)
10729
func TestSyncMultipleEnforcers(t *testing.T) {
@@ -219,31 +141,6 @@ func TestSyncMultipleEnforcers(t *testing.T) {
219141
}
220142
}
221143

222-
func TestAddPoliciesDuplication(t *testing.T) {
223-
want := []*Policy{
224-
PolicyWorkflowContractList,
225-
PolicyWorkflowContractRead,
226-
}
227-
228-
enforcer, closer := testEnforcer(t)
229-
defer closer.Close()
230-
sub := &SubjectAPIToken{ID: uuid.NewString()}
231-
232-
err := enforcer.AddPolicies(sub, want...)
233-
require.NoError(t, err)
234-
got, err := enforcer.GetFilteredPolicy(0, sub.String())
235-
require.NoError(t, err)
236-
assert.Len(t, got, 2)
237-
238-
// Update the list of policies we want to add by appending an extra one
239-
want = append(want, PolicyWorkflowContractUpdate)
240-
// AddPolicies only add the policies that are not already present preventing duplication
241-
err = enforcer.AddPolicies(sub, want...)
242-
assert.NoError(t, err)
243-
got, err = enforcer.GetFilteredPolicy(0, sub.String())
244-
assert.NoError(t, err)
245-
assert.Len(t, got, 3)
246-
}
247144

248145
func TestSyncRBACRoles(t *testing.T) {
249146
e, closer := testEnforcer(t)
@@ -346,39 +243,6 @@ func TestDoSync(t *testing.T) {
346243
assert.Len(t, got, 2)
347244
}
348245

349-
func TestClearPolicies(t *testing.T) {
350-
want := []*Policy{
351-
PolicyWorkflowContractList,
352-
PolicyWorkflowContractRead,
353-
}
354-
355-
enforcer, closer := testEnforcer(t)
356-
defer closer.Close()
357-
sub := &SubjectAPIToken{ID: uuid.NewString()}
358-
sub2 := &SubjectAPIToken{ID: uuid.NewString()}
359-
360-
// Create policies for two different subjects
361-
err := enforcer.AddPolicies(sub, want...)
362-
require.NoError(t, err)
363-
err = enforcer.AddPolicies(sub2, want...)
364-
require.NoError(t, err)
365-
// Each have 2 items
366-
got, err := enforcer.GetFilteredPolicy(0, sub.String())
367-
require.NoError(t, err)
368-
assert.Len(t, got, 2)
369-
370-
// Clear all the policies for the subject
371-
err = enforcer.ClearPolicies(sub)
372-
assert.NoError(t, err)
373-
// there should be no policies left for this user
374-
got, err = enforcer.GetFilteredPolicy(0, sub.String())
375-
require.NoError(t, err)
376-
assert.Len(t, got, 0)
377-
// but the other user should still have 2
378-
got, err = enforcer.GetFilteredPolicy(0, sub2.String())
379-
require.NoError(t, err)
380-
assert.Len(t, got, 2)
381-
}
382246

383247
func testEnforcer(t *testing.T) (*Enforcer, io.Closer) {
384248
f, err := os.CreateTemp(t.TempDir(), "policy*.csv")

0 commit comments

Comments
 (0)