2021-08-03 09:55:21 +00:00
|
|
|
package configuration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2021-08-03 09:55:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestKoanfEnvironmentCallback(t *testing.T) {
|
|
|
|
var (
|
|
|
|
key string
|
2022-10-05 05:05:23 +00:00
|
|
|
value any
|
2021-08-03 09:55:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
keyMap := map[string]string{
|
|
|
|
DefaultEnvPrefix + "KEY_EXAMPLE_UNDERSCORE": "key.example_underscore",
|
|
|
|
}
|
2023-04-19 04:24:05 +00:00
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
ignoredKeys := []string{DefaultEnvPrefix + "SOME_SECRET"}
|
|
|
|
|
|
|
|
callback := koanfEnvironmentCallback(keyMap, ignoredKeys, DefaultEnvPrefix, DefaultEnvDelimiter)
|
|
|
|
|
|
|
|
key, value = callback(DefaultEnvPrefix+"KEY_EXAMPLE_UNDERSCORE", "value")
|
|
|
|
assert.Equal(t, "key.example_underscore", key)
|
|
|
|
assert.Equal(t, "value", value)
|
|
|
|
|
|
|
|
key, value = callback(DefaultEnvPrefix+"KEY_EXAMPLE", "value")
|
|
|
|
assert.Equal(t, DefaultEnvPrefix+"KEY_EXAMPLE", key)
|
|
|
|
assert.Equal(t, "value", value)
|
|
|
|
|
|
|
|
key, value = callback(DefaultEnvPrefix+"THEME", "value")
|
|
|
|
assert.Equal(t, "theme", key)
|
|
|
|
assert.Equal(t, "value", value)
|
|
|
|
|
|
|
|
key, value = callback(DefaultEnvPrefix+"SOME_SECRET", "value")
|
|
|
|
assert.Equal(t, "", key)
|
|
|
|
assert.Nil(t, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKoanfSecretCallbackWithValidSecrets(t *testing.T) {
|
|
|
|
var (
|
|
|
|
key string
|
2022-10-05 05:05:23 +00:00
|
|
|
value any
|
2021-08-03 09:55:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
keyMap := map[string]string{
|
|
|
|
"AUTHELIA__JWT_SECRET": "jwt_secret",
|
|
|
|
"AUTHELIA_JWT_SECRET": "jwt_secret",
|
|
|
|
"AUTHELIA_FAKE_KEY": "fake_key",
|
|
|
|
"AUTHELIA__FAKE_KEY": "fake_key",
|
|
|
|
"AUTHELIA_STORAGE_MYSQL_FAKE_PASSWORD": "storage.mysql.fake_password",
|
|
|
|
"AUTHELIA__STORAGE_MYSQL_FAKE_PASSWORD": "storage.mysql.fake_password",
|
|
|
|
}
|
|
|
|
|
2022-12-04 09:57:28 +00:00
|
|
|
dir := t.TempDir()
|
2021-08-03 09:55:21 +00:00
|
|
|
|
|
|
|
secretOne := filepath.Join(dir, "secert_one")
|
|
|
|
secretTwo := filepath.Join(dir, "secret_two")
|
|
|
|
|
|
|
|
assert.NoError(t, testCreateFile(secretOne, "value one", 0600))
|
|
|
|
assert.NoError(t, testCreateFile(secretTwo, "value two", 0600))
|
|
|
|
|
|
|
|
val := schema.NewStructValidator()
|
|
|
|
|
|
|
|
callback := koanfEnvironmentSecretsCallback(keyMap, val)
|
|
|
|
|
|
|
|
key, value = callback("AUTHELIA_FAKE_KEY", secretOne)
|
|
|
|
assert.Equal(t, "fake_key", key)
|
|
|
|
assert.Equal(t, "value one", value)
|
|
|
|
|
|
|
|
key, value = callback("AUTHELIA__STORAGE_MYSQL_FAKE_PASSWORD", secretTwo)
|
|
|
|
assert.Equal(t, "storage.mysql.fake_password", key)
|
|
|
|
assert.Equal(t, "value two", value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKoanfSecretCallbackShouldIgnoreUndetectedSecrets(t *testing.T) {
|
|
|
|
keyMap := map[string]string{
|
|
|
|
"AUTHELIA__JWT_SECRET": "jwt_secret",
|
|
|
|
"AUTHELIA_JWT_SECRET": "jwt_secret",
|
|
|
|
}
|
|
|
|
|
|
|
|
val := schema.NewStructValidator()
|
|
|
|
|
|
|
|
callback := koanfEnvironmentSecretsCallback(keyMap, val)
|
|
|
|
|
|
|
|
key, value := callback("AUTHELIA__SESSION_DOMAIN", "/tmp/not-a-path")
|
|
|
|
assert.Equal(t, "", key)
|
|
|
|
assert.Nil(t, value)
|
|
|
|
|
|
|
|
assert.Len(t, val.Errors(), 0)
|
|
|
|
assert.Len(t, val.Warnings(), 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKoanfSecretCallbackShouldErrorOnFSError(t *testing.T) {
|
|
|
|
if runtime.GOOS == constWindows {
|
|
|
|
t.Skip("skipping test due to being on windows")
|
|
|
|
}
|
|
|
|
|
|
|
|
keyMap := map[string]string{
|
|
|
|
"AUTHELIA__THEME": "theme",
|
|
|
|
"AUTHELIA_THEME": "theme",
|
|
|
|
}
|
|
|
|
|
2022-12-04 09:57:28 +00:00
|
|
|
dir := t.TempDir()
|
2021-08-03 09:55:21 +00:00
|
|
|
|
|
|
|
secret := filepath.Join(dir, "inaccessible")
|
|
|
|
|
|
|
|
assert.NoError(t, testCreateFile(secret, "secret", 0000))
|
|
|
|
|
|
|
|
val := schema.NewStructValidator()
|
|
|
|
|
|
|
|
callback := koanfEnvironmentSecretsCallback(keyMap, val)
|
|
|
|
|
|
|
|
key, value := callback("AUTHELIA_THEME", secret)
|
2023-04-08 06:02:34 +00:00
|
|
|
assert.Equal(t, "", key)
|
|
|
|
assert.Equal(t, nil, value)
|
2021-08-03 09:55:21 +00:00
|
|
|
|
|
|
|
require.Len(t, val.Errors(), 1)
|
|
|
|
assert.Len(t, val.Warnings(), 0)
|
2023-04-08 06:02:34 +00:00
|
|
|
assert.EqualError(t, val.Errors()[0], fmt.Sprintf("secrets: error loading secret path %s into key 'theme': file permission error occurred: open %s: permission denied", secret, secret))
|
2021-08-03 09:55:21 +00:00
|
|
|
}
|