Commit Graph

3059 Commits

Author SHA1 Message Date
Stephan Renatus 12eec2acaa test/e2e: stop running benchmarks that output decision logs in GHA (#3273)
If you want to run them, use `make perf-noisy`.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-03-15 14:20:00 +01:00
Olivier Lemasle 16ae430a36 tests: fix failing test on 32-bit arch (#3265)
Signed-off-by: Olivier Lemasle <o.lemasle@gmail.com>
2021-03-15 13:04:16 +01:00
Gábor Lipták 06c89cade1 test/e2e: correct ineffassign (#3267)
Signed-off-by: Gábor Lipták <gliptak@gmail.com>
2021-03-15 10:07:21 +01:00
Stephan Renatus 2c1b2fcf9c Prepare v0.28.0 development
(again)

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-03-12 12:58:44 -05:00
Stephan Renatus e514c0353c prep 0.27.1 release (#3258)
Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
v0.27.1
2021-03-12 15:30:26 +01:00
Stephan Renatus bcb722925d wasm: fix offset bug
Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-03-12 09:11:26 -05:00
Anders Eknert 83a7079483 Fix crash in v0.27.0 when s3_signing is configured (#3256)
This was caused by new logger not getting properly initialized in NewClient
call.

Fixes #3255

Signed-off-by: Anders Eknert <anders@eknert.com>
2021-03-12 09:14:07 +01:00
Stephan Renatus c5b0b08f4f wasm: exclude re2 code if no entrypoint found (#3253)
We know which builtins are backed by re2. So, we'll drop all the re2-
related code if they're not used.

With this, the no-op policy gets down to 116K.

Fixes #3250.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-03-11 20:03:31 +01:00
wasm-updater 6957b6abbf wasm: Update generated binaries 2021-03-11 09:27:26 +00:00
Stephan Renatus 5a1ed9c7fa wasm: replace unused functions by stub (#3206)
We're in this situation: performing dead code analysis on wasm isn't too
hard, but it requires a representation of all wasm instructions: we'd need
to be able to parse the "runtime" wasm bits, i.e., what's built using llvm
from C code. When building upon that wasm module, we process the function
bodies uninterpreted -- they are all just `[]byte` to us.

This restriction lets us get by without implementing all the wasm
instructions -- we only write what we use, and read a bare minimum to work
as outlined above.

To still be able to remove dead code, this change employs a trick: at build
time, when the aforementioned runtime wasm module is compiled, we're calling
wasm-opt on it to extract its call graph. We'll use that, together with the
functions actually planned in our wasm compiler (using the subset of
instructions that we understand), to remove all unused functions from the
name section, and replace their function bodies with `unreachable`.

We cannot really remove them, since that would require reindexing all
functions; and we cannot do that without replacing the function indices at
their call sites in the "runtime" wasm module.

Another restriction to the impact of this approach is call_indirect: We
need to keep every function that's referenced in the table -- we don't know
which function might be calling them indirectly. In a follow-up, we could
record that information and use it to further reduce the code size: we know
that if none of the regex-related builtins are used, we could also stub out
the re2-related functions.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-03-11 10:25:16 +01:00
Stephan Renatus 79a5a55927 deps: bump wasmtime 0.23.0 -> 0.24.0 (#3246)
Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-03-11 08:04:43 +01:00
Anders Eknert 7614c626c6 Fix envoy links
The envoy-authorization page was a 404, probably after
the recent refactoring of those docs.

Signed-off-by: Anders Eknert <anders@eknert.com>
2021-03-10 18:27:30 -05:00
Torin Sandall 885fd69219 ast: Add ast.JSONWithOpt to complement ast.JSON
In some cases, the caller needs to be able to control the order that
sets are serialized into JSON arrays. This commit adds that wrapper
and exposes the option in the rego package.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2021-03-10 14:40:07 -05:00
Torin Sandall 24a2cb7c0e wasm: Add support for time option via rego package
This commit updates the rego package to propagate the time option all
the way down to the SDK and into the built-in dispatcher. This way
callers can control the time-of-day observed by wasm compiled policies.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2021-03-10 14:21:22 -05: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
Stephan Renatus c584540ab5 fix nightly fuzzing (#3243)
build/fuzzer: update, and preserve github.com/OneOfOne/xxhash

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-03-10 15:11:19 +01:00
Hiro Osaki 51b41725d6 ast: parse import without package
Before, resolveRefs ignored Imports when Package is nil.
This caused `opa eval --import` option to be ignored without `--package` option.

This PR makes query compiler generate temporary package with name of ""
(empty string) when package is nil but import one or many imports are provided.

Fixes #3228

Signed-off-by: Hiro Osaki <hiroyuki.osaki@gmail.com>
2021-03-10 09:07:59 -05:00
Jack Stevenson 5406cb3811 plugins/rest: SigV4 Signing for any AWS service (#3210)
This adds a new `service` option to the `s3_signing` config, allowing for other AWS services (such
as API Gateway endpoints) to be used for bundles, decision logs etc.

For example:

```
services:
  decision-log-service:
    url: https://myrestapi.execute-api.ap-southeast-2.amazonaws.com/prod/
    credentials:
      s3_signing:
        service: execute-api
        environment_credentials: {}

decision_logs:
  service: decision-log-service
  reporting:
    min_delay_seconds: 300
    max_delay_seconds: 600
```

If no service is specified, we default to `s3` to maintain backwards compatibility.

This updates the sigv4 signer to include the specified service in the signature, and to sign all
request headers for better compatibility with other AWS services, except an explicit ignore list,
as per https://github.com/aws/aws-sdk-go/blob/master/aws/signer/v4/v4.go#L92

Additionally, this fixes a bug in the signer where the body ReadCloser was consumed and not reset,
meaning requests that were signed were always sent with an empty body!

Fixes #3193

Signed-off-by: Jack Stevenson <jacsteve@amazon.com>
2021-03-10 13:30:50 +01:00
Anders Eknert 80de059208 Remove any() and all() built-ins from policy reference
Since there are better ways of doing what they do and they tend to confuse
people new to Rego we'll hide these from the policy reference for now. Will
eventually be deprecated and removed with issue #2437.

Signed-off-by: Anders Eknert <anders@eknert.com>
2021-03-09 13:00:37 -05:00
Torin Sandall b5054facee docs: Add missing discovery.service field to table
Fixes #3236

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2021-03-08 15:24:57 -05:00
Torin Sandall 39398c2536 Prepare v0.28.0 development
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2021-03-08 12:18:05 -05:00
Torin Sandall 43a12ecea4 Prepare v0.27.0 release
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
v0.27.0
2021-03-08 11:52:32 -05:00
Torin Sandall ad3d67702e Update the CHANGELOG in preparation for next release
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2021-03-08 10:58:41 -05:00
Luong Vo f6c30e76ca Internal/prometheus: Add smaller buckets
This pull request ditches some higher granularity buckets in favour of adding a
few smaller ones. The bucket that I chose was based on https://www.openpolicyagent.org/docs/latest/policy-performance/#high-performance-policy-decisions
where the expectation is "policy evaluation has a budget on the order of 1 millisecond".
Also, I tried to stay within Prometheus's default 10 buckets.

This fixes #3196

Signed-off-by: Luong Vo <vo.tran.thanh.luong@gmail.com>
2021-03-05 11:16:34 -05:00
Anders Eknert 968d49de3d Injectable logging implementation
Refactor logging to allow providing custom logging implementations to plugin
manager. This should allow us to keep logging as it is when running OPA as a
server, while injecting noop-loggers or custom, provided loggers for SDK client
implementations.

Fixes #3180

Signed-off-by: Anders Eknert <anders@eknert.com>
2021-03-05 14:42:39 +01:00
Jasper Van der Jeugt ef9814c9ab Add a --format=raw option for eval (#3207)
This is a very simple addition but it's proven extremely useful to us since
it means you can now use `opa` easily in bash scripts, e.g.:

```bash
ACCOUNT_ID="$(opa eval -d accounts.rego --format raw "data.accounts.account")"
```

This means `accounts.rego` can be used by policies as well as e.g. deploy
scripts.

The naming and functionality is inspired by the `-r` flag from `jq`.  In fact,
`jq` can be used to replicate this behaviour, but it is a bit nicer to not rely
on that system dependency.

When multiple queries and expressions are given, they are printed in a simple
table format using single newlines and spaces since that is consistent and plays
nice with `bash`.

Signed-off-by: Jasper Van der Jeugt <jasper@fugue.co>
2021-03-04 08:26:48 +01:00
wasm-updater 0557c5f4d4 wasm: Update generated binaries 2021-03-03 20:02:05 +00:00
Stephan Renatus bd5c572d0f wasm: misc optimizations (less locals, blocks, interning strings and booleans) (#3179)
* wasm: introduce OPA_STRING_INTERNED for interned strings

opa_value_type will report these as OPA_STRING, so special behaviour
should use node->type to discern OPA_STRING/OPA_STRING_INTERNED:

- shallow copies don't need to copy interned strings
- interned strings aren't free()'ed

* wasm: pass constants along, compile them accordingly
* wasm/src: switch to stdbool.h's bool

I'm not aware of any strong reason not to, it seems to be what's commonly
advised, and the memory use of this type is smaller.

* wasm: intern opa_boolean

The heap allocs for these probably don't amount to much, but interning
them allows for shovelling them through the IR as constants. This lets
us shortcut the evaluation of (n)eq when both operands would be known
at compile-time (not likely). However, it also lets us safe a few more
locals, namely all the ones for MakeBooleanStmt.

* wasm: replace `opa_boolean()` by func returning interned bools

The new function will end up having this body:

    00b742 func[188] <opa_boolean>:
     00b743: 41 8a d0 03                | i32.const 59402
     00b747: 41 8c d0 03                | i32.const 59404
     00b74b: 20 00                      | local.get 0
     00b74d: 1b                         | select
     00b74e: 0b                         | end

Where the addresses correspond to our interned boolean `opa_value *`.

The previous implementation, should anyone need it, is still available
as `opa_boolean_allocated`. It's used in tests, too, where we do not
have the `opa_boolean()` emitted by our Wasm compiler.

* wasm: br_if/br optimizations for constants
* wasm: remove AssignBooleanStmt and opa_value_boolean_set

This could be trouble for our interned opa_boolean_t's, but it's not used.
So, let's just get rid of it.

* wasm: avoid some blocks where possible

Due to how the planner plans functions, any partial rule defining a
set or an object would have a block like this:

    block
      call 208 <opa_object>
      local.set 2
    end

With this change, those will no longer be wrapped.

It's not a big deal, neither in what it gets us, nor in what it takes
to apply the optimization.

* wasm: add one-branched if, use in memoization
* wasm: de-block internal calls

I've been comparing our instructions to what wasm-opt does to them, and
this seems like a reasonable change.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-03-03 21:00:22 +01:00
Stephan Renatus dc820b5d82 tests: fix wasm tests
xyz_wasm_test.go makes go implicitly require the GOOS to be wasm.
That's not what we want here.

Example go test list before:

    $ go test --tags=opa_wasm ./repl -list TestReplWasmTarget
    ok      github.com/open-policy-agent/opa/repl   0.006s

After:

    $ go test --tags=opa_wasm ./repl -list TestReplWasmTarget
    TestReplWasmTarget
    ok      github.com/open-policy-agent/opa/repl   0.007s

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-03-03 09:32:11 -05:00
Kamil Potrec 7cdef1d66e fix eval command ignore flag support (#3215)
The eval command does not correctly apply ignore filters
to data files because it uses checkParams package variable.
This change replaces checkParams with params variable as input
to filter function.

Signed-off-by: p0tr3c <p0tr3c@protonmail.com>
2021-03-03 14:12:13 +01:00
amitkanfer b14464c692 Adding PHP Symfony middleware
Signed-off-by: amitkanfer <amit@build.security>
2021-03-03 13:50:26 +01:00
Stephan Renatus a6759d40f3 topdown: port big int strings test to yaml testcases (#3208)
Adding the "two obviously different numbers look the same" case, too.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-03-03 09:12:29 +01:00
André Håland a049a5cb59 Handle big integers in comparison and sprintf (#3174)
This fix makes it possible to compare big integers,
as well as casting big integers to string with builtin
sprintf() function.

Fixes: #3147

Signed-off-by: Andre Håland <andre.haland@gmail.com>
2021-03-02 16:25:46 +01:00
Ashutosh Narkar 2c3195fb07 docs: Add input-related content to the Envoy section
This commit adds more details explaining some of the
fields of the input available to OPA-Envoy.

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2021-03-01 10:27:09 -08:00
Stephan Renatus ff40fee57f wasm: wire wasm-opt into optimizations (experimental, opt-in) (#3189)
Set either EXPERIMENTAL_WASM_OPT to anything, or EXPERIMENTAL_WASM_OPT_ARGS to specific arguments
that will be passed to wasm-opt, to ENABLE this.

If the binary is not found in PATH, the optimization is skipped.

Flashes a hard-to-miss warning to discourage depending on this feature.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-02-26 14:41:21 +01:00
Stan Lagun 9063587794 logs: do not block Stop if there are no logs to publish
Addresses #3197

Signed-off-by: Stan Lagun <stan@styra.com>
2021-02-25 23:23:21 +01:00
Bojan Poprzen 79be94f509 plugins/bundle: properly unregister a listener
- fixed a bug to unregister a listener and not a bulk listener
- added assertions on existing tests

Fixes #3190

Signed-off-by: Bojan Poprzen <bojan.poprzen@sap.com>
2021-02-25 14:16:34 +01:00
Ashutosh Narkar 2b59293753 docs: Add section on Envoy Ext Authz with OPA
This change adds a dedicated Envoy section to
the OPA website with an overview, detailed examples and
debugging tips for using OPA with Envoy.

This change also removes the existing Envoy tutorial
and incorporates it in the new section.

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2021-02-24 10:22:57 -08:00
Robert Brennan 11c40848fe Add Fairwinds Insights to integrations
Signed-off-by: Robert Brennan <robertb@fairwinds.com>
Signed-off-by: Robert Brennan <contact@rbren.io>

Update integrations.yaml

Signed-off-by: Robert Brennan <contact@rbren.io>
2021-02-24 16:37:00 +01:00
Anders Eknert b02db2e98a Skip WASM tests on WASM_ENABLED=0
Signed-off-by: Anders Eknert <anders@eknert.com>
2021-02-24 13:24:15 +01:00
Anders Eknert 22907e7f3e Fix "make check" for go version 16+
```
test/e2e/concurrency/concurrency_test.go:51:6: call to (*T).Fatal from a non-test goroutine
test/e2e/concurrency/concurrency_test.go:54:6: call to (*T).Fatalf from a non-test goroutine
topdown/topdown_bench_test.go:161:7: call to (*B).Fatalf from a non-test goroutine
topdown/topdown_bench_test.go:164:7: call to (*B).Fatalf from a non-test goroutine
```

See https://golang.org/doc/go1.16#vet

Signed-off-by: Anders Eknert <anders@eknert.com>
2021-02-24 13:22:53 +01:00
Stephan Renatus b447f50d50 compiler/wasm: memoize in function (not callsite) (#3169)
As soon as a function is called twice, there should be a little bit
to gain from having the memoization happen in the function body.

Run on a big bundle, this results in the following change in opcode
counts:

    Total opcodes: 184881 -> 183719

    Opcode counts:
    local.get: 46920 -> 46760 -
    i32.const: 24152 -> 23706 -
    local.set: 14097 -> 14097 =
    call:      12547 -> 12061 -
    end:       10911 -> 11015 +
    br_if:     11062 -> 10826 -
    block:      8987 ->  9092 +

So, the change isn't dramatic at all. But less is more, so I guess
we could still do this.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-02-23 15:33:35 +01:00
Stephan Renatus 0532ad2636 compiler: emit debug messages for indexing decisions (#3157)
Why a comprehension is not indexed should now be a little
more transparent, when enabling debug output:

    $ opa build --debug test.rego
    compile.go:1832: test.rego:3: no index vars

While changing this, the debug mechanism got more stream-y:
the different WithDebug() options take io.Writer, and they're
passed along to different sub-components. Nothing is retrieved
after (for example) compiling, but during compilation, the
debug messages are written to the passed writer.

The debug logs for optimization and planning now also include
the source file locations (go) where a debug message was logged.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-02-23 14:57:35 +01:00
Torin Sandall 038da308f7 wasm: Do not emit unreachable instructions after opa_abort calls
The unreachable instructions cause a crash in the Go runtime on
macOS. It's unclear why they are ever executed after calling
opa_abort/opa_runtime_error but this was observed to fix the issue.

In the future it would be nice to get rid of the panics in the
Go-defined functions like opa_abort. Instead of panicking those
functions should return traps. The problem is that currently returning
a trap requires closing over a store related object which introduces a
memory leak.

Fixes #3168

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2021-02-22 15:32:30 -05:00
Stephan Renatus e703396183 bump go: 1.14.9 -> 1.15.8 (#3175)
1.14.x got archived with the release of 1.16.

Picking the latest release of the 1.15.x series.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-02-22 17:11:40 +01:00
Matthew Mahnke 930d1031b9 server: URL decode policy IDs
Policy IDs will be decoded in GET, PUT, and DELETE requests to the
/policies endpoint.  This will enable users to include non-alphanumeric
characters in policy IDs, as well as leading forward slashes, by URL
encoding the path component of their requests.

Fixes #2116
Signed-off-by: Matthew Mahnke <mmahnke18@gmail.com>
2021-02-22 09:08:21 +01:00
Iskandar Abudiab 9f8405abf6 Add cluetec.de to ADOPTERS.md
Signed-off-by: Iskandar Abudiab <mail@iabudiab.dev>
2021-02-21 00:01:16 -05:00
Torin Sandall 0d49911b21 CONTRIBUTING.md: Just file issues
I was reviewing the CONTRIBUTING.md file that we have had in place
forever and realized that we never used the mailing list
seriously--discussion has moved to Slack or GitHub issues.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2021-02-19 11:32:55 -05:00
Anders Eknert 1b7e0b0c4e Add terraform integrations
Signed-off-by: Anders Eknert <anders@eknert.com>
2021-02-19 09:45:38 -05:00
Anders Eknert 2f940153ba Re-arrange ast.Expr struct for optimal size (#3160)
This saves very little in terms of memory consumtion (8 bytes for most structs)
but on the other hand doesn't cost anything either. The only struct where I
think this will make a marginal difference is the Expr one since there can be
quite a few of those in a module.

Using https://github.com/orijtech/structslop

Signed-off-by: Anders Eknert <anders@eknert.com>
2021-02-19 13:39:21 +01:00