Commit Graph

1840 Commits

Author SHA1 Message Date
Patrick East 346aa964e8 Add support for multiple bundles
This change brings in support for multiple bundles to be downloaded
and activated OPA.

This is enabled by using the new config option `bundles` to define
the bundles, and deprecates the older `bundle` option.

The new `bundles` keyword and structure is propagated through to the
decision logs, status API, provenance, stored manifests, etc. Check
out the doc changes for all the updated structures.

That being said any existing configuration using `bundle` will *not*
see the new structure, everything is intended to be backwards
compatible (almost to a fault).

Fixes: #721

Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-07-31 03:43:38 -04:00
Patrick East c2d2d1b7fa Remove extra character in util/close.go
Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-07-31 03:43:38 -04:00
Patrick East 02ceb4f5d4 Move manifest pkg to a public location
This will allow others consuming OPA as a library to have easier
access to manage/query the bundle manifests.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-07-31 03:43:38 -04:00
Patrick East 3aea6a0922 Add an edge tag for opa docker images
This tag will follow the current master branch and lines up with what
is in the docs as the `edge` version alias.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-07-30 11:14:13 -07:00
Torin Sandall 221974f28a docs: Add example of merging two objects
This is answers a common question that was recently raised in #1588.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-07-30 08:54:53 -07:00
Jasper Van der Jeugt 66fb55f40e Move unsafe built-in check to compiler
This patch moves the check for unsafe built-ins from the server to the
compiler, so it can be used by the Go API as well as the HTTP API.

This was previously discussed in #1570.

Signed-off-by: Jasper Van der Jeugt <jasper@fugue.co>
2019-07-29 19:53:45 -04:00
Torin Sandall eabe7f1312 cmd: Update subcommands to support fails explanation mode
This change updates the test command to use the fail explanation mode
by default. Since the test framework expects test rules to be
defined/true failure events are usually what is required to debug test
failures (e.g., some assertion in the test rule will Fail and that
will _likely_ be based on some failure in the policy under test.)
Hopefully this makes the test output a bit less verbose and more readable.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-07-29 18:56:09 -04:00
Torin Sandall e37482d9cd repl: Update REPL to support fails explanation mode
Also, refactor how the REPL prints debug state a bit to reduce
boilerplate for each possible explanation mode.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-07-29 18:56:09 -04:00
Torin Sandall b55fc2e80d server: Update server to support fails explanations mode
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-07-29 18:56:09 -04:00
Torin Sandall 87347dc1a5 topdown: Rename notes package to lineage and add Fail filter
These changes just rename the notes package so that it can be used
more generally. In addition, it adds another filter to show Fail
events and their lineage. The Fail filter will be exposed like the
Notes filter and will hopefully be useful in test scenarios.

This change is backwards incompatible but it's unlikely that anyone
outside of OPA was depending on the notes package. Updating
dependencies to use lineage.Notes instead of notes.Filter should be
easy enough if needed.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-07-29 18:56:09 -04:00
Torin Sandall f3d1ef4859 Update CHANGELOG.md for v0.12.2
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-07-27 13:44:46 -04:00
Torin Sandall e7af9f8257 travis: Update install to include awscli
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-07-26 12:33:36 -04:00
Torin Sandall c9df4da104 travis: Update build to push edge binaries to S3
These changes update the travis build to push the linux/amd64 binary
to an S3 bucket that allows public reads. This provides a simple
way for people to obtain and execute OPA locally which is useful when
Docker is not available (e.g., as is the case with Netlify.)

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-07-26 10:52:32 -04:00
Patrick East dd7cb56aab internal/version: Fixup docstring for UserAgent
Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-07-24 10:41:23 -07:00
Brendan Ryan 350a74832e make path optional
Signed-off-by: Brendan Ryan <brendanjryan@github.com>
2019-07-23 14:23:54 -07:00
Brendan Ryan 57d72ae14a fixes usage of fmt command
Signed-off-by: Brendan Ryan <brendanjryan@github.com>
2019-07-23 14:23:54 -07:00
Christian Muehlhaeuser 0e5b449219 Fixed typos in code
Signed-off-by: Christian Muehlhaeuser <muesli@gmail.com>
2019-07-19 13:46:23 -07:00
Patrick East 1a673b8fd6 Add User-Agent to rest client as default header
This can be overridden by custom header configuration for services,
or requests but will default to something like:

