Skip to content

Commit cee3e7c

Browse files
authored
Merge pull request #49 from ElliotThiebaut/support-docker-secrets
feat: support file secrets for env variables
2 parents af6e1ca + 5c72a1e commit cee3e7c

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ providing a more secure way for users to access protected routes.
6969
| `SERVER_ADDRESS` | The server address | `:80` | No |
7070
| `DEBUG_MODE` | Enable debug mode and set log level to debug | `false` | No |
7171
| `LOG_LEVEL` | The log level, Available values: debug, info, warn, error | `info` | No |
72+
You can append `_FILE` to any of the environment variable names to load the value from a file.
73+
74+
E.g. `GITHUB_OAUTH_CLIENT_SECRET_FILE=/run/secrets/github_oauth_client_SECRET` where the content of the file `/run/secrets/github_oauth_client_SECRET` will be used as the environment variable.
7275

7376
### Middleware Configuration
7477

internal/app/traefik-github-oauth-server/config.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,35 @@ type Config struct {
1919
GithubOauthScopes []string
2020
}
2121

22+
func envFromFile(key string) string {
23+
fileEnvKey := key + "_FILE"
24+
25+
if value := os.Getenv(fileEnvKey); value != "" {
26+
content, err := os.ReadFile(value)
27+
if err == nil {
28+
return strings.TrimSpace(string(content))
29+
}
30+
}
31+
return ""
32+
}
33+
34+
func envString(key string) string {
35+
if value := envFromFile(key); value != "" {
36+
return value
37+
}
38+
return os.Getenv(key)
39+
}
40+
2241
func envWithDefault(key string, defaultValue string) string {
23-
value := os.Getenv(key)
42+
value := envString(key)
2443
if value == "" {
2544
return defaultValue
2645
}
2746
return value
2847
}
2948

3049
func githubOauthScopeConfigs() []string {
31-
scopesFromEnv := os.Getenv("GITHUB_OAUTH_SCOPES")
50+
scopesFromEnv := envString("GITHUB_OAUTH_SCOPES")
3251
if scopesFromEnv != "" {
3352
return strings.Split(scopesFromEnv, ",")
3453
}
@@ -38,13 +57,13 @@ func githubOauthScopeConfigs() []string {
3857

3958
func NewConfigFromEnv() *Config {
4059
return &Config{
41-
ApiBaseURL: os.Getenv("API_BASE_URL"),
42-
ApiSecretKey: os.Getenv("API_SECRET_KEY"),
43-
ServerAddress: os.Getenv("SERVER_ADDRESS"),
44-
DebugMode: cast.ToBool(os.Getenv("DEBUG_MODE")),
60+
ApiBaseURL: envString("API_BASE_URL"),
61+
ApiSecretKey: envString("API_SECRET_KEY"),
62+
ServerAddress: envString("SERVER_ADDRESS"),
63+
DebugMode: cast.ToBool(envString("DEBUG_MODE")),
4564
LogLevel: envWithDefault("LOG_LEVEL", "INFO"),
46-
GitHubOAuthClientID: os.Getenv("GITHUB_OAUTH_CLIENT_ID"),
47-
GitHubOAuthClientSecret: os.Getenv("GITHUB_OAUTH_CLIENT_SECRET"),
65+
GitHubOAuthClientID: envString("GITHUB_OAUTH_CLIENT_ID"),
66+
GitHubOAuthClientSecret: envString("GITHUB_OAUTH_CLIENT_SECRET"),
4867
GithubOauthScopes: githubOauthScopeConfigs(),
4968
}
5069
}

0 commit comments

Comments
 (0)