Skip to content

Commit bd77bc2

Browse files
fix: allow to override authorized keys with env variables
Signed-off-by: Thomas Poignant <[email protected]>
1 parent 7897235 commit bd77bc2

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

cmd/relayproxy/config/config.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ func New(flagSet *pflag.FlagSet, log *zap.Logger, version string) (*Config, erro
100100
}
101101
// Map environment variables
102102
_ = k.Load(env.ProviderWithValue("", ".", func(s string, v string) (string, interface{}) {
103-
if strings.HasPrefix(s, "RETRIEVERS") || strings.HasPrefix(s, "NOTIFIERS") {
103+
if strings.HasPrefix(s, "RETRIEVERS") ||
104+
strings.HasPrefix(s, "NOTIFIERS") {
104105
configMap := k.Raw()
105106
err := loadArrayEnv(s, v, configMap)
106107
if err != nil {
@@ -109,6 +110,14 @@ func New(flagSet *pflag.FlagSet, log *zap.Logger, version string) (*Config, erro
109110
}
110111
return s, v
111112
}
113+
114+
if strings.HasPrefix(s, "AUTHORIZEDKEYS_EVALUATION") {
115+
return "authorizedKeys.evaluation", strings.Fields(v)
116+
}
117+
if strings.HasPrefix(s, "AUTHORIZEDKEYS_ADMIN") {
118+
return "authorizedKeys.admin", strings.Fields(v)
119+
}
120+
112121
return strings.ReplaceAll(strings.ToLower(s), "_", "."), v
113122
}), nil)
114123

cmd/relayproxy/config/config_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package config_test
22

33
import (
44
"fmt"
5+
"github.com/stretchr/testify/require"
56
"io"
67
"os"
78
"testing"
@@ -834,6 +835,21 @@ func TestMergeConfig_FromOSEnv(t *testing.T) {
834835
}
835836
}
836837

838+
func TestSetAPIKeysFromEnv(t *testing.T) {
839+
os.Setenv("AUTHORIZEDKEYS_EVALUATION", "key1 key2 key_3")
840+
os.Setenv("AUTHORIZEDKEYS_ADMIN", "key4 key5")
841+
842+
fileLocation := "../testdata/config/valid-file.yaml"
843+
f := pflag.NewFlagSet("config", pflag.ContinueOnError)
844+
f.String("config", "", "Location of your config file")
845+
_ = f.Parse([]string{fmt.Sprintf("--config=%s", fileLocation)})
846+
847+
got, err := config.New(f, zap.L(), "1.X.X")
848+
require.NoError(t, err)
849+
assert.Equal(t, []string{"key1", "key2", "key_3"}, got.AuthorizedKeys.Evaluation)
850+
assert.Equal(t, []string{"key4", "key5"}, got.AuthorizedKeys.Admin)
851+
}
852+
837853
func TestConfig_LogLevel(t *testing.T) {
838854
tests := []struct {
839855
name string

0 commit comments

Comments
 (0)