| 39 | type InputValidationFunc func(context.Context, string, policy.Policy, bool) (*output.Output, error) |
| 40 | |
| 41 | func validateInputCmd(validate InputValidationFunc) *cobra.Command { |
| 42 | data := struct { |
| 43 | effectiveTime string |
| 44 | filePaths []string |
| 45 | filterType string |
| 46 | forceColor bool |
| 47 | info bool |
| 48 | namespaces []string |
| 49 | noColor bool |
| 50 | output []string |
| 51 | policy policy.Policy |
| 52 | policyConfiguration string |
| 53 | strict bool |
| 54 | workers int |
| 55 | }{ |
| 56 | strict: true, |
| 57 | workers: 5, |
| 58 | filterType: "include-exclude", // Default to include-exclude filter |
| 59 | } |
| 60 | cmd := &cobra.Command{ |
| 61 | Use: "input", |
| 62 | Short: "Validate arbitrary JSON or yaml file input conformance with the provided policies", |
| 63 | Long: hd.Doc(` |
| 64 | Validate conformance of arbitrary JSON or yaml file input with the provided policies |
| 65 | |
| 66 | For each file, validation is performed to determine if the file conforms to rego policies |
| 67 | defined in the EnterpriseContractPolicy. |
| 68 | `), |
| 69 | Example: hd.Doc(` |
| 70 | Use an EnterpriseContractPolicy spec from a local YAML file to validate a single file |
| 71 | ec validate input --file /path/to/file.json --policy my-policy.yaml |
| 72 | |
| 73 | Use an EnterpriseContractPolicy spec from a local YAML file to validate multiple files |
| 74 | The file flag can be repeated for multiple input files. |
| 75 | ec validate input --file /path/to/file.yaml --file /path/to/file2.yaml --policy my-policy.yaml |
| 76 | |
| 77 | Use an EnterpriseContractPolicy spec from a local YAML file to validate multiple files |
| 78 | The file flag can take a comma separated series of files. |
| 79 | ec validate input --file="/path/to/file.json,/path/to/file2.json" --policy my-policy.yaml |
| 80 | |
| 81 | Use a git url for the policy configuration. In the first example there should be a '.ec/policy.yaml' |
| 82 | or a 'policy.yaml' inside a directory called 'default' in the top level of the git repo. In the second |
| 83 | example there should be a '.ec/policy.yaml' or a 'policy.yaml' file in the top level |
| 84 | of the git repo. For git repos not hosted on 'github.com' or 'gitlab.com', prefix the url with |
| 85 | 'git::'. For the policy configuration files you can use json instead of yaml if you prefer. |
| 86 | |
| 87 | ec validate input --file /path/to/file.json --policy github.com/user/repo//default?ref=main |
| 88 | |
| 89 | ec validate input --file /path/to/file.yaml --policy github.com/user/repo |
| 90 | |
| 91 | `), |
| 92 | PreRunE: func(cmd *cobra.Command, args []string) (allErrors error) { |
| 93 | ctx := cmd.Context() |
| 94 | |
| 95 | policyConfiguration, err := validate_utils.GetPolicyConfig(ctx, data.policyConfiguration) |
| 96 | if err != nil { |
| 97 | allErrors = errors.Join(allErrors, err) |
| 98 | return |