1
1
import { describe , before , after , it } from "node:test" ;
2
2
import assert from "node:assert" ;
3
- import { GenericContainer , StartedTestContainer , Wait } from "testcontainers" ;
3
+ import {
4
+ GenericContainer ,
5
+ Network ,
6
+ StartedNetwork ,
7
+ StartedTestContainer ,
8
+ Wait ,
9
+ } from "testcontainers" ;
4
10
import { OPAClient , ToInput , Input , Result } from "../src/" ;
5
11
import { HTTPClient } from "../src/lib/http" ;
6
12
@@ -53,8 +59,11 @@ allow if {
53
59
` ;
54
60
55
61
let container : StartedTestContainer ;
62
+ let network : StartedNetwork ;
63
+ let proxy : StartedTestContainer ;
56
64
let serverURL : string ;
57
65
before ( async ( ) => {
66
+ network = await new Network ( ) . start ( ) ;
58
67
container = await new GenericContainer ( "openpolicyagent/opa:latest" )
59
68
. withCommand ( [
60
69
"run" ,
@@ -66,6 +75,8 @@ allow if {
66
75
"--set=default_decision=system/main/main" ,
67
76
"/authz.rego" ,
68
77
] )
78
+ . withName ( "opa" )
79
+ . withNetwork ( network )
69
80
. withExposedPorts ( 8181 )
70
81
. withWaitStrategy ( Wait . forHttp ( "/health" , 8181 ) . forStatusCode ( 200 ) )
71
82
. withCopyContentToContainer ( [
@@ -75,6 +86,23 @@ allow if {
75
86
} ,
76
87
] )
77
88
. start ( ) ;
89
+
90
+ proxy = await new GenericContainer ( "caddy:latest" )
91
+ . withNetwork ( network )
92
+ . withExposedPorts ( 8000 )
93
+ . withWaitStrategy ( Wait . forHttp ( "/opa/health" , 8000 ) . forStatusCode ( 200 ) )
94
+ . withCopyContentToContainer ( [
95
+ {
96
+ content : `
97
+ :8000 {
98
+ handle_path /opa/* {
99
+ reverse_proxy http://opa:8181
100
+ }
101
+ }` ,
102
+ target : "/etc/caddy/Caddyfile" ,
103
+ } ,
104
+ ] )
105
+ . start ( ) ;
78
106
serverURL = `http://${ container . getHost ( ) } :${ container . getMappedPort ( 8181 ) } ` ;
79
107
80
108
for ( const [ id , body ] of Object . entries ( policies ) ) {
@@ -228,5 +256,17 @@ allow if {
228
256
assert . strictEqual ( res , true ) ;
229
257
} ) ;
230
258
231
- after ( async ( ) => await container . stop ( ) ) ;
259
+ it ( "supports rules with slashes when proxied" , async ( ) => {
260
+ const serverURL = `http://${ proxy . getHost ( ) } :${ proxy . getMappedPort ( 8000 ) } /opa` ;
261
+ const res = await new OPAClient ( serverURL ) . evaluate (
262
+ "has/weird%2fpackage/but/it_is" ,
263
+ ) ;
264
+ assert . strictEqual ( res , true ) ;
265
+ } ) ;
266
+
267
+ after ( async ( ) => {
268
+ await container . stop ( ) ;
269
+ await proxy . stop ( ) ;
270
+ await network . stop ( ) ;
271
+ } ) ;
232
272
} ) ;
0 commit comments