Skip to content

Commit 1720d3a

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 b7cbbdc commit 1720d3a

File tree

2 files changed

+56
-68
lines changed

2 files changed

+56
-68
lines changed

docs/docs/configuration/skipping.md

Lines changed: 55 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,107 @@
1-
# Skipping Files and Directories
1+
# Selecting files for scanning
22

3-
This section details ways to specify the files and directories 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 customize which files Trivy looks for and how it processes them.
4+
5+
## Skip Files and Directories
6+
7+
You can skip specific files and directories using the `--skip-files` and `--skip-dirs` flags.
8+
9+
For example:
10+
11+
```bash
12+
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
13+
```
14+
15+
This feature is relevant for the following scanners:
416

5-
## Skip Files
617
| Scanner | Supported |
718
|:----------------:|:---------:|
819
| Vulnerability ||
920
| Misconfiguration ||
1021
| Secret ||
1122
| License ||
1223

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.
24+
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).
25+
26+
Examples:
1527

16-
Using the `--skip-files` flag:
1728
```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
29+
# skip any file named `bar` in the subdirectories of testdata
30+
trivy image --skip-files "./testdata/*/bar" .
1931
```
2032

21-
Using the Trivy YAML configuration:
22-
```yaml
23-
image:
24-
skip-files:
25-
- foo
26-
- "testdata/*/bar"
33+
```bash
34+
# skip any files with the extension `.tf` in subdirectories of foo at any depth
35+
trivy config --skip-files "./foo/**/*.tf" .
2736
```
2837

29-
It's possible to specify globs as part of the value.
30-
3138
```bash
32-
$ trivy image --skip-files "./testdata/*/bar" .
39+
# skip all subdirectories of the testdata directory.
40+
trivy image --skip-dirs "./testdata/*" .
3341
```
3442

35-
This will skip any file named `bar` in the subdirectories of testdata.
36-
3743
```bash
38-
$ trivy config --skip-files "./foo/**/*.tf" .
44+
# skip subdirectories at any depth named `.terraform/`.
45+
# this will match `./foo/.terraform` or `./foo/bar/.terraform`, but not `./.terraform`
46+
trivy config --skip-dirs "**/.terraform" .
3947
```
4048

41-
This will skip any files with the extension `.tf` in subdirectories of foo at any depth.
42-
43-
## Skip Directories
44-
| Scanner | Supported |
45-
|:----------------:|:---------:|
46-
| Vulnerability ||
47-
| Misconfiguration ||
48-
| Secret ||
49-
| License ||
50-
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.
49+
Like any other flag, this is available as Trivy YAML configuration.
5350

54-
Using the `--skip-dirs` flag:
55-
```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
57-
```
51+
For example:
5852

59-
Using the Trivy YAML configuration:
6053
```yaml
6154
image:
55+
skip-files:
56+
- foo
57+
- "testdata/*/bar"
6258
skip-dirs:
6359
- foo/bar/
6460
- "**/.terraform"
6561
```
6662
67-
It's possible to specify globs as part of the value.
63+
## Customizing file handling
6864
69-
```bash
70-
$ trivy image --skip-dirs "./testdata/*" .
71-
```
65+
You can customize which files Trivy scans and how it interprets them with the `--file-patterns` flag.
66+
A file pattern configuration takes the following form: `<analyzer>:<path>`, such that files matching the `<path>` will be processed with the respective `<analyzer>`.
7267

73-
This will skip all subdirectories of the testdata directory.
68+
For example:
7469

7570
```bash
76-
$ trivy config --skip-dirs "**/.terraform" .
71+
trivy fs --file-patterns "pip:.requirements-test.txt ."
7772
```
7873

79-
This will skip subdirectories at any depth named `.terraform/`. (Note: this will match `./foo/.terraform` or
80-
`./foo/bar/.terraform`, but not `./.terraform`.)
81-
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`).
74+
This feature is relevant for the following scanners:
8475

85-
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.
88-
89-
```bash
90-
$ trivy image --skip-files "**/foo" image:tag
91-
```
92-
93-
This will skip the file `foo` that happens to be nested under any parent(s).
94-
95-
## File patterns
9676
| Scanner | Supported |
9777
|:----------------:|:---------:|
9878
| Vulnerability | ✓ |
9979
| Misconfiguration | ✓ |
10080
| Secret | |
10181
| License | ✓[^1] |
10282

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).
83+
!!!note
84+
This flag is not applicable for parsers that accepts multiple files, for example the Terraform file parser which loads all `.tf` files into state.
10585

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.
86+
The list of analyzers can be found [here](https://github.com/aquasecurity/trivy/tree/{{ git.commit }}/pkg/fanal/analyzer/const.go)
10887

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

111-
A file pattern contains the analyzer it is used for, and the pattern itself, joined by a semicolon. For example:
90+
```bash
91+
# interpret any file with .txt extension as a python pip requirements file
92+
trivy fs --file-patterns "pip:requirements-.*\.txt .
11293
```
113-
--file-patterns "dockerfile:.*.docker" --file-patterns "kubernetes:*.tpl" --file-patterns "pip:requirements-.*\.txt"
94+
95+
The flag can be repeated for specifying multiple file patterns. For example:
96+
97+
```bash
98+
# look for Dockerfile called production.docker and a python pip requirements file called requirements-test.txt
99+
trivy fs --scanners misconfig,vuln --file-patterns "dockerfile:.production.docker" --file-patterns "pip:.requirements-test.txt ."
114100
```
115101

116-
The prefixes are listed [here](https://github.com/aquasecurity/trivy/tree/{{ git.commit }}/pkg/fanal/analyzer/const.go)
102+
[^1]: Only work with the [license-full](../scanner/license.md) flag
117103

104+
## Avoid full filesystem traversal
118105

119-
[^1]: Only work with the [license-full](../scanner/license.md) flag)
106+
In specific scenarios Trivy can avoid traversing the entire filesystem, which makes scanning faster and more efficient.
107+
For more information see [here](../target/rootfs.md#performance-optimization)

mkdocs.yml

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

0 commit comments

Comments
 (0)