From 069848b0e026245dcdda6d38796c266bc4d847cd Mon Sep 17 00:00:00 2001 From: Mark Phelps <209477+markphelps@users.noreply.github.com> Date: Sun, 26 May 2024 18:57:03 -0400 Subject: [PATCH] fix: use cloud auth token from JWT instead of API key if present for flipt cloud serve Signed-off-by: Mark Phelps <209477+markphelps@users.noreply.github.com> --- cmd/flipt/cloud.go | 8 +++++++- cmd/flipt/main.go | 13 +++++++++++-- internal/cmd/grpc.go | 1 - 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/cmd/flipt/cloud.go b/cmd/flipt/cloud.go index 99dba9758e..0b772f4b1d 100644 --- a/cmd/flipt/cloud.go +++ b/cmd/flipt/cloud.go @@ -196,7 +196,7 @@ func (c *cloudCommand) serve(cmd *cobra.Command, args []string) error { f, err := os.ReadFile(cloudAuthFile) if err != nil { if errors.Is(err, os.ErrNotExist) { - fmt.Println("No cloud authentication token found. Please run 'flipt cloud login' to authenticate with Flipt Cloud.") + fmt.Println("\nāœ— No cloud authentication token found. Please run 'flipt cloud login' to authenticate.") return nil } @@ -221,6 +221,11 @@ func (c *cloudCommand) serve(cmd *cobra.Command, args []string) error { parsed, err := jwt.Parse(auth.Token, k.Keyfunc, jwt.WithExpirationRequired()) if err != nil { + if errors.Is(err, jwt.ErrTokenExpired) { + fmt.Println("āœ— Existing cloud authentication token expired. Please run 'flipt cloud login' to re-authenticate.") + return nil + } + return fmt.Errorf("parsing JWT: %w", err) } @@ -364,6 +369,7 @@ func (c *cloudCommand) serve(cmd *cobra.Command, args []string) error { cfg.Cloud.Host = u.Hostname() cfg.Cloud.Instance = instance.Instance cfg.Cloud.Organization = instance.Organization + cfg.Cloud.Authentication.ApiKey = "" // clear API key if present to use JWT cfg.Server.Cloud.Enabled = true cfg.Authentication.Session.Domain = u.Host diff --git a/cmd/flipt/main.go b/cmd/flipt/main.go index 42db0e50a8..43b1e3ba17 100644 --- a/cmd/flipt/main.go +++ b/cmd/flipt/main.go @@ -257,6 +257,15 @@ func buildConfig(ctx context.Context) (*zap.Logger, *config.Config, error) { return logger, cfg, nil } +const ( + dntVar = "DO_NOT_TRACK" + ciVar = "CI" +) + +func isSet(env string) bool { + return os.Getenv(env) == "true" || os.Getenv(env) == "1" +} + func run(ctx context.Context, logger *zap.Logger, cfg *config.Config) error { isConsole := cfg.Log.Encoding == config.LogEncodingConsole @@ -298,12 +307,12 @@ func run(ctx context.Context, logger *zap.Logger, cfg *config.Config) error { } // see: https://consoledonottrack.com/ - if (os.Getenv("DO_NOT_TRACK") == "true" || os.Getenv("DO_NOT_TRACK") == "1") && cfg.Meta.TelemetryEnabled { + if isSet(dntVar) && cfg.Meta.TelemetryEnabled { logger.Debug("DO_NOT_TRACK environment variable set, disabling telemetry") cfg.Meta.TelemetryEnabled = false } - if (os.Getenv("CI") == "true" || os.Getenv("CI") == "1") && cfg.Meta.TelemetryEnabled { + if isSet(ciVar) && cfg.Meta.TelemetryEnabled { logger.Debug("CI detected, disabling telemetry") cfg.Meta.TelemetryEnabled = false } diff --git a/internal/cmd/grpc.go b/internal/cmd/grpc.go index 8ec17a83d2..0d7cb9308a 100644 --- a/internal/cmd/grpc.go +++ b/internal/cmd/grpc.go @@ -475,7 +475,6 @@ func NewGRPCServer( // This methods blocks until Shutdown is called. func (s *GRPCServer) Run() error { s.logger.Debug("starting grpc server") - return s.Serve(s.ln) }