341 Commits

Author SHA1 Message Date
Stan Lagun 69fbaacfde Add Prometheus metrics to the status updates
Prometheus metrics can give much of insight into OPA's health.
Run-time metrics are a natural part of the application state
so having them in status update seems like a right change
that can help server understand what's going in with the OPA
instance.

The commit also encapsulates all prometheus-related code in one package
and abstracts it with generic interface so that it would be possible
to add other metrics providers

Addresses #1606

Signed-off-by: Stan Lagun <stan@styra.com>
2019-08-15 15:08:38 -04:00
Guangming Wang ac6e42b800 cleanup: fix obvious typos in types.go
Signed-off-by: Guangming Wang <guangming.wang@daocloud.io>
2019-08-15 10:06:37 -04:00
Torin Sandall 4033f3d945 server: Remove deprecated diagnostic feature
This commit removes the deprecated diagnostic feature from the
server. The feature has been deprecated since November 2018 and it was
essentially unused at the time so it should be safe to
remove. Removing the diagnostic support from the server saves having
to perform an extra policy evaluation in the server.

Once the buffer is removed from the runtime.Params struct the related
issue can be closed (there is still one known user of that so it has
been left intact for backwards compatibility.)

Ref #1052

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-08-06 02:25:09 +09:00
Stan Lagun df0befd596 Monitor cancelled HTTP requests with Prometheus metrics
This commit adds `http_request_cancellations` counter to the Prometheus
metrics. Having such a counter helps identify requests that were timed
out or the client just got disconnected before the request result was
computed.

Signed-off-by: Stan Lagun <stan@styra.com>
2019-08-05 23:38:01 +09:00
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 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
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 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 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
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 844d4100ea server: Abort write transaction when parsing fails
The server was leaking the write transaction when parsing failed which
caused subsequent updates to block. In future we should refactor the
handler implementations to avoid having to manually abort on each
error path.

Fixes #1478

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-06-11 13:10:50 -07:00
Torin Sandall d821d80db8 server: Add request headers to authorization input
These changes update the server to include request headers in the
authorization input document.

Fixes #1456

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-06-03 07:11:59 -07:00
Torin Sandall ee62afa477 server: Update server to support notes explanation
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-05-10 10:33:40 -07:00
Patrick East 589105aff5 Add param to include bundle activation in /health response
There is a new parameter for /health REST API which will include the
Configured bundle activation in the response. Example:

GET /health?bundle=true HTTP/1.1

Without the parameter the behavior stays the same, with it the server
will respond with 500’s until the status has been updated with an
activation time.

The docs for kubernetes ready probe has been updated to show this as
it makes for a better ready check than the original behavior when
remote bundles are being used.

Fixes: #1153
Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-05-02 15:28:12 -06:00
Peter W. Morreale 40af92cb9f Add provenance query output
Add the new 'provenance' query parameter to the v1DataGet() and
v1DataPost() routines.

Set the fields of the DataResponseV1 struct with the version/build
values for possible return with the result of the data operations.

Add a unit test by setting version info, than ensuring we get the
strings in the response.  Note that the revision is left unset.

Signed-off-by: Peter W. Morreale <pmorreale@statestreet.com>
2019-05-01 20:26:37 -07:00
Peter W. Morreale 5c6882e176 Define 'provenance' data types
First of several commits that define a new query
parameter named 'provenance'.

This query parameter will return version/build information
in addition to the result to the caller for data REST calls.

Signed-off-by: Peter W. Morreale <pmorreale@statestreet.com>
2019-05-01 20:26:37 -07:00
Torin Sandall 1cb36f2728 server: Add early exit on PUT /v1/policies
These changes update the server to compare the body of the HTTP
request with the existing policy in the store. If the bytes are equal,
the server returns immediately.

These changes are being made to workaround poor parser performance
given deeply nested JSON objects that are commonly found in Kubernetes
use cases. Specifically, we've observed very high parse times (e.g.,
over a second) when processing policies in production
environments. The problem becomes worse when kube-mgmt is being used
because it periodically resyncs the policies causing load even if the
policies are not changing.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-04-29 16:35:47 -07:00
Patrick East 162e67a935 Add unit tests for http server shutdown timeout
To make this work it adds in a new shim interface for the server
to interact with the `http.Server`(s). We mock one out and can then
have it return errors on `Shutdown`.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-04-10 14:09:06 -07:00
Patrick East b96f8c3492 Add support for graceful shutdown of OPA server
There is a new CLI option to configure the grace period:

