Skip to content

Commit

Permalink
fix : clean up code and add more checks
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperBatata committed Jan 28, 2025
1 parent 7de48b7 commit 7cbbdaa
Showing 1 changed file with 5 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ class DynamicPolicy : CredentialDataValidatorPolicy() {
val regoCode = rules["rego"]
?: return Result.failure(Exception("The 'rego' code is required in the 'rules' field."))


println("regoCode: $regoCode")
println("argument: $argument")


val cleanedRegoCode = """
${
cleanCode(regoCode.jsonPrimitive.content)
Expand All @@ -98,35 +93,28 @@ class DynamicPolicy : CredentialDataValidatorPolicy() {
setBody(cleanedRegoCode)
}

println("upload: ${upload.bodyAsText()}")
check(upload.status.isSuccess()) {
"Failed to upload the policy to OPA. Check the policy code (rego) and try again."
}

val input = mapOf(
"parameter" to argument,
"credentialData" to data.toMap()
).toJsonObject()


println("input: $input")

// verify the policy
val response: HttpResponse = http.post("$policyServer/v1/data/$policyQuery/$policyName") {
contentType(ContentType.Application.Json)
setBody(mapOf("input" to input))
}

println("response: ${response.bodyAsText()}")



val result = response.body<JsonObject>()["result"]?.jsonObject
?: throw IllegalArgumentException("The response does not contain a 'result' field.")

println("result: $result")
?: throw IllegalArgumentException("Something went wrong while verifying the policy.")


// check if the policy is passed
val allow = result["allow"]
println("allow: $allow")


// delete the policy from OPA
http.delete("$policyServer/v1/policies/$policyName")
Expand Down

0 comments on commit 7cbbdaa

Please sign in to comment.