40 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
Colin J Lacy 0b9bbc5011 plugins/rest: masks X-AMZ-SECURITY-TOKEN header in decision logs (#6423)
Decision logs had previously been configured to hide the value of the
Authorization header, as that is considered sensitive information.
However, there are cases when additional headers are provided that
contain sensitive information, such as the X-AMZ-SECURITY-TOKEN header.
This PR creates an internal map of headers that should be masked, which
can be expanded if additional headers are required. It then loops over
the headers in a request, and performs a lookup on the internal map
to see if any of them match those that should be masked. If so, it
replaces their values with "REDACTED". An existing test was added to
check both the header keys that should be masked, as well as a key
that should not.

Additional work, out of scope for this PR, would be to open a config
setting that would allow users to pass in a list of headers that should
be masked.

Fixes: #5848

Signed-off-by: Colin Lacy <colinjlacy@gmail.com>
2023-11-29 17:26:25 +01:00
Magnus Jungsbluth 22619d26c0 Add tracing to bundle/discovery download
Signed-off-by: Magnus Jungsbluth <magnus@jungsbluth.de>
2023-06-06 09:36:40 -07:00
DerGut b626a2c93b Use existing auth plugins with OCIDownloader
This change addresses solutions 2) and 3) of the related issue #5553.
It mainly starts using the (now exposed) `Config.AuthPlugin()` function
of the `rest` package in the `download.OCIDownloader`. This allows it
to use any `HTTPAuthPlugin` that is defined in the `Config.Credentials`
section and makes it much more consistent with behavior of the
`download.Downloader` and potential other uses of the rest package.

Fixes #5553

Signed-off-by: DerGut <jannik.steinmann@gmx.de>
2023-04-25 17:15:46 -07:00
Ashutosh Narkar 9163bbd209 plugins/rest: Include truncated HTTP response in logs
This commit updates the client debug log to include
the full HTTP response in case of non-200 status codes.
Recording the response in the logs can help to provide
more information to debug error scenarios.

