Skip to content

Commit

Permalink
feat: move to go 1.16 and static embed files
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Mar 8, 2021
1 parent b46a14c commit 6fa591c
Show file tree
Hide file tree
Showing 28 changed files with 213 additions and 134 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ workflows:
only: master
- sdk/generate:
appname: Ory_Hydra
swagpath: spec/api.json
specignorepgks: internal/httpclient -x gopkg.in/square/go-jose.v2
requires:
- test
Expand All @@ -178,6 +179,7 @@ workflows:
ignore: /master/
- sdk/release:
specignorepgks: internal/httpclient -x gopkg.in/square/go-jose.v2
swagpath: spec/api.json
requires:
- test
- sdk/generate
Expand All @@ -192,6 +194,7 @@ workflows:
requires:
- test
- docs/build:
swag-spec-location: spec/api.json
requires:
- test
filters:
Expand Down
4 changes: 2 additions & 2 deletions .docker/Dockerfile-build
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.15-alpine AS builder
FROM golang:1.16-alpine AS builder

RUN apk -U --no-cache add build-base git gcc bash

Expand All @@ -14,7 +14,7 @@ RUN go mod download

ADD . .

RUN make pack && go build -tags sqlite -o /usr/bin/hydra
RUN go build -tags sqlite -o /usr/bin/hydra

FROM alpine:3.12

Expand Down
1 change: 0 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ before:
hooks:
- go mod download
- git checkout -- go.sum
- make pack

