Commit Graph

58 Commits

Author SHA1 Message Date
Jasper Van der Jeugt d8947db27d topdown: add yaml.is_valid and json.is_valid
Signed-off-by: Jasper Van der Jeugt <jasper@fugue.co>
2020-10-27 10:30:15 -04:00
Ashutosh Narkar 63560e0d1e topdown/http: handle http.send errors in-band
This change updates how errors from http.send are handled.
By default, an error returned by `http.send` halts the policy evaluation.
This commit allows users to return errors in the response object
returned by `http.send` instead of halting evaluation.

Fixes: #2187

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2020-10-09 09:11:19 -07:00
Ashutosh Narkar f8fcc2c9e0 topdown/http: Add support for controlling caching in http.send
This commit adds two new fields to the http.send builtin
that allow the user to override the caching directives defined
by the server and thus get more control over the caching
behavior.

Fixes: #2666

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2020-10-05 17:40:47 -07:00
Ashutosh Narkar e9aee648b9 topdown: Add net.cidr_merge function to produce smallest possible list of CIDRs
This commit adds a new builtin to merge adjacent subnets and return the
smallest possible list of CIDRs.

To help with computing CIDR blocks between two
IP networks, an implemetation from https://github.com/cilium/cilium
is leveraged.

Fixes: #2692

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2020-09-24 18:03:23 -07:00
Ashutosh Narkar 8cb34e48c2 topdown: Address negative duration for the current age of http response
The current age of a http response is calculated as the difference
between the current time and the value contained in the "Date" response
header. There are couple of scenarios that could lead to the current age
being represented as a negative duration.

1. Since the value of "Date" response header is parsed using Go's
time.Parse method, it does not contain a monotonic clock reading. As a result,
the time.Sub method uses wall clock readings to determine the difference between
current time and the parsed version of the response time.

2. The server could set a value for the "Date" response header which may not be a true indication of
when the response was generated.

This change updates the logic that determines whether a cached response is fresh or not,
to treat the resposne as stale if the current response age is represented as a negative duration.

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2020-09-21 10:46:53 -04:00
scevallos 56afac8817 Make correction to json.remove syntax in example
Signed-off-by: scevallos <sebastianlcevallos@gmail.com>
2020-09-09 13:25:07 -07:00
Frederic 870f28ff4a topdown: Add urlquery.decode_object builtin
This builtin is the reverse of the encode_object builtin and
makes it easier to use the URI query parameters in policies

Fixes #2647

Signed-off-by: Frederic <frederic.vanreet@icloud.com>
2020-08-31 11:55:15 -04:00
Ashutosh Narkar 7f65b04561 Add a new inter-query cache to cache responses across queries
This commit adds a new inter-query cache that built-in
functions can use to cache responses across queries.

The OPA config includes a new "caching" field that can be used
to set the size of the cache. By default there is no limit.

This change also updates `http.send` to optionally utilize the
inter-query cache.

Fixes #1753

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2020-08-06 14:33:18 -07:00
Charlie Egan 01b6e21812 Allow input as PEM strings to x509 built-ins
In the PR discussion here: https://github.com/open-policy-agent/opa/pull/2564#issuecomment-662012650
we decided that PEM string input was also a valid use case to support.

This change allows string PEM data to be passed as input to both cert
and csr functions.

It also moves the cert and csr functions to be co-located.

Signed-off-by: Charlie Egan <charlieegan3@users.noreply.github.com>
2020-07-22 11:50:02 -07:00
Charlie Egan 061f408b57 Parse PEM or DER data in crypto built-ins
Fixes https://github.com/open-policy-agent/opa/issues/2557

In #2557 I highlighted an inconsistency in our parsing of x509 data
where CSRs were PEM decoded and certificate chains where DER data.

This change allows both built-ins to accept data as DER or PEM wrapped
in base64.

While addressing the inconsistency, this also makes it possible to
decode a PEM certificate chain - the use case that prompted me to make
this change.

I have also updated the tests to use common names as the expected
output. I feel that this makes it clearer what we're dealing with
without detracting from the utility of the test cases (since we're not
really testing crypto/x509 from the standard library).

