Commit Graph

31 Commits

Author SHA1 Message Date
Torin Sandall 6a0fec7fb4 cmd: Fix eval and fmt to support file:// URLs
Users should be able to pass file:// URLs to any of the
sub-commands. In 3be55ed6 the eval and fmt sub-commands were not
updated to accept file:// URLs for the input file and normal paths
(respectively).

This commit just moves the unexported cleanFileURL function from the
loader package into it's own internal package so that it can be shared
in OPA.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-09-06 12:29:37 -07:00
Patrick East 53d1d737a6 cmd: Fix eval output formatting on error
Recently added changes switched to using prepared queries, and on
error would just return/exit. We needed to capture the error and
send it through the result formatter.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-09-04 16:36:31 -06:00
Patrick East ed61c850f5 cmd: Add support for -b/--bundle CLI options
The new `-b`/`--bundle` CLI options will accept a directory to be
loaded as a bundle, or a file which will be loaded as a tarball.

Closes: #1584
Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-09-04 13:39:52 -06:00
Torin Sandall 3be55ed660 loader: Update to accept file:// URLs.
The file loader splits paths on the first colon character and uses the
left-hand side for the prefix to root the document at under data. On
windows this is problematic because of drive lettesr (e.g., C:\X\Y\Z
is interpreted as load file at \X\Y\Z under data.C.

This change updates the loader to accept file:// URLs. This way
callers can unambiguously specify filenames that contain colon
characters. For now this will mainly be used by VS Code and other
programmatic callers. In future we can support other schemes (e.g., http).

Fixes #1505

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-08-22 10:44:35 -04:00
Torin Sandall eabe7f1312 cmd: Update subcommands to support fails explanation mode
This change updates the test command to use the fail explanation mode
by default. Since the test framework expects test rules to be
defined/true failure events are usually what is required to debug test
failures (e.g., some assertion in the test rule will Fail and that
will _likely_ be based on some failure in the policy under test.)
Hopefully this makes the test output a bit less verbose and more readable.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-07-29 18:56:09 -04:00
Torin Sandall 3f063595b6 cmd: Fix double print of rego errors
Fixes #1518

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-06-25 09:21:21 -07:00
Torin Sandall 62e22e0267 rego: Add ParsedModule option to API
These changes just add another option to the API to supply parsed
modules directly. This allows tools that rely on the file loader to
skip parsing of modules for a second time when running evaluation. For
large sets of modules this can reduce latency quite a bit.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-06-22 20:26:34 -07:00
Patrick East 9ab2e04868 Change opa eval input to parse as json first
It was getting parsed from string -> ast.Term but the generated
parser is (apparently) not as performant as the json parser and then
using `ast.InterfaceToValue`.

Fixes: #1488
Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-06-13 11:30:26 -07:00
Torin Sandall 7864d60dd7 eval: Add parameter to control partial eval inlining
These changes add a new parameter to partial eval to disable inlining
on virtual documents. This allows callers with knowledge of the policy
to reduce the cost of partial eval (at the expense of receiving
support rules.) In the future, OPA can be extended to automatically
disable inlining using the new parameter.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-06-11 16:22:57 -07:00
Torin Sandall 6d30ec72f3 cmd: Add --instrument flag to eval subcommand
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-06-04 09:44:05 -07:00
Torin Sandall c29ee923e0 cmd: Add notes explanation mode to eval
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-05-10 10:33:40 -07:00
Torin Sandall bffe06847c cmd: Refactor common flags for subcommands
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-05-10 10:33:40 -07:00
Torin Sandall ce9db831ed cmd: Add --fail-defined flag to eval subcommand
The eval subcommand currently supports a --fail flag that causes the
subcommand to exit with a non-zero status when the query result is
undefined. While this is useful in some cases, runners often want to
see an error message explaining _why_ the exit status is
non-zero. Since the query result is undefined, policy authors can't
surface information in the result set

With these changes, the eval subcommand includes a new --fail-defined
flag has the opposite behaviour: exit with non-zero status if the
query result is defined/non-empty. This works well for deny lists that
are commonly found in admission use cases.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-03-05 09:44:15 -08:00
Torin Sandall 883a9ecad6 cmd: Add coverage support to eval subcommand
Previously coverage was only available via the test runner. This
allows callers to run ad-hoc queries and see what parts of the policy
are evaluated.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-01-30 08:00:22 -08:00
repenno 73e8e3a96e Add profile command to REPL
For the time being, enabling profiling will disable tracing and vice versa. Once we add support for multiple tracers, this behavior can be changed.

Fixes #838 

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
Signed-off-by: repenno <rapenno@gmail.com>
2018-11-06 08:33:27 -08:00
Kim Christensen 58598d714a Add support for non-zero exit code (#1006)
* Add support for non-zero exit code

Adds support for non-zero exit code when providing the --fail flag to
the eval command. 0 means no error, 1 means undefined result and 2 means
an error.

Fixes #981

Signed-off-by: Kim Christensen <kimworking@gmail.com>
2018-10-18 10:50:49 -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 b3b390098b Add --partial/--unknown flags to eval subcommand
These changes update the eval subcommand to support partial evaluation.
As part of these changes, the pretty formatting of partial evaluation
results has been refactored and moved into the presentation package. The
new version uses the tablewriter like other output values.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-08-17 14:19:57 -07:00
Torin Sandall cb5a60d63b Refactor eval subcommand flag handling
Instead of checking flags in the main business logic, just default
.profile and .metrics in the pre-run.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-07-26 13:31:40 -07:00
Torin Sandall 4cfcd8498d Refactor presentation package interfaces
Previously, output was being printed in the presentation package, REPL,
and eval subcommand. Thes changes refactor the presentation package so
that it can handle all of the output printing required by the REPL and
the eval subcommand.

These changes affect the 'json' output format in the REPL. Previously,
the JSON output format would display either the expression value or
bindings. With these changes, the 'json' output format in the REPL is
the same as the one in the eval subcommand.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-07-26 13:31:40 -07:00
Torin Sandall 4b5a232cd2 Refactor presentation package and docs
Minor changes following eb5e5b243f

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-07-20 17:28:59 -07:00
Ashutosh Narkar eb5e5b243f Add profiler to OPA eval command
Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2018-07-20 16:00:05 -07:00
Ashutosh Narkar 45f589b2a7 Sort profiler result based on criteria to return top N results
Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2018-07-03 10:34:04 -07:00
Ashutosh Narkar 02aa6d3de7 Add profile option to OPA eval cmd
Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2018-07-03 10:34:04 -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 32c45d7064 Add extra output formats to eval subcommand
These output formats are handy for using Rego to extract values from
JSON documents on the command line.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-06-23 12:54:41 -07:00
Torin Sandall b5a223b8a4 Add support for providing input to eval via stdin
In some cases, it's more convenient to pass input via stdin than using
a path or process substitution.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-06-23 12:54:41 -07:00
Torin Sandall eee76c6365 Update subcommands to support loader filter
These changes update the subcommands to support a file/directory name
filter. This allows users to exclude certain files from being loaded.
With these changes users can excldue private directories created by
Kubernetes for volume-mounted ConfigMaps.

As part of this change, update the Kubernetes deployment documentation
to use the new --ignore flag, run OPA as a Deployment instead of as a
ReplicationController, and generally improve the example.

Fixes #782

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-06-22 14:22:14 -07:00
Brendan Devenney 433216a786 Make eval command's data flag repeatable
This allows the specification of multiple data/policy files, mirroring
the old behaviour of the run command's eval flag.

Resolves #713.

Signed-off-by: Brendan Devenney <brendan.devenney@cloudreach.com>
2018-04-28 11:04:55 -07:00
Torin Sandall 75c7fefa0e Fill in eval subcommand help text
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-04-08 12:41:09 -07:00
Torin Sandall 39cc4ef87f Add eval subcommand to run queries
With opa eval, the --eval flag on opa run is redundant and can be
removed.
2018-03-12 18:14:32 -07:00