Commit Graph

166 Commits

Author SHA1 Message Date
Stefan Bueringer 9493b1d37a metrics: Add go default metrics
Signed-off-by: Stefan Bueringer <sbueringer@gmail.com>
2019-02-23 08:21:39 -08: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
Torin Sandall e2ac20284c runtime: Refactor build version information
Previously the build version was recorded in the version package and
then different components would report it in an ad-hoc manner, e.g.,
the REPL has a module that generates a virtual doc with the version
info in it, the server was using templating to do the same, etc.

These changes remove the special code from the REPL and server
implementations to report the version. Instead the runtime writes the
version into /system/version at boot.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-02-22 14:55:36 -08:00
jrmarsha 5c934e545e Add trailing newlines when pretty printing
This change appends a newline to pretty printed API results to improve readability on the command line. Also, fix tests that were asserting on string equality instead of JSON value equality.

Signed-off-by: Josh Marshall <joshua.r.marshall.1991@gmail.com>
2019-02-22 08:55:04 -08:00
Torin Sandall c79eb41d86 server: Add checks to respect bundle scope
These changes update the server to check the bundle scope before
writing any policy and data. This way the caller won't accidentally
write policy and data into OPA only to have it overwritten the next
time a bundle is downloaded and activated.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-02-19 10:51:19 -08:00
Torin Sandall bb80fd7f74 Add path conflict checks
Previously there were no checks in place to ensure that base and
virtual documents do not overlap. As a result, if users loaded raw
JSON and rules into OPA that overlapped, the evaluation results were
not well defined. With these changes, we can detect the overlap and
reject updates (to policies or data) that would cause inconsistent
results.

Fixes #1207

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-02-15 08:00:22 -08:00
Stephan Renatus 39776afdbe server: don't alter http.DefaultTransport in tests
I wasn't paying enough attention to this detail in #1149. It's low-impact,
but let's avoid the trap anyways.

Also moves the client creating code into a helper method, with a comment
regarding http.Transport{}.

Signed-off-by: Stephan Renatus <srenatus@chef.io>
2019-01-18 07:46:52 -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 dcb99dfe9e Update server to include decision ID in error events
Previously, the server was not including a decision ID in log events
for errors. This commit changes that.

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
Torin Sandall 503b8c0645 Add metrics timer for server handler
These changes add a new metric into the server that calculates the
time taken from when the handler is invoked to the point where the
decison log is emitted. Hopefully this will prevent us from missing
important periods in the request path.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-01-16 12:45:47 -08:00
repenno e2ef042d3d Query API should return HTTP 400 if query does not parse
Fixes #1081
curl -G -v "http://localhost:8181/v1/query" --data-urlencode "q=^ -i"

