2021-01-16 20:41:39 +00:00
|
|
|
package proxy
|
2020-09-02 23:04:12 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2020-09-19 10:32:21 +01:00
|
|
|
"strings"
|
2020-09-02 23:04:12 +01:00
|
|
|
|
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
"github.com/justinas/alice"
|
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
|
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/pkg/middleware"
|
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/pkg/validation"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2021-01-16 20:45:24 +00:00
|
|
|
"goauthentik.io/outpost/pkg/client/crypto"
|
|
|
|
"goauthentik.io/outpost/pkg/models"
|
2020-09-02 23:04:12 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type providerBundle struct {
|
|
|
|
http.Handler
|
|
|
|
|
2021-01-16 20:41:39 +00:00
|
|
|
s *Server
|
|
|
|
proxy *OAuthProxy
|
2020-09-02 23:04:12 +01:00
|
|
|
Host string
|
|
|
|
|
|
|
|
cert *tls.Certificate
|
2021-01-16 20:41:39 +00:00
|
|
|
|
|
|
|
log *log.Entry
|
2020-09-02 23:04:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pb *providerBundle) prepareOpts(provider *models.ProxyOutpostConfig) *options.Options {
|
|
|
|
externalHost, err := url.Parse(*provider.ExternalHost)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Warning("Failed to parse URL, skipping provider")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
providerOpts := &options.Options{}
|
2021-01-16 20:41:39 +00:00
|
|
|
copier.Copy(&providerOpts, getCommonOptions())
|
2020-09-02 23:04:12 +01:00
|
|
|
providerOpts.ClientID = provider.ClientID
|
|
|
|
providerOpts.ClientSecret = provider.ClientSecret
|
|
|
|
|
|
|
|
providerOpts.Cookie.Secret = provider.CookieSecret
|
|
|
|
providerOpts.Cookie.Secure = externalHost.Scheme == "https"
|
|
|
|
|
|
|
|
providerOpts.SkipOIDCDiscovery = true
|
|
|
|
providerOpts.OIDCIssuerURL = *provider.OidcConfiguration.Issuer
|
|
|
|
providerOpts.LoginURL = *provider.OidcConfiguration.AuthorizationEndpoint
|
|
|
|
providerOpts.RedeemURL = *provider.OidcConfiguration.TokenEndpoint
|
|
|
|
providerOpts.OIDCJwksURL = *provider.OidcConfiguration.JwksURI
|
|
|
|
providerOpts.ProfileURL = *provider.OidcConfiguration.UserinfoEndpoint
|
|
|
|
|
2020-09-19 20:05:41 +01:00
|
|
|
if provider.SkipPathRegex != "" {
|
|
|
|
skipRegexes := strings.Split(provider.SkipPathRegex, "\n")
|
|
|
|
providerOpts.SkipAuthRegex = skipRegexes
|
|
|
|
}
|
2020-09-19 10:32:21 +01:00
|
|
|
|
2020-09-02 23:04:12 +01:00
|
|
|
providerOpts.UpstreamServers = []options.Upstream{
|
|
|
|
{
|
2020-09-23 11:21:19 +01:00
|
|
|
ID: "default",
|
|
|
|
URI: *provider.InternalHost,
|
|
|
|
Path: "/",
|
|
|
|
InsecureSkipTLSVerify: *&provider.InternalHostSslValidation,
|
2020-09-02 23:04:12 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if provider.Certificate != nil {
|
2021-01-16 20:41:39 +00:00
|
|
|
pb.log.WithField("provider", provider.ClientID).Debug("Enabling TLS")
|
|
|
|
cert, err := pb.s.ak.Client.Crypto.CryptoCertificatekeypairsRead(&crypto.CryptoCertificatekeypairsReadParams{
|
2020-09-02 23:04:12 +01:00
|
|
|
Context: context.Background(),
|
|
|
|
KpUUID: *provider.Certificate,
|
2021-01-16 20:41:39 +00:00
|
|
|
}, pb.s.ak.Auth)
|
2020-09-02 23:04:12 +01:00
|
|
|
if err != nil {
|
2021-01-16 20:41:39 +00:00
|
|
|
pb.log.WithField("provider", provider.ClientID).WithError(err).Warning("Failed to fetch certificate")
|
2020-09-02 23:04:12 +01:00
|
|
|
return providerOpts
|
|
|
|
}
|
|
|
|
x509cert, err := tls.X509KeyPair([]byte(*cert.Payload.CertificateData), []byte(cert.Payload.KeyData))
|
|
|
|
if err != nil {
|
2021-01-16 20:41:39 +00:00
|
|
|
pb.log.WithField("provider", provider.ClientID).WithError(err).Warning("Failed to parse certificate")
|
2020-09-02 23:04:12 +01:00
|
|
|
return providerOpts
|
|
|
|
}
|
|
|
|
pb.cert = &x509cert
|
2021-01-16 20:41:39 +00:00
|
|
|
pb.log.WithField("provider", provider.ClientID).WithField("certificate-key-pair", *cert.Payload.Name).Debug("Loaded certificates")
|
2020-09-02 23:04:12 +01:00
|
|
|
}
|
|
|
|
return providerOpts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pb *providerBundle) Build(provider *models.ProxyOutpostConfig) {
|
|
|
|
opts := pb.prepareOpts(provider)
|
|
|
|
|
|
|
|
chain := alice.New()
|
|
|
|
|
|
|
|
if opts.ForceHTTPS {
|
|
|
|
_, httpsPort, err := net.SplitHostPort(opts.HTTPSAddress)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("FATAL: invalid HTTPS address %q: %v", opts.HTTPAddress, err)
|
|
|
|
}
|
|
|
|
chain = chain.Append(middleware.NewRedirectToHTTPS(httpsPort))
|
|
|
|
}
|
|
|
|
|
|
|
|
healthCheckPaths := []string{opts.PingPath}
|
|
|
|
healthCheckUserAgents := []string{opts.PingUserAgent}
|
|
|
|
if opts.GCPHealthChecks {
|
|
|
|
healthCheckPaths = append(healthCheckPaths, "/liveness_check", "/readiness_check")
|
|
|
|
healthCheckUserAgents = append(healthCheckUserAgents, "GoogleHC/1.0")
|
|
|
|
}
|
|
|
|
|
|
|
|
// To silence logging of health checks, register the health check handler before
|
|
|
|
// the logging handler
|
|
|
|
if opts.Logging.SilencePing {
|
|
|
|
chain = chain.Append(middleware.NewHealthCheck(healthCheckPaths, healthCheckUserAgents), LoggingHandler)
|
|
|
|
} else {
|
|
|
|
chain = chain.Append(LoggingHandler, middleware.NewHealthCheck(healthCheckPaths, healthCheckUserAgents))
|
|
|
|
}
|
|
|
|
|
|
|
|
err := validation.Validate(opts)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("%s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-01-16 20:41:39 +00:00
|
|
|
oauthproxy, err := NewOAuthProxy(opts)
|
2020-09-02 23:04:12 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("ERROR: Failed to initialise OAuth2 Proxy: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2020-09-30 10:49:06 +01:00
|
|
|
if *&provider.BasicAuthEnabled {
|
|
|
|
oauthproxy.SetBasicAuth = true
|
|
|
|
oauthproxy.BasicAuthUserAttribute = provider.BasicAuthUserAttribute
|
|
|
|
oauthproxy.BasicAuthPasswordAttribute = provider.BasicAuthPasswordAttribute
|
|
|
|
}
|
|
|
|
|
2020-09-02 23:04:12 +01:00
|
|
|
pb.proxy = oauthproxy
|
|
|
|
pb.Handler = chain.Then(oauthproxy)
|
|
|
|
}
|