Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use cloud auth token from JWT instead of API key if present #3114

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cmd/flipt/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
}

Expand Down Expand Up @@ -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

Expand Down
13 changes: 11 additions & 2 deletions cmd/flipt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}
Expand Down
1 change: 0 additions & 1 deletion internal/cmd/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Loading