Skip to content
This repository was archived by the owner on Jul 1, 2024. It is now read-only.

Commit eb9425d

Browse files
committed
OpaApiClient: fix request path hook to work with proxies
Signed-off-by: Stephan Renatus <stephan@styra.com>
1 parent 1714db3 commit eb9425d

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

src/hooks/request-path-hook.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class RewriteRequestPathHook implements BeforeCreateRequestHook {
77
input: RequestInput,
88
): RequestInput {
99
const url = new URL(input.url);
10-
if (url.pathname.startsWith("/v1/data")) {
10+
if (url.pathname.indexOf("/v1/data/") != -1) {
1111
url.pathname = decodeURIComponent(url.pathname);
1212
return { ...input, url };
1313
}

tests/authorizer.test.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { describe, before, after, it } from "node:test";
22
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";
410
import { OPAClient, ToInput, Input, Result } from "../src/";
511
import { HTTPClient } from "../src/lib/http";
612

@@ -53,8 +59,11 @@ allow if {
5359
`;
5460

5561
let container: StartedTestContainer;
62+
let network: StartedNetwork;
63+
let proxy: StartedTestContainer;
5664
let serverURL: string;
5765
before(async () => {
66+
network = await new Network().start();
5867
container = await new GenericContainer("openpolicyagent/opa:latest")
5968
.withCommand([
6069
"run",
@@ -66,6 +75,8 @@ allow if {
6675
"--set=default_decision=system/main/main",
6776
"/authz.rego",
6877
])
78+
.withName("opa")
79+
.withNetwork(network)
6980
.withExposedPorts(8181)
7081
.withWaitStrategy(Wait.forHttp("/health", 8181).forStatusCode(200))
7182
.withCopyContentToContainer([
@@ -75,6 +86,23 @@ allow if {
7586
},
7687
])
7788
.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();
78106
serverURL = `http://${container.getHost()}:${container.getMappedPort(8181)}`;
79107

80108
for (const [id, body] of Object.entries(policies)) {
@@ -228,5 +256,17 @@ allow if {
228256
assert.strictEqual(res, true);
229257
});
230258

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+
});
232272
});

0 commit comments

Comments
 (0)