Commit Graph

25 Commits

Author SHA1 Message Date
Torin Sandall 6ee2d92a11 Fix test case to wait for correct error
We were seeing intermittent failures on Travis CI for this test case.
The error being delivered was an empty file parse error. With these
changes, the test will wait for up to 1 second to observe the expected
type error.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-03-22 10:57:32 -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
Torin Sandall 39f7b45fb7 Update directory loading convention
Previously, the loader would use directory names as top-level keys when
paths referred to directories. This meant that identical queries against
policies/data in differently named directories would return different
answers.

Now, the loader ignores the first directory name when recursing on
paths. Unfortunately this is not backwards compatible. Scripts and
workflows can be adapted as follows:

Before: opa test *
After:  opa test .

Before: opa test /some/path/to/dir/*
After:  opa test /some/path/to/dir

The same goes for opa run.
2018-03-12 18:14:32 -07:00
Torin Sandall 81e847ba37 Fix file watch bug causing panic in server mode
Previously, the runtime file watcher would not include the policies in
storage when recompiling after a file watch update. As a result, if a
policy snuck into storage "somehow" then the server would panic when it
attempted to reload the policies.

A policy could sneak into storage in two cases...

1) File removed AFTER FS notification sent but BEFORE loader.All()
called by file watcher.

2) File watcher enabled and policies pushed into server via API.

In either case, the policies would need to conflict with each other such
that compilation would fail in the server (e.g., with a type error.)
2017-09-29 10:02:13 -07:00
Torin Sandall 9570a4acc7 Refactor runtime to separate init and start
Previously, the runtime could only be initialized and started in one
shot. In some cases, callers want to be able to separate these into two
steps, especially since the start call will block.
2017-09-19 12:06:18 -07:00
Torin Sandall c612260af4 Refactor file loading for OPA
The file loading logic implemented in the runtime package is generally
useful within OPA. These changes factor the file loading into a separate
package that can be reused without taking a dependency on the runtime.
2017-09-07 11:40:22 -07:00
Matthew Mussomele ca783539f8 Add support to cancel compilation after some number of errors
Sometimes compiling large policies with many errors causes more output
errors than is easily sorted through. The compiler has been updated to
cancel after a configurable number of errors (default no limit), and the
server, repl and check command support options for setting that limit.
2017-07-21 07:59:51 -07:00
Torin Sandall 98d5da7826 Register server for policy change triggers
Users that embed the OPA server can now manage policies more easily via
the storage package instead of having to go through the REST API. This
also makes it trivial to support the --watch flag in server mode.
2017-06-23 13:18:15 -07:00
Torin Sandall ce24a5ae3b Add file watcher integration test 2017-06-23 13:18:15 -07:00
Torin Sandall 10f22906b5 Refactor storage layer interfaces
This is a large change set that contains a few backwards incompatible
changes. Summary of breaking changes:

- Remove storage.Storage in favour of storage.Store interface.
- Remove mount support.
- Remove storage of compiled policies.
- Modify storage.Store to support rollback.
- Modify storage.Store to support raw policy storage.
- Modify storage.Store to support indexing.
2017-06-23 13:18:15 -07:00
Torin Sandall b1fc681590 Remove persist/--policy-dir option
This is the first in series of Spring cleaning around the storage layer.
In the near future we will add local disk-based persistence support to
OPA. That support will handle storage of source files.

The --policy-dir option is almost entirely unused today. Removing it
will make it easier to get rid of the policyStore entirely.

The next thing to do will be to remove the specialized *Policy methods
from the storage layer. This way the storage layer can just accept
policies as normal data.

If policies need to be persisted until then, users can treat the
policies as config files and manage them outside of OPA.
2017-03-12 13:23:22 -07:00
Torin Sandall 6933a84482 Strip slashes in file loader
The REST API now supports slashes *inside* the path but the HTTP server
will not allow leading or trailing slashes (it redirects to a URL w/o
them). Because of this, the runtime should not add policies with these
leading or trailing slashes, as API callers won't be able to access
them.
2017-03-12 13:23:22 -07:00
Torin Sandall 49a963f16a Fix test cases to work with new syntax 2017-02-10 10:37:43 -08:00
Torin Sandall 86969090c6 Fix handling of prefixed paths with -w flag
Fixes #193
2016-12-13 18:44:16 -08:00
Torin Sandall c3d1542754 Update storage to support context.Context
This includes all of the changes to plumb the context through from the HTTP
server and the REPL.

Also, this removes the Travis CI build for Go 1.6 as the context package is
not part of the standard library before Go 1.7. Once Go 1.8 is released we can
go back to supporting the previous Go release.

Fixes #155
2016-12-07 14:18:15 -08:00
Torin Sandall fe25593707 Update underlying number representation
These changes modify OPA to use json.Number to represent number values in
storage and the AST. Comparisons and numeric operations are handled by the
math/big package. Using json.Number avoids loss of precision when loading
integers greater than 2**53 and use of math/big avoids need for type
conversions in the built-in implementations. In the long run, the math/big
usage may be replaced with specific paths for smaller numbers.

Fixes #154
2016-12-05 15:30:38 -08:00
Torin Sandall 26cd8e59ec Updates to use new storage.Path type
These changes refactor the storage layer to use storage.Path instead of ast.Ref
for Read/Write/Begin/Unmount/Mount operations.

Previously, the storage layer used ast.Ref values to refer to locations in
storage. Use of ast.Ref introduced unnecessary complexity for storage plugins
as they had to be aware of various details (e.g., array indices specified as
ast.Number/float64 values, potentially nested references, etc.) that were
unnecessary given that ast.Ref values passed to the storage layer were
intended represent JSON pointers.

These changes also remove the need for storage plugins to be aware of where
they are mounted. That is, the paths passed to the read call will be relative
to the mount point.

The remaining dependencies on the ast package from the storage package are for
(1) indexing and (2) policy storage. It may be possible to further decouple
these packages by revisiting how indexing is done and treating policies as
blobs.

Fixes #159
2016-12-02 10:29:33 -08:00
Torin Sandall 55768aba53 Add command line flag to eval, print, and exit
Can now evaluate queries from the command line, for example:

$ opa run -f json -e 'data.repl.version[x] = y'

Fixes #152
2016-11-25 10:27:01 -08:00
Torin Sandall 1a01fe76d1 Deep copy modules in compiler
Previously, the compiler would mutate the parsed modules and store them as
output. This made it difficult to reason about the state of modules
constructed programatically in places like the REPL.

This change adds a Copy() function to the AST types that are stored by
reference. This allows the compiler to deep copy the parsed modules before
operating on them. This removes the challenge of reasoning about how the
compiler will behave when compiling the same input multiple times.

Various callers had to be modified to deal with the new calling convention.
Unit tests have been added to verify the new behaviour.

Fixes #158
2016-11-23 09:13:12 -08:00
Torin Sandall 851711cfbb Remove unnecessary calls to storage.InsertPolicy
Previously, rules had to be installed into the built-in store in order for the
topdown implementation to access them. Now that the topdown implementation
receives an instance of the compiler, the rules do not have to be installed
into the built-in store anymore.
2016-08-30 12:06:47 -07:00
Torin Sandall f6d9904e83 Refactor to remove old storage dependencies
This is a large set of refactoring changes made to shrink the surface area of
the storage layer APIs. With these changes, PolicyStore and DataStore are no
longer directly used outside of the storage package.
2016-08-24 12:46:44 -07:00
Torin Sandall a6416e9e32 Refactor Runtime to make initialization private 2016-08-08 09:02:00 -07:00
Torin Sandall 3d678db610 Check body for unsafe variables and reorder
Also made miscellaneous changes outside the ast package to deal with the new
behaviour.
2016-05-17 12:43:16 -07:00
Torin Sandall 51527395b3 Move storage code into storage package
- Move storage-related code out of eval
- Rename old Storage type to DataStore
- A few miscellaneous renamings for store pointers
2016-05-13 17:37:35 -07:00
Torin Sandall 12dad370d5 Add basic REST API support to server mode
- REST APIs

    * CRUDL on policy modules
    * Ad-hoc queries
    * Query and patch base documents
    * Query virtual documents

- Add PolicyStore to manage policy definition/module CRUDL operations.

    * Supports persistence of policy definitons.
    * Serve REST API CRUDL operations.
    * Manage install/uninstall of rules into data store.
    * Manage persistence of policy definitions.

- Misc. refactoring

    * Move storage creation into runtime Init.
    * Make AST types JSON serializable. Tweaked ast.Import to use Term instead
    of Value for the path.
2016-05-13 15:34:02 -07:00