Use Prometheus duration parser (support days and weeks)

This commit is contained in:
Kristoffer Dalby 2022-05-30 16:10:39 +02:00
parent 848727a21d
commit d860270733
2 changed files with 36 additions and 10 deletions

View File

@ -7,6 +7,7 @@ import (
"github.com/juanfont/headscale" "github.com/juanfont/headscale"
v1 "github.com/juanfont/headscale/gen/go/headscale/v1" v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/prometheus/common/model"
"github.com/pterm/pterm" "github.com/pterm/pterm"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -15,7 +16,7 @@ import (
const ( const (
// 90 days. // 90 days.
DefaultAPIKeyExpiry = 90 * 24 * time.Hour DefaultAPIKeyExpiry = "90d"
) )
func init() { func init() {
@ -23,7 +24,7 @@ func init() {
apiKeysCmd.AddCommand(listAPIKeys) apiKeysCmd.AddCommand(listAPIKeys)
createAPIKeyCmd.Flags(). createAPIKeyCmd.Flags().
DurationP("expiration", "e", DefaultAPIKeyExpiry, "Human-readable expiration of the key (e.g. 30m, 24h)") StringP("expiration", "e", DefaultAPIKeyExpiry, "Human-readable expiration of the key (e.g. 30m, 24h)")
apiKeysCmd.AddCommand(createAPIKeyCmd) apiKeysCmd.AddCommand(createAPIKeyCmd)
@ -118,10 +119,22 @@ If you loose a key, create a new one and revoke (expire) the old one.`,
request := &v1.CreateApiKeyRequest{} request := &v1.CreateApiKeyRequest{}
duration, _ := cmd.Flags().GetDuration("expiration") durationStr, _ := cmd.Flags().GetString("expiration")
expiration := time.Now().UTC().Add(duration)
log.Trace().Dur("expiration", duration).Msg("expiration has been set") duration, err := model.ParseDuration(durationStr)
if err != nil {
ErrorOutput(
err,
fmt.Sprintf("Could not parse duration: %s\n", err),
output,
)
return
}
expiration := time.Now().UTC().Add(time.Duration(duration))
log.Trace().Dur("expiration", time.Duration(duration)).Msg("expiration has been set")
request.Expiration = timestamppb.New(expiration) request.Expiration = timestamppb.New(expiration)

View File

@ -6,6 +6,7 @@ import (
"time" "time"
v1 "github.com/juanfont/headscale/gen/go/headscale/v1" v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/prometheus/common/model"
"github.com/pterm/pterm" "github.com/pterm/pterm"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -13,7 +14,7 @@ import (
) )
const ( const (
DefaultPreAuthKeyExpiry = 1 * time.Hour DefaultPreAuthKeyExpiry = "1h"
) )
func init() { func init() {
@ -31,7 +32,7 @@ func init() {
createPreAuthKeyCmd.PersistentFlags(). createPreAuthKeyCmd.PersistentFlags().
Bool("ephemeral", false, "Preauthkey for ephemeral nodes") Bool("ephemeral", false, "Preauthkey for ephemeral nodes")
createPreAuthKeyCmd.Flags(). createPreAuthKeyCmd.Flags().
DurationP("expiration", "e", DefaultPreAuthKeyExpiry, "Human-readable expiration of the key (e.g. 30m, 24h)") StringP("expiration", "e", DefaultPreAuthKeyExpiry, "Human-readable expiration of the key (e.g. 30m, 24h)")
} }
var preauthkeysCmd = &cobra.Command{ var preauthkeysCmd = &cobra.Command{
@ -148,10 +149,22 @@ var createPreAuthKeyCmd = &cobra.Command{
Ephemeral: ephemeral, Ephemeral: ephemeral,
} }
duration, _ := cmd.Flags().GetDuration("expiration") durationStr, _ := cmd.Flags().GetString("expiration")
expiration := time.Now().UTC().Add(duration)
log.Trace().Dur("expiration", duration).Msg("expiration has been set") duration, err := model.ParseDuration(durationStr)
if err != nil {
ErrorOutput(
err,
fmt.Sprintf("Could not parse duration: %s\n", err),
output,
)
return
}
expiration := time.Now().UTC().Add(time.Duration(duration))
log.Trace().Dur("expiration", time.Duration(duration)).Msg("expiration has been set")
request.Expiration = timestamppb.New(expiration) request.Expiration = timestamppb.New(expiration)