Commit Graph

302 Commits

Author SHA1 Message Date
Ashutosh Narkar f48a4563db Bind OPA server to localhost interface by default
Currently OPA binds to the 0.0.0.0 interface by default, which allows
the OPA server to be exposed to services running outside of the same machine.
Though not inherently insecure in a trusted environment, it's good practice
to bind OPA to the localhost interface by default if OPA is not intended
to be exposed to remote services.

This change also adds a new feature flag to `opa run` to allow users to enable
future OPA compatible behavior.

Fixes: #6286

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2023-10-18 08:56:03 -07:00
Anders Eknert 6c3f363de3 Don't use rounding for coverage calculation (#6308)
Perhaps it looks prettier, but this is only reported in
JSON output, so I don't think we should decide on what
precision to use there.

On coverage < threshold error, we still print using 2
decimals, which is Pretty (tm).

Fixes #6307

Signed-off-by: Anders Eknert <anders@eknert.com>
2023-10-12 06:49:09 +02:00
Johan Fylling e01c2b23aa Silencing expected error output in tests (#6276)
to reduce risk of chasing down red herrings when troubleshooting a failing build.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2023-10-05 18:14:36 +02:00
Johan Fylling fb16650d23 Adding lines not covered to test coverage threshold error message (#6272)
Printing lines not covered when test coverage threshold isn't met for `opa test --threshold`, and `--verbose` flag is enabled.

Fixes: #2562
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2023-10-04 15:08:14 +02:00
Charlie Egan cd0bf5b4d2 Add option to marshal location text (#6234) 2023-09-21 09:40:07 +01:00
Ronnie-personal 38733ed746 ast: Add location to single entry rule head ref (#6212)
Fixes: #6199
Signed-off-by: Ronnie Personal <76408835+Ronnie-personal@users.noreply.github.com>
2023-09-18 11:45:51 +02:00
Ronnie-personal 00877d6c03 ast: Accept short-form else bodies (#6204)
Fixes: #6212
Signed-off-by: Ronnie Personal <76408835+Ronnie-personal@users.noreply.github.com>
2023-09-18 10:43:09 +02:00
Ashutosh Narkar 0e69dbba20 Extend type checking for authz policies
The schema of the input document for the authorization
policy is known to OPA. This feature leverages that
to perform automatic type checking on the authorization policy.
The checks happen on policies provided to OPA on start-up and
also those provided via bundles. This check is enabled by default
and can be disabled using the `--skip-known-schema-check` flag
on `opa run`. This feature will help catch errors such as
typos, mismatch types etc. in these policies and provide precise
feedback to the policy author.

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2023-09-11 15:34:10 -07:00
João Paulo G. Brandt 7bf2075c0d cmd: Fix opa test --ignore when used together with --bundle
When using the --bundle flag, the opa test was ignoring (pun intented)
the --ignore flags.

Signed-off-by: João Paulo G. Brandt <joao@miro.com>
2023-08-28 11:45:03 -07:00
Ronnie-personal c3854aa6ae feat(cmd): Adding fail-non-empty flag (#6153)
Add fail-non-empty flag to opa exec

Signed-off-by: Ronnie Personal <76408835+Ronnie-personal@users.noreply.github.com>
2023-08-21 11:30:25 +02:00
Anders Eknert 700e2c97de Bind test server to localhost interface (#6164)
Fixes #6162

Signed-off-by: Anders Eknert <anders@styra.com>
2023-08-17 15:56:35 +02:00
Florian Daguin ca7da22d00 cmd: make opa test -z fail with failing tests
Fixes the failed logic introduced in [1] to compute the exit code of a
test run. When used with the option `--exit-zero-on-skipped`, the
`opa test` command now returns the exit code 0 only if no failed tests
were found.

[1]: https://github.com/open-policy-agent/opa/commit/54f203c3843b23474afd9990787a1affdcaa40c5

Fixes: #6126
BREAKING-CHANGE
Signed-off-by: Florian Daguin <git@fdaguin.dev>
2023-07-28 17:51:53 -07:00
Ashutosh Narkar a3e7d0fdec cmd: Fix formatting of discard subcommand help text
Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2023-07-19 01:08:23 -07:00
26tanishabanik 67159a677c cmd: adds a discard subcommand for format to discard the result Fixes: #5863
Signed-off-by: 26tanishabanik <26tanishabanik@gmail.com>
2023-07-19 00:39:35 -07:00
Charlie Egan d584a15d53 [cmd] Implement rootless deprecation messages
I have added a system for showing fatal and non-fatal deprecation warnings. It's configurable by command and environment.

If we merge this PR, running a rootless image with any OPA command other than `opa run` will result in a fatal error and exit code 1.

It's possible for users to continue to use the image by unsetting: OPA_DOCKER_IMAGE_TAG=rootless.

`opa run` will show the message, but it's not fatal for this command. This is intended to avoid production disruption.

Signed-off-by: Charlie Egan <charlie@styra.com>
2023-07-14 11:38:17 -07:00
Johan Fylling 1e17d6a8de Adding configurable error output Writer to test cmd
For less verbose tests, where some successful tests might get interpreted as failing.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2023-06-26 11:51:43 -07:00
Ashutosh Narkar 7ea3ee034b Add ability to specify namespace for optimized files
Currently the namespace for partially evaluated files
in an optimized bundle cannot be modified. As a result
if more than one optimized bundle is loaded in OPA, a root
conflict error would occur as the optimized bundles have a
root called "partial" automatially added to their
manifest. This change allows the namespace to be configured via
the build command.

Fixes: #5933

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2023-06-25 09:42:39 -07:00
Johan Fylling 304be03e98 cmd: Making test watch-mode tests more robust (#6019)
* Waiting for output buffer to contain expected data rather than making exact matches on the entire content.
* Not aborting watcher on encountered errors

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2023-06-19 10:51:28 +02:00
Stephan Renatus cca81970d1 loader: change extension handler type (#6015)
This

1. changes the extension.Handler type to make it more flexible
2. simplifies the extension usage -- it used to be called in many places,
   but it could all be handled through util.Unmarshal and util.UnmarshalJSON
   instead

We've previously marked it as "EXPERIMENTAL", so we should have enough
leeway to change this now.

NOTE: As a consequence of (2.), we're no longer accepting trailing data for
json files loaded with OPA. I believe it wasn't intentional to ignore bad data
before -- now, it'll be an error.

Signed-off-by: Stephan Renatus <stephan@styra.com>
2023-06-16 09:37:33 +02:00
Boran Seref 59967431cc cmd/test: Update storage when a file remove op is detected
Previously if a data file was removed those changes would not be reflected on the store and OPA would continue using old data to run the test. This change attempts to fix that.

Fixes: #5986

Signed-off-by: boranx <boran.seref@gmail.com>
2023-06-14 13:50:42 -07:00
Ashutosh Narkar 33e20901e1 cmd: Add support for watch mode in opa test
Similar to the watch mode available in OPA when run as a server,
this change adds a watch mode in OPA test which
reloads the policy on file-system changes and re-runs the tests.
The watch mode in OPA test could be useful for example in TDD of
policies.

Fixes: #1719

Co-authored-by: Johan Fylling <johan.dev@fylling.se>
Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2023-06-06 16:59:39 -07:00
Renato abeed9cdf4 Add schema flag to test (#5957)
* test: Add --schema flag to opa test
* runner: added annotation/metadata reading for opa test

Fixes: #5923
Signed-off-by: Renato Cordeiro <renato@renatocordeiro.com>
2023-06-02 12:21:18 +02:00
Eve Ben Ezra a3058cb2b6 fix valid options to --profile-sort help output (#5862) (#5924)
Fixes #5924

Signed-off-by: Eve Ben Ezra <eve.benezra@nytimes.com>
2023-05-16 20:42:55 +02:00
Ashutosh Narkar 1ad567bf00 Add ability to configure Unix socket permissions
Currently if OPA listens on a Unix socket, the socket
file is created with 755 permissions. So if OPA is deployed
on k8s for example and the socket path is shared
via a volume between pods, due to the default permissions,
the socket will not be reachable for the caller. One way around
this is to match the user id for the OPA and caller containers
but that is not always possible.

This change adds a new flag to the OPA runtime that allows
to configure the permission of the socket file. In the
k8s scenario, if the file permission is updated to 777
for instance, the caller will be able to connect to OPA via
the socket.

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2023-05-02 09:07:23 -07:00
Ashutosh Narkar 7f2895ddc8 profiler: Add number of generated expr to profile o/p
The number of EVAL/REDO counts in the profile result
are sometimes difficult to understand. This is mainly due to the
fact that the compiler rewrites expressions and assigns the
same location to each generated expression and the profiler
keys the counters by the location. So users have no idea
that multiple expressions may be contributing to the profile
result for a given line in the policy.

This change attempts to provide more clarity to the profile
output by including the number of generated expressions for
each given expression thereby helping to better understand
the result and also how the evaluation works.

Fixes: #2552

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2023-04-18 23:54:19 -07:00
Gianluca Oldani 1e38591597 parse: include rule head location when requested
Prior to this commit, when the AST information
were serialized to JSON, rules expressed as

discussed in #5790 included only the location
of their value and not the one of the rule
ref var itself. Now the AST serialized to
JSON includes this information.

Fixes: #5790
Signed-off-by: Gianluca Oldani <oldanigianluca@gmail.com>
2023-04-18 16:55:55 -07:00
Peter Macdonald 26abc7e0c9 Add info message when binding to 0.0.0.0 interface on server initialization
run: added addrSetByUser to check if addr flag has been set by the user or not returns a bool to rt.ParamsAddrSetByUser in initRuntime
run_test: updated tests as we pass a bool now to initRuntime, added a test to check that changing the addr flag returns the expected output
runtime: added AddrSetByUser boolean and updated the server initialization info message, if AddrSetByUser is false, it will append an info message to the server initialization
runtime_test: added a test that will check the message is correctly set depending on if AddrSetByUser is true or false
docs/content/security: added paragraph addressing the potential issues of binding opa to the 0.0.0.0 interface

Fixes #5090

Signed-off-by: Peter Macdonald <macdonald.peter90@gmail.com>
2023-04-03 15:51:50 -07:00
Gianluca Oldani 76e5fda8b7 fmt: report wrong arity for built-in functions
This commit fixes the index out of range error discussed in #5646 and adds error
handling to avoid that `fmt` panics when such errors are encountered by the formatter

The error handling procedure introduced is similar to the one used by the Scanner struct
responsible for the parsing of a `rego` file, since it uses a slice which is filled with
all the eventual errors that can be found during the format procedure

Fixes: #5646
Signed-off-by: Gianluca Oldani <oldanigianluca@gmail.com>
2023-03-27 10:45:11 -07:00
Charlie Egan fbfa705063 [ast/opa parse] Support marshalling of all ast location data (#5576)
Signed-off-by: Charlie Egan <charlie@styra.com>
2023-03-09 09:49:30 +00:00
Johan Fylling ef3b8464e2 ast: Enforce inlined schemas without --schemas flag (#5689)
* ast: Enforce inlined schemas without --schemas flag

Enabled for commands:
* eval
* check
* test

Always parse annotations.
Always use `schemas` annotations with inlined schemas for type checking.
Ignore `schemas` annotations with schema refs if no `--schema` flag was provided.

Fixes: #5506

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2023-02-28 18:56:00 +01:00
Anders Eknert 39d0e4ec03 chore: capbilities fixes (#5700)
* Use two spaces to indent JSON, not one
* Fix wrong closing tag in built-in reference

Signed-off-by: Anders Eknert <anders@styra.com>
2023-02-25 01:39:59 +01:00
Anders Eknert 8f5500d078 cli: try fix issues reported in 'oracle' command (#5678)
Try and fix errors issues reported here:
https://github.com/open-policy-agent/vscode-opa/issues/81

1. Only include .rego files in definition location checks
2. Don't hang if input not provided on stdin

Signed-off-by: Anders Eknert <anders@styra.com>
2023-02-22 11:40:41 +01:00
Anders Eknert eb087de363 cli: allow remote bundle URL shorthand for run command (#5675)
Fixes: #5674

Signed-off-by: Anders Eknert <anders@styra.com>
2023-02-21 13:45:51 +01:00
Johan Fylling f93d0f8fea bundle: Retain metadata annotations for Wasm entrypoints during inspection (#5603)
* Pruning METADATA blocks associated with Wasm compiled entrypoints from Rego source in bundle
* Adding metadata annotations to wasm entrypoint declarations in bundle .manifest file
* Reading metadata annotations from both Rego source and .manifest file in bundle during `inspect`

Fixes: #5588
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2023-02-03 12:15:21 +01:00
Johan Fylling 9f7e5e6be6 compile: Allow object generating rules to be annotated as entrypoint (#5578)
Using ground ref of rule when generating entrypoint from annotation
as variable in head suffix cannot be included in generated entrypoint path.

Fixes: #5577
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2023-01-23 21:47:24 +01:00
Johan Fylling d0c50c35f1 docs: Updating entrypoint documentation
with note about transitive dependencies enumerated as "extra" entrypoints.

Fixes: #5565
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2023-01-23 11:31:15 -08:00
Iceber Gu 2131da3d9c remove github.com/pkg/errors
Signed-off-by: Iceber Gu <wei.cai-nat@daocloud.io>
2023-01-11 10:30:48 -08:00
Ashutosh Narkar 00152cbd9d cmd: Copy the loop variable into a new variable
Earlier we were appending the address of the iterator variable
to the slice. This results in unintended behavior of appending
the same address to the slice. We fix this by copying the
loop variable into a new variable and then appending.

Ref: https://github.com/golang/go/wiki/CommonMistakes#using-reference-to-loop-iterator-variable

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2023-01-06 14:12:13 -08:00
Charlie Egan ac64e78c69 topdown/rego: Add BuiltinErrorList support to rego package, add to eval command (#5487)
```
$ cat pol.rego
package play

this_errors(number) := result {
        result := number / 0
}

this_errors_too(number) := result {
        result := number / 0
}

res1 := this_errors(1)

res2 := this_errors_too(1)

$ go run main.go eval --show-builtin-errors -d pol.rego data.play
{
  "errors": [
    {
      "message": "div: divide by zero",
      "code": "eval_builtin_error",
      "location": {
        "file": "pol.rego",
        "row": 4,
        "col": 12
      }
    },
    {
      "message": "div: divide by zero",
      "code": "eval_builtin_error",
      "location": {
        "file": "pol.rego",
        "row": 8,
        "col": 12
      }
    }
  ],
  "result": [
    {
      "expressions": [
        {
          "value": {},
          "text": "data.play",
          "location": {
            "row": 1,
            "col": 1
          }
        }
      ]
    }
  ]
}
```

Signed-off-by: Charlie Egan <charlieegan3@users.noreply.github.com>
2023-01-03 13:25:49 +00:00
Johan Fylling 36fdcef93f ast&cmd: Only enforcing schemas annotations if --schema flag is used (#5502)
Fixing an issue where if a `rego.metadata.*` function is called in a rule, any present `schemas` annotation will be used to inform type checking, even if no `--schema` flag was used for the command.
Only commands that takes the `--schema` flag can now use the `schemas` annotation to inform type checking; i.e. `eval` and `check`.

Fixes: 5430
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2023-01-03 12:16:58 +01:00
Anders Eknert a13b6233bb Allow print calls in IR (#5501)
Since this was unset on the compiler for -t plan, print
calls got erased from the plans. Since this is useful for
debugging plan execution, we should allow print in this
context. If undesired, this can still be disabled via
the capabilities feature.

Signed-off-by: Anders Eknert <anders@eknert.com>
2023-01-02 10:37:49 +01:00
Peter Macdonald 77b6b3f7cb Adding strict flag to opa eval (#5228)
You can now enable strict mode with `opa eval` by passing `--strict` (`-S`).

Fixes #5182.

Signed-off-by: Peter Macdonald <macdonald.peter90@gmail.com>
2022-11-15 09:35:58 +01:00
Philip Conrad 7b6a04eac2 ast/annotations: Add type coercion guards to avoid panics. (#5373)
This commit adds type coercion guards around the AST annotation
set building logic, to avoid panics when nil values appear.

Nil node values can happen here after copy-propagation, because
the AST nodes can be ripped out from under the annotation system
when they are pruned away or inlined into other rules.

Fixes: #5368

Signed-off-by: Philip Conrad <philipaconrad@gmail.com>
2022-11-09 09:17:26 +01:00
Stephan Renatus 549cf4989c cmd/build: don't expect key if alg is non-empty (#5343)
Since `alg` has a default setting, we've gotten that error too often.

Follow-up to #5297 and #4972.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2022-11-02 13:53:34 +01:00
Mathis Joffre a855ab1bd4 cmd/build: exit with failure on empty signing key (#5297)
Fixes #4972.

Signed-off-by: Mathis Joffre <mariusjoffre@gmail.com>
2022-11-02 09:55:11 +01:00
Ashutosh Narkar 88cf5a100b cmd: Extend e2e benchmarking mode to include OPA config (#5335)
Currently the e2e bench mode doesn't support providing an
OPA configuration to enable features like decision logging
that can have an impact on the server overhead. This change
adds a new flag to the bench cmd to specify the OPA configuration.

Fixes: #4899

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2022-11-02 09:18:49 +01:00
Stephan Renatus d238828776 cmd/{build,check}: respect capabilities for parsing (#5326)
Before, the capabilities were plumbled through in most places:

1. checking which builtins exist
2. passed along to the optimizer
3. passed along to the planner

But they hadn't been passed along to the file loader. As such, it could not
pass the caps along to the parser either. This is now done, but adding a new
method to the FileLoader interface.

Fixes #5323.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2022-11-01 18:50:03 +01:00
Bin Liang f4ca3befa1 cmd/test: check coverage limit range (#5291)
It was possible to set this to more than 100 before, causing
the goal to never be met.

Fixes #5284

Signed-off-by: Bin Liang <hzliangbin@foxmail.com>
2022-10-31 09:04:50 +01:00
Anders Eknert 50d4e31d6b chore: Use t.Setenv in tests (#5321)
And enable the `tenv` linter for the future.

Also, bump version of golangci-lint and fix some new
warnings that came from that.

Signed-off-by: Anders Eknert <anders@eknert.com>
2022-10-27 17:44:56 +02:00
Anders Eknert 9a597feb2e chore: don't use the deprecated ioutil functions (#5319)
Another annoyance removed :P

Signed-off-by: Anders Eknert <anders@eknert.com>
2022-10-27 14:30:26 +02:00