From f8f864e566ba25206f7617bad0ee2080c2a7f8be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Wed, 24 Apr 2024 10:35:57 +0200 Subject: [PATCH] always initialize http handler MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jörn Friedrich Dreyer --- .../unreleased/fix-well-known-rewrite.md | 4 ++ services/proxy/pkg/command/server.go | 16 ++++--- .../proxy/pkg/staticroutes/oidc_well-known.go | 46 +++++++++++-------- .../proxy/pkg/staticroutes/staticroutes.go | 8 +--- 4 files changed, 40 insertions(+), 34 deletions(-) create mode 100644 changelog/unreleased/fix-well-known-rewrite.md diff --git a/changelog/unreleased/fix-well-known-rewrite.md b/changelog/unreleased/fix-well-known-rewrite.md new file mode 100644 index 0000000000..fefa1449ae --- /dev/null +++ b/changelog/unreleased/fix-well-known-rewrite.md @@ -0,0 +1,4 @@ +Bugfix: Fix well-known rewrite endpoint + +https://github.com/owncloud/ocis/pull/8946 +https://github.com/owncloud/ocis/issues/8703 diff --git a/services/proxy/pkg/command/server.go b/services/proxy/pkg/command/server.go index 41ff934228..dea2597c9f 100644 --- a/services/proxy/pkg/command/server.go +++ b/services/proxy/pkg/command/server.go @@ -4,11 +4,12 @@ import ( "context" "crypto/tls" "fmt" - "github.com/owncloud/ocis/v2/services/proxy/pkg/staticroutes" "net/http" "os" "time" + "github.com/owncloud/ocis/v2/services/proxy/pkg/staticroutes" + chimiddleware "github.com/go-chi/chi/v5/middleware" "github.com/justinas/alice" "github.com/oklog/run" @@ -134,12 +135,13 @@ func Server(cfg *config.Config) *cli.Command { ) lh := staticroutes.StaticRouteHandler{ - Prefix: cfg.HTTP.Root, - UserInfoCache: userInfoCache, - Logger: logger, - Config: *cfg, - OidcClient: oidcClient, - Proxy: rp, + Prefix: cfg.HTTP.Root, + UserInfoCache: userInfoCache, + Logger: logger, + Config: *cfg, + OidcClient: oidcClient, + OidcHttpClient: oidcHTTPClient, + Proxy: rp, } if err != nil { return fmt.Errorf("failed to initialize reverse proxy: %w", err) diff --git a/services/proxy/pkg/staticroutes/oidc_well-known.go b/services/proxy/pkg/staticroutes/oidc_well-known.go index 2cbd4bff94..3da7a53c68 100644 --- a/services/proxy/pkg/staticroutes/oidc_well-known.go +++ b/services/proxy/pkg/staticroutes/oidc_well-known.go @@ -3,6 +3,8 @@ package staticroutes import ( "io" "net/http" + "net/url" + "path" ) var ( @@ -10,29 +12,33 @@ var ( ) // OIDCWellKnownRewrite is a handler that rewrites the /.well-known/openid-configuration endpoint for external IDPs. -func (s *StaticRouteHandler) oIDCWellKnownRewrite(w http.ResponseWriter, r *http.Request) { - wellKnownRes, err := s.OidcHttpClient.Get(s.oidcURL.String()) - if err != nil { - s.Logger.Error(). - Err(err). - Str("handler", "oidc wellknown rewrite"). - Str("url", s.oidcURL.String()). - Msg("get information from url failed") - w.WriteHeader(http.StatusInternalServerError) - return - } +func (s *StaticRouteHandler) oIDCWellKnownRewrite(issuer string) http.HandlerFunc { + oidcURL, _ := url.Parse(issuer) + oidcURL.Path = path.Join(oidcURL.Path, wellKnownPath) + return func(w http.ResponseWriter, r *http.Request) { + wellKnownRes, err := s.OidcHttpClient.Get(oidcURL.String()) + if err != nil { + s.Logger.Error(). + Err(err). + Str("handler", "oidc wellknown rewrite"). + Str("url", oidcURL.String()). + Msg("get information from url failed") + w.WriteHeader(http.StatusInternalServerError) + return + } - defer wellKnownRes.Body.Close() + defer wellKnownRes.Body.Close() - copyHeader(w.Header(), wellKnownRes.Header) - w.WriteHeader(wellKnownRes.StatusCode) - _, err = io.Copy(w, wellKnownRes.Body) - if err != nil { - s.Logger.Error(). - Err(err). - Str("handler", "oidc wellknown rewrite"). - Msg("copying response body failed") + copyHeader(w.Header(), wellKnownRes.Header) + w.WriteHeader(wellKnownRes.StatusCode) + _, err = io.Copy(w, wellKnownRes.Body) + if err != nil { + s.Logger.Error(). + Err(err). + Str("handler", "oidc wellknown rewrite"). + Msg("copying response body failed") + } } } diff --git a/services/proxy/pkg/staticroutes/staticroutes.go b/services/proxy/pkg/staticroutes/staticroutes.go index e131d3b2e6..4be46c62e1 100644 --- a/services/proxy/pkg/staticroutes/staticroutes.go +++ b/services/proxy/pkg/staticroutes/staticroutes.go @@ -2,8 +2,6 @@ package staticroutes import ( "net/http" - "net/url" - "path" "github.com/go-chi/chi/v5" "github.com/owncloud/ocis/v2/ocis-pkg/log" @@ -21,8 +19,6 @@ type StaticRouteHandler struct { Config config.Config OidcClient oidc.OIDCClient OidcHttpClient *http.Client - - oidcURL *url.URL } type jse struct { @@ -31,8 +27,6 @@ type jse struct { } func (s *StaticRouteHandler) Handler() http.Handler { - s.oidcURL, _ = url.Parse(s.Config.OIDC.Issuer) - s.oidcURL.Path = path.Join(s.oidcURL.Path, wellKnownPath) m := chi.NewMux() m.Route(s.Prefix, func(r chi.Router) { @@ -41,7 +35,7 @@ func (s *StaticRouteHandler) Handler() http.Handler { // openid .well-known if s.Config.OIDC.RewriteWellKnown { - r.Get("/.well-known/openid-configuration", s.oIDCWellKnownRewrite) + r.Get("/.well-known/openid-configuration", s.oIDCWellKnownRewrite(s.Config.OIDC.Issuer)) } // Send all requests to the proxy handler