2020-10-08 02:18:34 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2020-10-08 18:28:21 +00:00
|
|
|
package manager
|
2020-10-08 02:18:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"k8s.io/klog/v2"
|
|
|
|
|
|
|
|
"go.pinniped.dev/internal/oidc"
|
|
|
|
"go.pinniped.dev/internal/oidc/discovery"
|
2020-10-08 18:28:21 +00:00
|
|
|
"go.pinniped.dev/internal/oidc/provider"
|
2020-10-08 02:18:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Manager can manage multiple active OIDC providers. It acts as a request router for them.
|
|
|
|
//
|
|
|
|
// It is thread-safe.
|
|
|
|
type Manager struct {
|
|
|
|
mu sync.RWMutex
|
2020-10-08 21:40:56 +00:00
|
|
|
providers []*provider.OIDCProvider
|
|
|
|
providerHandlers map[string]http.Handler // map of all routes for all providers
|
|
|
|
nextHandler http.Handler // the next handler in a chain, called when this manager didn't know how to handle a request
|
2020-10-08 02:18:34 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 18:28:21 +00:00
|
|
|
// NewManager returns an empty Manager.
|
2020-10-08 02:18:34 +00:00
|
|
|
// nextHandler will be invoked for any requests that could not be handled by this manager's providers.
|
|
|
|
func NewManager(nextHandler http.Handler) *Manager {
|
2020-10-08 21:40:56 +00:00
|
|
|
return &Manager{providerHandlers: make(map[string]http.Handler), nextHandler: nextHandler}
|
2020-10-08 02:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetProviders adds or updates all the given providerHandlers using each provider's issuer string
|
|
|
|
// as the name of the provider to decide if it is an add or update operation.
|
|
|
|
//
|
|
|
|
// It also removes any providerHandlers that were previously added but were not passed in to
|
|
|
|
// the current invocation.
|
|
|
|
//
|
|
|
|
// This method assumes that all of the OIDCProvider arguments have already been validated
|
|
|
|
// by someone else before they are passed to this method.
|
2020-10-08 21:40:56 +00:00
|
|
|
func (m *Manager) SetProviders(oidcProviders ...*provider.OIDCProvider) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
m.providers = oidcProviders
|
|
|
|
m.providerHandlers = make(map[string]http.Handler)
|
|
|
|
|
2020-10-08 02:18:34 +00:00
|
|
|
for _, incomingProvider := range oidcProviders {
|
2020-10-08 21:40:56 +00:00
|
|
|
m.providerHandlers[incomingProvider.IssuerHost()+"/"+incomingProvider.IssuerPath()+oidc.WellKnownEndpointPath] = discovery.New(incomingProvider.Issuer())
|
|
|
|
klog.InfoS("oidc provider manager added or updated issuer", "issuer", incomingProvider.Issuer())
|
2020-10-08 02:18:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP implements the http.Handler interface.
|
2020-10-08 21:40:56 +00:00
|
|
|
func (m *Manager) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
requestHandler := m.findHandler(req)
|
|
|
|
|
|
|
|
klog.InfoS(
|
|
|
|
"oidc provider manager examining request",
|
|
|
|
"method", req.Method,
|
|
|
|
"host", req.Host,
|
|
|
|
"path", req.URL.Path,
|
|
|
|
"foundMatchingIssuer", requestHandler != nil,
|
|
|
|
)
|
2020-10-08 02:18:34 +00:00
|
|
|
|
2020-10-08 21:40:56 +00:00
|
|
|
if requestHandler == nil {
|
|
|
|
requestHandler = m.nextHandler // couldn't find an issuer to handle the request
|
2020-10-08 02:18:34 +00:00
|
|
|
}
|
2020-10-08 21:40:56 +00:00
|
|
|
requestHandler.ServeHTTP(resp, req)
|
2020-10-08 02:18:34 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 21:40:56 +00:00
|
|
|
func (m *Manager) findHandler(req *http.Request) http.Handler {
|
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
|
|
|
|
return m.providerHandlers[req.Host+"/"+req.URL.Path]
|
2020-10-08 02:18:34 +00:00
|
|
|
}
|