`Open Policy Agent/<version> (<os>, <arch>)`

This is set on all outbound requests for status, decision logs, bundle
and discovery downloads, http requests from policies, etc.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-07-17 13:30:03 -07:00
Patrick East a02136fed4 topdown: Move all the jwt tests into one place
The tests were split up between the gigantic `topdown_test.go` file
and the `tokens_test.go`file. This just moves the unit tests for
stuff in `tokens.go` into `tokens_test.go`.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-07-17 11:02:32 -07:00
repenno 48a4128013 Add jws encoding built-in functions
fixes #1174

Signed-off-by: repenno <rapenno@gmail.com>
2019-07-16 09:04:36 -04:00
repenno a4b1a128ac Add jws encoding built-in functions
fixes #1174

Signed-off-by: repenno <rapenno@gmail.com>
2019-07-16 09:04:36 -04:00
repenno 15725be32a Add jws encoding built-in functions
fixes #1174

Signed-off-by: repenno <rapenno@gmail.com>
2019-07-16 09:04:36 -04:00
Jason Hoch 26e0b325cb docs: Tweaks to improve consistency
The preceding example uses `x>y`, without which the variable `y`
is unused.

Signed-off-by: Jason Hoch <jrshoch@gmail.com>
2019-07-15 12:34:43 -07:00
Patrick East 28ff1bcd7f rego: Fix prepared queries to not re-use old txns
When doing a prepared query Eval() or Partial() we would build an
EvalContext that defaulted to using the original Rego objects
transaction. This is problematic as that one might have been aborted,
or just be old. The expectation if you don't specify one at Eval()
time was that you would get a new one at the current state of the
store, so we should keep the prepared Eval() to work the same way.

This means that if you did have a specific transaction you wanted
to use you would now need to pass it in instead of only setting
it on the original Rego object. This changed some tests but in real
usage it shouldn't affect much.

This also fixes an issue where we would abort transactions auto
created on the EvalContext way too early. The helper (internal to
the Rego package) now returns a finish function that will do the
right thing and abort it at an appropriate time.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-07-12 10:39:02 -07:00
Patrick East 667f21d07f plugin/logs: Pass server txn to mask Eval()
We were only using it on the prepare step, but we needed to use the
passed in txn each time we eval the prepared query.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-07-12 10:39:02 -07:00
Torin Sandall 485f9dc17f ast: Optimize rewriteDynamics stage to reduce allocations
It was observed that the rewriteDynamics stage was causing too much
memory pressure on large policies. This commit contains an important
fix on the local var generator and a bunch of small changes that
reduce the # of allocations required. The before and after results are
below.

Specific changes:

* Fix local var generator to avoid recomputing var names
  unnecessarily. Keep track of counter instead of each generated var
  name.

* Allocate the local var generator once instead of on each stage. As a
  side note, if generated vars were not valid syntactically then we
  could avoid the expensive ast.Walk call entirely but that would
  prevent us from being able to roundtrip policies.

* Refactor Expr#IsEquality to avoid allocations.

* Refactor rewriteDynamics* functions to allocate a single Body
  instead of two.

* Add early exit to WalkRules to avoid unnecessary traversal of rule
  bodies (which cannot contain rules.)

BEFORE
------

goos: linux
goarch: amd64
pkg: github.com/open-policy-agent/opa/ast
BenchmarkRewriteDynamics/1-8 	  500000	      2614 ns/op	     872 B/op	      36 allocs/op
BenchmarkRewriteDynamics/10-8         	  100000	     28158 ns/op	    8720 B/op	     360 allocs/op
BenchmarkRewriteDynamics/100-8        	    5000	    273956 ns/op	   87200 B/op	    3600 allocs/op
BenchmarkRewriteDynamics/1000-8       	       1	1281000425 ns/op	110348200 B/op	 9079093 allocs/op
^Csignal: interrupt
FAIL	github.com/open-policy-agent/opa/ast	11.788s

AFTER
-----

