Skip to content

Commit 9d3a5be

Browse files
VikasMaddukuriHarnessHarness
authored andcommitted
fix: [PL-65144]: add create, revoke and get delegate token support (#188)
* 43ced6 fix: [PL-65226]: add delete support as well and added UTs * 774631 fix: [PL-61154]: rename revoke token tool name * 0b3135 fix: [PL-65144]: add create, revoke and get delegate token support
1 parent 59fab3f commit 9d3a5be

File tree

5 files changed

+770
-25
lines changed

5 files changed

+770
-25
lines changed

client/delegate_token.go

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
const (
11-
delegateTokenListPath = "/delegate-token-ng"
11+
delegateTokenPath = "/delegate-token-ng"
1212
)
1313

1414
type DelegateTokenClient struct {
@@ -37,7 +37,7 @@ func setDefaultPaginationForDelegateToken(opts *dto.DelegateTokenOptions) {
3737
// - []DelegateToken: List of delegate tokens for the current page
3838
// - error: Any error that occurred during the request
3939
func (d *DelegateTokenClient) ListDelegateTokens(ctx context.Context, scope dto.Scope, opts *dto.DelegateTokenOptions) ([]dto.DelegateToken, error) {
40-
path := delegateTokenListPath
40+
path := delegateTokenPath
4141
params := make(map[string]string)
4242
addScope(scope, params)
4343

@@ -73,3 +73,107 @@ func (d *DelegateTokenClient) ListDelegateTokens(ctx context.Context, scope dto.
7373

7474
return response.Resource, nil
7575
}
76+
77+
// GetDelegateToken retrieves a specific delegate token by name.
78+
// Parameters:
79+
// - ctx: Context for the request
80+
// - scope: The scope (account/org/project) for the token
81+
// - name: Name of the delegate token to retrieve
82+
// - status: Optional status filter (ACTIVE/REVOKED)
83+
// Returns:
84+
// - *DelegateToken: The delegate token if found
85+
// - error: Any error that occurred during the request
86+
func (d *DelegateTokenClient) GetDelegateToken(ctx context.Context, scope dto.Scope, name string, status string) ([]dto.DelegateToken, error) {
87+
path := delegateTokenPath
88+
params := make(map[string]string)
89+
params["name"] = name
90+
91+
addScope(scope, params)
92+
93+
if status != "" {
94+
params["status"] = status
95+
}
96+
97+
var response dto.DelegateTokenListResponse
98+
err := d.Client.Get(ctx, path, params, nil, &response)
99+
if err != nil {
100+
return nil, fmt.Errorf("failed to get delegate token: %w", err)
101+
}
102+
if len(response.Resource) == 0 {
103+
return nil, nil
104+
}
105+
106+
return response.Resource, nil
107+
}
108+
109+
// CreateDelegateToken creates a new delegate token with the specified parameters.
110+
// Parameters:
111+
// - ctx: Context for the request
112+
// - scope: The scope (account/org/project) for the token
113+
// - tokenName: Name of the delegate token
114+
// - revokeAfter: Optional epoch time in milliseconds after which the token will be revoked
115+
// Returns:
116+
// - *DelegateToken: The created delegate token
117+
// - error: Any error that occurred during the request
118+
func (d *DelegateTokenClient) CreateDelegateToken(ctx context.Context, scope dto.Scope, tokenName string, revokeAfter *int64) (dto.DelegateToken, error) {
119+
path := delegateTokenPath
120+
params := make(map[string]string)
121+
addScope(scope, params)
122+
params["tokenName"] = tokenName
123+
124+
if revokeAfter != nil {
125+
params["revokeAfter"] = fmt.Sprintf("%d", *revokeAfter)
126+
}
127+
128+
var response dto.DelegateTokenResponse
129+
err := d.Client.Post(ctx, path, params, nil, map[string]string{}, &response)
130+
if err != nil {
131+
return dto.DelegateToken{}, fmt.Errorf("failed to create delegate token: %w", err)
132+
}
133+
134+
return response.Resource, nil
135+
}
136+
137+
// RevokeDelegateToken revokes a delegate token with the specified name.
138+
// Parameters:
139+
// - ctx: Context for the request
140+
// - scope: The scope (account/org/project) for the token
141+
// - tokenName: Name of the delegate token to revoke
142+
// Returns:
143+
// - DelegateToken: The revoked delegate token
144+
// - error: Any error that occurred during the request
145+
func (d *DelegateTokenClient) RevokeDelegateToken(ctx context.Context, scope dto.Scope, tokenName string) (dto.DelegateToken, error) {
146+
path := delegateTokenPath
147+
params := make(map[string]string)
148+
addScope(scope, params)
149+
params["tokenName"] = tokenName
150+
151+
var response dto.DelegateTokenResponse
152+
err := d.Client.Put(ctx, path, params, nil, &response)
153+
if err != nil {
154+
return dto.DelegateToken{}, fmt.Errorf("failed to revoke delegate token: %w", err)
155+
}
156+
157+
return response.Resource, nil
158+
}
159+
160+
// DeleteDelegateToken deletes a revoked delegate token with the specified name.
161+
// Parameters:
162+
// - ctx: Context for the request
163+
// - scope: The scope (account/org/project) for the token
164+
// - tokenName: Name of the delegate token to delete
165+
// Returns:
166+
// - error: Any error that occurred during the request
167+
func (d *DelegateTokenClient) DeleteDelegateToken(ctx context.Context, scope dto.Scope, tokenName string) error {
168+
path := delegateTokenPath
169+
params := make(map[string]string)
170+
addScope(scope, params)
171+
params["tokenName"] = tokenName
172+
173+
err := d.Client.Delete(ctx, path, params, nil, nil)
174+
if err != nil {
175+
return fmt.Errorf("failed to delete delegate token. Please make sure the token exists and is REVOKED: %w", err)
176+
}
177+
178+
return nil
179+
}

client/dto/delegate_token.go

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,23 @@ const (
5959

6060
// DelegateToken represents a delegate token
6161
type DelegateToken struct {
62-
UUID *string `json:"uuid"`
63-
AccountID string `json:"accountId"`
64-
Name string `json:"name"`
65-
CreatedBy *string `json:"createdBy"`
66-
CreatedByNGUser *Principal `json:"createdByNgUser"`
67-
CreatedAt int64 `json:"createdAt"`
68-
CreatedAtTime string `json:"createdAtTime"`
69-
Status string `json:"status"`
70-
Value interface{} `json:"value"`
71-
OwnerIdentifier string `json:"ownerIdentifier"`
72-
ParentUniqueID string `json:"parentUniqueId"`
73-
RevokeAfter int64 `json:"revokeAfter"`
74-
RevokeAfterTime string `json:"revokeAfterTime"`
75-
IsNG bool `json:"isNg"`
76-
LastUsedAt int64 `json:"lastUsedAt"`
77-
LastUsedAtTime string `json:"lastUsedAtTime"`
78-
TokenHash string `json:"tokenHash"`
62+
UUID *string `json:"uuid"`
63+
AccountID string `json:"accountId"`
64+
Name string `json:"name"`
65+
CreatedBy *string `json:"createdBy"`
66+
CreatedByNGUser *Principal `json:"createdByNgUser"`
67+
CreatedAt int64 `json:"createdAt"`
68+
CreatedAtTime string `json:"createdAtTime"`
69+
Status string `json:"status"`
70+
Value string `json:"value"`
71+
OwnerIdentifier string `json:"ownerIdentifier"`
72+
ParentUniqueID string `json:"parentUniqueId"`
73+
RevokeAfter int64 `json:"revokeAfter"`
74+
RevokeAfterTime string `json:"revokeAfterTime"`
75+
IsNG bool `json:"isNg"`
76+
LastUsedAt int64 `json:"lastUsedAt"`
77+
LastUsedAtTime string `json:"lastUsedAtTime"`
78+
TokenHash string `json:"tokenHash"`
7979
}
8080

8181
// FormatTimestamps formats the Unix timestamps into human-readable format
@@ -91,8 +91,16 @@ func (d *DelegateToken) FormatTimestamps() {
9191

9292
// DelegateTokenListResponse represents the response from the list delegate tokens API
9393
type DelegateTokenListResponse struct {
94-
MetaData interface{} `json:"metaData"`
95-
Resource []DelegateToken `json:"resource"`
94+
MetaData interface{} `json:"metaData"`
95+
Resource []DelegateToken `json:"resource"`
96+
ResponseMessages []string `json:"responseMessages"`
97+
}
98+
99+
// DelegateTokenResponse represents the response from the create/get delegate token API
100+
type DelegateTokenResponse struct {
101+
MetaData map[string]interface{} `json:"metaData"`
102+
Resource DelegateToken `json:"resource"`
103+
ResponseMessages []string `json:"responseMessages"`
96104
}
97105

98106
// DelegateTokenOptions represents the options for listing delegate tokens

0 commit comments

Comments
 (0)