2020-04-13 13:50:34 +01:00
package options
import (
"crypto"
"net/url"
2020-09-30 01:44:42 +09:00
ipapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/ip"
2022-02-15 17:24:48 +00:00
internaloidc "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/providers/oidc"
2020-04-13 14:20:04 +01:00
"github.com/spf13/pflag"
2020-04-13 13:50:34 +01:00
)
// SignatureData holds hmacauth signature hash and key
type SignatureData struct {
Hash crypto . Hash
Key string
}
// Options holds Configuration Options that can be set by Command Line Flag,
// or Config File
type Options struct {
2023-10-25 11:25:01 +02:00
ProxyPrefix string ` flag:"proxy-prefix" cfg:"proxy_prefix" `
PingPath string ` flag:"ping-path" cfg:"ping_path" `
PingUserAgent string ` flag:"ping-user-agent" cfg:"ping_user_agent" `
ReadyPath string ` flag:"ready-path" cfg:"ready_path" `
ReverseProxy bool ` flag:"reverse-proxy" cfg:"reverse_proxy" `
RealClientIPHeader string ` flag:"real-client-ip-header" cfg:"real_client_ip_header" `
TrustedIPs [ ] string ` flag:"trusted-ip" cfg:"trusted_ips" `
ForceHTTPS bool ` flag:"force-https" cfg:"force_https" `
RawRedirectURL string ` flag:"redirect-url" cfg:"redirect_url" `
RelativeRedirectURL bool ` flag:"relative-redirect-url" cfg:"relative_redirect_url" `
2021-04-03 19:06:30 +03:00
AuthenticatedEmailsFile string ` flag:"authenticated-emails-file" cfg:"authenticated_emails_file" `
EmailDomains [ ] string ` flag:"email-domain" cfg:"email_domains" `
WhitelistDomains [ ] string ` flag:"whitelist-domain" cfg:"whitelist_domains" `
HtpasswdFile string ` flag:"htpasswd-file" cfg:"htpasswd_file" `
HtpasswdUserGroups [ ] string ` flag:"htpasswd-user-group" cfg:"htpasswd_user_groups" `
2020-04-13 13:50:34 +01:00
2021-02-06 17:40:51 +00:00
Cookie Cookie ` cfg:",squash" `
Session SessionOptions ` cfg:",squash" `
Logging Logging ` cfg:",squash" `
Templates Templates ` cfg:",squash" `
2020-04-13 13:50:34 +01:00
2020-05-26 19:56:10 +01:00
// Not used in the legacy config, name not allowed to match an external key (upstreams)
// TODO(JoelSpeed): Rename when legacy config is removed
2021-09-17 11:08:18 +00:00
UpstreamServers UpstreamConfig ` cfg:",internal" `
2020-05-26 19:56:10 +01:00
2020-07-29 20:08:46 +01:00
InjectRequestHeaders [ ] Header ` cfg:",internal" `
InjectResponseHeaders [ ] Header ` cfg:",internal" `
2021-02-14 18:47:15 +00:00
Server Server ` cfg:",internal" `
MetricsServer Server ` cfg:",internal" `
2021-04-03 19:06:30 +03:00
Providers Providers ` cfg:",internal" `
2022-09-11 17:09:32 +02:00
APIRoutes [ ] string ` flag:"api-route" cfg:"api_routes" `
2020-05-26 19:56:10 +01:00
SkipAuthRegex [ ] string ` flag:"skip-auth-regex" cfg:"skip_auth_regex" `
2020-09-22 18:54:32 -07:00
SkipAuthRoutes [ ] string ` flag:"skip-auth-route" cfg:"skip_auth_routes" `
2020-05-26 19:56:10 +01:00
SkipJwtBearerTokens bool ` flag:"skip-jwt-bearer-tokens" cfg:"skip_jwt_bearer_tokens" `
ExtraJwtIssuers [ ] string ` flag:"extra-jwt-issuers" cfg:"extra_jwt_issuers" `
SkipProviderButton bool ` flag:"skip-provider-button" cfg:"skip_provider_button" `
SSLInsecureSkipVerify bool ` flag:"ssl-insecure-skip-verify" cfg:"ssl_insecure_skip_verify" `
SkipAuthPreflight bool ` flag:"skip-auth-preflight" cfg:"skip_auth_preflight" `
2021-10-05 11:24:47 +02:00
ForceJSONErrors bool ` flag:"force-json-errors" cfg:"force_json_errors" `
2024-01-20 20:08:30 +01:00
EncodeState bool ` flag:"encode-state" cfg:"encode_state" `
2023-11-20 17:36:03 +08:00
AllowQuerySemicolons bool ` flag:"allow-query-semicolons" cfg:"allow_query_semicolons" `
2020-04-13 13:50:34 +01:00
2020-05-12 00:13:46 +01:00
SignatureKey string ` flag:"signature-key" cfg:"signature_key" `
GCPHealthChecks bool ` flag:"gcp-healthchecks" cfg:"gcp_healthchecks" `
2020-04-13 13:50:34 +01:00
2021-03-21 18:49:30 +00:00
// This is used for backwards compatibility for basic auth users
LegacyPreferEmailToUser bool ` cfg:",internal" `
2020-04-13 13:50:34 +01:00
// internal values that are set after config validation
redirectURL * url . URL
signatureData * SignatureData
2022-02-16 15:55:44 +00:00
oidcVerifier internaloidc . IDTokenVerifier
jwtBearerVerifiers [ ] internaloidc . IDTokenVerifier
2020-05-23 15:17:41 +01:00
realClientIPParser ipapi . RealClientIPParser
2020-04-13 13:50:34 +01:00
}
// Options for Getting internal values
2022-02-16 15:55:44 +00:00
func ( o * Options ) GetRedirectURL ( ) * url . URL { return o . redirectURL }
func ( o * Options ) GetSignatureData ( ) * SignatureData { return o . signatureData }
func ( o * Options ) GetOIDCVerifier ( ) internaloidc . IDTokenVerifier { return o . oidcVerifier }
func ( o * Options ) GetJWTBearerVerifiers ( ) [ ] internaloidc . IDTokenVerifier {
2022-02-15 17:12:22 +01:00
return o . jwtBearerVerifiers
}
2020-05-23 15:17:41 +01:00
func ( o * Options ) GetRealClientIPParser ( ) ipapi . RealClientIPParser { return o . realClientIPParser }
2020-04-13 13:50:34 +01:00
// Options for Setting internal values
2022-02-16 15:55:44 +00:00
func ( o * Options ) SetRedirectURL ( s * url . URL ) { o . redirectURL = s }
func ( o * Options ) SetSignatureData ( s * SignatureData ) { o . signatureData = s }
func ( o * Options ) SetOIDCVerifier ( s internaloidc . IDTokenVerifier ) { o . oidcVerifier = s }
func ( o * Options ) SetJWTBearerVerifiers ( s [ ] internaloidc . IDTokenVerifier ) { o . jwtBearerVerifiers = s }
func ( o * Options ) SetRealClientIPParser ( s ipapi . RealClientIPParser ) { o . realClientIPParser = s }
2020-04-13 13:50:34 +01:00
// NewOptions constructs a new Options with defaulted values
func NewOptions ( ) * Options {
return & Options {
2021-04-03 19:06:30 +03:00
ProxyPrefix : "/oauth2" ,
Providers : providerDefaults ( ) ,
PingPath : "/ping" ,
2022-12-23 11:08:12 +02:00
ReadyPath : "/ready" ,
2021-04-03 19:06:30 +03:00
RealClientIPHeader : "X-Real-IP" ,
ForceHTTPS : false ,
Cookie : cookieDefaults ( ) ,
Session : sessionOptionsDefaults ( ) ,
Templates : templatesDefaults ( ) ,
SkipAuthPreflight : false ,
Logging : loggingDefaults ( ) ,
2020-04-13 13:50:34 +01:00
}
}
2020-04-13 14:20:04 +01:00
// NewFlagSet creates a new FlagSet with all of the flags required by Options
func NewFlagSet ( ) * pflag . FlagSet {
flagSet := pflag . NewFlagSet ( "oauth2-proxy" , pflag . ExitOnError )
flagSet . Bool ( "reverse-proxy" , false , "are we running behind a reverse proxy, controls whether headers like X-Real-Ip are accepted" )
flagSet . String ( "real-client-ip-header" , "X-Real-IP" , "Header used to determine the real IP of the client (one of: X-Forwarded-For, X-Real-IP, or X-ProxyUser-IP)" )
2020-07-11 12:10:58 +02:00
flagSet . StringSlice ( "trusted-ip" , [ ] string { } , "list of IPs or CIDR ranges to allow to bypass authentication. WARNING: trusting by IP has inherent security flaws, read the configuration documentation for more information." )
2020-04-13 14:20:04 +01:00
flagSet . Bool ( "force-https" , false , "force HTTPS redirect for HTTP requests" )
flagSet . String ( "redirect-url" , "" , "the OAuth Redirect URL. ie: \"https://internalapp.yourcompany.com/oauth2/callback\"" )
2023-10-25 11:25:01 +02:00
flagSet . Bool ( "relative-redirect-url" , false , "allow relative OAuth Redirect URL." )
2020-09-22 18:54:32 -07:00
flagSet . StringSlice ( "skip-auth-regex" , [ ] string { } , "(DEPRECATED for --skip-auth-route) bypass authentication for requests path's that match (may be given multiple times)" )
2022-08-19 12:46:25 +02:00
flagSet . StringSlice ( "skip-auth-route" , [ ] string { } , "bypass authentication for requests that match the method & path. Format: method=path_regex OR method!=path_regex. For all methods: path_regex OR !=path_regex" )
2022-09-11 17:09:32 +02:00
flagSet . StringSlice ( "api-route" , [ ] string { } , "return HTTP 401 instead of redirecting to authentication server if token is not valid. Format: path_regex" )
2020-04-13 14:20:04 +01:00
flagSet . Bool ( "skip-provider-button" , false , "will skip sign-in-page to directly reach the next step: oauth/start" )
flagSet . Bool ( "skip-auth-preflight" , false , "will skip authentication for OPTIONS requests" )
flagSet . Bool ( "ssl-insecure-skip-verify" , false , "skip validation of certificates presented when using HTTPS providers" )
flagSet . Bool ( "skip-jwt-bearer-tokens" , false , "will skip requests that have verified JWT bearer tokens (default false)" )
2021-10-05 11:24:47 +02:00
flagSet . Bool ( "force-json-errors" , false , "will force JSON errors instead of HTTP error pages or redirects" )
2024-01-20 20:08:30 +01:00
flagSet . Bool ( "encode-state" , false , "will encode oauth state with base64" )
2023-11-20 17:36:03 +08:00
flagSet . Bool ( "allow-query-semicolons" , false , "allow the use of semicolons in query args" )
2020-04-13 14:20:04 +01:00
flagSet . StringSlice ( "extra-jwt-issuers" , [ ] string { } , "if skip-jwt-bearer-tokens is set, a list of extra JWT issuer=audience pairs (where the issuer URL has a .well-known/openid-configuration or a .well-known/jwks.json)" )
flagSet . StringSlice ( "email-domain" , [ ] string { } , "authenticate emails with the specified domain (may be given multiple times). Use * to authenticate any email" )
2021-07-28 10:12:00 +02:00
flagSet . StringSlice ( "whitelist-domain" , [ ] string { } , "allowed domains for redirection after authentication. Prefix domain with a . or a *. to allow subdomains (eg .example.com, *.example.com)" )
2020-04-13 14:20:04 +01:00
flagSet . String ( "authenticated-emails-file" , "" , "authenticate against emails via file (one per line)" )
2020-09-11 13:32:00 +03:00
flagSet . String ( "htpasswd-file" , "" , "additionally authenticate against a htpasswd file. Entries must be created with \"htpasswd -B\" for bcrypt encryption" )
2021-02-25 13:02:23 -08:00
flagSet . StringSlice ( "htpasswd-user-group" , [ ] string { } , "the groups to be set on sessions for htpasswd users (may be given multiple times)" )
2020-04-13 14:20:04 +01:00
flagSet . String ( "proxy-prefix" , "/oauth2" , "the url root path that this proxy should be nested under (e.g. /<oauth2>/sign_in)" )
flagSet . String ( "ping-path" , "/ping" , "the ping endpoint that can be used for basic health checks" )
2020-06-12 06:56:31 -07:00
flagSet . String ( "ping-user-agent" , "" , "special User-Agent that will be used for basic health checks" )
2022-12-23 11:08:12 +02:00
flagSet . String ( "ready-path" , "/ready" , "the ready endpoint that can be used for deep health checks" )
2020-04-13 14:20:04 +01:00
flagSet . String ( "session-store-type" , "cookie" , "the session storage provider to use" )
2020-07-14 15:02:10 -07:00
flagSet . Bool ( "session-cookie-minimal" , false , "strip OAuth tokens from cookie session stores if they aren't needed (cookie session store only)" )
2024-01-21 03:00:02 +07:00
flagSet . String ( "redis-connection-url" , "" , "URL of redis server for redis session storage (eg: redis://[USER[:PASSWORD]@]HOST[:PORT])" )
flagSet . String ( "redis-username" , "" , "Redis username. Applicable for Redis configurations where ACL has been configured. Will override any username set in `--redis-connection-url`" )
2020-08-05 18:34:25 -07:00
flagSet . String ( "redis-password" , "" , "Redis password. Applicable for all Redis configurations. Will override any password set in `--redis-connection-url`" )
2020-04-13 14:20:04 +01:00
flagSet . Bool ( "redis-use-sentinel" , false , "Connect to redis via sentinels. Must set --redis-sentinel-master-name and --redis-sentinel-connection-urls to use this feature" )
2020-08-05 18:34:25 -07:00
flagSet . String ( "redis-sentinel-password" , "" , "Redis sentinel password. Used only for sentinel connection; any redis node passwords need to use `--redis-password`" )
2020-04-13 14:20:04 +01:00
flagSet . String ( "redis-sentinel-master-name" , "" , "Redis sentinel master name. Used in conjunction with --redis-use-sentinel" )
flagSet . String ( "redis-ca-path" , "" , "Redis custom CA path" )
flagSet . Bool ( "redis-insecure-skip-tls-verify" , false , "Use insecure TLS connection to redis" )
2024-01-21 03:00:02 +07:00
flagSet . StringSlice ( "redis-sentinel-connection-urls" , [ ] string { } , "List of Redis sentinel connection URLs (eg redis://[USER[:PASSWORD]@]HOST[:PORT]). Used in conjunction with --redis-use-sentinel" )
2020-04-13 14:20:04 +01:00
flagSet . Bool ( "redis-use-cluster" , false , "Connect to redis cluster. Must set --redis-cluster-connection-urls to use this feature" )
2024-01-21 03:00:02 +07:00
flagSet . StringSlice ( "redis-cluster-connection-urls" , [ ] string { } , "List of Redis cluster connection URLs (eg redis://[USER[:PASSWORD]@]HOST[:PORT]). Used in conjunction with --redis-use-cluster" )
2022-08-09 23:57:13 +03:00
flagSet . Int ( "redis-connection-idle-timeout" , 0 , "Redis connection idle timeout seconds, if Redis timeout option is non-zero, the --redis-connection-idle-timeout must be less then Redis timeout option" )
2020-04-13 14:20:04 +01:00
flagSet . String ( "signature-key" , "" , "GAP-Signature request signature key (algorithm:secretkey)" )
flagSet . Bool ( "gcp-healthchecks" , false , "Enable GCP/GKE healthcheck endpoints" )
2020-05-23 17:04:32 +01:00
flagSet . AddFlagSet ( cookieFlagSet ( ) )
2020-05-12 00:51:23 +01:00
flagSet . AddFlagSet ( loggingFlagSet ( ) )
2021-02-06 17:40:51 +00:00
flagSet . AddFlagSet ( templatesFlagSet ( ) )
2020-05-12 00:51:23 +01:00
2020-04-13 14:20:04 +01:00
return flagSet
}