goos: linux
goarch: amd64
pkg: github.com/open-policy-agent/opa/ast
BenchmarkRewriteDynamics/1-8 	 3000000	       423 ns/op	      32 B/op	       1 allocs/op
BenchmarkRewriteDynamics/10-8         	  300000	      4651 ns/op	     320 B/op	      10 allocs/op
BenchmarkRewriteDynamics/100-8        	   30000	     58642 ns/op	    3200 B/op	     100 allocs/op
BenchmarkRewriteDynamics/1000-8       	    2000	    696140 ns/op	   32000 B/op	    1000 allocs/op
BenchmarkRewriteDynamics/10000-8      	     200	   8722337 ns/op	  320000 B/op	   10000 allocs/op
BenchmarkRewriteDynamics/100000-8     	      20	  86467719 ns/op	 3200000 B/op	  100000 allocs/op
PASS
ok  	github.com/open-policy-agent/opa/ast	14.863s

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-07-11 15:12:26 -07:00
Torin Sandall c25114fa4c ast: Add benchmark for rewriteDynamics stage
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-07-11 15:12:26 -07:00
Torin Sandall c11d43ce5a discovery: Fix race condition in test case
The goroutine polling the updates slice was not synchronized with the
goroutine that receives status updates. As a result, it was possible
for the polling goroutine to see partial values in the updates slice
(which would led to nil pointer dereferences.)

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-07-11 15:12:06 -07:00
Patrick East 41c0c97aee docs: Add details for .'s in --set keys
It is not very discoverable as to how the `--set` options work when
keys have `.` characters in them. This adds in a section to the config
docs on how to do it.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-07-09 17:26:51 -07:00
Torin Sandall ad9d2557cf Update CHANGELOG.md for v0.12.1
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-07-06 11:42:04 -04:00
Torin Sandall 424594395f travis: Update Travis deployments to support release branches
Previously, the Travis configuration would only trigger deployments
for the master branch. Also, the release-travis step would re-tag
:latest which is not what we want for release branches.

These changes add a new Travis deployment for release branches that
does not re-tag :latest and _will_ trigger for tags on release
branches.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-07-03 16:43:56 -04:00
Torin Sandall d1d4df9798 server: Pass transaction in decision log event
These changes update the server to pass the server's open transaction
to the decision logger. This prevents the same goroutine from
recursively opening a new transcation when the log masking decision is
evaluated.

Alternatively we could update the server to close it's transaction
before logging the decision however this could lead to the log masking
decision being generated from a different policy revision. Another
alternative would be extend the storage layer to support recursive
transactions however this would be quite a bit more work.

We should investigate whether we can cheaply detect recursive
transactions in the store to avoid potential deadlocks in the future.

Also, delete opa binary that was accidentally committed to the repo.

Fixes #1543

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-07-03 15:10:59 -04:00
Jessica de Leeuw 543b3a677f content: Fixed typo in language-reference.md
Signed-off-by: Jessica de Leeuw <jessicadl@google.com>
2019-07-03 06:48:09 +08:00
Torin Sandall 6bda0a482c server: Tweak server error string format
This just tweaks the error string format introduced in #1530 to be
consistent with other error strings in OPA (e.g., topdown, ast, etc.)

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-07-03 01:43:59 +08:00
Xin Jin 0da6c8dc35 server: add test for decision log for 404.
Signed-off-by: Xin Jin <xin@styra.com>
2019-07-03 00:07:02 +08:00
Xin Jin 8fb825a08a servers: fix comments.
Signed-off-by: Xin Jin <xin@styra.com>
2019-07-03 00:07:02 +08:00
Xin Jin 1b738bd642 server: add decision logging for undefined document error in v0 get request.
Signed-off-by: Xin Jin <xin@styra.com>
2019-07-03 00:07:02 +08:00
Patrick East 97d8ebffca Change e2e tests to bind on :0 instead of :8181
To do this we needed a way to get the actual address that was bound.
To do that we needed to refactor the server and runtime code a tad
to let us create our own `net.Listener`s and get their address _after_
they had been started. The code is pretty much 1:1 with what is in the
official `http` package.

Now when the tests run using the helpers to stand up server runtimes
they should all be on separate ports.. in theory we could run the
unit tests in parallel without concern (for the e2e parts anyway).

Fixes: #1533
Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-07-01 11:35:52 -07:00
Torin Sandall 7d15f462ec docs: Add section on how to express "FOR ALL" in Rego
This is a common question that comes up. Until we have a keyword that
lets users express "FOR ALL" we should have docs we can point to.