`--shutdown-grace-period`

The option defaults to 10 seconds.

When a SIGINT or SIGTERM is sent the runtime will catch them and
attempt to gracefully shutdown the http servers. If the timeout is reached
it will log an error and continue with exiting (in a less graceful way).

Unit testing is a little bit light on this. Its touching some parts of the
code that aren't super easy to mock out. We can probably refactor
things in the future to make it easier.

Fixes: #1291
Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-04-10 07:54:03 -07:00
Geoff Baskwill 7d65f190d1 server: Fix patch path escaping
In the Data API, the `path` attribute of `PATCH` operations was
not handling escaped `~` and `/` values (escaped as `~0` and `~1`,
respectively) according to the JSON-Pointer RFC.

The API was using (and continues to support) URL escaping of these
characters (so `%7E` and `%2F`, respectively), but this was not
documented and is not strictly correct according to the documentation.
However, this is being left unchanged to preserve backwards compatibility
for anyone who stumbled across this behaviour.

With this change, `~0` and `~1` in the `path` attribute of `PATCH`
operations will be unescaped into `~` and `/` as defined in the RFC.

Signed-off-by: Geoff Baskwill <me@geoffbaskwill.ca>
2019-03-13 08:46:56 -07:00
Stefan Bueringer 96289e8589 server: Add pprof endpoints & flag to enable/disable them
Signed-off-by: Stefan Bueringer <sbueringer@gmail.com>
2019-02-23 08:52:08 -08:00
Stefan Bueringer 9493b1d37a metrics: Add go default metrics
Signed-off-by: Stefan Bueringer <sbueringer@gmail.com>
2019-02-23 08:21:39 -08:00
Torin Sandall 510c5ce77e decision_logs: Update interface to return error
Previously, the decision logger interface did not allow plugin
implementations to return an error. In some cases, implementations may
prefer to make OPA fail-closed if the event cannot be emitted.

This is a backwards incompatible change to the custom decision logger
API that was added in v0.10.3 and it deprecates the old diagnostic
interface as well.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-02-22 20:47:00 -08:00
Torin Sandall e2ac20284c runtime: Refactor build version information
Previously the build version was recorded in the version package and
then different components would report it in an ad-hoc manner, e.g.,
the REPL has a module that generates a virtual doc with the version
info in it, the server was using templating to do the same, etc.

These changes remove the special code from the REPL and server
implementations to report the version. Instead the runtime writes the
version into /system/version at boot.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-02-22 14:55:36 -08:00
jrmarsha 5c934e545e Add trailing newlines when pretty printing
This change appends a newline to pretty printed API results to improve readability on the command line. Also, fix tests that were asserting on string equality instead of JSON value equality.

Signed-off-by: Josh Marshall <joshua.r.marshall.1991@gmail.com>
2019-02-22 08:55:04 -08:00
Torin Sandall c79eb41d86 server: Add checks to respect bundle scope
These changes update the server to check the bundle scope before
writing any policy and data. This way the caller won't accidentally
write policy and data into OPA only to have it overwritten the next
time a bundle is downloaded and activated.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-02-19 10:51:19 -08:00
Torin Sandall bb80fd7f74 Add path conflict checks
Previously there were no checks in place to ensure that base and
virtual documents do not overlap. As a result, if users loaded raw
JSON and rules into OPA that overlapped, the evaluation results were
not well defined. With these changes, we can detect the overlap and
reject updates (to policies or data) that would cause inconsistent
results.

Fixes #1207

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-02-15 08:00:22 -08:00
Stephan Renatus 39776afdbe server: don't alter http.DefaultTransport in tests
I wasn't paying enough attention to this detail in #1149. It's low-impact,
but let's avoid the trap anyways.

Also moves the client creating code into a helper method, with a comment
regarding http.Transport{}.

Signed-off-by: Stephan Renatus <srenatus@chef.io>
2019-01-18 07:46:52 -08:00
Torin Sandall 1db3023d45 Update decision log events to model paths and queries
Previously paths and queries were modelled with the same
attribute. This was going to cause headaches down the road for
decision log consumers that need to be able to deal with both kinds of
policy invocations.

