Skip to content

Commit b42f9df

Browse files
committed
Add auth revoke command
1 parent f8bb149 commit b42f9df

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
Added:
66
* Adds support for pool PriorityThresholds in `v1/config/ResourcePools`
7-
* Adds `chronoctl auth whoami` which prints the current user
7+
* Adds `chronoctl auth whoami` which prints the current user
8+
* Adds `chronoctl auth revoke` which revokes a session id
89

910
## v1.11.0
1011

src/cmd/pkg/auth/auth.go

+34
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package auth
1818
import (
1919
"encoding/json"
2020
"fmt"
21+
"io"
2122
"io/ioutil"
2223
"net/http"
2324

@@ -51,6 +52,7 @@ func NewCommand() *cobra.Command {
5152
c.newPrintAccessTokenCmd(),
5253
c.newListCmd(),
5354
c.newWhoAmICmd(),
55+
c.newRevokeCmd(),
5456
)
5557
return root
5658
}
@@ -234,3 +236,35 @@ func (c *subcommand) newWhoAmICmd() *cobra.Command {
234236
authFlags.AddFlags(cmd)
235237
return cmd
236238
}
239+
240+
func (c *subcommand) newRevokeCmd() *cobra.Command {
241+
authFlags := client.NewClientFlags()
242+
cmd := &cobra.Command{
243+
Use: "revoke",
244+
GroupID: groups.Commands.ID,
245+
Short: "Revoke a session id.",
246+
RunE: func(cmd *cobra.Command, args []string) error {
247+
req, err := authFlags.NewRequest(http.MethodGet, "/auth/logout", nil /* body */)
248+
if err != nil {
249+
return errors.WithStack(err)
250+
}
251+
client := http.Client{}
252+
resp, err := client.Do(req)
253+
if err != nil {
254+
return errors.WithStack(err)
255+
}
256+
defer resp.Body.Close() //nolint: errcheck
257+
body, err := io.ReadAll(resp.Body)
258+
if err != nil {
259+
return errors.WithStack(err)
260+
}
261+
if resp.StatusCode != http.StatusOK {
262+
return errors.Errorf("%d: %s", resp.StatusCode, string(body))
263+
}
264+
cmd.OutOrStdout().Write(body) //nolint: errcheck
265+
return nil
266+
},
267+
}
268+
authFlags.AddFlags(cmd)
269+
return cmd
270+
}

src/cmd/pkg/auth/auth_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -651,3 +651,30 @@ func TestAuthWhoAmI(t *testing.T) {
651651
}
652652

653653
}
654+
655+
func TestAuthRevoke(t *testing.T) {
656+
apiToken := "test-token"
657+
658+
requestReceived := false
659+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
660+
requestReceived = true
661+
require.Equal(t, http.MethodGet, r.Method)
662+
require.Equal(t, "/auth/logout", r.URL.Path)
663+
require.Equal(t, apiToken, r.Header.Get("API-Token"))
664+
665+
w.WriteHeader(http.StatusOK)
666+
}))
667+
t.Cleanup(ts.Close)
668+
669+
store := token.NewFileStore(t.TempDir())
670+
c := subcommand{store: store}
671+
cmd := c.newRevokeCmd()
672+
673+
require.NoError(t, cmd.Flags().Set("api-url", ts.URL+"/auth/logout"))
674+
require.NoError(t, cmd.Flags().Set("api-token", apiToken))
675+
676+
stdout := &bytes.Buffer{}
677+
cmd.SetOut(stdout)
678+
require.NoError(t, cmd.Execute())
679+
require.True(t, requestReceived)
680+
}

0 commit comments

Comments
 (0)