Fixes #1307

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-06-29 01:20:40 +08:00
Xin Jin 00db47315a docs: fix minor typos and add principal in AWS example.
Signed-off-by: Xin Jin <xin@styra.com>
2019-06-29 00:46:29 +08:00
Torin Sandall 6aa521515c docs: Remove cast_xyz functions from docs
Fixes #1405

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-06-28 03:30:30 +08:00
Patrick East 1b6ee8de21 docs: Clarify config for console decision logger
Clarify the requirements/caveats for using the local decision logger
and include a blurb on the decision log.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-06-26 06:37:32 -07:00
Torin Sandall 096670bda4 ast: Update rule index to support glob.match
These changes update the rule index to support glob.match calls. The
changes update the build step to transform glob.match(pattern,
delimiter, match) calls into ref/value pairs like equality
expressions. In addition, the build step creates a mapper function
that transforms resolved values into lookup values during traversal.

As part of these changes, the build operation has been refactored so
that most of the process is encapsulated by a new refindices struct
that's separate from the actual rule index/trie. Hopefully this makes
the code easier to follow.

The index build step for glob.match only recognizes statements where
the match operand was given as a reference (and then was rewritten by
the earlier step in the compiler.) If this is too brittle we could
revisit the build step and close over all intermediate assignments.

Fixes #1496

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-06-26 08:57:25 +08:00
Torin Sandall 3aff107c14 rego: Time compile of partial eval results during prepare
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-06-26 08:57:25 +08:00
Torin Sandall 5698383a72 topdown: Fix corrupt object panic caused by copy propagation
Mutating term values used as object keys (or set elements) is not safe
because the underlying hashtables get corrupted. This a known issue
that was previously fixed in the compiler (#1125). The simplest
solution in this case is to use the ast.Transform helper instead of
ast.Walk so that a copy of object and set terms is made.

Fixes #1177

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-06-26 08:56:57 +08:00
Torin Sandall 16197e4b7a ast: Report safety errors on line where expression starts
These changes just update the safety errors to report the line of the
expression where the error occurred instead of the rule/query that
contains the expressionm. Better locality should make it easier to
identify safety error causes.

Fixes #1497

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-06-25 11:36:22 -07:00
Torin Sandall 3f063595b6 cmd: Fix double print of rego errors
Fixes #1518

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-06-25 09:21:21 -07:00
Torin Sandall f33a2b29d1 topdown: Fix virtual cache to allow composite key terms
Previously the virtual cache was implemented using a
map[ast.Value]... which works for scalar key terms but not composites
(because they're not comparable.) This change updates the virtual
cache to use a util.HashMap that supports all term kinds. This
prevents the virtual cache lookup/insert from panicing when a key like
data.x.y[[1]] is received.

In addition to fixing the virtual cache, this change updates the
Term.Equal() function to include an early-exit for types that do not
allocate in their Equal() functions.

The early-exit was added because swapping out the map[ast.Value]...
for util.HashMap introduced allocations into the virtual cache
lookup/insert operations which doubled the benchmark latency. See
below for benchmark results before/after this commit.

```
BEFORE
======
goos: linux
goarch: amd64
pkg: github.com/open-policy-agent/opa/topdown
BenchmarkVirtualCache-8   	 5000000	       284 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/open-policy-agent/opa/topdown	1.731s
Success: Benchmarks passed.

AFTER
=====
goos: linux
goarch: amd64
pkg: github.com/open-policy-agent/opa/topdown
BenchmarkVirtualCache-8   	 5000000	       322 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/open-policy-agent/opa/topdown	1.969s
Success: Benchmarks passed.
```

This change will also let us memoize virtual sets using the same cache
(see #822).

Fixes #1197

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-06-24 13:13:56 -07:00
Julien Garcia Gonzalez 758f1bbcc9 Fix link in the kubernetes-admission-control docs
This PR is fixing docs link in the kubernetes-admission-control-validation docs

Signed-off-by: Julien Garcia Gonzalez <julien@giantswarm.io>
2019-06-24 12:11:01 -07:00
Torin Sandall e4ae6a70e5 bundle: Cache compiler on storage context
These changes update the manager and bundle plugin to avoid parsing
and compiling modules during the manager's trigger callback. Since
policy queries are blocked while triggers execute, it's adavantageous
to cache the compiler that is obtained during bundle activation and
reduce the work done in the trigger callback.

Also, as part of these changes, the bundle plugin incorporates
remaining modules when it recompiles. This ensures that remaining
modules are correct.

Fixes #1515

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-06-24 11:57:51 -07:00