2020-05-26 19:56:10 +01:00
package options
import (
"fmt"
"net/url"
"strconv"
2020-08-31 14:05:52 +01:00
"strings"
2020-05-26 19:56:10 +01:00
"time"
"github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
2020-07-12 16:47:25 +01:00
"github.com/spf13/pflag"
2020-05-26 19:56:10 +01:00
)
type LegacyOptions struct {
// Legacy options related to upstream servers
2020-07-12 16:47:25 +01:00
LegacyUpstreams LegacyUpstreams ` cfg:",squash" `
2020-05-26 19:56:10 +01:00
Options Options ` cfg:",squash" `
}
func NewLegacyOptions ( ) * LegacyOptions {
return & LegacyOptions {
2020-07-12 16:47:25 +01:00
LegacyUpstreams : LegacyUpstreams {
PassHostHeader : true ,
ProxyWebSockets : true ,
FlushInterval : time . Duration ( 1 ) * time . Second ,
} ,
2020-05-26 19:56:10 +01:00
Options : * NewOptions ( ) ,
}
}
func ( l * LegacyOptions ) ToOptions ( ) ( * Options , error ) {
2020-07-12 16:47:25 +01:00
upstreams , err := l . LegacyUpstreams . convert ( )
2020-05-26 19:56:10 +01:00
if err != nil {
return nil , fmt . Errorf ( "error converting upstreams: %v" , err )
}
l . Options . UpstreamServers = upstreams
return & l . Options , nil
}
2020-07-12 16:47:25 +01:00
type LegacyUpstreams struct {
FlushInterval time . Duration ` flag:"flush-interval" cfg:"flush_interval" `
PassHostHeader bool ` flag:"pass-host-header" cfg:"pass_host_header" `
ProxyWebSockets bool ` flag:"proxy-websockets" cfg:"proxy_websockets" `
SSLUpstreamInsecureSkipVerify bool ` flag:"ssl-upstream-insecure-skip-verify" cfg:"ssl_upstream_insecure_skip_verify" `
Upstreams [ ] string ` flag:"upstream" cfg:"upstreams" `
}
func legacyUpstreamsFlagSet ( ) * pflag . FlagSet {
flagSet := pflag . NewFlagSet ( "upstreams" , pflag . ExitOnError )
flagSet . Duration ( "flush-interval" , time . Duration ( 1 ) * time . Second , "period between response flushing when streaming responses" )
flagSet . Bool ( "pass-host-header" , true , "pass the request Host Header to upstream" )
flagSet . Bool ( "proxy-websockets" , true , "enables WebSocket proxying" )
flagSet . Bool ( "ssl-upstream-insecure-skip-verify" , false , "skip validation of certificates presented when using HTTPS upstreams" )
flagSet . StringSlice ( "upstream" , [ ] string { } , "the http url(s) of the upstream endpoint, file:// paths for static files or static://<status_code> for static response. Routing is based on the path" )
return flagSet
}
func ( l * LegacyUpstreams ) convert ( ) ( Upstreams , error ) {
2020-05-26 19:56:10 +01:00
upstreams := Upstreams { }
2020-07-12 16:47:25 +01:00
for _ , upstreamString := range l . Upstreams {
2020-05-26 19:56:10 +01:00
u , err := url . Parse ( upstreamString )
if err != nil {
return nil , fmt . Errorf ( "could not parse upstream %q: %v" , upstreamString , err )
}
if u . Path == "" {
u . Path = "/"
}
upstream := Upstream {
ID : u . Path ,
Path : u . Path ,
URI : upstreamString ,
2020-07-12 16:47:25 +01:00
InsecureSkipTLSVerify : l . SSLUpstreamInsecureSkipVerify ,
2020-07-19 14:00:52 +01:00
PassHostHeader : & l . PassHostHeader ,
ProxyWebSockets : & l . ProxyWebSockets ,
2020-07-12 16:47:25 +01:00
FlushInterval : & l . FlushInterval ,
2020-05-26 19:56:10 +01:00
}
switch u . Scheme {
case "file" :
if u . Fragment != "" {
upstream . ID = u . Fragment
upstream . Path = u . Fragment
2020-08-31 14:05:52 +01:00
// Trim the fragment from the end of the URI
upstream . URI = strings . SplitN ( upstreamString , "#" , 2 ) [ 0 ]
2020-05-26 19:56:10 +01:00
}
case "static" :
responseCode , err := strconv . Atoi ( u . Host )
if err != nil {
2020-08-10 11:44:08 +01:00
logger . Errorf ( "unable to convert %q to int, use default \"200\"" , u . Host )
2020-05-26 19:56:10 +01:00
responseCode = 200
}
upstream . Static = true
upstream . StaticCode = & responseCode
2020-08-31 11:22:10 +01:00
// This is not allowed to be empty and must be unique
2020-05-26 19:56:10 +01:00
upstream . ID = upstreamString
2020-08-31 11:22:10 +01:00
// We only support the root path in the legacy config
upstream . Path = "/"
2020-05-26 19:56:10 +01:00
// Force defaults compatible with static responses
upstream . URI = ""
upstream . InsecureSkipTLSVerify = false
2020-07-19 14:00:52 +01:00
upstream . PassHostHeader = nil
upstream . ProxyWebSockets = nil
2020-08-31 11:22:10 +01:00
upstream . FlushInterval = nil
2020-05-26 19:56:10 +01:00
}
upstreams = append ( upstreams , upstream )
}
return upstreams , nil
}