Skip to content

Commit 3133343

Browse files
authored
(MAINT) Add revoke token ability to RBAC APIs. (#89)
1 parent c5ba452 commit 3133343

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

pkg/rbac/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func NewClient(hostURL string, tlsConfig *tls.Config) *Client {
3939
type APIError struct {
4040
Kind string `json:"kind"`
4141
Msg string `json:"msg"`
42+
Details string `json:"details"`
4243
StatusCode int
4344
}
4445

pkg/rbac/common_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ func setUpOKResponder(t *testing.T, httpMethod string, path string, responseFile
3030
response.Body.Close()
3131
}
3232

33+
func setUpOKDeleteResponder(path string) {
34+
httpmock.Reset()
35+
36+
response := httpmock.NewStringResponse(http.StatusOK, `{}`)
37+
response.Header.Set("Content-Type", "application/json")
38+
39+
httpmock.RegisterResponder(http.MethodDelete, rbacAPIOrigin+path, httpmock.ResponderFromResponse(response))
40+
response.Body.Close()
41+
}
42+
3343
func setUpBadRequestResponder(t *testing.T, httpMethod string, path string) {
3444
httpmock.Reset()
3545

pkg/rbac/token.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package rbac
22

3+
import "fmt"
4+
35
const (
46
requestAuthTokenURI = "/rbac-api/v1/auth/token" // #nosec - this is the uri to g et RBAC tokens
57
tokenAuthenticateURI = "/rbac-api/v2/auth/token/authenticate" // #nosec - this is the uri to authenticate RBAC tokens
8+
tokenRevokeURI = "/rbac-api/v2/tokens/" // #nosec - this is the uri to revoke individual RBAC tokens
69
)
710

811
// GetRBACToken returns an auth token given user/password information
@@ -45,6 +48,24 @@ func (c *Client) AuthenticateRBACToken(token string) (*AuthenticateResponse, err
4548
return &payload, nil
4649
}
4750

51+
func (c *Client) RevokeRBACToken(token string) error {
52+
payload := AuthenticateResponse{}
53+
54+
r, err := c.resty.R().
55+
SetResult(&payload).
56+
Delete(fmt.Sprintf("%s%s", tokenRevokeURI, token))
57+
if err != nil {
58+
return FormatError(r, err.Error())
59+
}
60+
if r.IsError() {
61+
if r.Error() != nil {
62+
return FormatError(r)
63+
}
64+
return FormatError(r)
65+
}
66+
return nil
67+
}
68+
4869
// Token is the returned auth token
4970
type Token struct {
5071
Token string `json:"token"`

pkg/rbac/token_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package rbac
22

33
import (
4+
"fmt"
45
"net/http"
56
"testing"
67

@@ -49,3 +50,18 @@ func TestAuthenticateRBACToken(t *testing.T) {
4950
require.Nil(t, actual)
5051
require.Equal(t, expectedError, err)
5152
}
53+
54+
func TestRevokeRBACToken(t *testing.T) {
55+
tokenValue := "abc"
56+
57+
// Test success
58+
setUpOKDeleteResponder(fmt.Sprintf("%s%s", tokenRevokeURI, tokenValue))
59+
60+
err := rbacClient.RevokeRBACToken(tokenValue)
61+
require.Nil(t, err)
62+
63+
// Test error
64+
setUpBadRequestResponder(t, http.MethodDelete, fmt.Sprintf("%s%s", tokenRevokeURI, tokenValue))
65+
err = rbacClient.RevokeRBACToken(tokenValue)
66+
require.Equal(t, expectedError, err)
67+
}

0 commit comments

Comments
 (0)