2021-04-28 16:15:45 +02:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2023-05-11 09:09:18 +02:00
|
|
|
"errors"
|
2021-04-28 16:15:45 +02:00
|
|
|
"fmt"
|
2024-12-19 13:10:10 +01:00
|
|
|
"net/url"
|
2021-04-28 16:15:45 +02:00
|
|
|
|
2021-11-13 20:01:05 +01:00
|
|
|
survey "github.com/AlecAivazis/survey/v2"
|
2021-11-04 23:42:21 +01:00
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
2021-08-15 23:20:38 +02:00
|
|
|
"github.com/pterm/pterm"
|
2021-10-29 19:16:54 +02:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-04-28 16:15:45 +02:00
|
|
|
"github.com/spf13/cobra"
|
2021-11-07 11:15:32 +01:00
|
|
|
"google.golang.org/grpc/status"
|
2021-04-28 16:15:45 +02:00
|
|
|
)
|
|
|
|
|
2024-12-10 16:23:55 +01:00
|
|
|
func usernameAndIDFlag(cmd *cobra.Command) {
|
|
|
|
cmd.Flags().Int64P("identifier", "i", -1, "User identifier (ID)")
|
|
|
|
cmd.Flags().StringP("name", "n", "", "Username")
|
|
|
|
}
|
|
|
|
|
|
|
|
// usernameAndIDFromFlag returns the username and ID from the flags of the command.
|
|
|
|
// If both are empty, it will exit the program with an error.
|
|
|
|
func usernameAndIDFromFlag(cmd *cobra.Command) (uint64, string) {
|
|
|
|
username, _ := cmd.Flags().GetString("name")
|
|
|
|
identifier, _ := cmd.Flags().GetInt64("identifier")
|
|
|
|
if username == "" && identifier < 0 {
|
|
|
|
err := errors.New("--name or --identifier flag is required")
|
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf(
|
|
|
|
"Cannot rename user: %s",
|
|
|
|
status.Convert(err).Message(),
|
|
|
|
),
|
|
|
|
"",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return uint64(identifier), username
|
|
|
|
}
|
|
|
|
|
2021-07-25 15:03:45 +02:00
|
|
|
func init() {
|
2023-01-17 17:43:44 +01:00
|
|
|
rootCmd.AddCommand(userCmd)
|
|
|
|
userCmd.AddCommand(createUserCmd)
|
2024-12-19 13:10:10 +01:00
|
|
|
createUserCmd.Flags().StringP("display-name", "d", "", "Display name")
|
|
|
|
createUserCmd.Flags().StringP("email", "e", "", "Email")
|
|
|
|
createUserCmd.Flags().StringP("picture-url", "p", "", "Profile picture URL")
|
2023-01-17 17:43:44 +01:00
|
|
|
userCmd.AddCommand(listUsersCmd)
|
2024-12-10 16:23:55 +01:00
|
|
|
usernameAndIDFlag(listUsersCmd)
|
|
|
|
listUsersCmd.Flags().StringP("email", "e", "", "Email")
|
2023-01-17 17:43:44 +01:00
|
|
|
userCmd.AddCommand(destroyUserCmd)
|
2024-12-10 16:23:55 +01:00
|
|
|
usernameAndIDFlag(destroyUserCmd)
|
2023-01-17 17:43:44 +01:00
|
|
|
userCmd.AddCommand(renameUserCmd)
|
2024-12-10 16:23:55 +01:00
|
|
|
usernameAndIDFlag(renameUserCmd)
|
|
|
|
renameUserCmd.Flags().StringP("new-name", "r", "", "New username")
|
|
|
|
renameNodeCmd.MarkFlagRequired("new-name")
|
2021-07-25 15:03:45 +02:00
|
|
|
}
|
|
|
|
|
2023-05-11 09:09:18 +02:00
|
|
|
var errMissingParameter = errors.New("missing parameters")
|
2021-11-15 20:18:14 +01:00
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
var userCmd = &cobra.Command{
|
|
|
|
Use: "users",
|
|
|
|
Short: "Manage the users of Headscale",
|
2023-01-26 08:41:21 +01:00
|
|
|
Aliases: []string{"user", "namespace", "namespaces", "ns"},
|
2021-04-28 16:15:45 +02:00
|
|
|
}
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
var createUserCmd = &cobra.Command{
|
2022-03-02 05:35:20 +01:00
|
|
|
Use: "create NAME",
|
2023-01-17 17:43:44 +01:00
|
|
|
Short: "Creates a new user",
|
2022-03-02 05:35:20 +01:00
|
|
|
Aliases: []string{"c", "new"},
|
2021-04-28 16:15:45 +02:00
|
|
|
Args: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) < 1 {
|
2021-11-15 20:18:14 +01:00
|
|
|
return errMissingParameter
|
2021-04-28 16:15:45 +02:00
|
|
|
}
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-04-28 16:15:45 +02:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-11-04 23:42:21 +01:00
|
|
|
output, _ := cmd.Flags().GetString("output")
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
userName := args[0]
|
2021-10-29 19:16:54 +02:00
|
|
|
|
2024-09-07 09:23:58 +02:00
|
|
|
ctx, client, conn, cancel := newHeadscaleCLIWithConfig()
|
2021-10-31 17:26:51 +01:00
|
|
|
defer cancel()
|
2021-10-29 19:16:54 +02:00
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
log.Trace().Interface("client", client).Msg("Obtained gRPC client")
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
request := &v1.CreateUserRequest{Name: userName}
|
2021-10-29 19:16:54 +02:00
|
|
|
|
2024-12-19 13:10:10 +01:00
|
|
|
if displayName, _ := cmd.Flags().GetString("display-name"); displayName != "" {
|
|
|
|
request.DisplayName = displayName
|
|
|
|
}
|
|
|
|
|
|
|
|
if email, _ := cmd.Flags().GetString("email"); email != "" {
|
|
|
|
request.Email = email
|
|
|
|
}
|
|
|
|
|
|
|
|
if pictureURL, _ := cmd.Flags().GetString("picture-url"); pictureURL != "" {
|
|
|
|
if _, err := url.Parse(pictureURL); err != nil {
|
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf(
|
|
|
|
"Invalid Picture URL: %s",
|
|
|
|
err,
|
|
|
|
),
|
|
|
|
output,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
request.PictureUrl = pictureURL
|
|
|
|
}
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
log.Trace().Interface("request", request).Msg("Sending CreateUser request")
|
|
|
|
response, err := client.CreateUser(ctx, request)
|
2021-04-28 16:15:45 +02:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf(
|
2023-01-17 17:43:44 +01:00
|
|
|
"Cannot create user: %s",
|
2021-11-13 09:36:45 +01:00
|
|
|
status.Convert(err).Message(),
|
|
|
|
),
|
|
|
|
output,
|
|
|
|
)
|
2021-04-28 16:15:45 +02:00
|
|
|
}
|
2021-11-04 23:42:21 +01:00
|
|
|
|
2023-11-23 08:31:33 +01:00
|
|
|
SuccessOutput(response.GetUser(), "User created", output)
|
2021-04-28 16:15:45 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
var destroyUserCmd = &cobra.Command{
|
2024-12-10 16:23:55 +01:00
|
|
|
Use: "destroy --identifier ID or --name NAME",
|
2023-01-17 17:43:44 +01:00
|
|
|
Short: "Destroys a user",
|
2022-03-02 05:35:20 +01:00
|
|
|
Aliases: []string{"delete"},
|
2021-05-09 17:12:05 +02:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-11-04 23:42:21 +01:00
|
|
|
output, _ := cmd.Flags().GetString("output")
|
|
|
|
|
2024-12-10 16:23:55 +01:00
|
|
|
id, username := usernameAndIDFromFlag(cmd)
|
|
|
|
request := &v1.ListUsersRequest{
|
|
|
|
Name: username,
|
|
|
|
Id: id,
|
2021-11-13 20:01:05 +01:00
|
|
|
}
|
|
|
|
|
2024-09-07 09:23:58 +02:00
|
|
|
ctx, client, conn, cancel := newHeadscaleCLIWithConfig()
|
2021-11-04 23:42:21 +01:00
|
|
|
defer cancel()
|
|
|
|
defer conn.Close()
|
|
|
|
|
2024-12-10 16:23:55 +01:00
|
|
|
users, err := client.ListUsers(ctx, request)
|
2021-05-09 17:12:05 +02:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
2021-11-14 09:30:48 +01:00
|
|
|
fmt.Sprintf("Error: %s", status.Convert(err).Message()),
|
2021-11-13 09:36:45 +01:00
|
|
|
output,
|
|
|
|
)
|
2021-05-09 17:12:05 +02:00
|
|
|
}
|
2021-11-04 23:42:21 +01:00
|
|
|
|
2024-12-10 16:23:55 +01:00
|
|
|
if len(users.GetUsers()) != 1 {
|
|
|
|
err := fmt.Errorf("Unable to determine user to delete, query returned multiple users, use ID")
|
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Error: %s", status.Convert(err).Message()),
|
|
|
|
output,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
user := users.GetUsers()[0]
|
|
|
|
|
2021-11-13 20:01:05 +01:00
|
|
|
confirm := false
|
|
|
|
force, _ := cmd.Flags().GetBool("force")
|
|
|
|
if !force {
|
|
|
|
prompt := &survey.Confirm{
|
2021-11-14 09:30:48 +01:00
|
|
|
Message: fmt.Sprintf(
|
2024-12-10 16:23:55 +01:00
|
|
|
"Do you want to remove the user %q (%d) and any associated preauthkeys?",
|
|
|
|
user.GetName(), user.GetId(),
|
2021-11-14 09:30:48 +01:00
|
|
|
),
|
2021-11-13 20:01:05 +01:00
|
|
|
}
|
|
|
|
err := survey.AskOne(prompt, &confirm)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if confirm || force {
|
2024-12-10 16:23:55 +01:00
|
|
|
request := &v1.DeleteUserRequest{Id: user.GetId()}
|
2021-11-13 20:01:05 +01:00
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
response, err := client.DeleteUser(ctx, request)
|
2021-11-13 20:01:05 +01:00
|
|
|
if err != nil {
|
2021-11-14 09:30:48 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf(
|
2023-01-17 17:43:44 +01:00
|
|
|
"Cannot destroy user: %s",
|
2021-11-14 09:30:48 +01:00
|
|
|
status.Convert(err).Message(),
|
|
|
|
),
|
|
|
|
output,
|
|
|
|
)
|
2021-11-13 20:01:05 +01:00
|
|
|
}
|
2023-01-17 17:43:44 +01:00
|
|
|
SuccessOutput(response, "User destroyed", output)
|
2021-11-13 20:01:05 +01:00
|
|
|
} else {
|
2023-01-17 17:43:44 +01:00
|
|
|
SuccessOutput(map[string]string{"Result": "User not destroyed"}, "User not destroyed", output)
|
2021-11-13 20:01:05 +01:00
|
|
|
}
|
2021-05-09 17:12:05 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
var listUsersCmd = &cobra.Command{
|
2022-03-02 05:35:20 +01:00
|
|
|
Use: "list",
|
2023-01-17 17:43:44 +01:00
|
|
|
Short: "List all the users",
|
2022-03-02 05:35:20 +01:00
|
|
|
Aliases: []string{"ls", "show"},
|
2021-04-28 16:15:45 +02:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-11-04 23:42:21 +01:00
|
|
|
output, _ := cmd.Flags().GetString("output")
|
|
|
|
|
2024-09-07 09:23:58 +02:00
|
|
|
ctx, client, conn, cancel := newHeadscaleCLIWithConfig()
|
2021-11-04 23:42:21 +01:00
|
|
|
defer cancel()
|
|
|
|
defer conn.Close()
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
request := &v1.ListUsersRequest{}
|
2021-11-04 23:42:21 +01:00
|
|
|
|
2024-12-10 16:23:55 +01:00
|
|
|
id, _ := cmd.Flags().GetInt64("identifier")
|
|
|
|
username, _ := cmd.Flags().GetString("name")
|
|
|
|
email, _ := cmd.Flags().GetString("email")
|
|
|
|
|
|
|
|
// filter by one param at most
|
|
|
|
switch {
|
|
|
|
case id > 0:
|
|
|
|
request.Id = uint64(id)
|
|
|
|
break
|
|
|
|
case username != "":
|
|
|
|
request.Name = username
|
|
|
|
break
|
|
|
|
case email != "":
|
|
|
|
request.Email = email
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
response, err := client.ListUsers(ctx, request)
|
2021-04-28 16:15:45 +02:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
2023-01-17 17:43:44 +01:00
|
|
|
fmt.Sprintf("Cannot get users: %s", status.Convert(err).Message()),
|
2021-11-13 09:36:45 +01:00
|
|
|
output,
|
|
|
|
)
|
2021-05-08 13:28:22 +02:00
|
|
|
}
|
2021-11-04 23:42:21 +01:00
|
|
|
|
|
|
|
if output != "" {
|
2023-11-23 08:31:33 +01:00
|
|
|
SuccessOutput(response.GetUsers(), "", output)
|
2021-04-28 16:15:45 +02:00
|
|
|
}
|
2021-08-15 23:20:38 +02:00
|
|
|
|
2024-10-18 16:20:03 +02:00
|
|
|
tableData := pterm.TableData{{"ID", "Name", "Username", "Email", "Created"}}
|
2023-01-17 17:43:44 +01:00
|
|
|
for _, user := range response.GetUsers() {
|
2021-11-14 20:32:03 +01:00
|
|
|
tableData = append(
|
|
|
|
tableData,
|
2021-11-04 23:42:21 +01:00
|
|
|
[]string{
|
2024-12-10 16:23:55 +01:00
|
|
|
fmt.Sprintf("%d", user.GetId()),
|
2024-10-18 16:20:03 +02:00
|
|
|
user.GetDisplayName(),
|
2023-01-17 17:43:44 +01:00
|
|
|
user.GetName(),
|
2024-10-18 16:20:03 +02:00
|
|
|
user.GetEmail(),
|
2023-01-17 17:43:44 +01:00
|
|
|
user.GetCreatedAt().AsTime().Format("2006-01-02 15:04:05"),
|
2021-11-04 23:42:21 +01:00
|
|
|
},
|
2021-10-29 19:16:54 +02:00
|
|
|
)
|
2021-04-28 16:15:45 +02:00
|
|
|
}
|
2021-11-14 20:32:03 +01:00
|
|
|
err = pterm.DefaultTable.WithHasHeader().WithData(tableData).Render()
|
2021-08-15 23:35:03 +02:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Failed to render pterm table: %s", err),
|
|
|
|
output,
|
|
|
|
)
|
2021-08-15 23:35:03 +02:00
|
|
|
}
|
2021-04-28 16:15:45 +02:00
|
|
|
},
|
|
|
|
}
|
2021-10-16 17:20:06 +02:00
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
var renameUserCmd = &cobra.Command{
|
2024-12-10 16:23:55 +01:00
|
|
|
Use: "rename",
|
2023-01-17 17:43:44 +01:00
|
|
|
Short: "Renames a user",
|
2022-03-02 05:35:20 +01:00
|
|
|
Aliases: []string{"mv"},
|
2021-10-16 17:20:06 +02:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-11-04 23:42:21 +01:00
|
|
|
output, _ := cmd.Flags().GetString("output")
|
|
|
|
|
2024-09-07 09:23:58 +02:00
|
|
|
ctx, client, conn, cancel := newHeadscaleCLIWithConfig()
|
2021-11-04 23:42:21 +01:00
|
|
|
defer cancel()
|
|
|
|
defer conn.Close()
|
|
|
|
|
2024-12-10 16:23:55 +01:00
|
|
|
id, username := usernameAndIDFromFlag(cmd)
|
|
|
|
listReq := &v1.ListUsersRequest{
|
|
|
|
Name: username,
|
|
|
|
Id: id,
|
|
|
|
}
|
|
|
|
|
|
|
|
users, err := client.ListUsers(ctx, listReq)
|
|
|
|
if err != nil {
|
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Error: %s", status.Convert(err).Message()),
|
|
|
|
output,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(users.GetUsers()) != 1 {
|
|
|
|
err := fmt.Errorf("Unable to determine user to delete, query returned multiple users, use ID")
|
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Error: %s", status.Convert(err).Message()),
|
|
|
|
output,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
newName, _ := cmd.Flags().GetString("new-name")
|
|
|
|
|
|
|
|
renameReq := &v1.RenameUserRequest{
|
|
|
|
OldId: id,
|
|
|
|
NewName: newName,
|
2021-10-16 17:20:06 +02:00
|
|
|
}
|
2021-11-04 23:42:21 +01:00
|
|
|
|
2024-12-10 16:23:55 +01:00
|
|
|
response, err := client.RenameUser(ctx, renameReq)
|
2021-10-16 17:20:06 +02:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf(
|
2023-01-17 17:43:44 +01:00
|
|
|
"Cannot rename user: %s",
|
2021-11-13 09:36:45 +01:00
|
|
|
status.Convert(err).Message(),
|
|
|
|
),
|
|
|
|
output,
|
|
|
|
)
|
2021-10-16 17:20:06 +02:00
|
|
|
}
|
2021-11-04 23:42:21 +01:00
|
|
|
|
2023-11-23 08:31:33 +01:00
|
|
|
SuccessOutput(response.GetUser(), "User renamed", output)
|
2021-10-16 17:20:06 +02:00
|
|
|
},
|
|
|
|
}
|