Skip to content

Commit

Permalink
fix: minor bugfixes (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik authored Dec 14, 2020
1 parent 3ad32bc commit 185ee1e
Show file tree
Hide file tree
Showing 17 changed files with 130 additions and 29 deletions.
13 changes: 13 additions & 0 deletions cmd/migrate/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package migrate

import "github.com/spf13/cobra"

var migrateCmd = &cobra.Command{
Use: "migrate",
}

func RegisterCommandRecursive(parent *cobra.Command) {
migrateCmd.AddCommand(newStatusCmd(), newUpCmd())

parent.AddCommand(migrateCmd)
}
30 changes: 30 additions & 0 deletions cmd/migrate/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package migrate

import (
"context"
"fmt"

"github.com/ory/x/cmdx"
"github.com/ory/x/logrusx"
"github.com/spf13/cobra"

"github.com/ory/keto/internal/driver"
)

func newStatusCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "status",
RunE: func(cmd *cobra.Command, _ []string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

reg := driver.NewDefaultRegistry(ctx, logrusx.New("keto", "test"), cmd.Flags(), "test", "adf", "today")
if err := reg.Migrator().MigrationStatus(ctx, cmd.OutOrStdout()); err != nil {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Could not get migration status: %+v\n", err)
return cmdx.FailSilently(cmd)
}
return nil
},
}
return cmd
}
30 changes: 30 additions & 0 deletions cmd/migrate/up.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package migrate

import (
"context"
"fmt"

"github.com/ory/x/cmdx"
"github.com/ory/x/logrusx"
"github.com/spf13/cobra"

"github.com/ory/keto/internal/driver"
)

func newUpCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "up",
RunE: func(cmd *cobra.Command, _ []string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

reg := driver.NewDefaultRegistry(ctx, logrusx.New("keto", "test"), cmd.Flags(), "test", "adf", "today")
if err := reg.Migrator().MigrateUp(ctx); err != nil {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Could not apply migrations: %+v\n", err)
return cmdx.FailSilently(cmd)
}
return nil
},
}
return cmd
}
6 changes: 5 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"runtime"
"strings"

"github.com/ory/keto/cmd/migrate"

"github.com/ory/x/cmdx"

