Skip to content

Commit 0acf0e8

Browse files
OpaClient: Add generic object overload for check (#78)
* Styra/Opa/OpaClient: Add generic object overload for .check(). Signed-off-by: Philip Conrad <[email protected]> * test: Update tests for new check() case. Signed-off-by: Philip Conrad <[email protected]> --------- Signed-off-by: Philip Conrad <[email protected]>
1 parent 8a99a2c commit 0acf0e8

File tree

2 files changed

+656
-623
lines changed

2 files changed

+656
-623
lines changed

Styra/Opa/OpaClient.cs

+17
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,23 @@ public async Task<bool> check(string path, Dictionary<string, object> input)
144144
return await evaluate<bool>(path, input);
145145
}
146146

147+
/// <summary>
148+
/// Simple allow/deny-style check against a rule, using the provided object,
149+
/// This will round-trip an object through Newtonsoft.JsonConvert, in order
150+
/// to generate the input object for the eventual OPA API call.
151+
/// </summary>
152+
/// <param name="input">The input C# object OPA will use for evaluating the rule.</param>
153+
/// <param name="path">The rule to evaluate. (Example: "app/rbac")</param>
154+
/// <returns>Result, as a boolean</returns>
155+
public async Task<bool> check(string path, object input)
156+
{
157+
// Round-trip through JSON conversion, such that it becomes an Input.
158+
var jsonInput = JsonConvert.SerializeObject(input);
159+
var roundTrippedInput = JsonConvert.DeserializeObject<Input>(jsonInput) ?? throw new OpaException(string.Format("could not convert object type to a valid OPA input"));
160+
161+
return await evaluate<bool>(path, roundTrippedInput);
162+
}
163+
147164
/// <summary>
148165
/// Evaluate a policy, then coerce the result to type T.
149166
/// </summary>

0 commit comments

Comments
 (0)