As part of these changes, the decision logging test in the server has
been refactored to make it a bit more maintainable.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-01-16 12:45:47 -08:00
Torin Sandall dcb99dfe9e Update server to include decision ID in error events
Previously, the server was not including a decision ID in log events
for errors. This commit changes that.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-01-16 12:45:47 -08:00
Torin Sandall 4428a9bd31 Update server and decision logger to represent input properly
Previously the input attribute was no represented as *interface{}
which makes it impossible to differentiate between null and undefined
input. This commit changes that. Eventually we should just get rid of
server.Info in favour of the decision log event structure.

This change requires a release note.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-01-16 12:45:47 -08:00
Torin Sandall 2f5a0fe0a4 Update decision log event to include error
The error field from the server event was not being copied into the
decision log event. Also, we didn't have test cases to verify that the
error was being set correctly in the first place.

In the future, we should remove the duplication of the server event
and the decision log event (preferring the latter).

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-01-16 12:45:47 -08:00
Torin Sandall 503b8c0645 Add metrics timer for server handler
These changes add a new metric into the server that calculates the
time taken from when the handler is invoked to the point where the
decison log is emitted. Hopefully this will prevent us from missing
important periods in the request path.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-01-16 12:45:47 -08:00
repenno e2ef042d3d Query API should return HTTP 400 if query does not parse
Fixes #1081
curl -G -v "http://localhost:8181/v1/query" --data-urlencode "q=^ -i"

