-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add webapp docs and fix related issues (#163)
* add webapp docs and fix related issues Signed-off-by: Jim Bugwadia <jim@nirmata.com> * update status Signed-off-by: Jim Bugwadia <jim@nirmata.com> * update chart meta Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fixes Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * fix tests Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> --------- Signed-off-by: Jim Bugwadia <jim@nirmata.com> Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> Co-authored-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
- Loading branch information
1 parent
e949554
commit 005eb71
Showing
22 changed files
with
253 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,69 @@ | ||
package scan | ||
|
||
import ( | ||
"github.com/kyverno/kyverno-json/pkg/apis/v1alpha1" | ||
"net/http" | ||
|
||
jsonengine "github.com/kyverno/kyverno-json/pkg/json-engine" | ||
) | ||
|
||
type Response struct { | ||
Results []Result `json:"results"` | ||
} | ||
|
||
type PolicyResult string | ||
|
||
type Result struct { | ||
Policy *v1alpha1.ValidatingPolicy `json:"policy"` | ||
Rule v1alpha1.ValidatingRule `json:"rule"` | ||
Resource interface{} `json:"resource"` | ||
Failure error `json:"failure"` | ||
Error error `json:"error"` | ||
PolicyName string `json:"policy"` | ||
RuleName string `json:"rule"` | ||
Result PolicyResult `json:"status"` | ||
Message string `json:"message"` | ||
} | ||
|
||
func makeResponse(responses ...jsonengine.JsonEngineResponse) *Response { | ||
// Status specifies state of a policy result | ||
const ( | ||
StatusPass PolicyResult = "pass" | ||
StatusFail PolicyResult = "fail" | ||
StatusWarn PolicyResult = "warn" | ||
StatusError PolicyResult = "error" | ||
StatusSkip PolicyResult = "skip" | ||
) | ||
|
||
func makeResponse(responses ...jsonengine.JsonEngineResponse) (*Response, int) { | ||
var response Response | ||
for _, result := range responses { | ||
response.Results = append(response.Results, Result(result)) | ||
failCount := 0 | ||
errorCount := 0 | ||
for _, r := range responses { | ||
status, msg := getStatusAndMessage(r) | ||
if status == StatusError { | ||
errorCount++ | ||
} else if status == StatusFail { | ||
failCount++ | ||
} | ||
|
||
response.Results = append(response.Results, Result{ | ||
PolicyName: r.Policy.Name, | ||
RuleName: r.Rule.Name, | ||
Result: status, | ||
Message: msg, | ||
}) | ||
} | ||
|
||
httpStatus := http.StatusOK | ||
if failCount > 0 { | ||
httpStatus = http.StatusForbidden | ||
} else if errorCount > 0 { | ||
httpStatus = http.StatusNotAcceptable | ||
} | ||
|
||
return &response, httpStatus | ||
} | ||
|
||
func getStatusAndMessage(r jsonengine.JsonEngineResponse) (PolicyResult, string) { | ||
if r.Error != nil { | ||
return StatusError, r.Error.Error() | ||
} | ||
if r.Failure != nil { | ||
return StatusFail, r.Failure.Error() | ||
} | ||
return &response | ||
return StatusPass, "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,92 @@ | ||
# Usage | ||
# Overview | ||
|
||
tbd... | ||
The `kyverno-json` Command Line Interface (CLI) can be used to: | ||
|
||
## Pre-processing | ||
* scan JSON or YAML files | ||
* launch a web application with a REST API | ||
* launch a playground | ||
|
||
Additionally, you can provide preprocessing queries in [jmespath](https://jmespath.site) format to pre-process the input payload before evaluating *resources* against policies. | ||
Here is an example of scanning an Terraform plan that creates an S3 bucket: | ||
|
||
This is necessary if the input payload is not what you want to directly analyse. | ||
```sh | ||
./kyverno-json scan --policy test/commands/scan/tf-s3/policy.yaml --payload test/commands/scan/tf-s3/payload.json | ||
``` | ||
|
||
The output looks like: | ||
|
||
```sh | ||
Loading policies ... | ||
Loading payload ... | ||
Pre processing ... | ||
Running ( evaluating 1 resource against 1 policy ) ... | ||
- s3 / check-tags / (unknown) FAILED: all[0].check.planned_values.root_module.~.resources[0].values.(keys(tags_all)).(contains(@, 'Team')): Invalid value: false: Expected value: true | ||
Done | ||
``` | ||
|
||
## Installation | ||
|
||
See [Install](../install.md) for the available options to install the CLI. | ||
|
||
## Pre-processing payloads | ||
|
||
You can provide preprocessing queries in [jmespath](https://jmespath.site) format to pre-process the input payload before evaluating *resources* against policies. | ||
|
||
This is necessary if the input payload is not what you want to directly analyze. | ||
|
||
For example, here is a partial JSON which was produced by converting a Terraform plan that creates an EC2 instance: | ||
|
||
[kyverno/kyverno-json/main/test/commands/scan/tf-ec2/payload.json](https://github.com/kyverno/kyverno-json/blob/main/test/commands/scan/tf-ec2/payload.json) | ||
|
||
```json | ||
{ | ||
"format_version": "1.2", | ||
"terraform_version": "1.5.7", | ||
"planned_values": { | ||
"root_module": { | ||
"resources": [ | ||
{ | ||
"address": "aws_instance.app_server", | ||
"mode": "managed", | ||
"type": "aws_instance", | ||
"name": "app_server", | ||
"provider_name": "registry.terraform.io/hashicorp/aws", | ||
"schema_version": 1, | ||
"values": { | ||
"ami": "ami-830c94e3", | ||
"credit_specification": [], | ||
"get_password_data": false, | ||
"hibernation": null, | ||
"instance_type": "t2.micro", | ||
"launch_template": [], | ||
"source_dest_check": true, | ||
"tags": { | ||
"Name": "ExampleAppServerInstance" | ||
}, | ||
"tags_all": { | ||
"Name": "ExampleAppServerInstance" | ||
}, | ||
"timeouts": null, | ||
"user_data_replace_on_change": false, | ||
"volume_tags": null | ||
}, | ||
|
||
... | ||
|
||
``` | ||
|
||
To directly scan the `resources` element use `--pre-process planned_values.root_module.resources` as follows: | ||
|
||
```sh | ||
./kyverno-json scan --policy test/commands/scan/tf-ec2/policy.yaml --payload test/commands/scan/tf-ec2/payload.json --pre-process planned_values.root_module.resources | ||
``` | ||
|
||
This command will produce the output: | ||
|
||
```sh | ||
Loading policies ... | ||
Loading payload ... | ||
Pre processing ... | ||
Running ( evaluating 1 resource against 1 policy ) ... | ||
- required-ec2-tags / require-team-tag / (unknown) PASSED | ||
Done | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.