Signed-off-by: Charlie Egan <charlieegan3@users.noreply.github.com>
2020-07-22 11:50:02 -07:00
Torin Sandall 1500073fc7 ast: Deprecate re_match in favour of regex.match
This ought to have been done a while ago. Going forward we prefer all
built-in functions to be namespaced. We made that change after
introducing `re_match` but before introducing several other
regex-related functions. This commit deprecates `re_match` in a
backwards-compatible manner.

Also, move a few built-in declarations up in the ast/builtins.go file
because they were in the wrong sections.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2020-07-16 13:02:05 -04:00
Torin Sandall 91a85f79bf topdown: Add new regex.is_valid built-in function
The existing regex functions error if the pattern is invalid. Until
OPA exposes a way to treat built-in function errors as undefined, it
is useful to be able to check for validity. Also, some use cases may
simply want to verify a regex pattern from the outside.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2020-07-16 13:02:05 -04:00
Charlie Egan 8d321f0856 Add semver.compare & is_valid built-ins
These built-ins will allow policies to more easily make comparisons on
semantic version strings.

I am doing this because I recently needed to write policy concerning
versions of installed software in Kubernetes environments.

I feel this is a better solution than implementing such a feature in
Rego: https://gist.github.com/charlieegan3/76dbec05c65164ac98dfec74b1381c5a

is_valid is to allow users to gate access to the compare function which
fails if the input is not a valid version string.

The semver functionality is vendored from the coreos package based on
the discussion here:
https://github.com/open-policy-agent/opa/pull/2538#pullrequestreview-448422711

Signed-off-by: Charlie Egan <charlieegan3@users.noreply.github.com>
2020-07-15 16:06:14 -07:00
Torin Sandall 616d9473f1 topdown: Add new numbers.range built-in function
This commit adds a new built-in function to generate a range of
integers between two values (inclusive). This is useful in certain
cases where users need to enumerate a set of values (e.g., port
numbers).

Fixes #2479

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2020-07-14 13:12:56 -07:00
Vivek Bagade f279e31480 add builtin to parse certificate signing requests
Signed-off-by: Vivek Bagade <vivek.bagade92@gmail.com>
2020-06-01 11:38:32 -07:00
Frederic 45fbb7c096 Add power-of-two equivalents support in parse_bytes builtin
Before this change the parse_bytes builtin would fail when the unit
did not contain the bytes symbol (e.g. 128Mi would fail).

With this change we also accept units without the b/B at the end.

Fixes #2340

Signed-off-by: Frederic <frederic.vanreet@icloud.com>
2020-05-18 12:14:27 -07:00
Jasper Van der Jeugt 220e64d736 Add graph.reachable builtin
A lot of common use cases for recursion in Rego can be reduced to a graph
reachability problem.  This new builtin allows policies to evaluate such
recursive rules.

Signed-off-by: Jasper Van der Jeugt <jasper@fugue.co>
2020-05-13 11:37:23 -07:00
Torin Sandall f0c222c433 docs: Merge cheat sheet content into reference
This change merges the cheat sheet content into the reference page--so
that there is one place that users can go to answer basic questions
about how to do X inside of Rego. Also, reorder the policy language
pages to go from language definitions => reference => testing =>
performance as this is the order we want users to navigate.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2020-05-13 13:23:49 -04:00
Frederic 6b3d55e5b2 Add missing jwt verify functions
With this commit the following algorithms are now supported for verifying jwt tokens:
HS256, HS384, HS512, RS256, RS384, RS512, ES256, ES384, ES512, PS256, PS384 and PS512

Fixes #2324

Signed-off-by: Frederic <frederic.vanreet@icloud.com>
2020-05-08 12:39:04 -07:00
Charlie Egan 5549249fa7 Fix minor reference typo
Signed-off-by: Charlie Egan <charlieegan3@users.noreply.github.com>
2020-04-27 12:34:51 -07:00
Rene Klootwijk dbc5c16cd4 A built-in command to create a version 4 uuid
Signed-off-by: Rene Klootwijk <rene.klootwijk@rencon.nl>

A built-in command to create a version 4 uuid

Signed-off-by: Rene Klootwijk <rene.klootwijk@rencon.nl>

A built-in command to create a version 4 uuid

Signed-off-by: Rene Klootwijk <rene.klootwijk@rencon.nl>

