Commit Graph

102 Commits

Author SHA1 Message Date
Torin Sandall 7559ad0f51 build: Move VERSION into version/version.go
The decision logger unit tests had to be updated to always set the
version.Version value because they are sensitive to changes in the
payload sizes.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2020-09-11 13:26:11 -04:00
mikaelcabot 6b461a0adf fix: bundle download failed - context canceled
Bundle download failed: bundle read failed: archive read failed: context canceled

Signed-off-by: mikaelcabot <mikaelcabot@gmail.com>
2020-08-24 09:23:32 -07:00
Ashutosh Narkar 30271bc18b plugins/discovery: Set the last request and last successful request in discovery status
This commmit sets the values for the last request and last successful request
in the discovery bundle status.

Fixes: #2630

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2020-08-19 05:10:05 -07:00
Ashutosh Narkar b48aba82b0 plugins/rest: Add response header timeout for REST client
This commit adds a new configurable timeout to the Services
config to set the amount of time to wait for the server's
response headers. With this change, the client will no longer
wait indefinitely for the HTTP request to complete.

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2020-08-19 05:10:05 -07:00
Ashutosh Narkar 7f65b04561 Add a new inter-query cache to cache responses across queries
This commit adds a new inter-query cache that built-in
functions can use to cache responses across queries.

The OPA config includes a new "caching" field that can be used
to set the size of the cache. By default there is no limit.

This change also updates `http.send` to optionally utilize the
inter-query cache.

