mirror of
https://github.com/juanfont/headscale.git
synced 2026-10-06 06:40:06 +09:00
cmd/headscale: resolve api key id to prefix before delete
DeleteApiKey routes the prefix in the path; deleting by --id sent an empty segment and 404'd. Look the prefix up by id first.
This commit is contained in:
@@ -2,6 +2,7 @@ package cli
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
@@ -15,6 +16,8 @@ const (
|
|||||||
DefaultAPIKeyExpiry = "90d"
|
DefaultAPIKeyExpiry = "90d"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var errAPIKeyIDNotFound = errors.New("no api key with id")
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(apiKeysCmd)
|
rootCmd.AddCommand(apiKeysCmd)
|
||||||
apiKeysCmd.AddCommand(listAPIKeys)
|
apiKeysCmd.AddCommand(listAPIKeys)
|
||||||
@@ -144,10 +147,16 @@ var deleteAPIKeyCmd = &cobra.Command{
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = client.DeleteApiKey(ctx, apiv1.DeleteApiKeyParams{
|
// Delete is routed by prefix in the path, so resolve an --id to its
|
||||||
ID: optUint64(id),
|
// prefix first; an empty path segment would 404 at the router.
|
||||||
Prefix: prefix,
|
if prefix == "" {
|
||||||
})
|
prefix, err = apiKeyPrefixByID(ctx, client, id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = client.DeleteApiKey(ctx, apiv1.DeleteApiKeyParams{Prefix: prefix})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("deleting api key: %w", err)
|
return fmt.Errorf("deleting api key: %w", err)
|
||||||
}
|
}
|
||||||
@@ -155,3 +164,19 @@ var deleteAPIKeyCmd = &cobra.Command{
|
|||||||
return printOutput(cmd, map[string]string{colResult: "Key deleted"}, "Key deleted")
|
return printOutput(cmd, map[string]string{colResult: "Key deleted"}, "Key deleted")
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// apiKeyPrefixByID looks up an API key's prefix by its numeric ID.
|
||||||
|
func apiKeyPrefixByID(ctx context.Context, client *apiv1.Client, id uint64) (string, error) {
|
||||||
|
resp, err := client.ListApiKeys(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("listing api keys: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, key := range resp.GetApiKeys() {
|
||||||
|
if key.GetID().Or(0) == id {
|
||||||
|
return key.GetPrefix().Or(""), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", fmt.Errorf("%w: %d", errAPIKeyIDNotFound, id)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user