mirror of
https://github.com/pomerium/pomerium.git
synced 2025-04-29 18:36:30 +02:00
* databroker: add databroker, identity manager, update cache (#864) * databroker: add databroker, identity manager, update cache * fix cache tests * directory service (#885) * directory: add google and okta * add onelogin * add directory provider * initialize before sync, upate google provider, remove dead code * add azure provider * fix azure provider * fix gitlab * add gitlab test, fix azure test * hook up okta * remove dead code * fix tests * fix flaky test * authorize: use databroker data for rego policy (#904) * wip * add directory provider * initialize before sync, upate google provider, remove dead code * fix flaky test * update authorize to use databroker data * implement signed jwt * wait for session and user to appear * fix test * directory service (#885) * directory: add google and okta * add onelogin * add directory provider * initialize before sync, upate google provider, remove dead code * add azure provider * fix azure provider * fix gitlab * add gitlab test, fix azure test * hook up okta * remove dead code * fix tests * fix flaky test * remove log line * only redirect when no session id exists * prepare rego query as part of create * return on ctx done * retry on disconnect for sync * move jwt signing * use != * use parent ctx for wait * remove session state, remove logs * rename function * add log message * pre-allocate slice * use errgroup * return nil on eof for sync * move check * disable timeout on gRPC requests in envoy * fix gitlab test * use v4 backoff * authenticate: databroker changes (#914) * wip * add directory provider * initialize before sync, upate google provider, remove dead code * fix flaky test * update authorize to use databroker data * implement signed jwt * wait for session and user to appear * fix test * directory service (#885) * directory: add google and okta * add onelogin * add directory provider * initialize before sync, upate google provider, remove dead code * add azure provider * fix azure provider * fix gitlab * add gitlab test, fix azure test * hook up okta * remove dead code * fix tests * fix flaky test * remove log line * only redirect when no session id exists * prepare rego query as part of create * return on ctx done * retry on disconnect for sync * move jwt signing * use != * use parent ctx for wait * remove session state, remove logs * rename function * add log message * pre-allocate slice * use errgroup * return nil on eof for sync * move check * disable timeout on gRPC requests in envoy * fix dashboard * delete session on logout * permanently delete sessions once they are marked as deleted * remove permanent delete * fix tests * remove groups and refresh test * databroker: remove dead code, rename cache url, move dashboard (#925) * wip * add directory provider * initialize before sync, upate google provider, remove dead code * fix flaky test * update authorize to use databroker data * implement signed jwt * wait for session and user to appear * fix test * directory service (#885) * directory: add google and okta * add onelogin * add directory provider * initialize before sync, upate google provider, remove dead code * add azure provider * fix azure provider * fix gitlab * add gitlab test, fix azure test * hook up okta * remove dead code * fix tests * fix flaky test * remove log line * only redirect when no session id exists * prepare rego query as part of create * return on ctx done * retry on disconnect for sync * move jwt signing * use != * use parent ctx for wait * remove session state, remove logs * rename function * add log message * pre-allocate slice * use errgroup * return nil on eof for sync * move check * disable timeout on gRPC requests in envoy * fix dashboard * delete session on logout * permanently delete sessions once they are marked as deleted * remove permanent delete * fix tests * remove cache service * remove kv * remove refresh docs * remove obsolete cache docs * add databroker url option * cache: use memberlist to detect multiple instances * add databroker service url * remove cache service * remove kv * remove refresh docs * remove obsolete cache docs * add databroker url option * cache: use memberlist to detect multiple instances * add databroker service url * wip * remove groups and refresh test * fix redirect, signout * remove databroker client from proxy * remove unused method * remove user dashboard test * handle missing session ids * session: reject sessions with no id * sessions: invalidate old sessions via databroker server version (#930) * session: add a version field tied to the databroker server version that can be used to invalidate sessions * fix tests * add log * authenticate: create user record immediately, call "get" directly in authorize (#931)
150 lines
4.5 KiB
Go
150 lines
4.5 KiB
Go
package authorize
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
|
|
envoy_api_v2_core "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
|
|
envoy_service_auth_v2 "github.com/envoyproxy/go-control-plane/envoy/service/auth/v2"
|
|
envoy_type "github.com/envoyproxy/go-control-plane/envoy/type"
|
|
"google.golang.org/genproto/googleapis/rpc/status"
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"github.com/pomerium/pomerium/authorize/evaluator"
|
|
"github.com/pomerium/pomerium/internal/httputil"
|
|
"github.com/pomerium/pomerium/internal/log"
|
|
"github.com/pomerium/pomerium/internal/urlutil"
|
|
)
|
|
|
|
func (a *Authorize) okResponse(
|
|
reply *evaluator.Result,
|
|
rawSession []byte,
|
|
) *envoy_service_auth_v2.CheckResponse {
|
|
requestHeaders, err := a.getEnvoyRequestHeaders(rawSession)
|
|
if err != nil {
|
|
log.Warn().Err(err).Msg("authorize: error generating new request headers")
|
|
}
|
|
requestHeaders = append(requestHeaders,
|
|
mkHeader(httputil.HeaderPomeriumJWTAssertion, reply.SignedJWT))
|
|
|
|
return &envoy_service_auth_v2.CheckResponse{
|
|
Status: &status.Status{Code: int32(codes.OK), Message: "OK"},
|
|
HttpResponse: &envoy_service_auth_v2.CheckResponse_OkResponse{
|
|
OkResponse: &envoy_service_auth_v2.OkHttpResponse{
|
|
Headers: requestHeaders,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (a *Authorize) deniedResponse(
|
|
in *envoy_service_auth_v2.CheckRequest,
|
|
code int32, reason string, headers map[string]string,
|
|
) *envoy_service_auth_v2.CheckResponse {
|
|
|
|
returnHTMLError := true
|
|
inHeaders := in.GetAttributes().GetRequest().GetHttp().GetHeaders()
|
|
if inHeaders != nil {
|
|
returnHTMLError = strings.Contains(inHeaders["accept"], "text/html")
|
|
}
|
|
|
|
if returnHTMLError {
|
|
return a.htmlDeniedResponse(code, reason, headers)
|
|
}
|
|
return a.plainTextDeniedResponse(code, reason, headers)
|
|
}
|
|
|
|
func (a *Authorize) htmlDeniedResponse(code int32, reason string, headers map[string]string) *envoy_service_auth_v2.CheckResponse {
|
|
var details string
|
|
switch code {
|
|
case httputil.StatusInvalidClientCertificate:
|
|
details = "a valid client certificate is required to access this page"
|
|
case http.StatusForbidden:
|
|
details = "access to this page is forbidden"
|
|
default:
|
|
details = reason
|
|
}
|
|
|
|
if reason == "" {
|
|
reason = http.StatusText(int(code))
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
err := a.templates.ExecuteTemplate(&buf, "error.html", map[string]interface{}{
|
|
"Status": code,
|
|
"StatusText": reason,
|
|
"CanDebug": code/100 == 4,
|
|
"Error": details,
|
|
})
|
|
if err != nil {
|
|
buf.WriteString(reason)
|
|
log.Error().Err(err).Msg("error executing error template")
|
|
}
|
|
|
|
envoyHeaders := []*envoy_api_v2_core.HeaderValueOption{
|
|
mkHeader("Content-Type", "text/html"),
|
|
}
|
|
for k, v := range headers {
|
|
envoyHeaders = append(envoyHeaders, mkHeader(k, v))
|
|
}
|
|
|
|
return &envoy_service_auth_v2.CheckResponse{
|
|
Status: &status.Status{Code: int32(codes.PermissionDenied), Message: "Access Denied"},
|
|
HttpResponse: &envoy_service_auth_v2.CheckResponse_DeniedResponse{
|
|
DeniedResponse: &envoy_service_auth_v2.DeniedHttpResponse{
|
|
Status: &envoy_type.HttpStatus{
|
|
Code: envoy_type.StatusCode(code),
|
|
},
|
|
Headers: envoyHeaders,
|
|
Body: buf.String(),
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (a *Authorize) plainTextDeniedResponse(code int32, reason string, headers map[string]string) *envoy_service_auth_v2.CheckResponse {
|
|
envoyHeaders := []*envoy_api_v2_core.HeaderValueOption{
|
|
mkHeader("Content-Type", "text/plain"),
|
|
}
|
|
for k, v := range headers {
|
|
envoyHeaders = append(envoyHeaders, mkHeader(k, v))
|
|
}
|
|
|
|
return &envoy_service_auth_v2.CheckResponse{
|
|
Status: &status.Status{Code: int32(codes.PermissionDenied), Message: "Access Denied"},
|
|
HttpResponse: &envoy_service_auth_v2.CheckResponse_DeniedResponse{
|
|
DeniedResponse: &envoy_service_auth_v2.DeniedHttpResponse{
|
|
Status: &envoy_type.HttpStatus{
|
|
Code: envoy_type.StatusCode(code),
|
|
},
|
|
Headers: envoyHeaders,
|
|
Body: reason,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (a *Authorize) redirectResponse(in *envoy_service_auth_v2.CheckRequest) *envoy_service_auth_v2.CheckResponse {
|
|
opts := a.currentOptions.Load()
|
|
|
|
signinURL := opts.GetAuthenticateURL().ResolveReference(&url.URL{Path: "/.pomerium/sign_in"})
|
|
q := signinURL.Query()
|
|
q.Set(urlutil.QueryRedirectURI, getCheckRequestURL(in).String())
|
|
signinURL.RawQuery = q.Encode()
|
|
redirectTo := urlutil.NewSignedURL(opts.SharedKey, signinURL).String()
|
|
|
|
return a.deniedResponse(in, http.StatusFound, "Login", map[string]string{
|
|
"Location": redirectTo,
|
|
})
|
|
}
|
|
|
|
func mkHeader(k, v string) *envoy_api_v2_core.HeaderValueOption {
|
|
return &envoy_api_v2_core.HeaderValueOption{
|
|
Header: &envoy_api_v2_core.HeaderValue{
|
|
Key: k,
|
|
Value: v,
|
|
},
|
|
}
|
|
}
|