-
Notifications
You must be signed in to change notification settings - Fork 682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for OPA: add resource opa_cli and opa_api #5592
Conversation
Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
✔️ Deploy Preview for chef-inspec ready! 🔨 Explore the source changes: da9693a 🔍 Inspect the deploy log: https://app.netlify.com/sites/chef-inspec/deploys/60ffabc592bbb90008cba526 😎 Browse the preview: https://deploy-preview-5592--chef-inspec.netlify.app |
lib/inspec/resources/opa_api.rb
Outdated
def initialize(opts={}) | ||
@url = opts[:url] | ||
@data = opts[:data] | ||
fail_resource "policy and data are the mandatory for executing OPA." if @url.nil? && @data.nil? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be an ||
, and also you should update the message to be "url and data are mandatory", not "policy and data".
lib/inspec/resources/opa_cli.rb
Outdated
@policy = opts[:policy] || nil | ||
@data = opts[:data] || nil | ||
@query = opts[:query] || nil | ||
fail_resource "policy and data are the mandatory for executing OPA." if @policy.nil? && @data.nil? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"policy and data are mandataory..." - no "the".
Also, this should probably be a ||
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't query mandatory as well?
lib/inspec/resources/opa_cli.rb
Outdated
def load_result | ||
raise Inspec::Exceptions::ResourceFailed, "#{resource_exception_message}" if resource_failed? | ||
|
||
result = inspec.command("opa eval -i '#{@data}' -d '#{@policy}' '#{@query}'") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You will probably want to make the path to the opa command an option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was assuming that the user will export that in the PATH variable but this can be good option too. Should we consider "opa" as the default value for the path? considering that the user sets the path for executable in PATH variable ?
Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
Looks good to me! |
|
||
- `'url'` specifies the url of the OPA server on which OPA is running. | ||
- `'data'` specifies the json formatted data or json file. | ||
- `its(["result"]) { should eq 'value' }` compares the results of the query against the expected result in the test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- `its(["result"]) { should eq 'value' }` compares the results of the query against the expected result in the test | |
- `its(["returned_result"]) { should eq 'expected_result' }` compares the results of the query against the expected result in the test. |
parent = "inspec/resources/os" | ||
+++ | ||
|
||
Use the `opa_api` Chef InSpec audit resource to query the OPA using the OPA url and data. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the `opa_api` Chef InSpec audit resource to query the OPA using the OPA url and data. | |
Use the `opa_api` Chef InSpec audit resource to query Open Policy Agent (OPA) using the OPA URL and data. |
|
||
## Syntax | ||
|
||
A `opa_api` resource |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A `opa_api` resource | |
An `opa_api` resource block declares OPA policy configurations that can be tested. |
|
||
## parameters | ||
|
||
`opa_api` resource InSpec resource accepts `url` and `data` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`opa_api` resource InSpec resource accepts `url` and `data` | |
The `opa_api` resource InSpec resource requires a `url` and `data` as a JSON file or a string in JSON format. |
|
||
### `url` _(required)_ | ||
|
||
URL of the OPA API server. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
URL of the OPA API server. | |
The URL of the OPA API server. |
|
||
### `data` _(required)_ | ||
|
||
This accepts input.json file or input data in json format. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This accepts input.json file or input data in json format. | |
An OPA query as a JSON data file or a string in JSON format. |
its("allow") { should eq "true" } | ||
end | ||
|
||
Above example shows how `allow` value can be fetched in 2 ways. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Above example shows how `allow` value can be fetched in 2 ways. | |
The above example shows how the `allow` value can be fetched in two ways. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docs suggestions
parent = "inspec/resources/os" | ||
+++ | ||
|
||
Use the `opa_cli` Chef InSpec audit resource to query the OPA using the OPA policy file, data file and query. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the `opa_cli` Chef InSpec audit resource to query the OPA using the OPA policy file, data file and query. | |
Use the `opa_cli` Chef InSpec audit resource to query Open Policy Agent (OPA) using an OPA policy file, a data file, and a query. |
|
||
## Syntax | ||
|
||
A `opa_cli` resource |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A `opa_cli` resource | |
An `opa_cli` resource block declares OPA policy configurations that can be tested. |
|
||
## parameters | ||
|
||
`opa_cli` resource InSpec resource accepts `policy`, `data`, `query` and `opa_executable_path` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`opa_cli` resource InSpec resource accepts `policy`, `data`, `query` and `opa_executable_path` | |
The `opa_cli` resource InSpec resource accepts `policy`, `data`, `query`, and `opa_executable_path` as parameters. |
|
||
### `policy` _(required)_ | ||
|
||
Path to the OPA policy file. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Path to the OPA policy file. | |
The path to the OPA policy file. |
|
||
### `data` _(required)_ | ||
|
||
This accepts input.json file or input data in json format. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This accepts input.json file or input data in json format. | |
An OPA query as a JSON data file or a string in JSON format. |
|
||
### `query` _(required)_ | ||
|
||
Query input required to be evaluated against policy and input data. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Query input required to be evaluated against policy and input data. | |
The query to be evaluated against policy and input data. |
|
||
### `opa_executable_path` | ||
|
||
This is the full path to the OPA bindary or exe file used for running opa cli or opa commands. Default it will consider that the path is added in PATH variable. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the full path to the OPA bindary or exe file used for running opa cli or opa commands. Default it will consider that the path is added in PATH variable. | |
This is the full path to the OPA binary or EXE file used for running the OPA CLI or OPA commands. By default it will consider that the path is added in PATH variable. |
|
||
## Examples | ||
|
||
The following examples show how to use this Chef InSpec audit resource. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The following examples show how to use this Chef InSpec audit resource. | |
The following examples show how to use this Chef InSpec audit resource: |
its("allow") { should eq "true" } | ||
end | ||
|
||
Above example shows how `allow` value can be fetched in 2 ways. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Above example shows how `allow` value can be fetched in 2 ways. | |
The above example shows how the `allow` value can be fetched in two ways. |
Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
Thanks, @IanMadd. Updated the docs. |
[menu] | ||
[menu.inspec] | ||
title = "opa_api" | ||
identifier = "inspec/resources/os/opa_api.md mongodb_conf resource" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be mongodb_conf @IanMadd ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @tas50. I have created PR fot that.
Signed-off-by: Vasu1105 vasundhara.jagdale@chef.io
Description
This adds support for OPA by adding opa_cli resource and opa_api resource.
opa cli gives the result in the following JSON format
another example
OPA API gives output in the following format
local tested examples
On windows
Related Issue
Fix #5575
Types of changes
Checklist: