Commit Graph

38 Commits

Author SHA1 Message Date
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
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
Jakub Kulich 9dbdcc9048 plugins/logs: fixed reconfiguration race condition
Masking part of the logging plugin had incorrectly placed mutex -- only
for writing the variable p.mask, but not for reading it. This caused
race condition in the situation when bundle update calls plugin.Reconfigure()
method between setting the p.mask variable and then evaluating it.
Reconfiguration of the plugin sets p.mask to nil and therefore calling
p.mask.Eval() results in panic.

Signed-off-by: Jakub Kulich <jakub.kulich@exponea.com>
2020-12-03 10:18:37 -05:00
Patrick East 3449360596 plugins/logs: Refactor shutdown log flushing
This removes some time-related stuff from both the implementation and
the unit tests with the aim to make it less flakey.

For the implementation we will now rely only on the deadline set by
the original context. We don't mess with canceling it as the signal
that we've completed. We use a new more explicit done channel.

In the unit test we no longer check that it stopped immediately, it
will instead ensure that the plugin was forcefully stopped with logs
still in its buffer.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-11-11 09:54:03 -05:00
Anders Eknert f7f793c356 Separate console logger for plugins
This allows logging to console for decisions and status (and possibly other use cases) without having to follow the generic --log-level.

Fixes #2733

Signed-off-by: Anders Eknert <anders.eknert@bisnode.com>
2020-10-30 11:01:40 -07:00
Grant Shively 99a81435bc plugins/logs: Fixes unintended mutation of result
When mask rules targeted /result, it was modifying both the result
in the decision logs (intended) and the result in the API
response (unintended). Added a step to deep copy the result only once, if
there is at least one mask rule targeting the result.

Fixes #2752
Signed-off-by: Grant Shively <gshively@godaddy.com>
2020-10-27 10:27:42 -04:00
Anders Eknert deb61d0695 Fix decision log hang
..by breaking out of loop if context is cancelled.

Fixes #2792

Signed-off-by: Anders Eknert <anders.eknert@bisnode.com>
2020-10-19 12:43:13 -07:00
Anders Eknert 3cd195c925 Flush buffered decision logs on graceful shutdown
..re-attempting until either the graceful shutdown period is over or all logs have been uploaded.

Fixes #780

Signed-off-by: Anders Eknert <anders.eknert@bisnode.com>
2020-10-14 14:11:34 -07:00
Anders Eknert 777b31dee3 Fix discarded logs on chunk upload failure
If the size of the decision logs buffered exceeeds that of `upload_size_limit_bytes`, the upload will be split into chunks. If one of the attempted uploads return with an error the chunk is stored in the "new" buffer and will be re-attempted at the next invocation of the `oneShot` method. However, once that is done the function returns, leaving any decisions left in the buffer to be discarded. This PR fixes that, preserving all failed chunk uploads to the buffer.

Signed-off-by: Anders Eknert <anders.eknert@bisnode.com>
2020-09-30 17:12:33 -07:00
Teemu Koponen 98119fc00c ast: Introduce Array struct.
This decouples the consumers of the Array from its implementation, and
thus, paves the way for improved (more optimized) Array
operations. Note, the array memory foot print and the allocations
required with the array operations remain the same.

Signed-off-by: Teemu Koponen <koponen@styra.com>
2020-08-03 13:36:33 -04:00
Patrick East 739777cfff plugins/logs: Add custom EventV1 -> AST helper
This pretty substantially improves performance by avoiding to do a
JSON round trip and then converting into AST types.

There are a couple of fields left that require the round trip, we can
iterate on this as needed to supply better options for the results
and metrics.

This custom ASTer leaves out the rule counts, which also helps to
improve the performance.

On my local machine the numbers look like:

