Commit Graph

28 Commits

Author SHA1 Message Date
Drew Wells 726271753e file watching only watches parent directory
Watching files only works in situations where standard files are in
use. In k8s, configmaps are mounted via a set of symlinks. In those
situations, you will only get file events when watching the directory
containing the symlink.

Fixes #2588

Signed-off-by: Drew Wells <drew.wells00@gmail.com>
2020-09-02 09:30:40 -07:00
Ashutosh Narkar 338583c18a Add support for OPA bundle signatures
These changes add support for digital signatures for policy bundles which
can be used to verify their authenticity.

Bundle signature verification involves the following steps:

* Verify the JWT signature
* Verify the files in the JWT payload exist in the bundle
* Verify the file content of the files in bundle match with those in the payload

This commit adds a new `sign` command to generate a digital signature for policy bundles.

For more details, run "opa sign --help"

The signatures generated by the 'sign' command can be verified by the
'build' command. The 'build' command can also sign the bundle it generates.

The 'run' command can verify a signed bundle or skip verification altogether.

OPA 'sign', 'build' and 'run' can be used to
sign/verify bundles in bundle mode (--bundle) mode only. Verification
can be also be performed when bundle downloading is enabled.

Fixes: #1757

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2020-07-14 09:49:59 -04:00
Torin Sandall 0c8463e09b bundle: Add support for merging and other improvements
This commit does a few things:

* Extend the low-level file loader and callers to ensure ModuleFile
objects have a URL field. This means we can tell if two ModuleFile
objects refer to the same source file. Previously we had to keep track
of bundle names to do this. This commit does not touch the bundle
activation logic which does this already--we can revisit that in the
future.

* Update the bundle writer to use the URL field by default. This
allows us to write merged bundles correctly.

* Add a merge function that will be used by the new build command to
combine multiple bundles.

* Add a helper function for checking if roots contain a path. In the
process refactor the overlap helper to use the same logic. Also add a
helper function to ensure roots exist in the bundle manifest.

* Add a deepcopy function for bundles. This is useful if mutating
  bundles and there is a need to revert.

* Format bundle contents on write by default. Callers of the old
Write function will not be affected (it disables formatting.)

* Format the overlap error to quote roots in case they are empty.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2020-05-18 08:32:38 -04:00
Patrick East 55b474a4fe ast: Return an error when parsing an empty module
The documentation is pretty clear that a module must at least contain
a package, so it is safe to say that an empty file isn't valid.

Previously the helper would just return a nil module and nil error, it
will now return an error.

Fixes: #2054
Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-02-21 11:53:22 -08:00
Patrick East 3d8389e9b5 bundle: Use full system path for bundle dir
When reading a bundle from a directory we should be using the full
system path for the module files. This helps greatly with reducing
complexity of trying to read error messages. It also makes the
integration with tools like VSCode work better as they can provide
links from the console output file paths to the file in question.

This will not affect bundles loaded from tarballs.

Fixes: #1796
Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-12-18 11:08:01 -08:00
Patrick East 9c85dfc6fc Plumb metrics through loader and bundle helpers
This corrects the missing time in rego_module_parse timers as we now
have metrics collecting info as we parse *.rego files from file
loaders and from bundles as they are unpacked.

It also adds in a timer for the data files that are loaded through
similar mechanisms.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-12-12 10:31:22 -08:00
Patrick East 8688bc0ff6 bundle: Make the DirectoryLoader public
Previously we had it in an internal package but used by a public API,
which basically means it can't actually be used outside of OPA.
Initial thinking was that this was an OK situation, but by popular
demand we are making it available to everyone so OPA as a lib users
can use the bundle loading API's.

Fixes: #1840
Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-11-14 10:49:05 -08:00
Patrick East 25cd90da54 bundle: Add more details to manifest root errors
Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-10-28 09:59:24 -07:00
Patrick East f1b9c7586b Ensure all errors are in JSON formatted CLI output
Previously if the errors passed into the presentation Output were not
structured w/ JSON tags for marshaling the error would be an empty
string.

This changes to wrap the errors with a struct in cases where they
would otherwise not be formatted. We do this by forcing every error
into a structure and translating known error types into it.

Fixes: #1726
Fixes: #1724
Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-09-27 10:55:31 -04:00
Patrick East 76d1045a9a loader: Move merge helper to internal pkg
Make the private helper exposed internally as a utility function to
help merge data trees.

Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-09-20 12:36:21 -07:00
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 a14208496c loader: Support for loading bundle dirs
This adds a new API to load a bundle from a path which can be either
a tarball file or a directory to load as a bundle.

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
Guangming Wang 952437ebe3 cleanup: fix some typos in code comment
Signed-off-by: Guangming Wang <guangming.wang@daocloud.io>
2019-08-21 09:24:37 -04:00
Torin Sandall 7a4e82194a bundle: Add support for scoping bundle to subset of data
Previously, when OPA activated a bundle, it would erase ALL existing
policy and data that had been cached. This meant that the bundles and
components like kube-mgmt were mutually exclusive (because the
bundles would overwrite the other component's policy and data.)

With these changes, bundles can include a set of roots that scope the
bundle. When the bundle activates, only policy and data under those
roots are erased and overwitten.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-02-19 10:51:19 -08:00
Ashutosh Narkar 7525de0be3 Add manifest metadata to bundle data (#1079)
Fixes #1062

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2018-11-26 11:04:09 -08:00
Torin Sandall c5171ed506 Add extra helpers to loader result
This avoids the usual boilerplate of working with the loader results.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-09-27 15:02:47 -07:00
Torin Sandall 0c66566673 Add support for command line bundle loading
Previously OPA would only load JSON/YAML/Rego files off the command
line. With these changes, OPA will load .tar.gz files and interpret them
as bundles. This is useful if you want to test your bundles locally with
OPA without running OPA as a server, configuring it to pull down the
bundle, etc.

Also, update docs to mention that data files MUST be named data.json.

Fixes #870
Fixes #873

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-08-08 16:10:34 -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 be255c768e Add filtering to loader
These changes add filtering support to the loader. Users of the loader
package can provide a function that will filter the files included in
the loader.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-06-22 14:22:14 -07:00
Torin Sandall 942e870048 Refactor file loading to remove duplication
The file loader logic was unnecessarily duplicated across the top-level
file case and the recursive file case. These changes refactor the
implementation so that the logic is in one place and the file walker has
access to the depth which could be used in the future to place a limit
on recursion depth.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-06-22 14:22:14 -07:00
Stephan Renatus 2f1526c672 fix misspell
Signed-off-by: Stephan Renatus <srenatus@chef.io>
2018-06-05 09:50:13 -07:00
Torin Sandall b4c94fc108 Fix check subcommand parse error printing
The check subcommand was not formatting parse errors as JSON if
requested. Also, since the loader package returns a set of errors, parse
errors are unpacked to avoid double nesting.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-04-09 09:51:00 -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
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 c62d8129fb Expose path cleanup helper in loader pkg 2017-09-29 10:02:13 -07:00
Torin Sandall 9d21c8d564 Refactor loader to read inputs once
Previously, the loader would read the input file for each guess. For
non-file inputs (e.g., process substitution) this would fail. Now you
can pipe command output into OPA over the command line!

$ opa run <(echo '{"foo":"bar"}') -e 'data.foo'
"bar"
2017-09-12 12:19:53 -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