Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

always initialize http handler #8946

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/unreleased/fix-well-known-rewrite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Bugfix: Fix well-known rewrite endpoint

https://github.com/owncloud/ocis/pull/8946
https://github.com/owncloud/ocis/issues/8703
16 changes: 9 additions & 7 deletions services/proxy/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Copy link
Member Author

@butonic butonic Apr 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line is the actual fix, the rest is bonus

Proxy: rp,
}
if err != nil {
return fmt.Errorf("failed to initialize reverse proxy: %w", err)
Expand Down
46 changes: 26 additions & 20 deletions services/proxy/pkg/staticroutes/oidc_well-known.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,42 @@ package staticroutes
import (
"io"
"net/http"
"net/url"
"path"
)

var (
wellKnownPath = "/.well-known/openid-configuration"
)

// 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")

}
}
}

Expand Down
8 changes: 1 addition & 7 deletions services/proxy/pkg/staticroutes/staticroutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -21,8 +19,6 @@ type StaticRouteHandler struct {
Config config.Config
OidcClient oidc.OIDCClient
OidcHttpClient *http.Client

oidcURL *url.URL
}

type jse struct {
Expand All @@ -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) {

Expand All @@ -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
Expand Down