Yes, net.cidr_contains
is the right way to go if you know the specific blocks approved requests will originate from.
I assume your Rego looks something like this:
package validate
import input.attributes.request.http as http_request
import input.attributes.source.address as source_address
allowed_paths = {
"/foo",
"/bar"
}
allow {
http_request.method == "POST"
allowed_paths[http_request.path]
net.cidr_contains("127.0.0.1/24",source_address.Address.SocketAddress.address)
}
There's a few ways to test.
Manually, you can use the Rego Playground which allows you to hand write requests and test them. This isn't a good automated solution, but will work for spot/sanity-checking.
For CI or precommit checks, you can use the opa
CLI to do unit testing. The Gatekeeper Library repository provides excellent examples of how to do this. A test might look something like:
package validate
test_input_allowed_request {
input := {"attributes":{"request":{"http":{"method":"POST","path":"/foo"}},"source":{"address":{"Address":{"SocketAddress":{"address":"127.0.0.64/26"}}}}}}
results := allow with input as input
results.allow
}