2021-04-28 16:15:45 +02:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2021-11-04 23:44:49 +01:00
|
|
|
"context"
|
2021-04-28 16:15:45 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2021-08-15 23:29:55 +02:00
|
|
|
"strconv"
|
2021-04-28 16:15:45 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hako/durafmt"
|
2021-11-04 23:44:49 +01:00
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
2021-08-15 23:29:55 +02:00
|
|
|
"github.com/pterm/pterm"
|
2021-04-28 16:15:45 +02:00
|
|
|
"github.com/spf13/cobra"
|
2021-11-04 23:44:49 +01:00
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
2021-04-28 16:15:45 +02:00
|
|
|
)
|
|
|
|
|
2021-07-25 15:07:27 +02:00
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(preauthkeysCmd)
|
|
|
|
preauthkeysCmd.PersistentFlags().StringP("namespace", "n", "", "Namespace")
|
2021-07-25 16:26:15 +02:00
|
|
|
err := preauthkeysCmd.MarkPersistentFlagRequired("namespace")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
2021-07-25 15:07:27 +02:00
|
|
|
preauthkeysCmd.AddCommand(listPreAuthKeys)
|
|
|
|
preauthkeysCmd.AddCommand(createPreAuthKeyCmd)
|
2021-08-08 00:10:30 +02:00
|
|
|
preauthkeysCmd.AddCommand(expirePreAuthKeyCmd)
|
2021-07-25 15:07:27 +02:00
|
|
|
createPreAuthKeyCmd.PersistentFlags().Bool("reusable", false, "Make the preauthkey reusable")
|
|
|
|
createPreAuthKeyCmd.PersistentFlags().Bool("ephemeral", false, "Preauthkey for ephemeral nodes")
|
2021-11-04 23:44:49 +01:00
|
|
|
createPreAuthKeyCmd.Flags().
|
|
|
|
StringP("expiration", "e", "", "Human-readable expiration of the key (30m, 24h, 365d...)")
|
2021-07-25 15:07:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var preauthkeysCmd = &cobra.Command{
|
2021-06-28 20:04:05 +02:00
|
|
|
Use: "preauthkeys",
|
2021-04-28 16:15:45 +02:00
|
|
|
Short: "Handle the preauthkeys in Headscale",
|
|
|
|
}
|
|
|
|
|
2021-07-25 15:07:27 +02:00
|
|
|
var listPreAuthKeys = &cobra.Command{
|
2021-04-30 00:23:26 +02:00
|
|
|
Use: "list",
|
2021-04-28 16:15:45 +02:00
|
|
|
Short: "List the preauthkeys for this namespace",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-11-04 23:44:49 +01:00
|
|
|
output, _ := cmd.Flags().GetString("output")
|
|
|
|
|
2021-04-30 00:23:26 +02:00
|
|
|
n, err := cmd.Flags().GetString("namespace")
|
|
|
|
if err != nil {
|
2021-11-04 23:44:49 +01:00
|
|
|
ErrorOutput(err, fmt.Sprintf("Error getting namespace: %s", err), output)
|
|
|
|
return
|
2021-04-30 00:23:26 +02:00
|
|
|
}
|
|
|
|
|
2021-11-04 23:44:49 +01:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
client, conn := getHeadscaleGRPCClient(ctx)
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
request := &v1.ListPreAuthKeysRequest{
|
|
|
|
Namespace: n,
|
2021-04-28 16:15:45 +02:00
|
|
|
}
|
2021-11-04 23:44:49 +01:00
|
|
|
|
|
|
|
response, err := client.ListPreAuthKeys(ctx, request)
|
|
|
|
if err != nil {
|
|
|
|
ErrorOutput(err, fmt.Sprintf("Error getting the list of keys: %s", err), output)
|
2021-05-08 13:58:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-04 23:44:49 +01:00
|
|
|
if output != "" {
|
|
|
|
SuccessOutput(response.PreAuthKeys, "", output)
|
2021-04-28 16:15:45 +02:00
|
|
|
return
|
|
|
|
}
|
2021-08-15 23:29:55 +02:00
|
|
|
|
2021-10-13 22:51:55 +02:00
|
|
|
d := pterm.TableData{{"ID", "Key", "Reusable", "Ephemeral", "Used", "Expiration", "Created"}}
|
2021-11-04 23:44:49 +01:00
|
|
|
for _, k := range response.PreAuthKeys {
|
2021-04-30 00:23:26 +02:00
|
|
|
expiration := "-"
|
2021-11-04 23:44:49 +01:00
|
|
|
if k.GetExpiration() != nil {
|
|
|
|
expiration = k.Expiration.AsTime().Format("2006-01-02 15:04:05")
|
2021-04-30 00:23:26 +02:00
|
|
|
}
|
2021-07-11 13:14:25 +02:00
|
|
|
|
|
|
|
var reusable string
|
2021-11-04 23:44:49 +01:00
|
|
|
if k.GetEphemeral() {
|
2021-07-11 13:14:25 +02:00
|
|
|
reusable = "N/A"
|
|
|
|
} else {
|
2021-11-04 23:44:49 +01:00
|
|
|
reusable = fmt.Sprintf("%v", k.GetResuable())
|
2021-07-11 13:14:25 +02:00
|
|
|
}
|
|
|
|
|
2021-08-15 23:29:55 +02:00
|
|
|
d = append(d, []string{
|
2021-11-04 23:44:49 +01:00
|
|
|
k.GetId(),
|
|
|
|
k.GetKey(),
|
2021-07-11 13:14:25 +02:00
|
|
|
reusable,
|
2021-11-04 23:44:49 +01:00
|
|
|
strconv.FormatBool(k.GetEphemeral()),
|
|
|
|
strconv.FormatBool(k.GetUsed()),
|
2021-04-30 00:23:26 +02:00
|
|
|
expiration,
|
2021-11-04 23:44:49 +01:00
|
|
|
k.GetCreatedAt().AsTime().Format("2006-01-02 15:04:05"),
|
2021-08-15 23:29:55 +02:00
|
|
|
})
|
|
|
|
|
2021-04-28 16:15:45 +02:00
|
|
|
}
|
2021-08-15 23:35:03 +02:00
|
|
|
err = pterm.DefaultTable.WithHasHeader().WithData(d).Render()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-04-28 16:15:45 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-07-25 15:07:27 +02:00
|
|
|
var createPreAuthKeyCmd = &cobra.Command{
|
2021-04-30 00:23:26 +02:00
|
|
|
Use: "create",
|
2021-04-28 16:15:45 +02:00
|
|
|
Short: "Creates a new preauthkey in the specified namespace",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-11-04 23:44:49 +01:00
|
|
|
output, _ := cmd.Flags().GetString("output")
|
|
|
|
|
2021-04-30 00:23:26 +02:00
|
|
|
n, err := cmd.Flags().GetString("namespace")
|
|
|
|
if err != nil {
|
2021-11-04 23:44:49 +01:00
|
|
|
ErrorOutput(err, fmt.Sprintf("Error getting namespace: %s", err), output)
|
|
|
|
return
|
2021-04-30 00:23:26 +02:00
|
|
|
}
|
|
|
|
|
2021-04-28 16:15:45 +02:00
|
|
|
reusable, _ := cmd.Flags().GetBool("reusable")
|
2021-05-23 02:15:29 +02:00
|
|
|
ephemeral, _ := cmd.Flags().GetBool("ephemeral")
|
2021-04-28 16:15:45 +02:00
|
|
|
|
|
|
|
e, _ := cmd.Flags().GetString("expiration")
|
|
|
|
var expiration *time.Time
|
|
|
|
if e != "" {
|
|
|
|
duration, err := durafmt.ParseStringShort(e)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error parsing expiration: %s", err)
|
|
|
|
}
|
|
|
|
exp := time.Now().UTC().Add(duration.Duration())
|
|
|
|
expiration = &exp
|
|
|
|
}
|
|
|
|
|
2021-11-04 23:44:49 +01:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
client, conn := getHeadscaleGRPCClient(ctx)
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
request := &v1.CreatePreAuthKeyRequest{
|
|
|
|
Namespace: n,
|
|
|
|
Resuable: reusable,
|
|
|
|
Ephemeral: ephemeral,
|
|
|
|
Expiration: timestamppb.New(*expiration),
|
2021-05-08 13:58:51 +02:00
|
|
|
}
|
2021-11-04 23:44:49 +01:00
|
|
|
|
|
|
|
response, err := client.CreatePreAuthKey(ctx, request)
|
2021-04-28 16:15:45 +02:00
|
|
|
if err != nil {
|
2021-11-04 23:44:49 +01:00
|
|
|
ErrorOutput(err, fmt.Sprintf("Cannot create Pre Auth Key: %s\n", err), output)
|
2021-04-28 16:15:45 +02:00
|
|
|
return
|
|
|
|
}
|
2021-11-04 23:44:49 +01:00
|
|
|
|
|
|
|
SuccessOutput(response.PreAuthKey, response.PreAuthKey.Key, output)
|
2021-04-28 16:15:45 +02:00
|
|
|
},
|
|
|
|
}
|
2021-08-08 00:10:30 +02:00
|
|
|
|
|
|
|
var expirePreAuthKeyCmd = &cobra.Command{
|
2021-10-14 23:58:15 +02:00
|
|
|
Use: "expire KEY",
|
2021-08-08 00:10:30 +02:00
|
|
|
Short: "Expire a preauthkey",
|
|
|
|
Args: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) < 1 {
|
|
|
|
return fmt.Errorf("missing parameters")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-11-04 23:44:49 +01:00
|
|
|
output, _ := cmd.Flags().GetString("output")
|
2021-08-08 00:10:30 +02:00
|
|
|
n, err := cmd.Flags().GetString("namespace")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error getting namespace: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-11-04 23:44:49 +01:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
defer cancel()
|
2021-08-08 00:10:30 +02:00
|
|
|
|
2021-11-04 23:44:49 +01:00
|
|
|
client, conn := getHeadscaleGRPCClient(ctx)
|
|
|
|
defer conn.Close()
|
2021-08-08 00:10:30 +02:00
|
|
|
|
2021-11-04 23:44:49 +01:00
|
|
|
request := &v1.ExpirePreAuthKeyRequest{
|
|
|
|
Namespace: n,
|
|
|
|
Key: args[0],
|
2021-08-08 00:10:30 +02:00
|
|
|
}
|
2021-11-04 23:44:49 +01:00
|
|
|
|
|
|
|
response, err := client.ExpirePreAuthKey(ctx, request)
|
2021-08-08 00:10:30 +02:00
|
|
|
if err != nil {
|
2021-11-04 23:44:49 +01:00
|
|
|
ErrorOutput(err, fmt.Sprintf("Cannot expire Pre Auth Key: %s\n", err), output)
|
2021-08-08 00:10:30 +02:00
|
|
|
return
|
|
|
|
}
|
2021-11-04 23:44:49 +01:00
|
|
|
|
|
|
|
SuccessOutput(response, "Key expired", output)
|
2021-08-08 00:10:30 +02:00
|
|
|
},
|
|
|
|
}
|