Commit Graph

134 Commits

Author SHA1 Message Date
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
Stephan Renatus 33b125e5b4 server: handle foo%2fbar in path
Now, GET /v1/data/foo%2fbar will construct a query string of

    data["foo/bar"]

allowing to retrieve the key mentioned in issue #695.

However, I believe further work would be necessary, as the code path for
POST/PATCH/DELETE is different. (That has also blocked added a test for
this change, unfortunately.)

I also couldn't get this to work with POST /v0/data/foo%2fbar. (But I also
haven't tried very long, to be honest.)

Signed-off-by: Stephan Renatus <srenatus@chef.io>
2018-04-12 16:37:08 -07:00
Torin Sandall d478a832e5 Record the decision timestamp in UTC
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-04-10 13:58:36 -07:00
Torin Sandall b31412dc8e Update server to include revision in decision logs
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-04-10 13:58:36 -07:00
Torin Sandall 76a3ef3c0e Add storage.MakeDir helper function
It's often useful to be able to create a hierarchical structure in one
shot in storage. Previously this functionality was implemented in the
server, but it's better off implemented in the storage package.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-04-10 13:58:36 -07:00
Torin Sandall aacbd8bb91 Update server to accept a decision logger
In the past, services embedding OPA relied on the diagnostic buffer to
hook into decision logs. This required that all services embedding OPA
wrap the existing diagnostic buffer so that the server could still
support that feature.

With these changes, services embedding OPA can simply register a
function to be called whenver a decision is made. This simplifies the
implementation for services embedding OPA.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-04-10 13:58:36 -07:00
Torin Sandall 2a5192ec6c Minor changes to note support
* Check if node is non-nil to prevent panic
* Omit empty message field from API response
* Use native string type for message fields
2018-03-12 17:13:19 -07:00
Unknown dbfccf15e5 Adding test and documentation for the trace builtin 2018-03-12 16:08:54 -07:00
Torin Sandall 67022fc71a Add omitempty tag for ad-hoc query result field
Without omitempty, the server returns {"results": null} which is not
consistent with the rest of the APIs.
2018-02-24 10:16:27 -08:00
Torin Sandall d30a3f2418 Use rego.ParsedInput to provide input from form
The debug form was passing the parsed input as a Go native interface{}
which caused the Rego package to attempt to treat ast.Valeu as a Go
native value (which fails.)

Fixes #571
2018-02-24 10:16:27 -08:00
repenno 23a33ce0c1 Delete method support
- Support for DELETE(ing) /data resources
- Deleting non existing resources returns 404
- Deleting a resources and then GETting it returns 200
  with empty body
- GETting resources that were not explicitly deleted before
  will return 200OK with body {"result": {}}
- Fixes #609
-
2018-02-13 15:59:45 -08:00
Torin Sandall 658ae9fe75 Refactor AST type name strings
Previously we had constants defined for AST type names. These were used
in error messages in various places. The original goal was to make error
messages consistent, however, this approach made it difficult to locate
the source of the error in code.
2018-02-09 16:38:34 -08:00
Torin Sandall 744316dbaa Add basic query performance instrumentation
Previously OPA only tracked query performance a high level (e.g., parse,
compile, eval latencies.) In some cases, it's necessary to instrument
lower level evaluation operations to understand performance. These
changes update the eval implementation to support instrumentation:

* Eval has been instrumented to record time taken for various core
operations like term plugging, reading from the store, rule lookup,
cache hits, etc.

* Rego package has been updated to support a simple rego.Instrument
operation that enables query instrumentation.

* REPL and server have been updated to expose simple interfaces to turn
on instrumentation.

* Diagnostic policy config "all" will enable instrumentation.

Instrumentation can be expensive (because it requires timing frequently
executed operations) so it should be treated as a debugging tool and not
enabled all of the time.
2018-02-09 09:30:27 -08:00
Torin Sandall 7cc2fa5aaa Fix panic due to nil term value
We were not catching merge failures when combining base and virtual
documents. As a result, a term with a nil value (which is invalid) was
being created and returned.

It's arguable that these kinds of conflicts should be caught when data
or policies are inserted. Alternatively, we should revisit whether
policy decisions should be obtained by querying the same root document
as raw data (e.g., decisions could be namespaced under a separate root
document.)

Fixes #601
2018-02-08 08:54:24 -08:00
Torin Sandall 235af0e177 Add test case for OOB metrics API 2018-02-06 16:24:05 -08:00
Ross Guarino d69511349b Add basic out-of-band metrics collection
Collect Duration metrics form the http handlers. Instrument each handler
using the promhttp package. The metrics are partitioned on their status code,
the name of the handler serving the request and the HTTP method, as
"code", "handler" and "method" respectively.

To avoid unbounded cardinality an approximation of the request's url is
used as the "handler" dimension. The handlers roughly correspond to the
different api resource path & the api version.

Issue: #525

