From 947b0ce4377464bc3124e986a80d0776e4f80ace Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Fri, 25 Oct 2024 17:52:00 +0200 Subject: [PATCH] Implement command revoke-token. --- galenectl/galenectl.go | 46 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/galenectl/galenectl.go b/galenectl/galenectl.go index 64ce265..a9197be 100644 --- a/galenectl/galenectl.go +++ b/galenectl/galenectl.go @@ -82,11 +82,15 @@ var commands = map[string]command{ }, "create-token": { command: createTokenCmd, - description: "create token", + description: "request a token", + }, + "revoke-token": { + command: revokeTokenCmd, + description: "revoke a token", }, "delete-token": { command: deleteTokenCmd, - description: "delete token", + description: "delete a token", }, } @@ -910,6 +914,44 @@ func createTokenCmd(cmdname string, args []string) { println(location) } +func revokeTokenCmd(cmdname string, args []string) { + var groupname, token string + cmd := flag.NewFlagSet(cmdname, flag.ExitOnError) + setUsage(cmd, cmdname, "%v [option...] %v [option...]\n", + os.Args[0], cmdname, + ) + cmd.StringVar(&groupname, "group", "", "group `name`") + cmd.StringVar(&token, "token", "", "`token` to delete") + cmd.Parse(args) + + if cmd.NArg() != 0 { + cmd.Usage() + os.Exit(1) + } + + if groupname == "" || token == "" { + fmt.Fprintf(cmd.Output(), + "Options \"-group\" and \"-token\" are required\n") + os.Exit(1) + } + + u, err := url.JoinPath( + serverURL, "/galene-api/v0/.groups/", groupname, + ".tokens", token, + ) + if err != nil { + log.Fatalf("Build URL: %v", err) + } + + err = updateJSON(u, func(v map[string]any) map[string]any { + v["expires"] = time.Now().Add(-time.Minute) + return v + }) + if err != nil { + log.Fatalf("Update token: %v", err) + } +} + func deleteTokenCmd(cmdname string, args []string) { var groupname, token string cmd := flag.NewFlagSet(cmdname, flag.ExitOnError)