Skip to content

Commit 08b888a

Browse files
tommyktommy kellyjpweber
authored
Environment variable configuration (#12)
* adding environment variable config lookup * added MARGIN env var that was missing * Added not env lookups for new params have been added since this was last touched. Signed-off-by: Jim Weber <[email protected]> * Change env lookup function to non-exported functions per PR review. Signed-off-by: Jim Weber <[email protected]> --------- Signed-off-by: Jim Weber <[email protected]> Co-authored-by: tommy kelly <[email protected]> Co-authored-by: Jim Weber <[email protected]>
1 parent f5e3403 commit 08b888a

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

main.go

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,28 +70,47 @@ type oidcConfig struct {
7070
password string
7171
}
7272

73+
func lookupEnvOrString(key string, defaultVal string) string {
74+
if val, ok := os.LookupEnv(key); ok {
75+
return val
76+
}
77+
return defaultVal
78+
}
79+
80+
func lookupEnvOrDuration(key string, defaultVal time.Duration) time.Duration {
81+
if val, ok := os.LookupEnv(key); ok {
82+
d, err := time.ParseDuration(val)
83+
if err != nil {
84+
fmt.Sprintln("error trying to parse duration, using default value: ", err)
85+
return defaultVal
86+
}
87+
return d
88+
}
89+
return defaultVal
90+
}
91+
7392
func parseFlags() (*config, error) {
7493
cfg := &config{}
7594
flag.StringVar(&cfg.name, "debug.name", "token-refresher", "A name to add as a prefix to log lines.")
76-
logLevelRaw := flag.String("log.level", "info", "The log filtering level. Options: 'error', 'warn', 'info', 'debug'.")
77-
flag.StringVar(&cfg.logFormat, "log.format", "logfmt", "The log format to use. Options: 'logfmt', 'json'.")
78-
flag.StringVar(&cfg.server.listenInternal, "web.internal.listen", ":8081", "The address on which the internal server listens.")
79-
flag.StringVar(&cfg.server.listen, "web.listen", ":8080", "The address on which the proxy server listens.")
80-
flag.StringVar(&cfg.oidc.issuerURL, "oidc.issuer-url", "", "The OIDC issuer URL, see https://openid.net/specs/openid-connect-discovery-1_0.html#IssuerDiscovery.")
81-
flag.StringVar(&cfg.oidc.clientSecret, "oidc.client-secret", "", "The OIDC client secret, see https://tools.ietf.org/html/rfc6749#section-2.3.")
82-
flag.StringVar(&cfg.oidc.clientID, "oidc.client-id", "", "The OIDC client ID, see https://tools.ietf.org/html/rfc6749#section-2.3.")
83-
flag.StringVar(&cfg.oidc.audience, "oidc.audience", "", "The audience for whom the access token is intended, see https://openid.net/specs/openid-connect-core-1_0.html#IDToken.")
84-
flag.StringVar(&cfg.oidc.username, "oidc.username", "", "The username to use for OIDC authentication. If both username and password are set then grant_type is set to password.")
85-
flag.StringVar(&cfg.oidc.password, "oidc.password", "", "The password to use for OIDC authentication. If both username and password are set then grant_type is set to password.")
95+
logLevelRaw := flag.String("log.level", lookupEnvOrString("LOG_LEVEL", "info"), "The log filtering level. Options: 'error', 'warn', 'info', 'debug'.")
96+
flag.StringVar(&cfg.logFormat, "log.format", lookupEnvOrString("LOG_FORMAT", "logfmt"), "The log format to use. Options: 'logfmt', 'json'.")
97+
flag.StringVar(&cfg.server.listenInternal, "web.internal.listen", lookupEnvOrString("WEB_INTERNAL_LISTEN", ":8081"), "The address on which the internal server listens.")
98+
flag.StringVar(&cfg.server.listen, "web.listen", lookupEnvOrString("WEB_LISTEN", ":8080"), "The address on which the proxy server listens.")
99+
flag.StringVar(&cfg.oidc.issuerURL, "oidc.issuer-url", lookupEnvOrString("OIDC_ISSUER_URL", ""), "The OIDC issuer URL, see https://openid.net/specs/openid-connect-discovery-1_0.html#IssuerDiscovery.")
100+
flag.StringVar(&cfg.oidc.clientSecret, "oidc.client-secret", lookupEnvOrString("OIDC_CLIENT_SECRET", ""), "The OIDC client secret, see https://tools.ietf.org/html/rfc6749#section-2.3.")
101+
flag.StringVar(&cfg.oidc.clientID, "oidc.client-id", lookupEnvOrString("OIDC_CLIENT_ID", ""), "The OIDC client ID, see https://tools.ietf.org/html/rfc6749#section-2.3.")
102+
flag.StringVar(&cfg.oidc.audience, "oidc.audience", lookupEnvOrString("OIDC_AUDIENCE", ""), "The audience for whom the access token is intended, see https://openid.net/specs/openid-connect-core-1_0.html#IDToken.")
103+
flag.StringVar(&cfg.oidc.username, "oidc.username", lookupEnvOrString("OIDC_USERNAME", ""), "The username to use for OIDC authentication. If both username and password are set then grant_type is set to password.")
104+
flag.StringVar(&cfg.oidc.password, "oidc.password", lookupEnvOrString("OIDC_PASSWORD", ""), "The password to use for OIDC authentication. If both username and password are set then grant_type is set to password.")
86105
flag.StringSliceVar(&cfg.scope, "scope", []string{}, "The scope to be included in the payload data of the token. Scopes can either be comma-separated or space-separated.")
87-
flag.StringVar(&cfg.file, "file", "", "The path to the file in which to write the retrieved token.")
88-
flag.StringVar(&cfg.tempFile, "temp-file", "", "The path to a temporary file to use for atomically update the token file. If left empty, \".tmp\" will be suffixed to the token file.")
89-
rawURL := flag.String("url", "", "The target URL to which to proxy requests. All requests will have the acces token in the Authorization HTTP header.(DEPRECATED: Use -upstream.url instead)")
90-
rawUpstreamURL := flag.String("upstream.url", "", "The target URL to which to proxy requests. All requests will have the acces token in the Authorization HTTP header.")
91-
flag.StringVar(&cfg.upstream.caFile, "upstream.ca-file", "", "The path to the CA file to verify upstream server TLS certificates.")
92-
flag.DurationVar(&cfg.upstream.readTimeout, "upstream.read-timeout", 0, "The time from when the connection is accepted to when the request body is fully read.")
93-
flag.DurationVar(&cfg.upstream.writeTimeout, "upstream.write-timeout", 0, "The time from the end of the request header read to the end of the response write .")
94-
flag.DurationVar(&cfg.margin, "margin", 5*time.Minute, "The margin of time before a token expires to try to refresh it.")
106+
flag.StringVar(&cfg.file, "file", lookupEnvOrString("FILE", ""), "The path to the file in which to write the retrieved token.")
107+
flag.StringVar(&cfg.tempFile, "temp-file", lookupEnvOrString("TEMP_FILE", ""), "The path to a temporary file to use for atomically update the token file. If left empty, \".tmp\" will be suffixed to the token file.")
108+
rawURL := flag.String("url", lookupEnvOrString("URL", ""), "The target URL to which to proxy requests. All requests will have the acces token in the Authorization HTTP header. (DEPRECATED: Use -upstream.url instead)")
109+
rawUpstreamURL := flag.String("upstream.url", lookupEnvOrString("UPSTREAM_URL", ""), "The target URL to which to proxy requests. All requests will have the acces token in the Authorization HTTP header.")
110+
flag.StringVar(&cfg.upstream.caFile, "upstream.ca-file", lookupEnvOrString("UPSTREAM_CA_FILE", ""), "The path to the CA file to verify upstream server TLS certificates.")
111+
flag.DurationVar(&cfg.upstream.readTimeout, "upstream.read-timeout", lookupEnvOrDuration("UPSTREAM_READ_TIMEOUT", 0), "The time from when the connection is accepted to when the request body is fully read.")
112+
flag.DurationVar(&cfg.upstream.writeTimeout, "upstream.write-timeout", lookupEnvOrDuration("UPSTREAM_WRITE_TIMEOUT", 0), "The time from the end of the request header read to the end of the response write .")
113+
flag.DurationVar(&cfg.margin, "margin", lookupEnvOrDuration("MARGIN", 5*time.Minute), "The margin of time before a token expires to try to refresh it.")
95114

96115
flag.Parse()
97116

0 commit comments

Comments
 (0)