Skip to content

Commit 7417d66

Browse files
authored
fix(config): dotenvx loading (#3042)
* fix(secrets): look for and try all DOTENV_PRIVATE_KEY * fix(env): look for dotenv in supabase folder and look for local env.keys * fix: apply pr comments * fix: apply PR comments
1 parent d27db81 commit 7417d66

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

pkg/config/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,14 +806,15 @@ func sanitizeProjectId(src string) string {
806806

807807
func loadDefaultEnv() error {
808808
env := viper.GetString("ENV")
809+
809810
if env == "" {
810811
env = "development"
811812
}
812813
filenames := []string{".env." + env + ".local"}
813814
if env != "test" {
814815
filenames = append(filenames, ".env.local")
815816
}
816-
filenames = append(filenames, ".env."+env, ".env")
817+
filenames = append(filenames, ".env."+env, ".env", filepath.Join("supabase", ".env."+env), filepath.Join("supabase", ".env"))
817818
for _, path := range filenames {
818819
if err := loadEnvIfExists(path); err != nil {
819820
return err

pkg/config/secret.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,20 @@ func DecryptSecretHookFunc(hashKey string) mapstructure.DecodeHookFunc {
6464
if t != reflect.TypeOf(result) {
6565
return data, nil
6666
}
67+
// Get all env vars and filter for DOTENV_PRIVATE_KEY
68+
var privateKeys []string
69+
for _, env := range os.Environ() {
70+
key := strings.Split(env, "=")[0]
71+
if key == "DOTENV_PRIVATE_KEY" || strings.HasPrefix(key, "DOTENV_PRIVATE_KEY_") {
72+
if value := os.Getenv(key); value != "" {
73+
privateKeys = append(privateKeys, value)
74+
}
75+
}
76+
}
77+
78+
// Try each private key
6779
var err error
68-
privKey := os.Getenv("DOTENV_PRIVATE_KEY")
80+
privKey := strings.Join(privateKeys, ",")
6981
for _, k := range strings.Split(privKey, ",") {
7082
// Use the first private key that successfully decrypts the secret
7183
if result.Value, err = decrypt(k, data.(string)); err == nil {
@@ -77,6 +89,7 @@ func DecryptSecretHookFunc(hashKey string) mapstructure.DecodeHookFunc {
7789
break
7890
}
7991
}
92+
// If we get here, none of the keys worked
8093
return result, err
8194
}
8295
}

0 commit comments

Comments
 (0)