*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8181 (#0)
> GET /v1/query?q=%5E%20-i HTTP/1.1
> Host: localhost:8181
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 400 Bad Request
< Content-Type: application/json
< Date: Sat, 15 Dec 2018 09:13:58 GMT
< Content-Length: 118
<
{
  "code": "invalid_parameter",
  "message": "1 error occurred: 1:1: rego_parse_error: no match found\n\t^ -i\n\t^"
* Connection #0 to host localhost left intact
}%

Signed-off-by: repenno <rapenno@gmail.com>

Query API should return HTTP 400 if query does not parse

Fixes #1081
curl -G -v "http://localhost:8181/v1/query" --data-urlencode "q=^ -i"

*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8181 (#0)
> GET /v1/query?q=%5E%20-i HTTP/1.1
> Host: localhost:8181
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 400 Bad Request
< Content-Type: application/json
< Date: Sat, 15 Dec 2018 09:13:58 GMT
< Content-Length: 118
<
{
  "code": "invalid_parameter",
  "message": "1 error occurred: 1:1: rego_parse_error: no match found\n\t^ -i\n\t^"
* Connection #0 to host localhost left intact
}%

Signed-off-by: repenno <rapenno@gmail.com>

Query API should return HTTP 400 if query does not parse

Fixes #1081

Reworked fix based on comments and current code.
v1QueryGet/Post already call ast.parseBody through validateQuery,
 therefore the solution:

 - Avoids double parsing query
 - Keeps original query string around so logs can work
 - Performs http error checking at the server.go level
 - Enhanced testing to make sure all messages are sane

 Optionally error checking in server.go could be factored in a
 unified function.

Signed-off-by: repenno <rapenno@gmail.com>

Query API should return HTTP 400 if query does not parse

Fixes #1081

Reworked fix based on comments and current code.
v1QueryGet/Post already call ast.parseBody through validateQuery,
 therefore the solution:

 - Avoids double parsing query
 - Keeps original query string around so logs can work
 - Performs http error checking at the server.go level
 - Enhanced testing to make sure all messages are sane

 Optionally error checking in server.go could be factored in a
 unified function.

Signed-off-by: repenno <rapenno@gmail.com>

Query API should return HTTP 400 if query does not parse

Fixes #1081

Reworked fix based on comments and current code.
v1QueryGet/Post already call ast.parseBody through validateQuery,
therefore the solution:

- Avoids double parsing query
- Keeps original query string around so logs can work
- Performs http error checking at the server.go level
- Enhanced testing to make sure all messages are sane
- Fixed existing server_test.go tests affected by requested changes

Optionally error checking in server.go could be factored in a
unified function.

Signed-off-by: repenno <rapenno@gmail.com>
2019-01-16 07:48:08 -08:00
Stephan Renatus 3286c39822 server: add authentication based on TLS
* identifier: add TLSBased

This is only the identifier, the server setup still has to be done.

Note that it diverges a little from what was proposed in the issue:
not every client cert needs to have a CN record -- so instead, we'll
use whatever is the cert's subject as client identity.

* Drive-by fix: identifier_test: don't use same package for TokenBased
  tests.
* server: require and verify client cert for AuthenticationTLS
* server: allow setting CA pool via --tls-ca-cert-file
* server: expose new authentication via parameter
* [nit] server: simplify getListenerForHTTPServer
* server_test: use httptest for integration-y TLS tests
* book/security: mention TLS authn with example

Signed-off-by: Stephan Renatus <srenatus@chef.io>
2019-01-14 12:57:35 -08:00
repenno a353f25cb6 Fixes #1086
curl -v http://:8181/health
*  Trying ::1...
* TCP_NODELAY set
* Connected to  (::1) port 8181 (#0)
> GET /health HTTP/1.1
> Host: :8181
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Date: Mon, 07 Jan 2019 20:09:36 GMT
< Content-Length: 2
<
* Connection #0 to host  left intact
{}%

Signed-off-by: repenno <rapenno@gmail.com>
2019-01-08 07:59:30 -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
NIkhil Bhatia 93542dbbec Post query (#1)
* add POST method for /query

Signed-off-by: Nikhil Bhatia <nbhatia@microsoft.com>
2018-10-23 16:16:57 -07:00
Torin Sandall 8cd94fc982 Add router option to the HTTP server
This allows callers to embed the OPA HTTP server inside another HTTP
server (e.g., under a path prefix like /opa).

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-10-18 12:46:25 -07:00
Torin Sandall b95227688b Add default decision configuration
Previously, OPA would serve POST requests with an empty URL path by
querying data.system.main and returning the generated value. In some
cases, it's useful to be able to reconfigure OPA to use a different
document to serve these kinds of requests. The same goes for the OPA
authorization policy.

These changes make the default decision and default authorization
decision paths configurable.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-10-17 15:07:26 -07:00
Torin Sandall 38a988765d Add built-in function to get runtime info
These changes add support for accessing runtime information inside of
policies. In some cases, policies need to access environment variables
or configuration that OPA was booted with. These changes add a built-in
function that allows policies to gain access to this information. The
built-in function itself is relatively trivial. Most of the required
changes were plumbing the runtime information from the entrypoint down
into the evaluation engine. The alternative would have been to introduce
a global variable containing this information however that would be have
been harder to reason about in library integrations.

Fixes #420

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-10-16 14:20:03 -07:00
Torin Sandall ca74ca1aa4 Add YAML input support to webhook APIs (#1014)
Also, remove printlns from redirection test cases.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-10-15 17:38:33 -07:00
repenno 843e64fddc Add support for YAML encoded input values
These changes add support or YAML encoded input values in the V1 Data API.

Fixes #290

Signed-off-by: repenno <rapenno@gmail.com>
2018-10-15 15:44:02 -07:00
repenno 277abcf91c Redirect HTTP requests with trailing slashes
Fixes #972

Signed-off-by: repenno <rapenno@gmail.com>
2018-10-15 15:41:35 -07:00
repenno ed2c05a77e Expose version information via REST API
/v1/data/system/version will return information similar to:

Version: 0.9.3-dev
Build Commit: c5171ed5-dirty
Build Timestamp: 2018-10-07T02:12:58Z
Build Hostname: JOHNDOE-M-9130

Signed-off-by: repenno <rapenno@gmail.com>
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-10-08 08:25:17 -07:00
Ashutosh Narkar 2e0381949b Validate queries by checking unsafe builtins
Fixes #919

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2018-09-04 16:31:50 -07:00
Torin Sandall 7be7fa718b Fix XSS in debug page
Previously the query/input/error forms were being returned to the client
unescaped. This would allow for XSS attacks against admins with access
to the OPA debug page. While this scenario is quite uncommon (e.g., OPA
should be configured to listen on localhost/unix domain, authenticate
and authorize requests, etc.) it's still a possibility.

These changes escape the query/input/error forms returned to the browser
to prevent XSS attacks.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-09-04 14:30:36 -07:00
Torin Sandall 4aa9d715e4 Improve token authentication docs and handler
With these changes, the identity will be undefined if a token is not
specified. This is less surprising than the empty string that would be
set prior to these changes.

Fixes #901

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-09-04 14:30:07 -07:00
Torin Sandall 4e443459c6 Remove support rule if default value is not needed
Previously, partial eval would always generate a support rule when it
evaluated a rule with a default value. These changes tweak this
behaviour so that support rules are only generated if the default value
is required in the partial eval output. It's better to avoid generating
support rules as it simplifies the assumptions that the consumer of
partial eval can make.

Fixes #820

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-07-24 07:49:18 -07:00
Stephan Renatus fd938f0661 Remove dead code
I don't know why I ended up looking into this, but I've run

    golangci-lint run --disable-all -E deadcode

on the code base and removed everything that came up :)

Signed-off-by: Stephan Renatus <srenatus@chef.io>
2018-07-02 09:19:15 -07:00
Torin Sandall 8c95fa4a39 Update Compile API empty body representation
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-06-29 10:29:28 -07:00
Torin Sandall 24dc8dbb0e Add handler for /v1/compile API
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-06-28 08:11:45 -07:00
Torin Sandall a55a542809 Add Partial function to Rego objects
Partial allows callers to partially evaluate a query and obtain the
resulting partially evaluated queries and support modules from the
evaluation engine.

This is a lower level interface compared to the other API (PartialEval)
that returns a result which can be evaluated normally (which takes
advantage of partial evaluation for optimization purposes.) This new
interface is targetted at use cases where callers are not interested in
a binary policy decision but instead need the conditions that should be
evaluated at a later time.

Deprecate the old PartialEval function and rename it to PartialResult to
avoid some (but certainly not all) confusion.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-06-28 08:11:45 -07:00
Ashutosh Narkar 530cb486a7 Add query parameters to input
Closes #781

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2018-06-18 11:10:09 -07:00
Stephan Renatus bc0e04851d fix ineffassign
This actually revealed a missing error check. Since we've started a
transaction at this point, I guessed that `s.abortAuto` is what we want
there.

Signed-off-by: Stephan Renatus <srenatus@chef.io>
2018-06-06 08:44:08 -07:00
Stephan Renatus 2f1526c672 fix misspell
Signed-off-by: Stephan Renatus <srenatus@chef.io>
2018-06-05 09:50:13 -07:00
Stephan Renatus 28ff5613ac fix 'gofmt -s'
Signed-off-by: Stephan Renatus <srenatus@chef.io>
2018-06-05 09:50:13 -07:00
Torin Sandall c19e342fbe Fix partial cache invalidation for data changes
The partial cache was not be invalidated when data changed. As a result,
callers would receive stale results when data updated.

Fixes #589

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-06-04 13:46:44 -07:00
Torin Sandall d1551d2366 Refactor HTTP and HTTPS listener creation
With the #762 and #764 there is no need to keep the logic for creating
the HTTP and HTTPS listeners inside the function. These changes just
split listener creation into separate functions for clarity.

Also, update runtime to use logrus for reporting initialization errors
instead of println for consistency.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-05-30 15:34:47 -07:00
Juan Antonio Osorio Robles e6bb5d6b90 Allow passing multiple addresses to the server
This makes the --addr CLI parameter to be a slice and refactors the code
to allow several addresses to be passed. Hence we can listen on as many
HTTP and UNIX sockets as we want.

Signed-off-by: Juan Antonio Osorio Robles <jaosorior@redhat.com>
2018-05-30 10:58:36 -07:00
Juan Antonio Osorio Robles 565786ba63 Refactor server listen to take 'http(s)' scheme in URL
This refactor consists of two things:

* Accept 'http(s)' as a scheme

* Move the code so it's easier to add new protocols to --addr

Signed-off-by: Juan Antonio Osorio Robles <jaosorior@redhat.com>
2018-05-30 09:57:02 -07:00
Juan Antonio Osorio Robles 61600034e9 Fix unix socket removal to take relative paths as well
The socket removal was using the URIs path, which is fine for absolute
paths, but doesn't work properly with relative ones.

The socket itself is listening on the Host + Path, which works for
relative paths, so we use that same path for the removal.

Signed-off-by: Juan Antonio Osorio Robles <jaosorior@redhat.com>
2018-05-29 07:59:25 -07:00
Juan Antonio Osorio Robles 59bee17b2b Use slices instead of explicit output variables for server loops
Previously, the server loop functions were returned explicitly as
outputs (having three outputs for the loop functions and one for the
error). If we want to add more this approach doesn't work very well. So
this introduced a slice as the output of the Listen function for the
Server struct, which allows us to loop over it and iterate over however
many loop functions we need.

This keeps the same logic as before, and is merely a first step in
refactoring this logic.

Signed-off-by: Juan Antonio Osorio Robles <jaosorior@redhat.com>
2018-05-29 07:56:38 -07:00
Torin Sandall b8b826dbd6 Update UNIX domain socket support
These changes tweak the UNIX domain socket to (1) return an error
instead of panicing (because the server may be embedded as a library)
and (2) to unlink the domain socket file before binding. The latter is
required so that OPA can be stopped and started without manually
removing the socket file.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-05-24 18:23:31 -07:00
Juan Antonio Osorio Robles 1cb83d4783 Add support for listening on a UNIX domain socket
This enabled the OPA server to listen on a UNIX domain socket through
the already existing --addr option. This option now parses the passed
argument as a URL and based on the scheme (which for this case has to be
"unix://"), it listens on the path for the socket.

Signed-off-by: Juan Antonio Osorio Robles <jaosorior@redhat.com>
2018-05-24 07:53:26 -07:00
Ashutosh Narkar 7c5ad4ba62 Add trace event for rule index lookups
Closes #716

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2018-05-16 08:31:08 -04:00
Torin Sandall 9e27dc2883 Remove unnecessary comments
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-04-16 08:35:15 -07:00
Ashutosh Narkar 5b7442cabc Registration framework for integrating plugins
Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2018-04-13 16:12:25 -07:00
Ashutosh Narkar 135911bb5e Register server for compiler change updates
Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2018-04-13 16:12:25 -07:00
Torin Sandall a623dcd4c8 Handle escaped paths in data writes
These changes extend #702 to include writes. If the path segments are
escaped, the should be unescaped during parsing. This allows callers to
write keys like "foo/bar" into storage.

Fixes #695

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-04-12 17:07:48 -07:00