Skip to content

Commit 7206670

Browse files
authored
feat(api-tokens): replace include_revoked boolean with StatusFilter enum (#2746)
Signed-off-by: Miguel Martinez <miguel@chainloop.dev>
1 parent 20a3b78 commit 7206670

13 files changed

Lines changed: 381 additions & 61 deletions

File tree

app/cli/cmd/organization_apitoken_list.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright 2023-2025 The Chainloop Authors.
2+
// Copyright 2023-2026 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.
@@ -28,6 +28,7 @@ import (
2828
func newAPITokenListCmd() *cobra.Command {
2929
var (
3030
includeRevoked bool
31+
statusFilter string
3132
project string
3233
scope string
3334
)
@@ -37,6 +38,12 @@ func newAPITokenListCmd() *cobra.Command {
3738
"global",
3839
}
3940

41+
var availableStatusFilters = []string{
42+
"active",
43+
"revoked",
44+
"all",
45+
}
46+
4047
cmd := &cobra.Command{
4148
Use: "list",
4249
Aliases: []string{"ls"},
@@ -46,10 +53,22 @@ func newAPITokenListCmd() *cobra.Command {
4653
return fmt.Errorf("invalid scope %q, please chose one of: %v", scope, availableScopes)
4754
}
4855

56+
if statusFilter != "" && !slices.Contains(availableStatusFilters, statusFilter) {
57+
return fmt.Errorf("invalid status %q, please choose one of: %v", statusFilter, availableStatusFilters)
58+
}
59+
4960
return nil
5061
},
5162
RunE: func(cmd *cobra.Command, args []string) error {
52-
res, err := action.NewAPITokenList(ActionOpts).Run(context.Background(), includeRevoked, project, scope)
63+
// --all is deprecated: map it to --status all
64+
if includeRevoked {
65+
cmd.PrintErr("Warning: --all is deprecated, use --status all instead\n")
66+
if statusFilter == "" {
67+
statusFilter = "all"
68+
}
69+
}
70+
71+
res, err := action.NewAPITokenList(ActionOpts).Run(context.Background(), statusFilter, project, scope)
5372
if err != nil {
5473
return fmt.Errorf("listing API tokens: %w", err)
5574
}
@@ -58,7 +77,11 @@ func newAPITokenListCmd() *cobra.Command {
5877
},
5978
}
6079

61-
cmd.Flags().BoolVarP(&includeRevoked, "all", "a", false, "show all API tokens including revoked ones")
80+
cmd.Flags().BoolVarP(&includeRevoked, "all", "a", false, "Deprecated: use --status all instead")
81+
if err := cmd.Flags().MarkDeprecated("all", "use --status all instead"); err != nil {
82+
panic(err)
83+
}
84+
cmd.Flags().StringVar(&statusFilter, "status", "", fmt.Sprintf("filter by token status, available values: %v", availableStatusFilters))
6285
cmd.Flags().StringVarP(&project, "project", "p", "", "filter by project name")
6386
cmd.Flags().StringVarP(&scope, "scope", "s", "", fmt.Sprintf("filter by scope, available scopes: %v", availableScopes))
6487
return cmd

app/cli/documentation/cli-reference.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2139,10 +2139,10 @@ chainloop organization api-token list [flags]
21392139
Options
21402140

21412141
```
2142-
-a, --all show all API tokens including revoked ones
21432142
-h, --help help for list
21442143
-p, --project string filter by project name
21452144
-s, --scope string filter by scope, available scopes: [project global]
2145+
--status string filter by token status, available values: [active revoked all]
21462146
```
21472147

21482148
Options inherited from parent commands

app/cli/pkg/action/apitoken_list.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright 2024-2025 The Chainloop Authors.
2+
// Copyright 2024-2026 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.
@@ -30,10 +30,10 @@ func NewAPITokenList(cfg *ActionsOpts) *APITokenList {
3030
return &APITokenList{cfg}
3131
}
3232

33-
func (action *APITokenList) Run(ctx context.Context, includeRevoked bool, project string, scope string) ([]*APITokenItem, error) {
33+
func (action *APITokenList) Run(ctx context.Context, statusFilter string, project string, scope string) ([]*APITokenItem, error) {
3434
client := pb.NewAPITokenServiceClient(action.cfg.CPConnection)
3535

36-
req := &pb.APITokenServiceListRequest{IncludeRevoked: includeRevoked}
36+
req := &pb.APITokenServiceListRequest{StatusFilter: mapStatusFilter(statusFilter)}
3737
if project != "" {
3838
req.Project = &pb.IdentityReference{Name: &project}
3939
}
@@ -65,3 +65,14 @@ func mapScope(scope string) pb.APITokenServiceListRequest_Scope {
6565
return pb.APITokenServiceListRequest_SCOPE_UNSPECIFIED
6666
}
6767
}
68+
69+
func mapStatusFilter(status string) pb.APITokenServiceListRequest_StatusFilter {
70+
switch status {
71+
case "revoked":
72+
return pb.APITokenServiceListRequest_STATUS_FILTER_REVOKED
73+
case "all":
74+
return pb.APITokenServiceListRequest_STATUS_FILTER_ALL
75+
default:
76+
return pb.APITokenServiceListRequest_STATUS_FILTER_ACTIVE
77+
}
78+
}

app/controlplane/api/controlplane/v1/api_token.pb.go

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

app/controlplane/api/controlplane/v1/api_token.proto

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright 2024-2025 The Chainloop Authors.
2+
// Copyright 2024-2026 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.
@@ -55,7 +55,8 @@ message APITokenServiceRevokeRequest {
5555
message APITokenServiceRevokeResponse {}
5656

5757
message APITokenServiceListRequest {
58-
bool include_revoked = 1;
58+
// Deprecated: use status_filter instead.
59+
bool include_revoked = 1 [deprecated = true];
5960

6061
// optional project reference to filter by
6162
IdentityReference project = 4;
@@ -68,6 +69,21 @@ message APITokenServiceListRequest {
6869

6970
// filter by the scope of the token
7071
Scope scope = 2;
72+
73+
// buf:lint:ignore ENUM_ZERO_VALUE_SUFFIX
74+
// STATUS_FILTER_ACTIVE = 0 is intentional: active tokens are the default,
75+
// matching proto3 zero-value semantics and preserving backward compatibility.
76+
enum StatusFilter {
77+
// Only active (non-revoked) tokens. This is the default.
78+
STATUS_FILTER_ACTIVE = 0;
79+
// Only revoked tokens.
80+
STATUS_FILTER_REVOKED = 1;
81+
// All tokens regardless of revocation status.
82+
STATUS_FILTER_ALL = 2;
83+
}
84+
85+
// Filter tokens by their revocation status. Defaults to STATUS_FILTER_ACTIVE.
86+
StatusFilter status_filter = 5;
7187
}
7288

7389
message APITokenServiceListResponse {

app/controlplane/api/controlplane/v1/api_token_grpc.pb.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.

0 commit comments

Comments
 (0)