diff --git a/.docker/Dockerfile-build b/.docker/Dockerfile-build index a093c4271be..4a13000ae6f 100644 --- a/.docker/Dockerfile-build +++ b/.docker/Dockerfile-build @@ -1,6 +1,6 @@ FROM golang:1.14-alpine AS builder -RUN apk -U --no-cache add build-base git gcc +RUN apk -U --no-cache add build-base git gcc bash WORKDIR /go/src/github.com/ory/kratos @@ -11,11 +11,10 @@ ENV GO111MODULE on ENV CGO_ENABLED 1 RUN go mod download -RUN go install github.com/gobuffalo/packr/v2/packr2 ADD . . -RUN packr2 +RUN make pack RUN go build -tags sqlite -a -o /usr/bin/kratos FROM alpine:3.11 diff --git a/.goreleaser.yml b/.goreleaser.yml index 8749502a212..c38740906fb 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -8,8 +8,7 @@ env: before: hooks: - go mod download - - go install github.com/gobuffalo/packr/v2/packr2 - - packr2 + - make pack builds: - diff --git a/Makefile b/Makefile index 3d1b509c7ca..f8ec9b223d3 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ SHELL=/bin/bash -o pipefail -EXECUTABLES = docker-compose docker node npm go -K := $(foreach exec,$(EXECUTABLES),\ - $(if $(shell which $(exec)),some string,$(error "No $(exec) in PATH"))) +# EXECUTABLES = docker-compose docker node npm go +# K := $(foreach exec,$(EXECUTABLES),\ +# $(if $(shell which $(exec)),some string,$(error "No $(exec) in PATH"))) export GO111MODULE := on export PATH := .bin:${PATH} @@ -10,11 +10,11 @@ export PATH := .bin:${PATH} GO_DEPENDENCIES = github.com/ory/go-acc \ github.com/sqs/goreturns \ github.com/ory/x/tools/listx \ + github.com/markbates/pkger/cmd/pkger \ github.com/golang/mock/mockgen \ github.com/go-swagger/go-swagger/cmd/swagger \ golang.org/x/tools/cmd/goimports \ - github.com/mikefarah/yq \ - github.com/gobuffalo/packr/v2/packr2 + github.com/mikefarah/yq define make-go-dependency # go install is responsible for not re-building when the code hasn't changed @@ -57,8 +57,8 @@ mocks: .bin/mockgen mockgen -mock_names Manager=MockLoginExecutorDependencies -package internal -destination internal/hook_login_executor_dependencies.go github.com/ory/kratos/selfservice loginExecutorDependencies .PHONY: install -install: .bin/packr2 - packr2 +install: .bin/packr2 .bin/pack + make pack GO111MODULE=on go install -tags sqlite . packr2 clean @@ -124,3 +124,7 @@ migrations-render: .bin/cli .PHONY: migrations-render-replace migrations-render-replace: .bin/cli cli dev pop migration render -r persistence/sql/migrations/templates persistence/sql/migrations/sql + +.PHONY: pack +pack: .bin/pkger + pkger -exclude node_modules -exclude docs -exclude .bin -exclude test -exclude script -exclude contrib diff --git a/cmd/daemon/serve.go b/cmd/daemon/serve.go index 8318253d2ff..8a9b9b025ca 100644 --- a/cmd/daemon/serve.go +++ b/cmd/daemon/serve.go @@ -135,10 +135,10 @@ func sqa(cmd *cobra.Command, d driver.Driver) *metricsx.Service { registration.RouteGetFlow, session.RouteWhoami, identity.IdentitiesPath, - profile.PublicSettingsProfilePath, + profile.RouteSettings, settings.RouteInitBrowserFlow, settings.RouteGetFlow, - profile.PublicSettingsProfilePath, + profile.RouteSettings, verification.PublicVerificationCompletePath, strings.ReplaceAll(strings.ReplaceAll(verification.PublicVerificationConfirmPath, ":via", "email"), ":code", ""), strings.ReplaceAll(verification.PublicVerificationInitPath, ":via", "email"), diff --git a/courier/template/load_template.go b/courier/template/load_template.go index 7adcba24bcf..a864e6ab4f5 100644 --- a/courier/template/load_template.go +++ b/courier/template/load_template.go @@ -7,12 +7,13 @@ import ( "text/template" "github.com/Masterminds/sprig/v3" - "github.com/gobuffalo/packr/v2" lru "github.com/hashicorp/golang-lru" + "github.com/markbates/pkger" "github.com/pkg/errors" ) -var box = packr.New("templates", "templates") +var _ = pkger.Dir("/courier/template/templates") + var cache, _ = lru.New(16) func loadTextTemplate(path string, model interface{}) (string, error) { @@ -26,7 +27,7 @@ func loadTextTemplate(path string, model interface{}) (string, error) { return tb.String(), nil } - if file, err := box.Open(path); err == nil { + if file, err := pkger.Open(path); err == nil { defer file.Close() if _, err := io.Copy(&b, file); err != nil { return "", errors.WithStack(err) diff --git a/go.mod b/go.mod index e17325d42fe..fd1065657a0 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,9 @@ go 1.14 replace github.com/justinas/nosurf => github.com/aeneasr/nosurf v1.1.1-0.20200817183017-ee7691f82e7d +// See https://github.com/markbates/pkger/pull/112 +replace github.com/markbates/pkger => github.com/falafeljan/pkger v0.17.1-0.20200722132747-95726f5b9b9b + require ( github.com/Masterminds/sprig/v3 v3.0.0 github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect @@ -26,8 +29,7 @@ require ( github.com/go-swagger/go-swagger v0.25.0 github.com/gobuffalo/fizz v1.11.1-0.20200706214517-4e9fb9ced4ce github.com/gobuffalo/httptest v1.0.2 - github.com/gobuffalo/logger v1.0.3 - github.com/gobuffalo/packr/v2 v2.8.0 + github.com/gobuffalo/packr/v2 v2.8.0 // indirect github.com/gobuffalo/pop/v5 v5.2.4-0.20200706214017-1dd950510b30 github.com/gobuffalo/uuid v2.0.5+incompatible github.com/gofrs/uuid v3.2.0+incompatible @@ -48,6 +50,7 @@ require ( github.com/julienschmidt/httprouter v1.2.0 github.com/justinas/nosurf v1.1.0 github.com/leodido/go-urn v1.1.0 // indirect + github.com/markbates/pkger v0.17.0 github.com/mattn/goveralls v0.0.5 github.com/mikefarah/yq v1.15.0 github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 diff --git a/go.sum b/go.sum index 66bbe480e3c..9e7e44a88fa 100644 --- a/go.sum +++ b/go.sum @@ -167,6 +167,8 @@ github.com/elazarl/goproxy v0.0.0-20181003060214-f58a169a71a5/go.mod h1:/Zj4wYkg github.com/envoyproxy/go-control-plane v0.6.9/go.mod h1:SBwIajubJHhxtWwsL9s8ss4safvEdbitLhGGK48rN6g= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/falafeljan/pkger v0.17.1-0.20200722132747-95726f5b9b9b h1:6NU5UkilmBq093qzKE/rthGQfa8fElR5va1WaVtevxM= +github.com/falafeljan/pkger v0.17.1-0.20200722132747-95726f5b9b9b/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQDXbLhiuI= github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s= @@ -922,8 +924,6 @@ github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2 h1:JgVTCPf0uBVcUSW github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE= github.com/markbates/oncer v1.0.0 h1:E83IaVAHygyndzPimgUYJjbshhDTALZyXxvk9FOlQRY= github.com/markbates/oncer v1.0.0/go.mod h1:Z59JA581E9GP6w96jai+TGqafHPW+cPfRxz2aSZ0mcI= -github.com/markbates/pkger v0.17.0 h1:RFfyBPufP2V6cddUyyEVSHBpaAnM1WzaMNyqomeT+iY= -github.com/markbates/pkger v0.17.0/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQDXbLhiuI= github.com/markbates/refresh v1.4.10/go.mod h1:NDPHvotuZmTmesXxr95C9bjlw1/0frJwtME2dzcVKhc= github.com/markbates/safe v1.0.0/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0= github.com/markbates/safe v1.0.1 h1:yjZkbvRM6IzKj9tlu/zMJLS0n/V351OZWRnF3QfaUxI= diff --git a/go_mod_indirect_pins.go b/go_mod_indirect_pins.go index f1eb82ae61c..533dbd9c653 100644 --- a/go_mod_indirect_pins.go +++ b/go_mod_indirect_pins.go @@ -15,7 +15,6 @@ import ( _ "github.com/ory/go-acc" _ "github.com/ory/x/tools/listx" - _ "github.com/gobuffalo/packr/v2" _ "github.com/jteeuwen/go-bindata" _ "github.com/mikefarah/yq" diff --git a/persistence/sql/migratest/migration_test.go b/persistence/sql/migratest/migration_test.go index f41f7b99445..c670ebe88ea 100644 --- a/persistence/sql/migratest/migration_test.go +++ b/persistence/sql/migratest/migration_test.go @@ -12,7 +12,6 @@ import ( "strconv" "testing" - "github.com/gobuffalo/packr/v2/plog" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" @@ -26,8 +25,6 @@ import ( "github.com/gobuffalo/pop/v5" "github.com/stretchr/testify/require" - gobuffalologger "github.com/gobuffalo/logger" - "github.com/ory/kratos/driver" "github.com/ory/kratos/driver/configuration" "github.com/ory/kratos/internal/testhelpers" @@ -49,7 +46,6 @@ func TestMigrations(t *testing.T) { "sqlite": sqlite, } l := logrusx.New("", "", logrusx.ForceLevel(logrus.TraceLevel)) - plog.Logger = gobuffalologger.Logrus{FieldLogger: l.Entry} if !testing.Short() { dockertest.Parallel([]func(){ diff --git a/persistence/sql/persister.go b/persistence/sql/persister.go index 52056208971..36bcc88b63e 100644 --- a/persistence/sql/persister.go +++ b/persistence/sql/persister.go @@ -4,8 +4,8 @@ import ( "context" "io" - "github.com/gobuffalo/packr/v2" "github.com/gobuffalo/pop/v5" + "github.com/markbates/pkger" "github.com/pkg/errors" "github.com/ory/kratos/driver/configuration" @@ -16,7 +16,8 @@ import ( ) var _ persistence.Persister = new(Persister) -var migrations = packr.New("migrations", "migrations/sql") + +var migrations = pkger.Dir("/persistence/sql/migrations/sql") // do not remove this! type ( persisterDependencies interface { @@ -26,7 +27,7 @@ type ( } Persister struct { c *pop.Connection - mb pop.MigrationBox + mb x.MigrationPkger r persisterDependencies cf configuration.Provider isSQLite bool @@ -34,7 +35,7 @@ type ( ) func NewPersister(r persisterDependencies, conf configuration.Provider, c *pop.Connection) (*Persister, error) { - m, err := pop.NewMigrationBox(migrations, c) + m, err := x.NewPkgerMigration(migrations, c) if err != nil { return nil, errors.WithStack(err) } diff --git a/schema/contrib/extension/identity.schema.json b/schema/.schema/extension/identity.schema.json similarity index 100% rename from schema/contrib/extension/identity.schema.json rename to schema/.schema/extension/identity.schema.json diff --git a/schema/extension.go b/schema/extension.go index cff8500f270..54806466460 100644 --- a/schema/extension.go +++ b/schema/extension.go @@ -3,14 +3,17 @@ package schema import ( "bytes" "encoding/json" + "path" - "github.com/gobuffalo/packr/v2" + "github.com/markbates/pkger" "github.com/pkg/errors" "github.com/ory/jsonschema/v3" + + "github.com/ory/kratos/x" ) -var box = packr.New("contrib", "contrib") +var schemas = pkger.Dir("/schema/.schema") const ( ExtensionRunnerIdentityMetaSchema ExtensionRunnerMetaSchema = "extension/identity.schema.json" @@ -56,13 +59,13 @@ type ( func NewExtensionRunner(meta ExtensionRunnerMetaSchema, runners ...Extension) (*ExtensionRunner, error) { var err error - schema, err := box.FindString(string(meta)) + schema, err := x.PkgerRead(pkger.Open(path.Join(string(schemas), string(meta)))) if err != nil { return nil, errors.WithStack(err) } r := new(ExtensionRunner) - r.meta, err = jsonschema.CompileString(string(meta), schema) + r.meta, err = jsonschema.CompileString(string(meta), string(schema)) if err != nil { return nil, errors.WithStack(err) } diff --git a/selfservice/strategy/password/schema.go b/selfservice/strategy/password/schema.go index 6f99ff932a1..66533665c23 100644 --- a/selfservice/strategy/password/schema.go +++ b/selfservice/strategy/password/schema.go @@ -1,26 +1,17 @@ package password import ( - "github.com/gobuffalo/packr/v2" + "github.com/markbates/pkger" + + "github.com/ory/kratos/x" ) -var schemas = packr.New(".schema", ".schema") +var _ = pkger.Dir("/selfservice/strategy/password/.schema") + var loginSchema, registrationSchema, settingsSchema []byte func init() { - var err error - loginSchema, err = schemas.Find("login.schema.json") - if err != nil { - panic(err) - } - - registrationSchema, err = schemas.Find("registration.schema.json") - if err != nil { - panic(err) - } - - settingsSchema, err = schemas.Find("settings.schema.json") - if err != nil { - panic(err) - } + loginSchema = x.MustPkgerRead(pkger.Open("/selfservice/strategy/password/.schema/login.schema.json")) + registrationSchema = x.MustPkgerRead(pkger.Open("/selfservice/strategy/password/.schema/registration.schema.json")) + settingsSchema = x.MustPkgerRead(pkger.Open("/selfservice/strategy/password/.schema/settings.schema.json")) } diff --git a/x/config.go b/x/config.go index 3dbc5bdbc65..ce661fbd391 100644 --- a/x/config.go +++ b/x/config.go @@ -1,19 +1,14 @@ package x import ( - "github.com/gobuffalo/packr/v2" + "github.com/markbates/pkger" "github.com/ory/x/logrusx" "github.com/ory/x/viperx" ) -var schemas = packr.New("schemas", "../.schema") +var schema = MustPkgerRead(pkger.Open("/.schema/config.schema.json")) func WatchAndValidateViper(log *logrusx.Logger) { - schema, err := schemas.Find("config.schema.json") - if err != nil { - log.WithError(err).Fatal("Unable to open configuration JSON Schema.") - } - viperx.WatchAndValidateViper(log, schema, "ORY Kratos", []string{"serve", "profiling", "log"}, "") } diff --git a/x/migration_box.go b/x/migration_box.go new file mode 100644 index 00000000000..581e3bdd677 --- /dev/null +++ b/x/migration_box.go @@ -0,0 +1,95 @@ +package x + +import ( + "bytes" + "io" + "io/ioutil" + "os" + + "github.com/gobuffalo/pop/v5" + "github.com/markbates/pkger" + "github.com/pkg/errors" +) + +// MigrationPkger is a wrapper around pkger.Dir and Migrator. +// This will allow you to run migrations from migrations packed +// inside of a compiled binary. +type MigrationPkger struct { + pop.Migrator + Dir pkger.Dir +} + +// NewPkgerMigration from a packr.Box and a Connection. +// +// migrations, err := NewPkgerMigration(pkger.Dir("/migrations")) +// +func NewPkgerMigration(dir pkger.Dir, c *pop.Connection) (MigrationPkger, error) { + fm := MigrationPkger{ + Migrator: pop.NewMigrator(c), + Dir: dir, + } + + runner := func(f io.Reader) func(mf pop.Migration, tx *pop.Connection) error { + return func(mf pop.Migration, tx *pop.Connection) error { + content, err := pop.MigrationContent(mf, tx, f, true) + if err != nil { + return errors.Wrapf(err, "error processing %s", mf.Path) + } + if content == "" { + return nil + } + err = tx.RawQuery(content).Exec() + if err != nil { + return errors.Wrapf(err, "error executing %s, sql: %s", mf.Path, content) + } + return nil + } + } + + err := fm.findMigrations(runner) + if err != nil { + return fm, err + } + + return fm, nil +} + +func (fm *MigrationPkger) findMigrations(runner func(f io.Reader) func(mf pop.Migration, tx *pop.Connection) error) error { + return pkger.Walk(string(fm.Dir), func(p string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + match, err := pop.ParseMigrationFilename(info.Name()) + if err != nil { + return err + } + + if match == nil { + return nil + } + + file, err := pkger.Open(p) + if err != nil { + return err + } + defer file.Close() + + content, err := ioutil.ReadAll(file) + if err != nil { + return err + } + + mf := pop.Migration{ + Path: p, + Version: match.Version, + Name: match.Name, + DBType: match.DBType, + Direction: match.Direction, + Type: match.Type, + Runner: runner(bytes.NewReader(content)), + } + fm.Migrations[mf.Direction] = append(fm.Migrations[mf.Direction], mf) + return nil + }) +} diff --git a/x/pkger.go b/x/pkger.go new file mode 100644 index 00000000000..b4636598362 --- /dev/null +++ b/x/pkger.go @@ -0,0 +1,23 @@ +package x + +import ( + "io/ioutil" + + "github.com/markbates/pkger/pkging" +) + +func MustPkgerRead(f pkging.File, err error) []byte { + if err != nil { + panic(err) + } + defer f.Close() + return MustReadAll(f) +} + +func PkgerRead(f pkging.File, err error) ([]byte, error) { + if err != nil { + return nil,err + } + defer f.Close() + return ioutil.ReadAll(f) +}