Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions app/controlplane/pkg/biz/apitoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,11 @@ func (uc *APITokenUseCase) Create(ctx context.Context, name string, description
return nil, err
}

// Use provided policies if present, otherwise use defaults
// Explicit policies are stored as given, whatever the scope. Only a token created without
// them gets the defaults, plus the organization-level policies when it is organization-wide.
policies := options.policies
if policies == nil {
explicitPolicies := policies != nil
if !explicitPolicies {
policies = uc.DefaultAuthzPolicies
}

Expand All @@ -522,7 +524,7 @@ func (uc *APITokenUseCase) Create(ctx context.Context, name string, description
}

// Concat, not append: policies may alias the shared defaultAuthzPolicies slice.
if *scope == authz.ResourceTypeOrganization {
if *scope == authz.ResourceTypeOrganization && !explicitPolicies {
policies = slices.Concat(policies, orgLevelTokenPolicies)
}

Expand Down
28 changes: 23 additions & 5 deletions app/controlplane/pkg/biz/apitoken_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1192,11 +1192,29 @@ func (s *apiTokenTestSuite) TestExplicitPoliciesAreNotWidenedForScopedTokens() {
ctx := context.Background()
explicit := []*authz.Policy{authz.PolicyWorkflowRunRead}

scoped, err := s.APIToken.Create(ctx, randomName(), nil, nil, &s.org.ID,
biz.APITokenWithPolicies(explicit),
biz.APITokenWithScope(authz.ResourceTypeProduct, biz.ToPtr(uuid.New())), biz.APITokenWithProjectIDs(nil))
s.Require().NoError(err)
s.ElementsMatch(explicit, scoped.Policies)
testCases := []struct {
name string
opts []biz.APITokenCreateOpt
}{
{name: "organization-scoped token"},
{name: "project-scoped token", opts: []biz.APITokenCreateOpt{biz.APITokenWithProject(s.p1)}},
{
name: "product-scoped token",
opts: []biz.APITokenCreateOpt{
biz.APITokenWithScope(authz.ResourceTypeProduct, biz.ToPtr(uuid.New())),
biz.APITokenWithProjectIDs(nil),
},
},
}

for _, tc := range testCases {
s.Run(tc.name, func() {
opts := append([]biz.APITokenCreateOpt{biz.APITokenWithPolicies(explicit)}, tc.opts...)
token, err := s.APIToken.Create(ctx, randomName(), nil, nil, &s.org.ID, opts...)
s.Require().NoError(err)
s.ElementsMatch(explicit, token.Policies)
})
}
}

// A product token is created with the projects it reaches, stored deduplicated and sorted.
Expand Down
Loading