Skip to content

Commit d639c33

Browse files
committed
docs: improve skipping files documentation
- improve general structure and readability - rename doc title to more appropriate (it's not just about skipping) - document file pattern TF limitation - document glob pattern syntax - mention rootfs perf optimization here since it's related
1 parent 0229eb7 commit d639c33

File tree

3 files changed

+59
-72
lines changed

3 files changed

+59
-72
lines changed

docs/docs/configuration/skipping.md

Lines changed: 57 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,110 @@
1-
# Skipping Artifacts
1+
# Selecting files for scanning
22

3-
This section details ways to specify the files, directories and resources that Trivy should not scan.
3+
When scanning a target (image, code repository, etc), Trivy traverses all directories and files in that target and looks for known files to scan. For example, vulnerability scanner might look for `/lib/apk/db/installed` for Alpine APK scanning or `requirements.txt` file for Python pip scanning, and misconfiguration scanner might look for `Dockerfile` for Dockerfile scanning. This document explains how to control which files Trivy looks (including skipping files) for and how it should process them.
44

5-
## Skip Files
6-
| Scanner | Supported |
7-
|:----------------:|:---------:|
8-
| Vulnerability ||
9-
| Misconfiguration ||
10-
| Secret ||
11-
| License ||
12-
13-
By default, Trivy traverses directories and searches for all necessary files for scanning.
14-
You can skip files that you don't maintain using the `--skip-files` flag, or the equivalent Trivy YAML config option.
15-
16-
Using the `--skip-files` flag:
17-
```bash
18-
$ trivy image --skip-files "/Gemfile.lock" --skip-files "/var/lib/gems/2.5.0/gems/http_parser.rb-0.6.0/Gemfile.lock" quay.io/fluentd_elasticsearch/fluentd:v2.9.0
19-
```
20-
21-
Using the Trivy YAML configuration:
22-
```yaml
23-
image:
24-
skip-files:
25-
- foo
26-
- "testdata/*/bar"
27-
```
5+
!!! note
6+
Selecting/skipping files is different from filtering/ignoring results, which is covered in the [Filtering document](./filtering.md)
287

29-
It's possible to specify globs as part of the value.
8+
## Skip Files and Directories
309

31-
```bash
32-
$ trivy image --skip-files "./testdata/*/bar" .
33-
```
10+
You can skip specific files and directories using the `--skip-files` and `--skip-dirs` flags.
3411

35-
This will skip any file named `bar` in the subdirectories of testdata.
12+
For example:
3613

3714
```bash
38-
$ trivy config --skip-files "./foo/**/*.tf" .
15+
trivy image --skip-files "/Gemfile.lock" --skip-dirs "/var/lib/gems/2.5.0/gems/http_parser.rb-0.6.0" quay.io/fluentd_elasticsearch/fluentd:v2.9.0
3916
```
4017

41-
This will skip any files with the extension `.tf` in subdirectories of foo at any depth.
18+
This feature is relevant for the following scanners:
4219

43-
## Skip Directories
4420
| Scanner | Supported |
4521
|:----------------:|:---------:|
4622
| Vulnerability ||
4723
| Misconfiguration ||
4824
| Secret ||
4925
| License ||
5026

51-
By default, Trivy traverses directories and searches for all necessary files for scanning.
52-
You can skip directories that you don't maintain using the `--skip-dirs` flag, or the equivalent Trivy YAML config option.
27+
It's possible to specify glob patterns when referring to a file or directory. The glob expression follows the ["doublestar" library syntax](https://pkg.go.dev/github.com/bmatcuk/doublestar/[email protected]#readme-patterns).
28+
29+
Examples:
5330

54-
Using the `--skip-dirs` flag:
5531
```bash
56-
$ trivy image --skip-dirs /var/lib/gems/2.5.0/gems/fluent-plugin-detect-exceptions-0.0.13 --skip-dirs "/var/lib/gems/2.5.0/gems/http_parser.rb-0.6.0" quay.io/fluentd_elasticsearch/fluentd:v2.9.0
32+
# skip any file named `bar` in the subdirectories of testdata
33+
trivy image --skip-files "./testdata/*/bar" .
5734
```
5835

59-
Using the Trivy YAML configuration:
60-
```yaml
61-
image:
62-
skip-dirs:
63-
- foo/bar/
64-
- "**/.terraform"
36+
```bash
37+
# skip any files with the extension `.tf` in subdirectories of foo at any depth
38+
trivy config --skip-files "./foo/**/*.tf" .
6539
```
6640

67-
It's possible to specify globs as part of the value.
68-
6941
```bash
70-
$ trivy image --skip-dirs "./testdata/*" .
42+
# skip all subdirectories of the testdata directory.
43+
trivy image --skip-dirs "./testdata/*" .
7144
```
7245

73-
This will skip all subdirectories of the testdata directory.
74-
7546
```bash
76-
$ trivy config --skip-dirs "**/.terraform" .
47+
# skip subdirectories at any depth named `.terraform/`.
48+
# this will match `./foo/.terraform` or `./foo/bar/.terraform`, but not `./.terraform`
49+
trivy config --skip-dirs "**/.terraform" .
7750
```
7851

79-
This will skip subdirectories at any depth named `.terraform/`. (Note: this will match `./foo/.terraform` or
80-
`./foo/bar/.terraform`, but not `./.terraform`.)
52+
Like any other flag, this is available as Trivy YAML configuration.
8153

82-
!!! tip
83-
Glob patterns work with any trivy subcommand (image, config, etc.) and can be specified to skip both directories (with `--skip-dirs`) and files (with `--skip-files`).
54+
For example:
8455

56+
```yaml
57+
image:
58+
skip-files:
59+
- foo
60+
- "testdata/*/bar"
61+
skip-dirs:
62+
- foo/bar/
63+
- "**/.terraform"
64+
```
65+
66+
## Customizing file handling
8567
86-
### Advanced globbing
87-
Trivy also supports bash style [extended](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Pattern-Matching) glob pattern matching.
68+
You can customize which files Trivy scans and how it interprets them with the `--file-patterns` flag.
69+
A file pattern configuration takes the following form: `<analyzer>:<path>`, such that files matching the `<path>` will be processed with the respective `<analyzer>`.
70+
71+
For example:
8872

8973
```bash
90-
$ trivy image --skip-files "**/foo" image:tag
74+
trivy fs --file-patterns "pip:.requirements-test.txt ."
9175
```
9276

93-
This will skip the file `foo` that happens to be nested under any parent(s).
77+
This feature is relevant for the following scanners:
9478

95-
## File patterns
9679
| Scanner | Supported |
9780
|:----------------:|:---------:|
9881
| Vulnerability | ✓ |
9982
| Misconfiguration | ✓ |
10083
| Secret | |
10184
| License | ✓[^1] |
10285

103-
When a directory is given as an input, Trivy will recursively look for and test all files based on file patterns.
104-
The default file patterns are [here](../scanner/misconfiguration/custom/index.md).
86+
!!!note
87+
This flag is not applicable for parsers that accepts multiple files, for example the Terraform file parser which loads all `.tf` files into state.
10588

106-
In addition to the default file patterns, the `--file-patterns` option takes regexp patterns to look for your files.
107-
For example, it may be useful when your file name of Dockerfile doesn't match the default patterns.
89+
The list of analyzers can be found [here](https://github.com/aquasecurity/trivy/tree/{{ git.commit }}/pkg/fanal/analyzer/const.go)
10890

109-
This can be repeated for specifying multiple file patterns.
91+
The file path can use a [regular expression](https://pkg.go.dev/regexp/syntax). For example:
11092

111-
A file pattern contains the analyzer it is used for, and the pattern itself, joined by a semicolon. For example:
112-
```
113-
--file-patterns "dockerfile:.*.docker" --file-patterns "kubernetes:*.tpl" --file-patterns "pip:requirements-.*\.txt"
93+
```bash
94+
# interpret any file with .txt extension as a python pip requirements file
95+
trivy fs --file-patterns "pip:requirements-.*\.txt .
11496
```
11597

116-
The prefixes are listed [here](https://github.com/aquasecurity/trivy/tree/{{ git.commit }}/pkg/fanal/analyzer/const.go)
98+
The flag can be repeated for specifying multiple file patterns. For example:
11799

100+
```bash
101+
# look for Dockerfile called production.docker and a python pip requirements file called requirements-test.txt
102+
trivy fs --scanners misconfig,vuln --file-patterns "dockerfile:.production.docker" --file-patterns "pip:.requirements-test.txt ."
103+
```
118104

119-
[^1]: Only work with the [license-full](../scanner/license.md) flag)
105+
[^1]: Only work with the [license-full](../scanner/license.md) flag
120106

121-
## Skip Resources
107+
## Avoid full filesystem traversal
122108

123-
You can skip IaC resources by providing inline comments. More details are provided [here](../scanner/misconfiguration/config/config.md#skipping-resources-by-inline-comments)
109+
In specific scenarios Trivy can avoid traversing the entire filesystem, which makes scanning faster and more efficient.
110+
For more information see [here](../target/rootfs.md#performance-optimization)

docs/docs/scanner/misconfiguration/config/config.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ From the Terraform [docs](https://developer.hashicorp.com/terraform/cli/config/c
6565
If multiple variables evaluate to the same hostname, Trivy will choose the environment variable name where the dashes have not been encoded as double underscores.
6666

6767

68-
### Skipping resources by inline comments
68+
### Filtering resources by inline comments
6969

7070
Trivy supports ignoring misconfigured resources by inline comments for Terraform, CloudFormation and Helm configuration files only.
7171

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ nav:
119119
- Configuration:
120120
- Overview: docs/configuration/index.md
121121
- Filtering: docs/configuration/filtering.md
122-
- Skipping Files: docs/configuration/skipping.md
122+
- Selecting Files: docs/configuration/skipping.md
123123
- Reporting: docs/configuration/reporting.md
124124
- Cache: docs/configuration/cache.md
125125
- Databases: docs/configuration/db.md

0 commit comments

Comments
 (0)