Commit Graph

136 Commits

Author SHA1 Message Date
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
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 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
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
Anders Eknert fe97f335fc Allow PKCS8 encoded private keys (#3117)
Fixes #3116

Signed-off-by: Anders Eknert <anders@eknert.com>
2021-02-03 20:16:14 +01:00
Anders Eknert 635d8a52d8 Configurable persistence_directory
This allows configuring the persistence_directory OPA should use for persisting
bundles to disk. While this currently only covers bundles I didn't want to close
the door for persisting other type of objects later, so the
persistence_directory option is kept at the top level of the configuration,
defaulting to $PWD/.opa if not provided.

Bundles will be persisted to ${persistence_directory}/bundles.

Closes #3085

Signed-off-by: Anders Eknert <anders@eknert.com>
2021-02-02 10:14:03 +01:00
Anders Eknert 36ba4454e8 OAuth2 JWT bearer grant type and JWT client auth
Allow OPA to issue JWT's which it uses to authenticate a configured
OAuth2 client, as described in RFC7523. This replaces the client_secret
as the actual credential and allows for either using an entirely new
grant type called "JWT bearer", or using the previously supported
client_credentials grant type, only with the client_secret replaced
by a signed JWT. This change covers both scenarios described in
RFC7523.

Other changes made to accomodate this feature:

- Add `private_key` attribute to keys struct to allow for both public and
private keys to be stored there.
- Refactored the keys configuration struct and logic to its
own package no longer coupled to bundles.

Closes #3055

Signed-off-by: Anders Eknert <anders@eknert.com>
2021-01-20 13:36:15 +01:00
Grant Shively a1d8381fc8 plugins: inter-query cache config discovery
InterQueryBuiltinCacheConfig now responds to the plugin manager's reconfigure event, which allows cache config to exist in discovery config. Previously, cache config would be ignored if it was only declared in discovery config.

Related to #2978.

Signed-off-by: Grant Shively <gshively@godaddy.com>
2021-01-05 16:09:58 -08:00
Torin Sandall 57ccd83c68 bundle: Add deprecation warning for old 'bundle' configuration
Fixes #1598

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2020-12-14 14:28:57 -05:00
Grant Shively 7a28f26ae7 plugins/plugins: WARN state and optional message
The WARN state can be used to signal admins that a plugin is in a
potentially dangerous or degraded state. The optional message may be
used to provide context about the warning.

Fixes #2932

Signed-off-by: Grant Shively <gshively@godaddy.com>
2020-12-10 13:54:55 -05:00
Grant Shively 65aa409004 plugins, plugins/rest: Support custom auth plugins
Plugins that implement the HTTPAuthPlugin can be used with a new
credentials options under services:

```
services:
  my_service:
    credentials:
      plugin: my_plugin
plugins:
  my_plugin: {}
```

Fixes #2758

Signed-off-by: Grant Shively <gshively@godaddy.com>
2020-12-09 13:49:48 -05: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
Kelsey Hightower 07e7867c1b plugin/rest: Add GCP metadata server support
Adds support for fetching access and identity tokens from a GCP
metadata server. Identity tokens are used to authenticate to third
party applications running behind Google authentication proxies
such as containers deployed to Google's Cloud Run.

Access tokens are used to authenticate to first party GCP services
such as Google Cloud Storage.

Signed-off-by: Kelsey Hightower <kelsey.hightower@gmail.com>
2020-11-24 12:05:54 -05:00
Patrick East 3fb39b560e plugins/logs: Remove empty test case
Removing dead code..

Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-11-11 09:54:03 -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
Patrick East 1b44b159b0 plugins/bundle: Fix default size limit
For the deprecated `bundle` config style bundle configuration we were
not setting the default bundle size limit, instead a zero value was
being propagated through when we "upgraded" the older style config
to the newer values.

This corrects that issue by always setting it to the default size
limit, which maintains the previous behavior.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-11-09 11:10:01 -08:00
Patrick East 4157ac0b3e plugins: Use data patching API's on wasm resolvers
Instead of loading the entire data tree on every update we call to
either remove or set data using the storage path.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-11-06 15:12: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 eb6bbfc0c1 Revert "plugins/logs: Close channel in unit test"
This reverts commit ac06c3b73c.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-11-05 08: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
Ashutosh Narkar f85139aeef plugins/bundle: Support for loading signed bundles from disk
Earlier when loading bundles from disk, we weren't providing
the bundle verification config that would be needed to read
a signed bundle that may have been persisted to disk in the past.

This changes adds support for loading signed bundles from disk by
providing the bundle verification config (if any) to the bundle
reader.

Fixes: #2824

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2020-10-28 10:16:15 -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
Richard Simpson ab9e9add70 plugin/rest: Adds AWS Web Identity support
Adds support for signing S3 requests using Web Identity credentials. Specifically supports the Env Var version that's used in EKS IRSA.

Closes: #2463
Signed-off-by: Richard Simpson <richardsimpson@outlook.com>
2020-10-23 10:01:31 -07:00
Patrick East ac06c3b73c plugins/logs: Close channel in unit test
One of the unit tests created a new channel for the testServer and
was not closing it when done. This changes the test to close the
channel after stopping the plugin but before the server gets shut down

Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-10-21 15:00:33 -07:00
Patrick East a23158e044 plugins/logs: Correct hang in unit test fixture
Periodically we would get hangs in unit tests because the testServer
http handler would be stuck trying to send an event through the
channel. If the unit test was completed nothing would be reading,
and the send would be blocked indefinitely... as it turns out, the
http test server cannot shutdown while handlers are still running,
so the unit tests hang indefinitely.

To prevent this from happening we will drain any pending events when
stopping the testServer.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-10-21 15:00:33 -07:00
Torin Sandall 544c2b0cbd plugins/bundle: Add file size limit configuration option
This commit lets users override the 1GB file size limit on the bundle
reader with a configuration setting.

Fixes #2781

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2020-10-21 11:00:24 -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 a7aed3aab2 Add REST client option for OAuth2 client credentials
Allows OPA to obtain OAuth2 access tokens and present them as bearer tokens for authentication at remote endpoints.

Fixes #1205

Signed-off-by: Anders Eknert <anders.eknert@bisnode.com>
2020-10-15 12:43:44 -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 0c63381e7e Fix plugin config instantiation tests
While toying around with a custom credential plugin I noticed that changing the `wantErr` values had no effect to the outcome for the TestNew tests - they would always pass. This was due to:

1. The `New` function only unmarshalling the JSON config, not actually instantiating plugins and clients (which is where the validation step is done).
2. wantErr was only checked if false on errors, not its opposite, i.e. not if wantErr == true and error == nil.

Signed-off-by: Anders Eknert <anders.eknert@bisnode.com>
2020-10-08 17:10:22 -07:00
Torin Sandall c560ec3bac plugins/bundle: Tweak bundle save directory
Store bundles under a specific sub-directory of the new working
directory (as the same directory may be useful in future for other
persitence features.) Also, update the docs to mention where the files
are saved to as this was unclear from the user's perspective.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2020-10-05 15:59:26 -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
Ashutosh Narkar 5a79a45e4f plugins/bundle: Support for saving and reading bundles from disk
This commit adds support to persist and load bundles from disk.
A new field is introduced in OPA's bundle configuration that can
be optionally set to enable OPA to write and read bundles from disk.
This feature will allow OPA to serve policy decisions in scenarios
such as OPA being unable to communicate with the bundle server.

Fixes #2097

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2020-09-30 15:17:51 -07:00
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