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

Commit cb15c5e

Browse files
committed
sdk: add evaluateDefault()
Signed-off-by: Stephan Renatus <stephan@styra.com>
1 parent dd967f8 commit cb15c5e

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/opaclient.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class OPAClient {
5656
this.opa = new Opa(sdk);
5757
}
5858

59-
/** `evaluate` is used to evaluate the policy at the specified.
59+
/** `evaluate` is used to evaluate the policy at the specified path with optional input.
6060
*
6161
* @param path - The path to the policy, without `/v1/data`: use `authz/allow` to evaluate policy `data.authz.allow`.
6262
* @param input - The input to the policy, if needed.
@@ -87,4 +87,23 @@ export class OPAClient {
8787
const res = result.successfulPolicyEvaluation.result;
8888
return fromResult ? fromResult(res) : (res as Res);
8989
}
90+
91+
/** `evaluateDefault` is used to evaluate the server's default policy with optional input.
92+
*
93+
* @param input - The input to the default policy, defaults to `{}`.
94+
* @param fromResult - A function that is used to transform the policy evaluation result (which could be `undefined`).
95+
*/
96+
async evaluateDefault<In extends Input | ToInput, Res>(
97+
input?: In,
98+
fromResult?: (res?: Result) => Res,
99+
): Promise<Res> {
100+
let inp = input ?? {};
101+
if (implementsToInput(inp)) {
102+
inp = inp.toInput();
103+
}
104+
const resp = await this.opa.executeDefaultPolicyWithInput(inp);
105+
if (!resp.result) throw `no result in API response`;
106+
const res = resp.result;
107+
return fromResult ? fromResult(res) : (res as Res);
108+
}
90109
}

tests/authorizer.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ it_is := true`,
2929
token: `package token
3030
import rego.v1
3131
p := true
32+
`,
33+
main: `package system.main
34+
import rego.v1
35+
36+
main.has_input if input
37+
main.different_input if input.foo == "bar"
3238
`,
3339
};
3440
const authzPolicy = `package system.authz
@@ -39,6 +45,7 @@ allow if input.method == "PUT"
3945
allow if input.path[0] == "health"
4046
allow if input.path[2] == "test"
4147
allow if input.path[2] == "has"
48+
allow if count(input.path) == 1 # default policy
4249
allow if {
4350
input.path[2] = "token"
4451
input.identity = "opensesame"
@@ -56,6 +63,7 @@ allow if {
5663
"--log-level=debug",
5764
"--authentication=token",
5865
"--authorization=basic",
66+
"--set=default_decision=system/main/main",
5967
"/authz.rego",
6068
])
6169
.withExposedPorts(8181)
@@ -92,6 +100,18 @@ allow if {
92100
assert.strictEqual(res, true);
93101
});
94102

103+
it("default can be called without types, without input", async () => {
104+
const res = await new OPAClient(serverURL).evaluateDefault();
105+
assert.deepStrictEqual(res, { has_input: true });
106+
});
107+
108+
it("default can be called with input", async () => {
109+
const res = await new OPAClient(serverURL).evaluateDefault({
110+
foo: "bar",
111+
});
112+
assert.deepStrictEqual(res, { has_input: true, different_input: true });
113+
});
114+
95115
it("supports rules with slashes", async () => {
96116
const res = await new OPAClient(serverURL).evaluate(
97117
"has/weird%2fpackage/but/it_is",

0 commit comments

Comments
 (0)