Signed-off-by: Ross Guarino <rssguar@gmail.com>
2018-02-04 15:17:16 -08:00
Torin Sandall 082e445ec9 Refactor callers to support calls as values 2018-01-26 18:05:05 -08:00
Torin Sandall 501d3f3826 Reorganize helper functions in server.go 2018-01-20 13:18:40 -08:00
Torin Sandall 44dc94e9a6 Refactor server to use rego package partial eval
Now that the Rego package supports partial evaluation, use that
implementation instead of the initial one added into the server.
2018-01-20 13:18:40 -08:00
Torin Sandall 343925a268 Add test case to check for partial eval latency 2018-01-18 14:57:05 -08:00
Torin Sandall ae9ae105f4 Update topdown to record partial eval latency 2018-01-18 14:11:47 -08:00
Torin Sandall 819be66ec7 Update server to expose partial evaluation
These changes expose the partial evaluation optimization through the
HTTP API. Clients can request the optimization by including a query
parameter and the server will lazily run partial evaluation and keep a
cache of the results for subsequent queries.
2018-01-18 07:10:31 -08:00
repenno b89ddece0b Catch all REST handlers for forbidden methods
- Fixes #445
2018-01-11 09:08:15 -08:00
Tristan Swadell 582f47074d Remove lint errors detected during 'make check' 2018-01-10 15:17:10 -08:00
Torin Sandall 3ebbeede6c Refactor topdown evaluation/unification
These changes modify topdown evaluation to use a binding list that
namespaces variables. This allows topdown to propagate partially ground
ref operands into child query evaluation.

These changes also prepare topdown evaluation to support a partial
evaluation mode.

With these changes, evaluation is no longer performed in two steps
(i.e., first pass of evaluating individual terms, second pass of
evaluating built-in expressions.) Instead, evaluation assumes queries
have been rewritten to eagerly evaluate refs and comprehension. This
way, ref and comprehension bindings do not have to be maintained
separately: they are handled by the normal variable binding list.

This commit contains some breaking changes to the topdown APIs,
namely...

1. Truth explanation has been removed. This feature was not used and the
tracing changes broke it. We can revisit in future if necessary.

2. Data indexing has been removed. Data indexing can be re-added in
future if necessary however it should be handled outside of topdown to
avoid potential memory leaks.

3. Built-in functions produce at-most-one output now. Functions that
used to produce multiple outputs (e.g., io.jwt.decode) can produce a
composite value if they need to.

Fixes #131
2017-11-09 09:07:48 -08:00
Torin Sandall ea2ea9b12b Modify AST to represent function names as refs
These changes update the AST to represent function names as refs.
Previously, function names were represented as strings. Representing the
names as strings was fine, however, once functions and rules are
merged, it will be desirable to refer to functions using references.
This is a bit of preemptive refactoring to make that change easier.
Instead of having functions referred to with both strings and
references, all functions will be referred to with references.
2017-10-10 08:57:58 -07:00
Torin Sandall 05e9eb601c Refactor diagnostics support in server and runtime
- These changes update the server to always report performance metrics
  and the remote client address. The metrics are cheap to compute and
  there is no significant reason to require more complex configuration
  at this point.

- These changes update the runtime to allow callers to supply the
  diagnostics buffer implementation. This way callers can hook up their
  own sinks to the server's diagnostics.
2017-09-19 15:54:26 -07:00
Torin Sandall 2ec6770956 Add optional decision_id to server
Include an optional decision_id field in Data API responses. The
decision_id can be used for correlating application transactions with
policy decisions. The decision_id field is intended to be separate from
any kind of application tracing that OPA may eventually support.
2017-09-19 15:54:26 -07:00
Torin Sandall b140b7490b Refactor input parsing in v0 API
Previously the server was using the Rego parse to conveniently parse
inputs into ast.Value. For large inputs this was taking unnecessarily
long (e.g., 60s for 500KB of JSON.)
2017-09-14 17:50:25 -07:00
Torin Sandall 2a3b97cf96 Refactor query watch tests 2017-09-05 14:18:39 -07:00
Torin Sandall b8f0762505 Fix type inference bug
The type checker was starting from the head for the refs to base docs
and inferring type Any for the head var as well as any other vars found
in the ref. The checker should never have inferred type Any for the
head.

With this change in place, the server test of watch invalidation now
correctly reports a compile-time error.

Fixes #381
2017-09-05 14:18:39 -07:00
Torin Sandall 4a4af6b178 Remove dead code associated with input doc errors
In b23cb4e the compiler was changed to allow queries to refer to the
input document without the input document being defined. Those changes
did not remove all of the code associated with input errors.

These changes remove the remaining (dead) code associated with input
errors and also update the server to allow Data API POST requests that
do not specify an input document.
2017-08-29 10:49:23 -07:00
Torin Sandall c45bdf9e8d Refactor Policy API responses
These changes update the Policy API responses to:

- Return empty objects for PUT/DELETE with optional support for ?metrics
- Return AST and raw/source for GET

Support for the ?source param has been removed as the raw/source version
is now included by default.
2017-08-24 11:29:26 -07:00