*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8181 (#0)
> GET /v1/query?q=%5E%20-i HTTP/1.1
> Host: localhost:8181
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 400 Bad Request
< Content-Type: application/json
< Date: Sat, 15 Dec 2018 09:13:58 GMT
< Content-Length: 118
<
{
  "code": "invalid_parameter",
  "message": "1 error occurred: 1:1: rego_parse_error: no match found\n\t^ -i\n\t^"
* Connection #0 to host localhost left intact
}%

Signed-off-by: repenno <rapenno@gmail.com>

Query API should return HTTP 400 if query does not parse

Fixes #1081
curl -G -v "http://localhost:8181/v1/query" --data-urlencode "q=^ -i"

*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8181 (#0)
> GET /v1/query?q=%5E%20-i HTTP/1.1
> Host: localhost:8181
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 400 Bad Request
< Content-Type: application/json
< Date: Sat, 15 Dec 2018 09:13:58 GMT
< Content-Length: 118
<
{
  "code": "invalid_parameter",
  "message": "1 error occurred: 1:1: rego_parse_error: no match found\n\t^ -i\n\t^"
* Connection #0 to host localhost left intact
}%

Signed-off-by: repenno <rapenno@gmail.com>

Query API should return HTTP 400 if query does not parse

Fixes #1081

Reworked fix based on comments and current code.
v1QueryGet/Post already call ast.parseBody through validateQuery,
 therefore the solution:

 - Avoids double parsing query
 - Keeps original query string around so logs can work
 - Performs http error checking at the server.go level
 - Enhanced testing to make sure all messages are sane

 Optionally error checking in server.go could be factored in a
 unified function.

Signed-off-by: repenno <rapenno@gmail.com>

Query API should return HTTP 400 if query does not parse

Fixes #1081

Reworked fix based on comments and current code.
v1QueryGet/Post already call ast.parseBody through validateQuery,
 therefore the solution:

 - Avoids double parsing query
 - Keeps original query string around so logs can work
 - Performs http error checking at the server.go level
 - Enhanced testing to make sure all messages are sane

 Optionally error checking in server.go could be factored in a
 unified function.

Signed-off-by: repenno <rapenno@gmail.com>

Query API should return HTTP 400 if query does not parse

Fixes #1081

Reworked fix based on comments and current code.
v1QueryGet/Post already call ast.parseBody through validateQuery,
therefore the solution:

- Avoids double parsing query
- Keeps original query string around so logs can work
- Performs http error checking at the server.go level
- Enhanced testing to make sure all messages are sane
- Fixed existing server_test.go tests affected by requested changes

Optionally error checking in server.go could be factored in a
unified function.

Signed-off-by: repenno <rapenno@gmail.com>
2019-01-16 07:48:08 -08:00
Stephan Renatus 3286c39822 server: add authentication based on TLS
* identifier: add TLSBased

This is only the identifier, the server setup still has to be done.

Note that it diverges a little from what was proposed in the issue:
not every client cert needs to have a CN record -- so instead, we'll
use whatever is the cert's subject as client identity.

* Drive-by fix: identifier_test: don't use same package for TokenBased
  tests.
* server: require and verify client cert for AuthenticationTLS
* server: allow setting CA pool via --tls-ca-cert-file
* server: expose new authentication via parameter
* [nit] server: simplify getListenerForHTTPServer
* server_test: use httptest for integration-y TLS tests
* book/security: mention TLS authn with example

Signed-off-by: Stephan Renatus <srenatus@chef.io>
2019-01-14 12:57:35 -08:00
repenno a353f25cb6 Fixes #1086
curl -v http://:8181/health
*  Trying ::1...
* TCP_NODELAY set
* Connected to  (::1) port 8181 (#0)
> GET /health HTTP/1.1
> Host: :8181
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Date: Mon, 07 Jan 2019 20:09:36 GMT
< Content-Length: 2
<
* Connection #0 to host  left intact
{}%

Signed-off-by: repenno <rapenno@gmail.com>
2019-01-08 07:59:30 -08:00
Torin Sandall 2d425494aa Refactor discovery implementation
These changes refactor the discovery implementation a bit to improve
test coverage and remove duplication of common logic shared with the
bundle plugin.

Specifically, the downloading logic has been moved into a separate
package that is shared by bundle and discovery. Second, test coverage in
the discovery implementation is increased from ~15% to ~85%.

These changes also include a few functional improvements:

- The default decision paths can be updated dynamically
- The decision logger can be enabled dynamically
- Discovery downloading errors are reported in status updates
- Discovery bundle is evaluated with all runtime params
- Custom plugins can be created dynamically
- Status updates include both discovery and bundle status

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-12-08 00:45:36 +01:00
Ashutosh Narkar 2185f1eb86 Add support for configuration discovery
Previously OPA configuration for bundle downloading, status reporting, etc. had to be supplied in a configuration file on startup. With these changes, OPA can be configured to download a bundle that generates the OPA configuration. This allows OPA to boot with minimal configuration and dynamically update that configuration on-the-fly making it much easier to manage large deployments of OPAs for different use cases within the same system.

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2018-11-29 09:19:58 -08:00
NIkhil Bhatia 93542dbbec Post query (#1)
* add POST method for /query

Signed-off-by: Nikhil Bhatia <nbhatia@microsoft.com>
2018-10-23 16:16:57 -07:00
Torin Sandall 8cd94fc982 Add router option to the HTTP server
This allows callers to embed the OPA HTTP server inside another HTTP
server (e.g., under a path prefix like /opa).

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-10-18 12:46:25 -07:00
Torin Sandall b95227688b Add default decision configuration
Previously, OPA would serve POST requests with an empty URL path by
querying data.system.main and returning the generated value. In some
cases, it's useful to be able to reconfigure OPA to use a different
document to serve these kinds of requests. The same goes for the OPA
authorization policy.

These changes make the default decision and default authorization
decision paths configurable.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-10-17 15:07:26 -07:00
Torin Sandall 38a988765d Add built-in function to get runtime info
These changes add support for accessing runtime information inside of
policies. In some cases, policies need to access environment variables
or configuration that OPA was booted with. These changes add a built-in
function that allows policies to gain access to this information. The
built-in function itself is relatively trivial. Most of the required
changes were plumbing the runtime information from the entrypoint down
into the evaluation engine. The alternative would have been to introduce
a global variable containing this information however that would be have
been harder to reason about in library integrations.

Fixes #420

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-10-16 14:20:03 -07:00
Torin Sandall ca74ca1aa4 Add YAML input support to webhook APIs (#1014)
Also, remove printlns from redirection test cases.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-10-15 17:38:33 -07:00
repenno 843e64fddc Add support for YAML encoded input values
These changes add support or YAML encoded input values in the V1 Data API.

Fixes #290

Signed-off-by: repenno <rapenno@gmail.com>
2018-10-15 15:44:02 -07:00
repenno 277abcf91c Redirect HTTP requests with trailing slashes
Fixes #972

Signed-off-by: repenno <rapenno@gmail.com>
2018-10-15 15:41:35 -07:00
repenno ed2c05a77e Expose version information via REST API
/v1/data/system/version will return information similar to:

Version: 0.9.3-dev
Build Commit: c5171ed5-dirty
Build Timestamp: 2018-10-07T02:12:58Z
Build Hostname: JOHNDOE-M-9130

Signed-off-by: repenno <rapenno@gmail.com>
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-10-08 08:25:17 -07:00