builds:
-
Expand Down
7 changes: 6 additions & 1 deletion cmd/cli/handler_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) {

if flagx.MustGetBool(cmd, "read-from-env") {
d = driver.New(
cmd.Context(),
driver.WithOptions(
configx.SkipValidation(),
configx.WithFlags(cmd.Flags())),
Expand All @@ -48,6 +49,7 @@ func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) {
return
}
d = driver.New(
cmd.Context(),
driver.WithOptions(
configx.WithFlags(cmd.Flags()),
configx.SkipValidation(),
Expand Down Expand Up @@ -83,11 +85,14 @@ func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) {
// print migration status
fmt.Println("The following migration is planned:")
fmt.Println("")
if err := p.MigrationStatus(context.Background(), os.Stdout); err != nil {

status, err := p.MigrationStatus(context.Background())
if err != nil {
fmt.Printf("Could not get the migration status:\n%+v\n", errorsx.WithStack(err))
os.Exit(1)
return
}
_ = status.Write(os.Stdout)

if !flagx.MustGetBool(cmd, "yes") {
fmt.Println("")
Expand Down
12 changes: 6 additions & 6 deletions cmd/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func isDSNAllowed(r driver.Registry) {
}

func RunServeAdmin(cmd *cobra.Command, args []string) {
d := driver.New(driver.WithOptions(configx.WithFlags(cmd.Flags())))
d := driver.New(cmd.Context(), driver.WithOptions(configx.WithFlags(cmd.Flags())))
isDSNAllowed(d)

admin, _, adminmw, _ := setup(d, cmd)
Expand All @@ -106,7 +106,7 @@ func RunServeAdmin(cmd *cobra.Command, args []string) {
}

func RunServePublic(cmd *cobra.Command, args []string) {
d := driver.New(driver.WithOptions(configx.WithFlags(cmd.Flags())))
d := driver.New(cmd.Context(), driver.WithOptions(configx.WithFlags(cmd.Flags())))
isDSNAllowed(d)

_, public, _, publicmw := setup(d, cmd)
Expand All @@ -124,7 +124,7 @@ func RunServePublic(cmd *cobra.Command, args []string) {
}

func RunServeAll(cmd *cobra.Command, args []string) {
d := driver.New(driver.WithOptions(configx.WithFlags(cmd.Flags())))
d := driver.New(cmd.Context(), driver.WithOptions(configx.WithFlags(cmd.Flags())))

admin, public, adminmw, publicmw := setup(d, cmd)
cert := GetOrCreateTLSCertificate(cmd, d) // we do not want to run this concurrently.
Expand Down Expand Up @@ -162,7 +162,7 @@ func setup(d driver.Registry, cmd *cobra.Command) (admin *x.RouterAdmin, public
admin = x.NewRouterAdmin()
public = x.NewRouterPublic()

if tracer := d.Tracer(); tracer.IsLoaded() {
if tracer := d.Tracer(cmd.Context()); tracer.IsLoaded() {
adminmw.Use(tracer)
publicmw.Use(tracer)
}
Expand Down Expand Up @@ -274,8 +274,8 @@ func serve(d driver.Registry, cmd *cobra.Command, wg *sync.WaitGroup, handler ht
},
})

if d.Tracer().IsLoaded() {
srv.RegisterOnShutdown(d.Tracer().Close)
if d.Tracer(cmd.Context()).IsLoaded() {
srv.RegisterOnShutdown(d.Tracer(cmd.Context()).Close)
}

if err := graceful.Graceful(func() error {
Expand Down
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"scripts": {
"gen": "npm run widdershins && cd .. && node ./docs/scripts/fix-api.js ./docs/docs/reference/api.mdx && node ./docs/scripts/config.js docs/config.js",
"docusaurus": "docusaurus",
"widdershins": "widdershins -u .widdershins/templates -e .widdershins/config.json ../.schema/api.swagger.json -o ./docs/reference/api.mdx",
"widdershins": "widdershins -u .widdershins/templates -e .widdershins/config.json ../spec/api.json -o ./docs/reference/api.mdx",
"start": "docusaurus start",
"build": "docusaurus build",
"swizzle": "docusaurus swizzle",
Expand Down
17 changes: 3 additions & 14 deletions driver/config/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package config

import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"time"

"github.com/ory/x/dbal"
"github.com/ory/hydra/spec"

"github.com/markbates/pkger"
"github.com/ory/x/dbal"

"github.com/ory/x/configx"

Expand Down Expand Up @@ -98,24 +97,14 @@ func MustNew(l *logrusx.Logger, opts ...configx.OptionModifier) *Provider {
}

func New(l *logrusx.Logger, opts ...configx.OptionModifier) (*Provider, error) {
f, err := pkger.Open("/.schema/config.schema.json")
if err != nil {
return nil, err
}

schema, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}

opts = append([]configx.OptionModifier{
configx.WithStderrValidationReporter(),
configx.OmitKeysFromTracing("dsn", "secrets.system", "secrets.cookie"),
configx.WithImmutables("log", "serve", "dsn", "profiling"),
configx.WithLogrusWatcher(l),
}, opts...)

p, err := configx.New(schema, opts...)
p, err := configx.New(spec.ConfigValidationSchema, opts...)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions driver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func DisablePreloading() OptionsModifier {
}
}

func New(opts ...OptionsModifier) Registry {
func New(ctx context.Context, opts ...OptionsModifier) Registry {
o := newOptions()
for _, f := range opts {
f(o)
Expand All @@ -67,21 +67,21 @@ func New(opts ...OptionsModifier) Registry {
config.MustValidate(l, c)
}

r, err := NewRegistryFromDSN(c, l)
r, err := NewRegistryFromDSN(ctx, c, l)
if err != nil {
l.WithError(err).Fatal("Unable to create service registry.")
}

if err = r.Init(); err != nil {
if err = r.Init(ctx); err != nil {
l.WithError(err).Fatal("Unable to initialize service registry.")
}

// Avoid cold cache issues on boot:
if o.preload {
CallRegistry(r)
CallRegistry(ctx, r)
}

c.Source().SetTracer(context.Background(), r.Tracer())
c.Source().SetTracer(context.Background(), r.Tracer(ctx))

return r
}
16 changes: 8 additions & 8 deletions driver/registry.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package driver

import (
"context"

"github.com/pkg/errors"

"github.com/ory/x/errorsx"
Expand All @@ -12,8 +14,6 @@ import (

"github.com/ory/hydra/persistence"

"github.com/ory/x/tracing"

"github.com/ory/hydra/metrics/prometheus"

"github.com/ory/x/dbal"
Expand All @@ -30,7 +30,7 @@ import (
type Registry interface {
dbal.Driver

Init() error
Init(ctx context.Context) error

WithConfig(c *config.Provider) Registry
WithLogger(l *logrusx.Logger) Registry
Expand All @@ -45,7 +45,7 @@ type Registry interface {
jwk.Registry
oauth2.Registry
PrometheusManager() *prometheus.MetricsManager
Tracer() *tracing.Tracer
x.TracingProvider

RegisterRoutes(admin *x.RouterAdmin, public *x.RouterPublic)
ClientHandler() *client.Handler
Expand All @@ -59,7 +59,7 @@ type Registry interface {
WithConsentStrategy(c consent.Strategy)
}

func NewRegistryFromDSN(c *config.Provider, l *logrusx.Logger) (Registry, error) {
func NewRegistryFromDSN(ctx context.Context, c *config.Provider, l *logrusx.Logger) (Registry, error) {
driver, err := dbal.GetDriverFor(c.DSN())
if err != nil {
return nil, errorsx.WithStack(err)
Expand All @@ -72,14 +72,14 @@ func NewRegistryFromDSN(c *config.Provider, l *logrusx.Logger) (Registry, error)

registry = registry.WithLogger(l).WithConfig(c)

if err := registry.Init(); err != nil {
if err := registry.Init(ctx); err != nil {
return nil, err
}

return registry, nil
}

func CallRegistry(r Registry) {
func CallRegistry(ctx context.Context, r Registry) {
r.ClientValidator()
r.ClientManager()
r.ClientHasher()
Expand All @@ -97,5 +97,5 @@ func CallRegistry(r Registry) {
r.OpenIDJWTStrategy()
r.OpenIDConnectRequestValidator()
r.PrometheusManager()
r.Tracer()
r.Tracer(ctx)
}
24 changes: 20 additions & 4 deletions driver/registry_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package driver

import (
"context"
"errors"
"net"
"net/http"
"strings"
"time"

"github.com/pkg/errors"

"github.com/ory/hydra/x/oauth2cors"

"github.com/ory/hydra/persistence"
Expand Down Expand Up @@ -153,7 +154,7 @@ func (m *RegistryBase) AuditLogger() *logrusx.Logger {

func (m *RegistryBase) ClientHasher() fosite.Hasher {
if m.fh == nil {
if m.Tracer().IsLoaded() {
if m.Tracer(context.TODO()).IsLoaded() {
m.fh = &tracing.TracedBCrypt{WorkFactor: m.C.BCryptCost()}
} else {
m.fh = x.NewBCrypt(m.C)
Expand Down Expand Up @@ -182,10 +183,25 @@ func (m *RegistryBase) KeyHandler() *jwk.Handler {
}
return m.kh
}

func (m *RegistryBase) HealthHandler() *healthx.Handler {
if m.hh == nil {
m.hh = healthx.NewHandler(m.Writer(), m.buildVersion, healthx.ReadyCheckers{
"database": m.r.Ping,
"database": func(_ *http.Request) error {
return m.r.Ping()
},
"migrations": func(r *http.Request) error {
status, err := m.r.Persister().MigrationStatus(r.Context())
if err != nil {
return err
}

if status.HasPending() {
return errors.WithStack(herodot.ErrInternalServerError.WithReasonf("Migrations have not yet been applied."))
}

return nil
},
})
}

Expand Down Expand Up @@ -402,7 +418,7 @@ func (m *RegistryBase) SubjectIdentifierAlgorithm() map[string]consent.SubjectId
return m.sia
}

func (m *RegistryBase) Tracer() *tracing.Tracer {
func (m *RegistryBase) Tracer(ctx context.Context) *tracing.Tracer {
if m.trc == nil {
t, err := tracing.New(m.l, m.C.Tracing())
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion driver/registry_base_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package driver

import (
"context"
"io/ioutil"
"testing"

Expand All @@ -27,7 +28,7 @@ func TestRegistryBase_newKeyStrategy_handlesNetworkError(t *testing.T) {
c := config.MustNew(l, configx.WithConfigFiles("../internal/.hydra.yaml"))
c.MustSet(config.KeyDSN, "postgres://user:password@127.0.0.1:9999/postgres")

registry, err := NewRegistryFromDSN(c, l)
registry, err := NewRegistryFromDSN(context.Background(), c, l)
if err != nil {
t.Error("failed to create registry: ", err)
return
Expand Down
8 changes: 4 additions & 4 deletions driver/registry_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ func NewRegistrySQL() *RegistrySQL {
return r
}

func (m *RegistrySQL) Init() error {
func (m *RegistrySQL) Init(ctx context.Context) error {
if m.persister == nil {
var opts []instrumentedsql.Opt
if m.Tracer().IsLoaded() {
if m.Tracer(ctx).IsLoaded() {
opts = []instrumentedsql.Opt{
instrumentedsql.WithTracer(opentracing.NewTracer(true)),
instrumentedsql.WithOmitArgs(),
Expand All @@ -66,7 +66,7 @@ func (m *RegistrySQL) Init() error {
IdlePool: idlePool,
ConnMaxLifetime: connMaxLifetime,
Pool: pool,
UseInstrumentedDriver: m.Tracer().IsLoaded(),
UseInstrumentedDriver: m.Tracer(ctx).IsLoaded(),
InstrumentedDriverOptions: opts,
})
if err != nil {
Expand All @@ -75,7 +75,7 @@ func (m *RegistrySQL) Init() error {
if err := resilience.Retry(m.l, 5*time.Second, 5*time.Minute, c.Open); err != nil {
return errorsx.WithStack(err)
}
m.persister, err = sql.NewPersister(c, m, m.C, m.l)
m.persister, err = sql.NewPersister(ctx, c, m, m.C, m.l)
if err != nil {
return err
}
Expand Down

0 comments on commit 6fa591c

Please sign in to comment.