Skip to content
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

Merged
merged 7 commits into from
Jul 28, 2021

Conversation

Vasu1105
Copy link
Contributor

@Vasu1105 Vasu1105 commented Jul 8, 2021

Signed-off-by: Vasu1105 vasundhara.jagdale@chef.io

Description

This adds support for OPA by adding opa_cli resource and opa_api resource.

  • It assumes that opa command is executable on the targeted system.
  • It assumes the data file and the policy file required for opa_cli is present on the target system
  • For opa_api resource use need to provide OPA server URL and the data to be used.
  • Have both Linux and Windows support

opa cli gives the result in the following JSON format

$ opa eval -i input.json -d example.rego "data.example.allow"
{
  "result": [
    {
      "expressions": [
        {
          "value": false,
          "text": "data.example.allow",
          "location": {
            "row": 1,
            "col": 1
          }
        }
      ]
    }
  ]
}

another example

$ opa eval -i input.json -d example.rego "data.example.violation[x]"
{
  "result": [
    {
      "expressions": [
        {
          "value": "ci",
          "text": "data.example.violation[x]",
          "location": {
            "row": 1,
            "col": 1
          }
        }
      ],
      "bindings": {
        "x": "ci"
      }
    },
    {
      "expressions": [
        {
          "value": "busybox",
          "text": "data.example.violation[x]",
          "location": {
            "row": 1,
            "col": 1
          }
        }
      ],
      "bindings": {
        "x": "busybox"
      }
    }
  ]
}

OPA API gives output in the following format

$ curl -X POST localhost:8181/v1/data/example/violation -d @v1-data-input.json -H 'Content-Type: application/json'
{"result":["ci","busybox"]}
curl -X POST localhost:8181/v1/data/example/allow -d @v1-data-input.json -H 'Content-Type: application/json'
{"result":false}

local tested examples

describe opa_cli(query: "data.example.violation[x]", policy: "/path-to-policy-file/example.rego", data: "path-to-data-file/input.json") do
  its(["result", 0, "expressions", 0, "value"]) { should eq 'ci' }
  its(["result", 1, "expressions", 0, "value"]) { should eq 'busybox' }
end

describe opa_cli(query: "data.example.allow", policy: "/path-to-policy-file/example.rego", data: "path-to-data-file/input.json") do
  its(["result", 0, "expressions", 0, "value"]) { should eq true }
  its(["result", 0, "expressions", 0, "value"]) { should eq false }
  its("allow") { should eq false }
  its("allow") { should cmp "false" }
end

On windows

describe opa_cli(query: "data.example.violation[x]", policy: "C:\\Users\\Vasundhara\\workspace\\opa-work\\example.rego", data: "C:\\Users\\Vasundhara\\workspace\\opa-work\\input.json") do
  its(["result", 0, "expressions", 0, "value"]) { should eq 'ci' }
  its(["result", 1, "expressions", 0, "value"]) { should eq 'busybox' }
end

describe opa_cli(query: "data.example.allow", policy: "C:\\Users\\Vasundhara\\workspace\\opa-work\\example.rego", data: "C:\\Users\\Vasundhara\\workspace\\opa-work\\input.json") do
  its(["result", 0, "expressions", 0, "value"]) { should eq true }
  its(["result", 0, "expressions", 0, "value"]) { should eq false }
  its("allow") { should eq false }
  its("allow") { should cmp "false" }
end

Related Issue

Fix #5575

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New content (non-breaking change)
  • Breaking change (a content change which would break existing functionality or processes)

Checklist:

  • I have read the CONTRIBUTING document.

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
@Vasu1105 Vasu1105 requested a review from a team as a code owner July 8, 2021 13:14
@Vasu1105 Vasu1105 requested review from alexpop and Nik08 July 8, 2021 13:14
@netlify
Copy link

netlify bot commented Jul 8, 2021

✔️ 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

@Vasu1105 Vasu1105 changed the title Add support for OPA: add resource opa_cli and opa_api [W.I.P] Add support for OPA: add resource opa_cli and opa_api Jul 8, 2021
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?
Copy link
Contributor

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".

@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?
Copy link
Contributor

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 ||.

Copy link
Contributor

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?

def load_result
raise Inspec::Exceptions::ResourceFailed, "#{resource_exception_message}" if resource_failed?

result = inspec.command("opa eval -i '#{@data}' -d '#{@policy}' '#{@query}'")
Copy link
Contributor

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.

Copy link
Contributor Author

@Vasu1105 Vasu1105 Jul 9, 2021

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>
@Vasu1105 Vasu1105 requested a review from a team as a code owner July 9, 2021 10:21
Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
@Vasu1105 Vasu1105 changed the title [W.I.P] Add support for OPA: add resource opa_cli and opa_api Add support for OPA: add resource opa_cli and opa_api Jul 19, 2021
Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
@IanMadd IanMadd added the Documentation ZH multi-repo label for the docs-team label Jul 19, 2021
@Nik08
Copy link
Contributor

Nik08 commented Jul 20, 2021

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Contributor

@IanMadd IanMadd left a 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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>
@sonarcloud
Copy link

sonarcloud bot commented Jul 27, 2021

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@Vasu1105
Copy link
Contributor Author

Thanks, @IanMadd. Updated the docs.

@clintoncwolfe clintoncwolfe merged commit e1f63cc into master Jul 28, 2021
[menu]
[menu.inspec]
title = "opa_api"
identifier = "inspec/resources/os/opa_api.md mongodb_conf resource"
Copy link
Contributor

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 ?

Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Core Resources Resources shipped with InSpec. Documentation ZH multi-repo label for the docs-team Expeditor: Bump Minor Version Type: New Feature Adds new functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OPA Support
5 participants