24 Commits

Author SHA1 Message Date
Johan Fylling a179a24c48 v1 API
All packages, except for `cmd` and `internal`, have been moved into a new `v1` root package.

Old packages are kept for backwards-compatibility reasons. All contained code is replaced with simple type aliases and proxy functions to `v1` implementations.

Old packages default to the Rego v0 syntax, new `v1` packages default to the Rego v1 syntax.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-12-12 15:27:34 +01:00
Johan Fylling 7bb6dbe36b Preparing for v1 API
Moving (most) source to v1 root package to prepare for v0/v1 API separation.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-12-12 15:09:03 +01:00
Anders Eknert 700e2c97de Bind test server to localhost interface (#6164)
Fixes #6162

Signed-off-by: Anders Eknert <anders@styra.com>
2023-08-17 15:56:35 +02:00
Anders Eknert 65c7d22563 Ensure force_cache attribute ignores Date header (#5298)
When force_cache is true, do not use the Date header set
by the server as the value to use for TTL initialization,
but rather create it from the current instant.

* Rename all current interQuery* tests to intraQuery*
* Add e2e test cases for forced http.send interQueryCaching

Fixes #4960

Signed-off-by: Anders Eknert <anders@eknert.com>
2022-10-25 11:26:46 +02:00
Stephan Renatus cd08da0345 topdown/eval: copy without modifying expr, update test/e2e/concurrency (#4503)
What we previously did turned into a race condition with multiple
concurrent calls to /v1/compile.

With a change introduced with 0.38.0 (the `every` keyword), the
`nil` Terms of an `ast.Expr` node was surfaced: previously, it would
go unnoticed, but could potentially have yielded bad results.

The effect of this change is proven using a new e2e test that would
fail on the code we had previous.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2022-03-28 22:09:20 +02:00
Stephan Renatus d8bdd3b830 server: add tracing to remaining handlers, cleanup code (#4136)
This deals with the first two bullets of #4128:

1. tracing for remaining handlers
2. decisions IDs are added to the server spans

I'm not sure if that's the convention, but I've put the decision ID into the server
spans: the client spans we get from http.send usage in policies will not carry
them, but they do refer to their parents, and they'll have the `opa.decision_id`
attribute.

Also includes some general cleanup:
* server/writer: use switch for ErrorAuto()
* server: replace http statuses with their constants

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-12-15 09:25:20 +01:00
Stephan Renatus f1b97f30bc test/e2e: silence logs by default with test params (#3989)
Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-11-09 12:05:58 +01:00
Torin Sandall 1e0de7633a test/e2e: Extend TestRuntime to avoid global fixture
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2021-10-14 09:31:16 -07:00
Torin Sandall e9d04fc1f5 runtime: Refactor logger usage
This commit does not change any functionality except it provides
callers with a way to provide a logger when instantiating the
runtime. Previously, the runtime had hardcoded dependencies on the
global logrus logger which made it problematic to test logging
behaviour. With this change, the logger can be supplied as a
parameter (which allows the caller to mock out the logger in tests...)

As part of this change, the dependencies on logrus have been moved out
of the runtime package entirely.

This commit includes a breaking change to the
runtime.NewLoggingHandler function: the function now requires a logger
to be supplied.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2021-10-14 09:31:16 -07:00
Will Beason 3be1d08b87 Change check-lint to use golangci-lint (#3465)
golint is deprecated. The author of the code no longer supports the
codebase. golangci-lint is faster than golint, and is in use by other
opa repositories (e.g. Gatekeeper).

This commit changes tools.go to reference golangci (so it ends up in
vendor) and modifies check-lint to use golangci instead.

Breaking API Changes:

- plugins/rest/rest.go: Fix typo "AllowInsureTLS" -> "AllowInsecureTLS"
- storage/errors.go: Removed unused IndexingNotSupportedErr

Signed-off-by: Will Beason <willbeason@google.com>
2021-05-19 07:52:02 +02:00
Torin Sandall 3fcc875a55 logging: Move logging infrastructure into separate package
This commit moves the logging interface and implementations out of the
sdk package into the logging package.

This commit also updates the status and decision log plugins to use a
logger obtained from the plugin manager instead of going to the global
console logger in the plugins package. The latter change will be
important for SDK consumers. This change is backwards incompatible but
it's unlikely that anyone is relying on that export. The test for
console logger independence has also been moved into the plugins
package (from the status package.)

Fixes #3275

Co-authored-by: Torin Sandall <torinsandall@gmail.com>
Co-authored-by: Anders Eknert <anders@eknert.com>

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
Signed-off-by: Anders Eknert <anders@eknert.com>
2021-05-10 11:34:14 -04:00
Stephan Renatus 00b2896774 wasm_sdk: use context, enable and use interrupts (#3211)
* wasm_sdk: use context, enable and use interrupts

All in all, there's three cases where cancellation is somewhat interesting:

- native functions: numbers.range
- host functions using topdown.Cancel: net.cidr_expand
- host functions using context.Context: http.send

The tests also pin down the behaviour of these three cases in topdown eval.
There, the numbers.range and net.cidr_expand cases _should_ be the same,
but as it turns out, the former didn't check for cancellation.

This is also fixed here.

The comparison of the wasmtime.Trap's Message() using strings.HasPrefix
is not great, but gets the job done for now.

If you see this in your test run,

    === RUN   TestEvalWithContextTimeout/wasm/net.cidr_expand
        rego_wasmtarget_test.go:209: failed checking error, got context deadline exceeded (context.deadlineExceededError)

we have not been able to acquire a VM from the pool within the deadline
of the context. It's been increased to 1s to make this not the reason
for test failures in github actions.

However, the test time for the rego package got inflated a bit now:

    github.com/open-policy-agent/opa/rego	8.764s	coverage: 75.8% of statements

----

There is some inherent race condition here: the context could be cancelled
when the Eval() function has already stopped calling into the VM. We then
set a trap, and the next call into the wasm instance will be interrupted.
To avoid that, we're "clearing interrupts" at the beginning of every call
path that leads into one or more wasm instance function calls. This is a
price to pay, but I couldn't find any robust solution to avoid the
problematic scenario.


* deps: revendor

This is for leaktest.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-03-10 16:51:17 +01:00
jkbschmid 81b7428d3e server: add metrics timer for CompilePost (#3097)
This change adds a server_handler timer to the metrics for any POSTs to /v1/compile.

So `curl -X POST localhost:8181/v1/compile?metrics ...` will result in:

    "metrics": {
        "timer_rego_partial_eval_ns": 145020,
        "timer_rego_query_compile_ns": 86415,
        "timer_rego_query_parse_ns": 56104,
        "timer_server_handler_ns": 377557  #this line is new
      }

Fixes #3096.

Signed-off-by: Jakob Schmid <jakob.schmid@sap.com>
2021-01-26 14:24:28 +01:00
Stephan Renatus f9e0332dcf server: pass instrumentation via rego.EvalInstrument
Fixes #3000.

The assertions on the response metrics object should be enough to
cover the bug -- depending on what is happening during eval, the
keys of that object may differ. (E.g. if there's a ref to be resolved,
that operation is timed; if there are none, there's no timer data.)

Small change to test/e2e: close some request bodies

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2020-12-14 09:17:38 -05:00
Patrick East f84f4674a1 Add support for evaluating Wasm bundles
This is largely plumbing changes required to get Wasm modules loaded
from bundles and configured as external resolvers for evaluations.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-11-06 15:12:38 -05:00
Patrick East fd519b1b10 test/e2e: Refactor and add decision log tests
This restructures the directories a little bit so there can be a
test runtime configure for console logging and a different one with
a remote log endpoint.

There are also new benchmarks added for both cases.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-07-15 12:01:12 -07:00
Patrick East a3bd62e848 test/e2e: Add "RunTests" API on TestRuntime
This replaces the `RunAPIServerBenchmarks` and `RunAPIServerTests`
methods, and fixes some hard-to-troubleshoot log output.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-07-15 12:01:12 -07:00
Patrick East 8b5cbfa07c server: Add param for diagnostic address binding
This adds a new config option for the OPA server (along with plumbing
from `opa run` downward to the server) to configure separate
diagnostic addresses to listen on. These will only be configured to
serve the /metrics and /health.

This will allow for more secure OPA deployments with the normal "data"
or "policies" API's made to be only accessible on localhost.

Fixes: #2002
Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-05-13 13:11:19 -07:00
Patrick East 2766f151fa cmd: Refactor run to be more easily testable
No feature changes, just moving things around.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-05-13 13:11:19 -07:00
Patrick East b48c534722 Run make fmt with new goimports cmd
Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-09-27 09:55:11 -04:00
Torin Sandall b0523466c1 Refactor metric provider implementation
* Remove metric provider config to avoid introducing new public
  interfaces. Since there is only one provider (prometheus) and it
  doesn't have any configurable settings, remove the configuration
  changes for now. We can always add these in the future.

* Remove dummy metric provider implementation. This isn't needed now
  that we're using the metrics.Metrics interface instead of
  metrics.GlobalMetrics.

* Remove metrics.GlobalMetrics in favour of metrics.Metrics. Move the
  HTTP handler instrumentation interfaces into the server package to
  avoid coupling the metrics package to the net/http package.

* Refactor the prometheus provider to implement the metrics.Metrics
  interface. Since the prometheus registry can error on Gather()
  calls, the provider has been updated to accept a logger and use ti
  when the Gather() call fails. This doesn't affect any public
  interfaces so it can be revisited in future if needed. Alteratnively
  we could add a Gather() interface onto metrics.Metrics which could
  return the error.

* Refactor status plugin to include metrics in status update by
  default. Users implementing the status API are likely to need
  performance metrics to gauge the OPA's health. Moreover if they are
  implementing the status API it's unlikely they will want to poll the
  /metrics endpoint on the OPA HTTP API (which may not even be
  exposed.)

* Move the prometheus endpoint test case into the e2e package so the
  server package has no dependencies on prometheus anymore.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-08-15 15:08:38 -04: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
Patrick East c761f8353c Add in end-to-end authz benchmark testing
These will spin up a server runtime and perform similar tests to
The other authz benchmarks, except that they do it through the full
OPA server stack.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-06-13 19:42:46 -07:00
Patrick East a1abbbf4c9 logs: console decision logging option
Add option to log decision logs locally. They'll get logged via
Logrus at info level.

To enable configure OPA with something like:

```
decision_logs:
    console: true
```

This will work alongside remote services and plugins. It will also
log the masked events in the case a masking policy is set.

Fixes: #1334
Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-06-13 13:27:51 -07:00