Fixes: #2961

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2023-03-22 00:33:54 -07:00
carabasdaniel 39125a034c downloader: support for downloading bundles from an OCI registry (#4558)
Initial support for #4518.

Configuration uses the 'services' config for registries, via the "type: oci" field.
Bundles configured to pull from that service will then use OCI.

```
services:
  ghcr-registry:
    url: https://ghcr.io
    type: oci
bundles:
  authz:
    service: ghcr-registry
    resource: ghcr.io/${ORGANIZATION}/${REPOSITORY}:${TAG}
    persist: true
    polling:
      min_delay_seconds: 60
      max_delay_seconds: 120
persistence_directory: ${PERSISTENCE_PATH}
```

Service credentials are supported: if you want to pull from a private registry,
use
```
services:
  ghcr-registry:
    url: https://ghcr.io
    type: oci
    credentials:
      bearer:
        token: ${GH_PAT}
```

If no `persistence_directory` is configured, the data is stored in a directory under /tmp.

See docs/devel/OCI.md for manual steps to test this feature with some
OCI registry (like ghcr.io).

Signed-off-by: carabasdaniel <dani@aserto.com>
2022-04-28 11:27:58 +02:00
Anders Eknert 52b621301f logging: mask authorization header value in debug logs (#4496)
Fixes #4495

Signed-off-by: Anders Eknert <anders@eknert.com>
2022-03-26 08:25:28 +01:00
José Carlos Chávez 449fdfee1e chore: improves auth plugin resolution. (#4175)
* chore: improves auth plugin resolution.

Currently when aiming to use a Plugin in credentials section, if the plugin is known then it will be resolved, if it isn't, it will be passed to the supported credentials and tried to be cast as HTTPAuthPlugin which ends up in a casting issue without further feedback on what was the plugin string.

Signed-off-by: José Carlos Chávez <jcchavezs@gmail.com>
2022-01-06 09:13:57 +01:00
Vlad Iovanov c0a692d1ee logging: Remove logger GetFields function (#4116)
This removes the GetFields function from the logger interface, as mentioned in #4114.

GetFields used to be called in one place, creating a new logger using fields from an
http client afaict. I am not sure if my changes have the desired effect in that case,
or how this was desired to work - since the fields of the client are always changing
when making requests.

Fixes #4114.

Signed-off-by: viovanov <vlad@aserto.com>
2022-01-06 07:42:22 +01:00
Stephan Renatus 74473468f2 download+rest: code cosmetics (#4120)
Not much of consequence here, a few code cleanups in tests and interfaces.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-12-13 09:35:26 +01:00
David Lu 41fe76862b Add Azure HTTP auth plugin
This commit adds an HTTP auth plugin that fetches bearer access tokens using managed identities for Azure resources. This plugin will complement the existing AWS and GCP auth plugins.

Signed-off-by: David Lu <david.scowluga@gmail.com>
2021-11-08 19:14:02 +01:00
Torin Sandall 8b40acea0a logging: Fix console logger instantiation
This commit fixes the console loggers so that messages are emitted
regardless of the debug log level. The problem was that in 3fcc875 we
updated the plugins to use a console logger obtained from the plugin
manager as opposed to a global logger instantiated in the plugins
package--the console logger obtained from the plugin manager was
instantiated in the runtime package by calling
logging.NewStandardLogger. Unfortunately, logging.NewStandardLogger
does not create a new logger--it returns the global logrus
logger.

This commit fixes the issue by deprecating logging.NewStandardLogger
and introducing two new functions in the logging package:

* logging.Get() - this replaces the old logging.NewStandardLogger
  function--this function should be called to obtain the debug logger
  used throughout OPA.

* logging.New() - this actually returns a new logger that can be
  configured independently from the debug logger used throughout
  OPA.

The runtime and sdk packages have been updated to call logging.New()
to obtain console loggers and the rest of the codebase has been
updated to call logging.Get() in place of logging.NewStandardLogger().

Fixes #3654

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2021-07-27 09:39:01 -07:00
Ashutosh Narkar 5528361c69 plugins/rest: Add option to specify CA for remote services
This change allows users to specify a certificate for the services
that implement the bundle, status etc. APIs. This cert will be
used to create the root CA pool.

Fixes: #1954

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2021-06-23 08:36: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
Ashutosh Narkar 0be08fd04a download: Add support for http long polling
Earlier the downloader package only supported the http
short polling technique where the client sends periodic
requests to the server to fetch bundles. A drawback of this
method is that a low polling frequency could add unnecessary
burden on the server and network.

This commit adds support for http long polling which helps
to minimize server/network resource usage and also reduces
the delay in delivery of updates to the client.

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2021-05-06 09:11:57 -07:00
Magnus Jungsbluth bdb3d0f658 Allow configuring logger + its fields in library usage
Signed-off-by: Magnus Jungsbluth <magnus.jungsbluth@zalando.de>
2021-03-16 09:22:31 -04: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
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 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
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
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
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 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 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 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
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 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 1a673b8fd6 Add User-Agent to rest client as default header
This can be overridden by custom header configuration for services,
or requests but will default to something like:

`Open Policy Agent/<version> (<os>, <arch>)`

This is set on all outbound requests for status, decision logs, bundle
and discovery downloads, http requests from policies, etc.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-07-17 13:30:03 -07:00
Nick Williams a360905c5d Support AWS signatures for bundle HTTP operations
ref. Issue #1340

Signed-off-by: Nick Williams <nhw@me.com>
2019-06-03 21:52:27 +02:00
Stephan Renatus c77f9ed228 plugin/rest: avoid zero values in http.Transport{}
When instantiating a http.Transport struct without providing non-zero
values for some of its fields, it will leak connections.

The docs hint at this (https://golang.org/pkg/net/http/#Transport):

  By default, Transport caches connections for future re-use. This may
  leave many open connections when accessing many hosts. This behavior
  can be managed using Transport's CloseIdleConnections method and the
  MaxIdleConnsPerHost and DisableKeepAlives fields.

See this issue for details: https://github.com/golang/go/issues/19620

I have not observed any leaks in the wild for this, but I'm also
not using this plugin. I came to fix this because I had recently
introduced a bug in the server tests -- they were altering the
settings for the global http.DefaultTransport when my intention
was to create a copy. Digging in while fixing that revealed this
other location. (The fix for the server tests is in the next commit).

Signed-off-by: Stephan Renatus <srenatus@chef.io>
2019-01-18 07:46:52 -08:00
Torin Sandall 61420f8e71 Add support for specifying services as object (#1046)
* Fix typo in run short description

Signed-off-by: Torin Sandall <torinsandall@gmail.com>

* Add support for specifying services as object

Previously the services configuration had to be specified as an array,
for example:

services:
- name: foo
  url: https://example.com

In some cases, it's easier to structure the configuration as an object.
Specifically, the Helm package manager does not allow you to override
values nested under arrays. These changes allow the services
configuration to be structured as an object:

services:
  foo:
    url: https://example.com

Ref: https://github.com/helm/helm/issues/1987

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-10-30 08:02:30 -07:00
Kim Christensen d52bfaa0b2 Only allow insecure TLS when HTTPS is used
Signed-off-by: Kim Christensen <kimworking@gmail.com>
2018-10-24 21:53:05 -07:00
Kim Christensen 6f3f5b34a9 Add support for client certificates
Adds support for usings client certificates to authenticate against
service endpoints. It also enables users to allow insecure TLS
connections to service endpoints.

Fixes #684

Signed-off-by: Kim Christensen <kimworking@gmail.com>
2018-10-24 21:53:05 -07:00
Torin Sandall 72c5301ec1 Add response debug logging to REST client
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-04-10 13:58:36 -07:00
Torin Sandall 0f93fb83a9 Add byte slice message body to REST client
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-04-10 13:58:36 -07:00
Torin Sandall a475f32f82 Add message body support to REST client
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-04-09 11:05:06 -07:00
Torin Sandall f131cfcff3 Add support for bundle downloading
Previously, policies and data had to be pushed into OPA via the REST API
or loaded via command line arguments at startup.

With these changes, OPA can now be configured to pull down bundles of
policy and data from remote HTTP servers. When a bundle is downloaded
successfully, the policies and data are loaded out of the bundle file
and inserted into storage.
2018-03-16 08:51:37 -07:00