```
name                              old time/op    new time/op    delta
MaskingNop-8                         129µs ± 1%     110µs ± 1%  -15.19%  (p=0.000 n=7+10)
MaskingRuleCountsNop/1Rules-8        138µs ± 1%     111µs ± 1%  -19.34%  (p=0.000 n=10+10)
MaskingRuleCountsNop/10Rules-8       180µs ± 1%     116µs ± 4%  -35.70%  (p=0.000 n=10+10)
MaskingRuleCountsNop/100Rules-8      614µs ± 1%     113µs ± 1%  -81.66%  (p=0.000 n=10+8)
MaskingRuleCountsNop/1000Rules-8    5.16ms ± 2%    0.11ms ± 1%  -97.79%  (p=0.000 n=10+10)
MaskingErase-8                       148µs ± 2%     129µs ± 0%  -12.65%  (p=0.000 n=10+10)

name                              old alloc/op   new alloc/op   delta
MaskingNop-8                        56.7kB ± 0%    48.0kB ± 0%  -15.43%  (p=0.000 n=10+10)
MaskingRuleCountsNop/1Rules-8       59.5kB ± 0%    48.0kB ± 0%  -19.41%  (p=0.000 n=10+10)
MaskingRuleCountsNop/10Rules-8      84.9kB ± 0%    48.0kB ± 0%  -43.47%  (p=0.000 n=10+10)
MaskingRuleCountsNop/100Rules-8      362kB ± 0%      48kB ± 0%  -86.73%  (p=0.000 n=9+10)
MaskingRuleCountsNop/1000Rules-8    3.26MB ± 1%    0.05MB ± 0%  -98.52%  (p=0.000 n=10+10)
MaskingErase-8                      65.7kB ± 0%    57.0kB ± 0%  -13.33%  (p=0.000 n=10+10)

name                              old allocs/op  new allocs/op  delta
MaskingNop-8                         1.23k ± 0%     1.12k ± 0%   -8.46%  (p=0.000 n=10+10)
MaskingRuleCountsNop/1Rules-8        1.31k ± 0%     1.12k ± 0%  -14.32%  (p=0.000 n=10+10)
MaskingRuleCountsNop/10Rules-8       1.98k ± 0%     1.12k ± 0%  -43.30%  (p=0.000 n=10+10)
MaskingRuleCountsNop/100Rules-8      8.74k ± 0%     1.12k ± 0%  -87.13%  (p=0.000 n=10+10)
MaskingRuleCountsNop/1000Rules-8     76.3k ± 0%      1.1k ± 0%  -98.52%  (p=0.000 n=10+10)
MaskingErase-8                       1.33k ± 0%     1.22k ± 0%   -7.84%  (p=0.000 n=10+10)
```

So the performance no longer scales with the number of rules hit, and
overall the base cost is almost 20% faster. This time directly impacts
round trip latency for OPA REST clients performing evaluations.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-07-15 12:01:12 -07:00
Domingo Kiser 95532908c1 logs: Add feature to mutate decision log data in addition to default erase masking feature.
This feature adds the ability to mutate decision logs in addition to the default behavior
of erasing object paths.  A new upsert command was added to a structured way to define
mask rules in a backwards compatible manner.

Fixes: #2379
Signed-off-by: Domingo Kiser <domingo.kiser@gmail.com>
2020-06-11 13:14:18 -04:00
Torin Sandall 73619ee8a2 internal/ref: Add helper package for parsing refs
This logic was duplicated for config processing and is useful inside
the new build command when dealing with entrypoints so just move it
into an internal package.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2020-05-18 08:32:38 -04:00
Patrick East ff900144a7 plugins/logs: Add plugin status updates
The log plugin will report its status as "ok" at start time and "not
ready" at stop. It currently doesn't change status or report any
errors. This can potentially be extended in the future.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-02-07 09:49:41 -05:00
Patrick East 9d9367d0ce decision logger: Leave the path unchanged for decisions
The logger was swapping `.`'s with `/`'s but this isn't safe when a
valid path should be /foo/a.b.c/main. The server was already doing
the right thing by passing in the url path where applicable, or only
specifying a query instead of the path.

This might affect anyone using the decision logger golang API passing
in something in dot-notation and expecting it to come out with paths.
Anyone using the HTTP server should be unaffected.

