From bdf72caffdf304888e3875b823080c5a86887211 Mon Sep 17 00:00:00 2001 From: Florian Zouhar Date: Tue, 6 Aug 2024 20:05:00 -0700 Subject: [PATCH] default value implementation * default value config * set default value in context source if metadata empty fixes #34412 --- extension/headerssetterextension/config.go | 9 +++++---- extension/headerssetterextension/extension.go | 3 ++- .../headerssetterextension/internal/source/context.go | 5 +++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/extension/headerssetterextension/config.go b/extension/headerssetterextension/config.go index 5b73e9912f8f..6eade8416b4d 100644 --- a/extension/headerssetterextension/config.go +++ b/extension/headerssetterextension/config.go @@ -19,10 +19,11 @@ type Config struct { } type HeaderConfig struct { - Action ActionValue `mapstructure:"action"` - Key *string `mapstructure:"key"` - Value *string `mapstructure:"value"` - FromContext *string `mapstructure:"from_context"` + Action ActionValue `mapstructure:"action"` + Key *string `mapstructure:"key"` + Value *string `mapstructure:"value"` + FromContext *string `mapstructure:"from_context"` + DefaultValue *string `mapstructure:"default_value"` } // ActionValue is the enum to capture the four types of actions to perform on a header diff --git a/extension/headerssetterextension/extension.go b/extension/headerssetterextension/extension.go index ae2c0afbb752..5bdf447c0ed4 100644 --- a/extension/headerssetterextension/extension.go +++ b/extension/headerssetterextension/extension.go @@ -36,7 +36,8 @@ func newHeadersSetterExtension(cfg *Config, logger *zap.Logger) (auth.Client, er } } else if header.FromContext != nil { s = &source.ContextSource{ - Key: *header.FromContext, + Key: *header.FromContext, + DefaultValue: *header.DefaultValue, } } diff --git a/extension/headerssetterextension/internal/source/context.go b/extension/headerssetterextension/internal/source/context.go index 7cd40b7dadb2..20b15fb2f7eb 100644 --- a/extension/headerssetterextension/internal/source/context.go +++ b/extension/headerssetterextension/internal/source/context.go @@ -13,7 +13,8 @@ import ( var _ Source = (*ContextSource)(nil) type ContextSource struct { - Key string + Key string + DefaultValue string } func (ts *ContextSource) Get(ctx context.Context) (string, error) { @@ -21,7 +22,7 @@ func (ts *ContextSource) Get(ctx context.Context) (string, error) { ss := cl.Metadata.Get(ts.Key) if len(ss) == 0 { - return "", nil + return ts.DefaultValue, nil } if len(ss) > 1 {