Removed external dependency and created helper for uuid

Signed-off-by: Rene Klootwijk <rene.klootwijk@rencon.nl>

Added idempotency for the uuid.rfc4122() command

Signed-off-by: Rene Klootwijk <rene.klootwijk@rencon.nl>
2020-04-20 19:42:03 -07:00
marco 5e6a138559 docs: Fix the EBNF for the '=' terminal symbol
The rule-body non-terminal symbol did not quote the '=' symbol.

Signed-off-by: marco <marco@styra.com>
2020-04-16 13:21:19 -07:00
James Peach ce92d19f65 topdown: support inline TLS certificates and keys
`http.send` already supports TLS keys and certificates obtained from
external files and environment variables. Add support for specifying
certificates and keys as direct parameters to `http.send`. This is
useful when you already have policy variables with this data.

There are 3 options that this affects: client certificates, client
keys and CA bundles. All of these can now be specified from either a
file, an environment variable or from raw data. In the case of client
certificates and keys, each access method must use a matched pair (e.g.
both the certificate and the key must come from environment variables).
CA certificate bundles are the union of all the specified access methods.

Signed-off-by: James Peach <jpeach@vmware.com>
2020-04-09 10:59:04 -07:00
Torin Sandall 221c1ba6c9 docs: Fix EBNF grammar to quote terms correctly
A user pointed out that our EBNF grammar did not quote a few of the
terms correctly. This commit fixes those cases.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2020-04-08 18:25:21 -07:00
Patrick East a2ae154f0c topdown: Return response headers with http.send
Previously we left them off the returned AST object, this will now
just massage them into valid types and pass them along into the
return value.

Fixes: #2238
Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-04-06 05:48:40 -07:00
Brian Atkinson 29d33be6ea docs: Fix incorrect example for regex.template_match
regex.template_match both per the code as well as the documentation
takes 4 string arguments and returns one boolean. The example had a 5th
argument that looked to be an out value. Remove the extra argument and
clean up the text.

Signed-off-by: Brian Atkinson <brian@atkinson.mn>
2020-04-06 05:26:45 -07:00
Frederic dc72c13f0a Add time.add_date builtin
New builtin for adding years, months and days to a time
This might be useful for computing a day in the future or past
more easily

Fixes #1990

Signed-off-by: Frederic <frederic.vanreet@icloud.com>
2020-04-01 13:59:13 -07:00
James Peach 29d8fbbef6 topdown: Add http.send support for setting the TLS server name
If the caller sets the Host header, then we can reasonably impute that
they wanted to use that at the TLS layer as well. To handle test cases
where the caller needs to explicitly set the TLS server name, add a
`tls_server_name` parameter that overrides both the `Host` header and
the Go default.

Signed-off-by: James Peach <jpeach@vmware.com>
2020-03-26 15:52:21 -07:00
Torin Sandall 5a5d2a42f4 topdown: Add net.cidr_contains_matches built-in function
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2020-03-25 12:28:54 -07:00
Patrick East 16b6e29a97 docs: Fix spacing in policy reference
Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-03-20 17:16:25 -07:00
Michael Gilliland 834aaee4c2 docs: minor typo resposne -> response
Signed-off-by: Michael "Gilli" Gilliland <mjg.py3@gmail.com>
2020-03-10 09:21:00 -07:00
James Peach 048c72408f topdown: Add Host header support to http.send()
If the headers map given to `http.send` includes the `Host` header, hoist
it up to the request. This lets users specify the server to connect to
and the `Host` header to send independently, much like curl.

Signed-off-by: James Peach <jpeach@vmware.com>
2020-03-02 16:31:04 -08:00
Michael "Gilli" Gilliland ff3766dfb3 Add bitwise operators after bits..
Fixes #1919

Signed-off-by: Michael "Gilli" Gilliland <mjg.py3@gmail.com>
2020-03-01 16:31:18 -08:00
Michael Krotscheck 863f0524dc Permit verifying JWT's with a public key
Add the ability to verify a JWT signature using only the PEM
encoded public key, instead of the whole certificate. The test
case key was extracted from the certPem in the line directly above.

Closes #2158