Fixes #1753

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2020-08-06 14:33:18 -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 dd683027c0 plugins/rest/aws: Include port in Host header
The AWS v4 signing feature for bundle requests would automatically
add a `Host` header by using the URL hostname. This would break for
URLs that specified a port number (eg: https://127.0.0.1:9000/). That
causes issues with sending a valid request.

This commit changes to use the `URL.Host` which will have the full
`host:port` string, when a port was specified.

Fixes: #2568
Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-07-22 15:07:10 -07: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
Patrick East f3a9657920 plugins/logs: Move benchmarks into separate file
Just refactoring, no code changes.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-07-15 12:01:12 -07:00
Ashutosh Narkar 338583c18a Add support for OPA bundle signatures
These changes add support for digital signatures for policy bundles which
can be used to verify their authenticity.

Bundle signature verification involves the following steps:

* Verify the JWT signature
* Verify the files in the JWT payload exist in the bundle
* Verify the file content of the files in bundle match with those in the payload

This commit adds a new `sign` command to generate a digital signature for policy bundles.

For more details, run "opa sign --help"

The signatures generated by the 'sign' command can be verified by the
'build' command. The 'build' command can also sign the bundle it generates.

The 'run' command can verify a signed bundle or skip verification altogether.

OPA 'sign', 'build' and 'run' can be used to
sign/verify bundles in bundle mode (--bundle) mode only. Verification
can be also be performed when bundle downloading is enabled.

Fixes: #1757

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2020-07-14 09:49:59 -04:00
Nick Williams d60c9fe018 Implement IMDSv2 for AWS metadata service
Fixes: #2482
Signed-off-by: Nick Williams <nhw@me.com>
2020-06-26 12:37:33 -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
Vineeth Pothulapati 28e8ebbd33 Remove gopkg.in/yaml.v2 dependency
To mainatin the consistency in project using github.com/ghodss/yaml as this is used for yaml utilities across the project.

Signed-off-by: Vineeth Pothulapati <vineethpothulapati@outlook.com>
2020-05-08 11:51:49 -07:00
Kamil Piotrowski a4412df0fa Add AWS_SESSION_TOKEN and AWS_SECURITY_TOKEN to aws environemnt credentials
AWS_SESSION_TOKEN or AWS_SECURITY_TOKEN is required when signing AWS requests
using ENV credentials from IAM assumed role. Missing token
results with S3 403 error when trying to download bundle.

Signed-off-by: Kamil Piotrowski <kamil.piotrowski@nordcloud.com>
2020-05-04 11:32:37 -07:00
Ashutosh Narkar 5d2b9df39d plugins: Additive updates to services when discovery enabled
Earlier with discovery enabled, there was no protection against accidental
changes to the discovery service. This change prevents the discovery service
from being modified by checking it's config in the service bundle.

Fixes #2058

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2020-04-29 06:36:10 -04:00
Torin Sandall 62f292b6e3 internal: Refactor load/store/compile implementation
This commit refactors the load/store/compile implementation that used
to live inside the runtime package. Specifically:

* Move init-time file loading logic into separate internal package
  (initload) along with store/compile logic. Add tests around
  load/store/compile that don't require the entire Runtime object.
  This also avoids duplication of the "version overwriting" logic.

* Move store/compile calls into the manager. This avoids the need for
  two compile operations on startup.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2020-04-27 15:26:43 -07:00
Torin Sandall f962990758 plugins: Fix race between manager and plugin startup
This change fixes a race condition in the manager that was caused by
registering the storage trigger _after_ the plugins had been
started. The problem was that if the bundle plugin was able to
download and activate before the trigger registration in the manager
went through, the store and the manager would be out-of-sync after
startup. The bundle would activate successfully but the plugin
manager would not see the change. This meant that the server health
check, status plugin, etc. would report successful activation and
clients using either of those APIs for synchronization could start
querying. If they executed a query within this window, virtual docs
would not be visible because the plugin manager would not yet have a
compiler to return to the server. Similarly, if clients queried the
v1/policies API they would see the raw policy contents but no AST
(since the latter is retrieved from the compiler.)

To remove the race condition the plugin manager simply registers the
trigger before starting any of the plugins. This ensures that it sees
all changes made by any of the plugins.

Fixes #2343

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2020-04-27 15:26:43 -07:00
Torin Sandall e21a93330e Revert "plugins: Additive updates to services when discovery enabled"
This reverts commit d67c0f7f13.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2020-04-17 13:03:08 -04:00
Ashutosh Narkar d67c0f7f13 plugins: Additive updates to services when discovery enabled
Earlier with discovery enabled updates to the 'services' configuration
was not allowed to protect against accidental changes to the discovery service
itself. Since adding new services could be useful, this change allows modifications
to the 'services' configuration. The only exception is that the service used to download
the discovery bundle cannot be modified.

Fixes #2058

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2020-04-16 20:46:14 -07:00
Ashutosh Narkar a5be4f49a2 plugins: Add support to specify bearer token path
This change updates the bearer token config to allow clients to specify
a path to the token. With this refreshing tokens becomes easier as OPA
will now reload the token from file.

Fixes #2241

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2020-04-10 17:29:29 -07:00
Ashutosh Narkar fec62e8522 Update downloader's Etag based on bundle activation
Earlier the Etag on the downloader would be updated unconditionally after every attempt to download a bundle. This could lead to a situation wherein a bundle fails to activate and would remain in an unactivated state since any subsequent downloads of the same version of the bundle would not trigger the activation process. This change attempts to resolve the issue by allowing the client to reset the Etag on the downloader incase of downloader errors and bundle activation failures. The drawback now is that we could end up re-downloading the same version of a bundle multiple times till it successfully activates. This situtation is likely to occur when using multiple bundle sources where a bundle may depend on some other. Generally using multiple bundle sources isn't recommended so the extra network traffic as a result of the re-downloads although not ideal may not too harmful.

Fixes #2220
Fixes #2279

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2020-04-10 11:17:23 -07:00
Patrick East af86b60432 Fix url error checking for go 1.14
It seems as though go 1.14 changes some of the url errors to have
quotes around some fields. To make things compatible across a wider
number of platforms we can relax the checks a tiny bit to only look
at the actual error string and ignore the quoted part.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-04-06 11:09:45 -07:00
Torin Sandall 985b85489a plugins/bundle: Include last successful request timestamp in status
This commit updates the bundle plugin to record the last successful
download _attempt_ timestamp in the bundle status. This way Status API
implementations can easily check whether the OPA has been able to
recently check-in for bundle updates.

Fixes #2009

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2020-02-07 13:01:21 -05:00
Patrick East 766fd8f4f1 plugins/status: Update to report plugin statuses
This adds in the status updates for the status plugin (reported to the
plugin manager) as well as hooking the status plugin up as a listener
for *all* plugin status updates to include in the status API events.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-02-07 09:49:41 -05: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 a4e4303a22 plugins/bundle: Add plugin status updates
The bundle plugin will emit status based on it having an activated
bundle for each configured bundle. At startup it will remain in a "not
ready" state until all bundles are activated. Upon reconfiguration it
will drop back into "not ready" if new bundles are added or the config
for an existing one was changed, until the bundles have been
activated.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-02-07 09:49:41 -05:00
Patrick East 5fc3c42602 plugins/discovery: Add plugin status updates
The discovery plugin will update its status based on the first time
it initializes and activates configuration. If a bundle is configured
it will switch to OK the first time it activates the configuration of
that bundle. If no dynamic configuration is present it will just go
to an "ok" status at Start() time.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-02-07 09:49:41 -05:00
Patrick East 99a6f4da67 plugins: Allow plugins to report status to manager
This defines a new status API on the plugins.Manager for plugins
to be able to update their status.

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 64512c1c90 plugins/bundle: Pass copy of status to bulk listeners
Previously it would pass a reference to the status map on the plugin,
which is potentially dangerous as the map can be changed (happens
explicitly on plugin reconfigure).

This now gives each bulk listener their own copy of the map.

Fixes: #1962
Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-01-13 11:24:38 -08:00
Patrick East e46400d232 Create new HTTP rest client for each download
The HTTP client we were using for bundle downloads was being loaded
once and then re-used. This was problematic for users that configure
cert files and then have them change.

This updates the client wrapper in OPA to re-create the underlying
client for each request.

Fixes: #1898
Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-12-13 12:39:14 -08:00
Patrick East 3c470e639f plugins/status: Add console option
This works almost exactly the same as the `decision_logs.console`
option. When enabled it will dump status updates through the normal
service logs (logrus).

Fixes: #1937
Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-12-12 10:31:22 -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 82567d2778 bundles: Add metrics for each bundle update
This will track parse time for each bundles parse and compile steps.
The data is included as an addition to the bundle status updates which
are propagated to any listeners (including the status API).

Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-12-12 10:31:22 -08:00
Torin Sandall 1ede0df5c8 discovery: Fix log level message when on HTTP 304
Fixes #1826

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-10-08 10:41:44 -04: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
Patrick East 58e4eee128 discovery: Support resource configuration option
This makes the configuration of discovery bundles match the newer
`bundles` config style more closely.

The older style configuration with `prefix` is still supported (the
default behavior is still there if neither `resource` or `prefix` is
provided). It has just been marked as deprecated.

Fixes: #1597
Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-09-26 15:34:54 -04:00
omerlh fd251c316e fix build
Signed-off-by: omerlh <omerl@soluto.com>
2019-09-10 10:10:41 -07:00
omerlh 9f643443ac try to fix the test
Signed-off-by: omerlh <omerl@soluto.com>
2019-09-10 10:10:41 -07:00
omerlh cffb2a6dfa fix the build + tests
Signed-off-by: omerlh <omerl@soluto.com>
2019-09-10 10:10:41 -07:00
omerlh 7115dbbce0 allow to configure more then 1 service with discovery
Signed-off-by: omerlh <omerl@soluto.com>
2019-09-10 10:10:41 -07: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 5418908e1c bundles: Fix duplicate file names overriding modules
Previously we would internally reference modules by only their `path`
which was, for data files, the system path but bundles it is relative
to the root of the bundle. In theory data paths and bundle paths could
collide, but the real trouble is caused by multiple bundles. It was
very easy to have two bundles with identical file paths but different
packages and policies defined in them.

Internally we now reference bundle module id's as a combination of the
bundle name (or the file path for the bundle if loaded from CLI) and
the path within the bundle.

This does change the `id` a particular policy will show up at via the
storage ListPolicies and in turn REST API for OPA. This only affects
users that have switched to the `bundles` configuration option, or
that are using the `-b`/`--bundle` CLI options to load bundles. The
older style `bundle` config keyword and loading tarballs from as data
paths are still going to use the older ID.

Fixes: #1725
Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-09-06 12:24:16 -07:00
Patrick East 9af059ade6 bundle: Add support for activating multiple bundles
This moves the implementation of the bundle activation API's into
the `bundle` package and makes them public. We are also beefing them
up so that they can handle >1 bundle at a time, compile additional
modules, and more. They kind of becomes a one-stop shop for getting
data and rego ready to evaluate.

Signed-off-by: Patrick East <east.patrick@gmail.com>

squash! bundle: Add support for activating multiple bundles

Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-09-04 13:39:52 -06:00
Patrick East 019b07b9cd bundle: Fix for root overlaps on partial segments
We would previously detect overlapping roots on partial path segments
for bundle roots defined in manifests.

This changes to make them be full path segments or else they won't
conflict.

Fixes: #1654
Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-08-22 12:02:26 -07:00
Patrick East 3410d491b9 plugins/bundle: raise error on root conflict
This changes to detect bundle root conflicts when "activating" a
bundle. The bundle in question will go into an error state and be
prevented from loading its data or policies.

If multiple bundles are being used, and one didn't define roots or
have a manifest (ie they claim all roots), it will conflict with all
other bundles and raise errors.

**Note: This does NOT affect bundles loaded from CLI with --data**

Fixes: #1635
Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-08-20 12:52:50 -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
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