@@ -4,12 +4,8 @@ import (
44 "context"
55 "errors"
66 "fmt"
7- "os"
8- "path/filepath"
97 "strings"
108
11- "github.com/docker/docker/api/types/mount"
12- container "github.com/docker/docker/api/types/container"
139 "github.com/testcontainers/testcontainers-go"
1410 "github.com/testcontainers/testcontainers-go/wait"
1511
@@ -53,7 +49,7 @@ namespaces:
5349`
5450)
5551
56- // NamespaceFile represents an OPL namespace file to mount into the Keto container.
52+ // NamespaceFile represents an OPL namespace file to copy into the Keto container.
5753type NamespaceFile struct {
5854 // ContainerPath is the absolute path inside the container (e.g. "/home/ory/namespaces/tenancy.ts").
5955 ContainerPath string
@@ -65,7 +61,6 @@ type dependancy struct {
6561 * definition.DefaultImpl
6662 configuration string
6763 namespaceFiles []NamespaceFile
68- tmpDir string // temp directory for host-mounted files
6964}
7065
7166func New () definition.TestResource {
@@ -113,60 +108,7 @@ func NewWithNamespaces(
113108 }
114109}
115110
116- // writeHostFiles writes the configuration and namespace files to a temp directory
117- // on the host, returning bind mount specs for docker. This is more reliable than
118- // testcontainers ContainerFile for Keto's namespace file watcher.
119- func (d * dependancy ) writeHostFiles () ([]mount.Mount , error ) {
120- if d .tmpDir == "" {
121- tmpDir , err := os .MkdirTemp ("" , "keto-test-*" )
122- if err != nil {
123- return nil , fmt .Errorf ("failed to create temp dir: %w" , err )
124- }
125- // Make traversable by all users (container runs as non-root ory user)
126- if err := os .Chmod (tmpDir , 0o755 ); err != nil { //nolint:gosec // test dir
127- return nil , fmt .Errorf ("failed to chmod temp dir: %w" , err )
128- }
129- d .tmpDir = tmpDir
130- }
131-
132- // Write keto config
133- configPath := filepath .Join (d .tmpDir , "keto.yml" )
134- if err := os .WriteFile (configPath , []byte (d .configuration ), 0o644 ); err != nil { //nolint:gosec // test file
135- return nil , fmt .Errorf ("failed to write keto config: %w" , err )
136- }
137-
138- mounts := []mount.Mount {
139- {
140- Type : mount .TypeBind ,
141- Source : configPath ,
142- Target : "/home/ory/keto.yml" ,
143- },
144- }
145-
146- // Write namespace files
147- for i , ns := range d .namespaceFiles {
148- nsDir := filepath .Join (d .tmpDir , fmt .Sprintf ("ns_%d" , i ))
149- if err := os .MkdirAll (nsDir , 0o755 ); err != nil { //nolint:gosec,gocritic // test dir
150- return nil , fmt .Errorf ("failed to create namespace dir: %w" , err )
151- }
152-
153- nsPath := filepath .Join (nsDir , filepath .Base (ns .ContainerPath ))
154- if err := os .WriteFile (nsPath , []byte (ns .Content ), 0o644 ); err != nil { //nolint:gosec // test file
155- return nil , fmt .Errorf ("failed to write namespace file: %w" , err )
156- }
157-
158- mounts = append (mounts , mount.Mount {
159- Type : mount .TypeBind ,
160- Source : nsPath ,
161- Target : ns .ContainerPath ,
162- })
163- }
164-
165- return mounts , nil
166- }
167-
168111// containerFiles returns file copies including the config and any namespace files.
169- // Used for containers that need config parsing but not OPL file watching (e.g. migration).
170112func (d * dependancy ) containerFiles () []testcontainers.ContainerFile {
171113 files := []testcontainers.ContainerFile {
172114 {
@@ -243,24 +185,10 @@ func (d *dependancy) Setup(ctx context.Context, ntwk *testcontainers.DockerNetwo
243185 "LOG_LEAK_SENSITIVE_VALUES" : "true" ,
244186 "DSN" : databaseURL ,
245187 }),
188+ Files : d .containerFiles (),
246189 WaitingFor : wait .ForHTTP ("/health/ready" ).WithPort (d .DefaultPort ),
247190 }
248191
249- // Use bind mounts for the serve container when namespace files are present,
250- // as Keto's namespace file watcher requires host-mounted files to work
251- // correctly with OPL evaluation.
252- if len (d .namespaceFiles ) > 0 {
253- mounts , mountErr := d .writeHostFiles ()
254- if mountErr != nil {
255- return fmt .Errorf ("failed to write host files: %w" , mountErr )
256- }
257- containerRequest .HostConfigModifier = func (hc * container.HostConfig ) {
258- hc .Mounts = append (hc .Mounts , mounts ... )
259- }
260- } else {
261- containerRequest .Files = d .containerFiles ()
262- }
263-
264192 d .Configure (ctx , ntwk , & containerRequest )
265193
266194 ketoContainer , err := testcontainers .GenericContainer (ctx ,
@@ -276,12 +204,3 @@ func (d *dependancy) Setup(ctx context.Context, ntwk *testcontainers.DockerNetwo
276204 d .SetContainer (ketoContainer )
277205 return nil
278206}
279-
280- func (d * dependancy ) Cleanup (ctx context.Context ) {
281- d .DefaultImpl .Cleanup (ctx )
282-
283- // Clean up temp files
284- if d .tmpDir != "" {
285- _ = os .RemoveAll (d .tmpDir )
286- }
287- }
0 commit comments