Signed-off-by: Michael Krotscheck <krotscheck@gmail.com>
2020-02-28 11:26:27 -08:00
Tim Garrity ad6b46683c skip tls verification with built in http.send
fixes #1865

Signed-off-by: Victor Pupim <pupimvictor@gmail.com>
2020-02-28 10:05:27 -08:00
Patrick East 1603723262 Add json.remove builtin
This adds in a new built-in function `json.remove` which will take in
an object and list of json pointer paths (similar to `json.filter`)
and create a new object with all of the paths removed from the base
object.

Reference: #1617
Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-02-24 14:03:53 -08:00
Patrick East 7e77ac5088 Add timeout for http.send builtin
There is a new optional `timeout` option to specify with `http.send`
which will set a client timeout on the request. This will override the
default 5 second timeout.

This change also corrects the request to use the builtin context. This
means that if the evaluation is canceled the request will also now be
canceled.

There is an environment variable for adjusting the default timeout
which we should aim to remove in future OPA versions (when
appropriate). For now though it is still supported, and will panic if
supplied with an invalid value rather than previously ignoring it and
effectively disable the timeout (0 == unlimited).

Fixes: #2099
Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-02-18 13:42:30 -08:00
Patrick East 7a9046f8aa topdown: http.send use provided CA without client certs
Previously it would ignore the provided CA parameters unless client
cert info was provided. This change now allows for providing CA info
without any client auth details.

Fixes: #1976
Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-02-17 08:35:32 -05:00
Patrick East 9c2022ebfa Add object manipulation built-ins
This commit adds in the following built-ins:

`object.remove`
`object.union`
`object.filter`

All of which are helpers for object manipulation in policies.

Reference: #1617
Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-02-13 11:31:50 -08:00
Fredrik Appelros e650576e36 Fix glob.match docs
Correct the syntax for the pattern-alternatives examples of
`glob.match`.

Signed-off-by: Fredrik Appelros <fredrik.appelros@sinch.com>
2020-02-06 13:05:16 -08:00
Fredrik Appelros 6a60087434 Clarify glob.match docs
Attempt to clarify how `glob.match` works with respect to delimiters by
adding an additional example.

Signed-off-by: Fredrik Appelros <fredrik.appelros@sinch.com>
2020-02-06 13:05:16 -08:00
Marcin Wyszynski ffffcd758c Rename lookup to object.get
Signed-off-by: Marcin Wyszynski <marcin.pixie@gmail.com>
2020-01-22 16:07:30 -05:00
Marcin Wyszynski f5f56e460f Add lookup built-in
Signed-off-by: Marcin Wyszynski <marcin.pixie@gmail.com>
2020-01-22 16:07:30 -05:00
menendezjaume 4e599e3e79 Extending REGO with hashing functions
Signed-off-by: menendezjaume <m.menendezjaume@gmail.com>
2020-01-09 08:37:23 -05:00
Jasper Van der Jeugt 54861cdd12 Small doc updates
Signed-off-by: Jasper Van der Jeugt <jasper@fugue.co>
2019-12-20 15:13:40 -05:00
Torin Sandall 089a191208 topdown: Add net.cidr_expand built-in function
In some cases, policies may need to expand CIDRs to check if hosts in
the range are valid (e.g., due to limitations of external APIs being
accessed from inside the policy.)

This change adds a built-in function to enumerate the addresses in a
CIDR. The built-in function supports cancellation in case the range is
too large and taking too long to generate.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-12-12 16:06:18 -08:00
Patrick East f01610e058 Add json.filter builtin
The new builtin takes an object and list of paths to keep.

Reference: #1617
Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-12-12 10:24:25 -08:00
Patrick East 514240f480 docs: Add some clarifications to JWT docs
This adds some highlights to sections that commonly trip up users and
gives some additional examples for how the functions work and can
interact.

Fixes: #1916
Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-12-04 10:18:19 -08:00
Torin Sandall cdcebd805e topdown: Add raw_body parameter to http.send
Instead of changing the behaviour of the body parameter (which is
always sent as JSON) just add a new parameter that gives the caller
complete control over the message body.

Also, refactor the docs for the function to be a bit more readable.

Fixes #1903

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-11-14 13:45:39 -05:00
Torin Sandall c4fb8e214b docs: Add JWT verification examples to reference
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-10-30 11:41:00 -04:00