1
0
mirror of https://github.com/juanfont/headscale.git synced 2024-12-20 19:09:07 +01:00
juanfont.headscale/cmd/headscale/cli/preauthkeys.go

122 lines
3.0 KiB
Go
Raw Normal View History

package cli
import (
"fmt"
"log"
2021-05-08 13:58:51 +02:00
"strings"
"time"
"github.com/hako/durafmt"
"github.com/spf13/cobra"
)
2021-07-25 15:07:27 +02:00
func init() {
rootCmd.AddCommand(preauthkeysCmd)
preauthkeysCmd.PersistentFlags().StringP("namespace", "n", "", "Namespace")
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)
createPreAuthKeyCmd.PersistentFlags().Bool("reusable", false, "Make the preauthkey reusable")
createPreAuthKeyCmd.PersistentFlags().Bool("ephemeral", false, "Preauthkey for ephemeral nodes")
2021-07-30 16:42:26 +02: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",
Short: "Handle the preauthkeys in Headscale",
}
2021-07-25 15:07:27 +02:00
var listPreAuthKeys = &cobra.Command{
Use: "list",
Short: "List the preauthkeys for this namespace",
Run: func(cmd *cobra.Command, args []string) {
n, err := cmd.Flags().GetString("namespace")
if err != nil {
log.Fatalf("Error getting namespace: %s", err)
}
2021-05-08 13:58:51 +02:00
o, _ := cmd.Flags().GetString("output")
h, err := getHeadscaleApp()
if err != nil {
log.Fatalf("Error initializing: %s", err)
}
keys, err := h.GetPreAuthKeys(n)
2021-05-08 13:58:51 +02:00
if strings.HasPrefix(o, "json") {
2021-05-08 17:06:36 +02:00
JsonOutput(keys, err, o)
2021-05-08 13:58:51 +02:00
return
}
if err != nil {
2021-05-08 13:58:51 +02:00
fmt.Printf("Error getting the list of keys: %s\n", err)
return
}
for _, k := range *keys {
expiration := "-"
if k.Expiration != nil {
expiration = k.Expiration.Format("2006-01-02 15:04:05")
}
var reusable string
if k.Ephemeral {
reusable = "N/A"
} else {
reusable = fmt.Sprintf("%v", k.Reusable)
}
fmt.Printf(
"key: %s, namespace: %s, reusable: %s, ephemeral: %v, expiration: %s, created_at: %s\n",
k.Key,
k.Namespace.Name,
reusable,
k.Ephemeral,
expiration,
k.CreatedAt.Format("2006-01-02 15:04:05"),
)
}
},
}
2021-07-25 15:07:27 +02:00
var createPreAuthKeyCmd = &cobra.Command{
Use: "create",
Short: "Creates a new preauthkey in the specified namespace",
Run: func(cmd *cobra.Command, args []string) {
n, err := cmd.Flags().GetString("namespace")
if err != nil {
log.Fatalf("Error getting namespace: %s", err)
}
2021-05-08 13:58:51 +02:00
o, _ := cmd.Flags().GetString("output")
h, err := getHeadscaleApp()
if err != nil {
log.Fatalf("Error initializing: %s", err)
}
reusable, _ := cmd.Flags().GetBool("reusable")
ephemeral, _ := cmd.Flags().GetBool("ephemeral")
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
}
k, err := h.CreatePreAuthKey(n, reusable, ephemeral, expiration)
2021-05-08 13:58:51 +02:00
if strings.HasPrefix(o, "json") {
2021-05-08 17:06:36 +02:00
JsonOutput(k, err, o)
2021-05-08 13:58:51 +02:00
return
}
if err != nil {
fmt.Println(err)
return
}
2021-05-08 13:58:51 +02:00
fmt.Printf("Key: %s\n", k.Key)
},
}