Skip to content

Commit 3671251

Browse files
authored
docs: improve documentation for scanning raw IaC configurations (aquasecurity#9571)
Signed-off-by: nikpivkin <[email protected]>
1 parent c638fc6 commit 3671251

File tree

2 files changed

+107
-13
lines changed

2 files changed

+107
-13
lines changed

docs/docs/scanner/misconfiguration/custom/index.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,18 @@ Trivy supports extra fields in the `custom` section as described below.
121121
If you are creating checks for your Trivy misconfiguration scans, some fields are optional as referenced in the table below. The `schemas` field should be used to enable policy validation using a built-in schema. It is recommended to use this to ensure your checks are
122122
correct and do not reference incorrect properties/values.
123123

124-
| Field name | Allowed values | Default value | In table | In JSON |
125-
|------------------------------|---------------------------------------------------------------------|:----------------------------:|:--------:|:-------:|
126-
| title | Any characters | N/A |||
127-
| description | Any characters | | - ||
128-
| schemas.input | `schema["kubernetes"]`, `schema["dockerfile"]`, `schema["cloud"]` | (applied to all input types) | - | - |
129-
| custom.id | Any characters | N/A |||
130-
| custom.severity | `LOW`, `MEDIUM`, `HIGH`, `CRITICAL` | UNKNOWN |||
131-
| custom.recommended_actions | Any characters | | - ||
132-
| custom.deprecated | `true`, `false` | `false` | - ||
133-
| custom.input.selector.type | Any item(s) in [this list][source-types] | | - ||
134-
| custom.minimum_trivy_version | The minimum version of Trivy that's required to evaluate this check | | - ||
135-
| url | Any characters | | - ||
124+
| Field name | Allowed values | Default value | In table | In JSON |
125+
|------------------------------|----------------------------------------------------------------------------------------------|:----------------------------:|:--------:|:-------:|
126+
| title | Any characters | N/A |||
127+
| description | Any characters | | - ||
128+
| schemas.input | `schema["kubernetes"]`, `schema["dockerfile"]`, `schema["cloud"]`, `schema["terraform-raw"]` | (applied to all input types) | - | - |
129+
| custom.id | Any characters | N/A |||
130+
| custom.severity | `LOW`, `MEDIUM`, `HIGH`, `CRITICAL` | UNKNOWN |||
131+
| custom.recommended_actions | Any characters | | - ||
132+
| custom.deprecated | `true`, `false` | `false` | - ||
133+
| custom.input.selector.type | Any item(s) in [this list][source-types] | | - ||
134+
| custom.minimum_trivy_version | The minimum version of Trivy that's required to evaluate this check | | - ||
135+
| url | Any characters | | - ||
136136

137137
#### custom.avd_id and custom.id
138138

@@ -224,4 +224,4 @@ See [here](schema.md) for the detail.
224224

225225
[rego]: https://www.openpolicyagent.org/docs/latest/policy-language/
226226
[package]: https://www.openpolicyagent.org/docs/latest/policy-language/#packages
227-
[source-types]: https://github.com/aquasecurity/trivy/blob/9361cdb7e28fd304d6fd2a1091feac64a6786672/pkg/iac/types/sources.go#L4
227+
[source-types]: https://github.com/aquasecurity/trivy/blob/e4af279b29ed5b77ed1d62e31b232b1f9b92ef4f/pkg/iac/types/sources.go#L5-L17

docs/docs/scanner/misconfiguration/index.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,100 @@ Tests: 20 (SUCCESSES: 18, FAILURES: 2)
311311
Failures: 2 (MEDIUM: 2, HIGH: 0, CRITICAL: 0)
312312
```
313313
314+
## Scan raw configurations
315+
IaC configurations from cloud providers such as Terraform, CloudFormation, and ARM are converted into a unified structure that is exported to Rego. Checks are developed only for the unified structure, not for each configuration type with its own structure. This avoids duplication and simplifies maintenance. Using the unified structure has a limitation: it is not possible to create checks for resources or attributes that are not exported.
316+
317+
The `--raw-config-scanners` flag allows scanning the raw configuration — that is, evaluated but not converted into the unified structure. Currently, only `terraform` is supported.
318+
319+
!!! note
320+
The raw configuration scanner does not work on its own. To use `--raw-config-scanners`, you must also specify the corresponding `--misconfig-scanners`. The report will include results from both scanners.
321+
322+
For more information on custom checks and exported data schemas, see [here](../misconfiguration/custom/index.md).
323+
324+
Example check:
325+
```rego
326+
# METADATA
327+
# title: AWS required resource tags
328+
# description: Ensure required tags are set on AWS resources
329+
# scope: package
330+
# schemas:
331+
# - input: schema["terraform-raw"]
332+
# custom:
333+
# id: USR-TFRAW-0001
334+
# severity: CRITICAL
335+
# short_code: required-aws-resource-tags
336+
# recommended_actions: Add the required tags to AWS resources.
337+
# input:
338+
# selector:
339+
# - type: terraform-raw
340+
package user.terraform.required_aws_tags
341+
342+
import rego.v1
343+
344+
resource_types_to_check := {"aws_s3_bucket"}
345+
346+
resources_to_check := {block |
347+
some module in input.modules
348+
some block in module.blocks
349+
block.kind == "resource"
350+
block.type in resource_types_to_check
351+
}
352+
353+
required_tags := {"Access", "Owner"}
354+
355+
deny contains res if {
356+
some block in resources_to_check
357+
not block.attributes.tags
358+
res := result.new(
359+
sprintf("The resource %q does not contain the following required tags: %v", [block.type, required_tags]),
360+
block,
361+
)
362+
}
363+
364+
deny contains res if {
365+
some block in resources_to_check
366+
tags_attr := block.attributes.tags
367+
tags := object.keys(tags_attr.value)
368+
missing_tags := required_tags - tags
369+
count(missing_tags) > 0
370+
res := result.new(
371+
sprintf("The resource %q does not contain the following required tags: %v", [block.type, missing_tags]),
372+
tags_attr,
373+
)
374+
}
375+
```
376+
377+
Running Trivy:
378+
```bash
379+
trivy conf main.tf \
380+
--check-namespaces user \
381+
--config-check examples/terraform-raw/required-aws-tags.rego \
382+
--misconfig-scanners terraform --raw-config-scanners terraform
383+
```
384+
385+
Example output:
386+
```bash
387+
main.tf (terraform)
388+
389+
Tests: 10 (SUCCESSES: 0, FAILURES: 10)
390+
Failures: 10 (UNKNOWN: 0, LOW: 2, MEDIUM: 1, HIGH: 6, CRITICAL: 1)
391+
392+
(CRITICAL): The resource "aws_s3_bucket" does not contain the following required tags: {"Access"}
393+
═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
394+
Ensure required tags are set on AWS resources
395+
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
396+
main.tf:3-5
397+
via main.tf:1-6 (aws_s3_bucket.this)
398+
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
399+
1 resource "aws_s3_bucket" "this" {
400+
2 bucket = "test"
401+
3 ┌ tags = {
402+
4 │ Owner: "user"
403+
5 └ }
404+
6 }
405+
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
406+
```
407+
314408
## External connectivity
315409
Trivy needs to connect to the internet to download the checks bundle. If you are running Trivy in an air-gapped environment, or an tightly controlled network, please refer to the [Advanced Network Scenarios document](../../advanced/air-gap.md).
316410

0 commit comments

Comments
 (0)