Skip to content
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
148 changes: 72 additions & 76 deletions cmd/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,29 @@ import (
"fmt"
"net/http"
"strings"
"sync"
"time"

"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"

"github.com/ory/x/otelx/semconv"

"github.com/ory/x/servicelocatorx"

"github.com/ory/x/httprouterx"

"github.com/ory/analytics-go/v5"
"github.com/ory/x/configx"

"github.com/ory/x/reqlog"

"github.com/julienschmidt/httprouter"
"github.com/rs/cors"
"github.com/spf13/cobra"
"github.com/urfave/negroni"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.uber.org/automaxprocs/maxprocs"
"golang.org/x/sync/errgroup"

"github.com/ory/analytics-go/v5"
"github.com/ory/graceful"
"github.com/ory/x/configx"
"github.com/ory/x/healthx"
"github.com/ory/x/httprouterx"
"github.com/ory/x/metricsx"
"github.com/ory/x/networkx"
"github.com/ory/x/otelx"
"github.com/ory/x/otelx/semconv"
"github.com/ory/x/prometheusx"
"github.com/ory/x/reqlog"
"github.com/ory/x/servicelocatorx"
"github.com/ory/x/tlsx"

"github.com/ory/hydra/v2/client"
"github.com/ory/hydra/v2/consent"
Expand All @@ -44,14 +40,17 @@ import (
"github.com/ory/hydra/v2/jwk"
"github.com/ory/hydra/v2/oauth2"
"github.com/ory/hydra/v2/x"
prometheus "github.com/ory/x/prometheusx"
)

var _ = &consent.Handler{}

func EnhanceMiddleware(ctx context.Context, sl *servicelocatorx.Options, d driver.Registry, n *negroni.Negroni, address string, router *httprouter.Router, iface config.ServeInterface) http.Handler {
if !networkx.AddressIsUnixSocket(address) {
n.UseFunc(x.RejectInsecureRequests(d, d.Config().TLS(ctx, iface)))
func EnhanceMiddleware(ctx context.Context, sl *servicelocatorx.Options, d driver.Registry, n *negroni.Negroni, address string, router *httprouter.Router, iface config.ServeInterface) (http.Handler, error) {
if !networkx.AddressIsUnixSocket(address) && d.Config().TLS(ctx, iface).Enabled() {
mw, err := tlsx.EnforceTLSRequests(d, d.Config().TLS(ctx, iface).AllowTerminationFrom())
if err != nil {
return nil, err
}
n.Use(mw)
}

for _, mw := range sl.HTTPMiddlewares() {
Expand All @@ -68,7 +67,7 @@ func EnhanceMiddleware(ctx context.Context, sl *servicelocatorx.Options, d drive

n.UseHandler(router)

return n
return n, nil
}

func ensureNoMemoryDSN(r driver.Registry) {
Expand All @@ -91,22 +90,18 @@ func RunServeAdmin(slOpts []servicelocatorx.Option, dOpts []driver.OptionsModifi
admin, _, adminmw, _ := setup(ctx, d, cmd)
d.PrometheusManager().RegisterRouter(admin.Router)

var wg sync.WaitGroup
wg.Add(1)

go serve(
h, err := EnhanceMiddleware(ctx, sl, d, adminmw, d.Config().ListenOn(config.AdminInterface), admin.Router, config.AdminInterface)
if err != nil {
return err
}
return serve(
ctx,
d,
cmd,
&wg,
config.AdminInterface,
EnhanceMiddleware(ctx, sl, d, adminmw, d.Config().ListenOn(config.AdminInterface), admin.Router, config.AdminInterface),
h,
d.Config().ListenOn(config.AdminInterface),
d.Config().SocketPermission(config.AdminInterface),
)

wg.Wait()
return nil
}
}

Expand All @@ -124,22 +119,18 @@ func RunServePublic(slOpts []servicelocatorx.Option, dOpts []driver.OptionsModif
_, public, _, publicmw := setup(ctx, d, cmd)
d.PrometheusManager().RegisterRouter(public.Router)

var wg sync.WaitGroup
wg.Add(1)

go serve(
h, err := EnhanceMiddleware(ctx, sl, d, publicmw, d.Config().ListenOn(config.PublicInterface), public.Router, config.PublicInterface)
if err != nil {
return err
}
return serve(
ctx,
d,
cmd,
&wg,
config.PublicInterface,
EnhanceMiddleware(ctx, sl, d, publicmw, d.Config().ListenOn(config.PublicInterface), public.Router, config.PublicInterface),
h,
d.Config().ListenOn(config.PublicInterface),
d.Config().SocketPermission(config.PublicInterface),
)

wg.Wait()
return nil
}
}

Expand All @@ -158,33 +149,40 @@ func RunServeAll(slOpts []servicelocatorx.Option, dOpts []driver.OptionsModifier
d.PrometheusManager().RegisterRouter(admin.Router)
d.PrometheusManager().RegisterRouter(public.Router)

var wg sync.WaitGroup
wg.Add(2)

go serve(
ctx,
d,
cmd,
&wg,
config.PublicInterface,
EnhanceMiddleware(ctx, sl, d, publicmw, d.Config().ListenOn(config.PublicInterface), public.Router, config.PublicInterface),
d.Config().ListenOn(config.PublicInterface),
d.Config().SocketPermission(config.PublicInterface),
)

go serve(
ctx,
d,
cmd,
&wg,
config.AdminInterface,
EnhanceMiddleware(ctx, sl, d, adminmw, d.Config().ListenOn(config.AdminInterface), admin.Router, config.AdminInterface),
d.Config().ListenOn(config.AdminInterface),
d.Config().SocketPermission(config.AdminInterface),
)
ph, err := EnhanceMiddleware(ctx, sl, d, publicmw, d.Config().ListenOn(config.PublicInterface), public.Router, config.PublicInterface)
if err != nil {
return err
}
ah, err := EnhanceMiddleware(ctx, sl, d, adminmw, d.Config().ListenOn(config.AdminInterface), admin.Router, config.AdminInterface)
if err != nil {
return err
}

wg.Wait()
return nil
eg, ctx := errgroup.WithContext(ctx)

eg.Go(func() error {
return serve(
ctx,
d,
config.PublicInterface,
ph,
d.Config().ListenOn(config.PublicInterface),
d.Config().SocketPermission(config.PublicInterface),
)
})

eg.Go(func() error {
return serve(
ctx,
d,
config.AdminInterface,
ah,
d.Config().ListenOn(config.AdminInterface),
d.Config().SocketPermission(config.AdminInterface),
)
})

return eg.Wait()
}
}

Expand All @@ -209,7 +207,7 @@ func setup(ctx context.Context, d driver.Registry, cmd *cobra.Command) (admin *h
NewMiddlewareFromLogger(d.Logger(),
fmt.Sprintf("hydra/admin: %s", d.Config().IssuerURL(ctx).String()))
if d.Config().DisableHealthAccessLog(config.AdminInterface) {
adminLogger = adminLogger.ExcludePaths(healthx.AliveCheckPath, healthx.ReadyCheckPath, "/admin"+prometheus.MetricsPrometheusPath)
adminLogger = adminLogger.ExcludePaths(healthx.AliveCheckPath, healthx.ReadyCheckPath, "/admin"+prometheusx.MetricsPrometheusPath)
}

adminmw.UseFunc(semconv.Middleware)
Expand Down Expand Up @@ -279,8 +277,8 @@ func setup(ctx context.Context, d driver.Registry, cmd *cobra.Command) (admin *h
"/admin" + healthx.ReadyCheckPath,
healthx.VersionPath,
"/admin" + healthx.VersionPath,
prometheus.MetricsPrometheusPath,
"/admin" + prometheus.MetricsPrometheusPath,
prometheusx.MetricsPrometheusPath,
"/admin" + prometheusx.MetricsPrometheusPath,
"/",
},
BuildVersion: config.Version,
Expand All @@ -307,16 +305,12 @@ func setup(ctx context.Context, d driver.Registry, cmd *cobra.Command) (admin *h
func serve(
ctx context.Context,
d driver.Registry,
cmd *cobra.Command,
wg *sync.WaitGroup,
iface config.ServeInterface,
handler http.Handler,
address string,
permission *configx.UnixPermission,
) {
defer wg.Done()

if tracer := d.Tracer(cmd.Context()); tracer.IsLoaded() {
) error {
if tracer := d.Tracer(ctx); tracer.IsLoaded() {
handler = otelx.TraceHandler(
handler,
otelhttp.WithTracerProvider(tracer.Provider()),
Expand All @@ -333,7 +327,7 @@ func serve(
tlsConfig = &tls.Config{GetCertificate: GetOrCreateTLSCertificate(ctx, d, iface, stopReload)}
}

var srv = graceful.WithDefaults(&http.Server{
srv := graceful.WithDefaults(&http.Server{
Handler: handler,
TLSConfig: tlsConfig,
ReadHeaderTimeout: time.Second * 5,
Expand Down Expand Up @@ -363,6 +357,8 @@ func serve(
close(stopReload)
return srv.Shutdown(ctx)
}); err != nil {
d.Logger().WithError(err).Fatal("Could not gracefully run server")
d.Logger().WithError(err).Error("Could not gracefully run server")
return err
}
return nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ require (
github.com/ory/hydra-client-go/v2 v2.2.1
github.com/ory/jsonschema/v3 v3.0.9-0.20250317235931-280c5fc7bf0e
github.com/ory/kratos-client-go v1.3.8
github.com/ory/x v0.0.707
github.com/ory/x v0.0.712
github.com/pborman/uuid v1.2.1
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.21.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,8 @@ github.com/ory/pop/v6 v6.2.1-0.20241121111754-e5dfc0f3344b h1:BIzoOe2/wynZBQak1p
github.com/ory/pop/v6 v6.2.1-0.20241121111754-e5dfc0f3344b/go.mod h1:okVAYKGtgunD/wbW3NGhZTndJCS+6FqO+cA89rQ4doc=
github.com/ory/x v0.0.707 h1:dcOlbIlzAS2vhsBcKwCFG685gZy9aO2h2SGJej87hpo=
github.com/ory/x v0.0.707/go.mod h1:by9HRTEZgIS48FIoF/RjYHb2s1eSiycCZy0m/BMhsf8=
github.com/ory/x v0.0.712 h1:3HRRXBlKXwkIh5ag55fs25tIMipb058muNtEWTFOqfI=
github.com/ory/x v0.0.712/go.mod h1:FxgJl980fq/41JTPPloNawYPCY25KRYuMO98SRk1czc=
github.com/pborman/uuid v1.2.1 h1:+ZZIw58t/ozdjRaXh/3awHfmWRbzYxJoAdNJxe/3pvw=
github.com/pborman/uuid v1.2.1/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
Expand Down
95 changes: 0 additions & 95 deletions x/tls_termination.go

This file was deleted.

Loading
Loading