"github.com/ory/keto/cmd/namespace"
Expand Down Expand Up @@ -67,7 +69,7 @@ func init() {
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags, which, if defined here,
// will be global for your application.
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "Config file (default is $HOME/.keto.yaml)")
RootCmd.PersistentFlags().StringSlice("config", []string{}, "Config file (default is $HOME/.keto.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
Expand All @@ -76,6 +78,8 @@ func init() {
relationtuple.RegisterCommandRecursive(RootCmd)

namespace.RegisterCommandsRecursive(RootCmd)

migrate.RegisterCommandRecursive(RootCmd)
}

// initConfig reads in config file and ENV variables if set.
Expand Down
4 changes: 4 additions & 0 deletions internal/check/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func (e *Engine) subjectIsAllowed(ctx context.Context, requested *relationtuple.

var res bool
for _, sr := range rels {
// TODO move this to input validation
if requested.Subject == nil {
return false, fmt.Errorf("subject is unexpectedly nil for %+v", requested)
}
// we only have to check Subject here as we know that sr was reached from requested.ObjectID, requested.Relation through 0...n indirections
if requested.Subject.Equals(sr.Subject) {
// found the requested relation
Expand Down
2 changes: 1 addition & 1 deletion internal/driver/config/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ type Provider interface {
Set(key string, v interface{})
}

const DSNMemory = "memory"
const DSNMemory = "sqlite://:memory:?_fk=true"
6 changes: 5 additions & 1 deletion internal/driver/config/provider_koanf.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ func (k *KoanfProvider) CORS() (cors.Options, bool) {
}

func (k *KoanfProvider) DSN() string {
return k.p.StringF(KeyDSN, "memory")
dsn := k.p.StringF(KeyDSN, DSNMemory)
if dsn == "memory" {
return DSNMemory
}
return dsn
}

func (k *KoanfProvider) TracingServiceName() string {
Expand Down
3 changes: 3 additions & 0 deletions internal/driver/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ory/keto/internal/check"
"github.com/ory/keto/internal/expand"
"github.com/ory/keto/internal/namespace"
"github.com/ory/keto/internal/persistence"
"github.com/ory/keto/internal/relationtuple"

"github.com/ory/keto/internal/x"
Expand All @@ -29,6 +30,8 @@ type Registry interface {
namespace.MigratorProvider
expand.EngineProvider
check.EngineProvider
persistence.MigratorProvider
persistence.Provider

HealthHandler() *healthx.Handler
Tracer() *tracing.Tracer
Expand Down
17 changes: 8 additions & 9 deletions internal/driver/registry_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ func (r *RegistryDefault) Persister() persistence.Persister {
return r.p
}

func (r *RegistryDefault) Migrator() (persistence.Migrator, error) {
return r.p.(persistence.Migrator), nil
func (r *RegistryDefault) Migrator() persistence.Migrator {
return r.p.(persistence.Migrator)
}

func (r *RegistryDefault) Init(ctx context.Context) error {
c, err := pop.NewConnection(&pop.ConnectionDetails{
URL: "sqlite://:memory:?_fk=true",
URL: r.c.DSN(),
})
if err != nil {
return errors.WithStack(err)
Expand All @@ -139,12 +139,11 @@ func (r *RegistryDefault) Init(ctx context.Context) error {
return err
}

m, err := r.Migrator()
if err != nil {
return err
}
if err := m.MigrateUp(context.Background()); err != nil {
return err
m := r.Migrator()
if r.c.DSN() == config.DSNMemory {
if err := m.MigrateUp(context.Background()); err != nil {
return err
}
}

namespaceConfigs, err := nm.Namespaces(ctx)
Expand Down
25 changes: 17 additions & 8 deletions internal/persistence/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,29 @@ package persistence
import (
"context"
"errors"
"io"

"github.com/ory/keto/internal/namespace"

"github.com/ory/keto/internal/relationtuple"
)

type Persister interface {
relationtuple.Manager
namespace.Migrator
}

type Migrator interface {
MigrateUp(ctx context.Context) error
}
type (
Persister interface {
relationtuple.Manager
namespace.Migrator
}
Migrator interface {
MigrateUp(context.Context) error
MigrationStatus(context.Context, io.Writer) error
}
MigratorProvider interface {
Migrator() Migrator
}
Provider interface {
Persister() Persister
}
)

var (
ErrNamespaceUnknown = errors.New("namespace unknown")
Expand Down
9 changes: 8 additions & 1 deletion internal/persistence/sql/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package sql

import (
"context"
"database/sql"
"fmt"

"github.com/pkg/errors"

"github.com/gobuffalo/pop/v5"

"github.com/ory/keto/internal/namespace"
"github.com/ory/keto/internal/persistence"
)

type (
Expand Down Expand Up @@ -54,7 +56,8 @@ func (p *Persister) MigrateNamespaceUp(ctx context.Context, n *namespace.Namespa
ID: n.ID,
Version: mostRecentSchemaVersion,
}
if err := c.Create(&nr); err != nil {

if err := c.RawQuery(fmt.Sprintf("INSERT INTO %s (id, schema_version) VALUES (?, ?)", nr.TableName()), nr.ID, nr.Version).Exec(); err != nil {
return errors.WithStack(err)
}

Expand All @@ -70,6 +73,10 @@ func (p *Persister) NamespaceFromName(ctx context.Context, name string) (*namesp
func (p *Persister) NamespaceStatus(ctx context.Context, id int) (*namespace.Status, error) {
var n namespaceRow
if err := p.connection(ctx).Find(&n, id); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, persistence.ErrNamespaceUnknown
}

return nil, err
}

Expand Down
5 changes: 5 additions & 0 deletions internal/persistence/sql/persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sql
import (
"context"
"fmt"
"io"
"strconv"

"github.com/ory/keto/internal/namespace"
Expand Down Expand Up @@ -59,6 +60,10 @@ func (p *Persister) MigrateUp(_ context.Context) error {
return p.mb.Up()
}

func (p *Persister) MigrationStatus(_ context.Context, w io.Writer) error {
return p.mb.Status(w)
}

func (p *Persister) connection(ctx context.Context) *pop.Connection {
tx := ctx.Value(transactionContextKey)
if tx == nil {
Expand Down
4 changes: 0 additions & 4 deletions internal/persistence/sql/relationtuples.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ func (r *relationTuple) toInternal() (*relationtuple.InternalRelationTuple, erro
}

func (p *Persister) GetRelationTuples(ctx context.Context, query *relationtuple.RelationQuery, options ...x.PaginationOptionSetter) ([]*relationtuple.InternalRelationTuple, string, error) {
pop.Debug = true
defer func() {
pop.Debug = false
}()
const (
whereRelation = "relation = ?"
whereObject = "object = ?"
Expand Down
4 changes: 0 additions & 4 deletions internal/relationtuple/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,6 @@ func (r *InternalRelationTuple) ToGRPC() *acl.RelationTuple {
}

func (r *InternalRelationTuple) FromURLQuery(query url.Values) (*InternalRelationTuple, error) {
if r == nil {
r = &InternalRelationTuple{}
}

if s := query.Get("subject"); s != "" {
var err error
r.Subject, err = SubjectFromString(s)
Expand Down
1 change: 1 addition & 0 deletions internal/relationtuple/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func (h *handler) createRelation(w http.ResponseWriter, r *http.Request, _ httpr
}

if err := h.d.RelationTupleManager().WriteRelationTuples(r.Context(), &rel); err != nil {
h.d.Logger().WithError(err).WithField("relationtuple", rel).Errorf("got an error while creating the relation tuple")
h.d.Writer().WriteError(w, r, errors.WithStack(herodot.ErrInternalServerError))
return
}
Expand Down

0 comments on commit 185ee1e

Please sign in to comment.