Fixes: #2031
Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-01-30 14:11:05 -08:00
Patrick East 6406d82574 decision logs: Remove unused parameter
Cleanup a function with unused parameter.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-12-12 10:31:22 -08: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
omerlh 4ad085c1da fix the build
Signed-off-by: omerlh <omerl@soluto.com>
2019-09-10 10:08:28 -07:00
omerlh 34bbff655a added type field to decision logs
Signed-off-by: omerlh <omerl@soluto.com>
2019-09-10 10:08:28 -07: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 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 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
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
Torin Sandall c0cc24676d logs: Update decision logger to support masking
These changes enhance the in-built decision logger to support masking
of input and result fields for cases where sensitive information is
passed to OPA (or generated by the policy.)

Also, fix rego package to avoid panic-ing on programmatically created
queriest that lack Location information.

Fixes #779

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-06-04 16:14:46 -07: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
Andy Curtis 778594ec79 config: add version to labels
Adds OPA version to the config labels, so that the version
appears in status updates. Previously, decision logs had a
top-level version field. This removes that field because
version will be contained in the labels instead.

Signed-off-by: Andy Curtis <arcurtis@gmail.com>
2019-02-08 15:55:27 -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 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
repenno d85a3e15ca Add query metrics to decision logs
Fixes #1033

Signed-off-by: repenno <rapenno@gmail.com>
2019-01-16 12:45:47 -08:00
Torin Sandall cffc3d996d Add support for custom backends in decision logger
These changes modify the decision logger to support custom
backends. The docs now include an example of how to implement a custom
backend that writes out to stdout.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-01-10 14:52:14 -08:00
repenno 1773cf64e8 Add OPA version to decision logs
Fixes #1089

=== RUN   TestPluginStartSameInput
--- PASS: TestPluginStartSameInput (0.02s)
=== RUN   TestPluginStartChangingInputValues
--- PASS: TestPluginStartChangingInputValues (0.01s)
=== RUN   TestPluginStartChangingInputKeysAndValues
--- PASS: TestPluginStartChangingInputKeysAndValues (0.01s)
=== RUN   TestPluginRequeue
--- PASS: TestPluginRequeue (0.00s)
=== RUN   TestPluginReconfigure
time="2019-01-09T09:34:03-08:00" level=info msg="Starting decision log uploader." plugin=decision_logs
time="2019-01-09T09:34:03-08:00" level=info msg="Log upload skipped." plugin=decision_logs
time="2019-01-09T09:34:03-08:00" level=info msg="Stopping decision log uploader." plugin=decision_logs
time="2019-01-09T09:34:03-08:00" level=info msg="Decision log uploader configuration changed." plugin=decision_logs
time="2019-01-09T09:34:03-08:00" level=info msg="Log upload skipped." plugin=decision_logs
--- PASS: TestPluginReconfigure (0.00s)
PASS

Signed-off-by: repenno <rapenno@gmail.com>
2019-01-09 19:09:19 -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
Torin Sandall c5106fa49d Remove decision log buffer limit
This change removes the default decision log buffer limit to avoid
unintentionally dropping decision logs. If users are concerned about
memory usage they can set the limit, but by default they will not be
surprised by log discards.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-06-18 11:32:22 -07:00
Ashutosh Narkar 1b33bf3f6f Reduce memory overhead of decision logs
Fixes #705

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2018-06-15 09:23:01 -07:00
Torin Sandall db708b8490 Fix query to path conversion in decision logger
The decision logger was using strings.TrimLeft when it should have been
using strings.TrimPrefix. As a result, the prefix was not being trimmed
correctly--leading characters in the cutset "data." were being removed.

Fixes #783

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-06-13 09:17:57 -07:00
Torin Sandall 49bba1faa8 Add decision logging plugin
Previously, decision logs were kept in-memory and enabled via
diagnostics configuration. With these changes, admins can configure OPA
to log decisions to a remote HTTP endpoint.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-04-10 13:58:36 -07:00