Skip to content

Commit fcba081

Browse files
committed
Support in-memory Keto without PostgreSQL dependency
When no database dependency is provided, skip migration and use whatever DSN is in the config (typically memory). This enables OPL permit evaluation which requires in-memory storage in testcontainer environments.
1 parent 7403581 commit fcba081

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

frametests/deps/testoryketo/keto.go

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package testoryketo
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
76
"strings"
87

@@ -89,6 +88,8 @@ func NewWithOpts(
8988
// NewWithNamespaces creates a new Keto test resource with OPL namespace files.
9089
// The configuration string should reference the namespace files via
9190
// "namespaces: location: file:///path/to/file.ts".
91+
// When no database dependency is provided, Keto uses in-memory storage
92+
// with auto-migration, which is required for OPL permit evaluation.
9293
func NewWithNamespaces(
9394
configuration string,
9495
namespaceFiles []NamespaceFile,
@@ -160,31 +161,39 @@ func (d *dependancy) migrateContainer(
160161
return nil
161162
}
162163

163-
func (d *dependancy) Setup(ctx context.Context, ntwk *testcontainers.DockerNetwork) error {
164-
if len(d.Opts().Dependencies) == 0 || !d.Opts().Dependencies[0].GetDS(ctx).IsDB() {
165-
return errors.New("no ByIsDatabase dependencies was supplied")
166-
}
167-
168-
ketoDB, _, err := testpostgres.CreateDatabase(ctx, d.Opts().Dependencies[0].GetInternalDS(ctx), "keto")
169-
if err != nil {
170-
return err
171-
}
164+
// hasDBDependency checks if a database dependency is available.
165+
func (d *dependancy) hasDBDependency(ctx context.Context) bool {
166+
deps := d.Opts().Dependencies
167+
return len(deps) > 0 && deps[0].GetDS(ctx).IsDB()
168+
}
172169

173-
databaseURL := ketoDB.String()
170+
func (d *dependancy) Setup(ctx context.Context, ntwk *testcontainers.DockerNetwork) error {
171+
env := d.Opts().Env(map[string]string{
172+
"LOG_LEVEL": "debug",
173+
"LOG_LEAK_SENSITIVE_VALUES": "true",
174+
})
174175

175-
err = d.migrateContainer(ctx, ntwk, databaseURL)
176-
if err != nil {
177-
return err
176+
// When a database dependency is available, use it with separate migration.
177+
// Otherwise, use in-memory storage with auto-migration (supports OPL evaluation).
178+
if d.hasDBDependency(ctx) {
179+
ketoDB, _, err := testpostgres.CreateDatabase(ctx, d.Opts().Dependencies[0].GetInternalDS(ctx), "keto")
180+
if err != nil {
181+
return err
182+
}
183+
184+
databaseURL := ketoDB.String()
185+
env["DSN"] = databaseURL
186+
187+
err = d.migrateContainer(ctx, ntwk, databaseURL)
188+
if err != nil {
189+
return err
190+
}
178191
}
179192

180193
containerRequest := testcontainers.ContainerRequest{
181-
Image: d.Name(),
182-
Cmd: []string{"serve", "--config", "/home/ory/keto.yml"},
183-
Env: d.Opts().Env(map[string]string{
184-
"LOG_LEVEL": "debug",
185-
"LOG_LEAK_SENSITIVE_VALUES": "true",
186-
"DSN": databaseURL,
187-
}),
194+
Image: d.Name(),
195+
Cmd: []string{"serve", "--config", "/home/ory/keto.yml"},
196+
Env: env,
188197
Files: d.containerFiles(),
189198
WaitingFor: wait.ForHTTP("/health/ready").WithPort(d.DefaultPort),
190199
}

0 commit comments

Comments
 (0)