|
1 |
| -# Skipping Files and Directories |
| 1 | +# Selecting files for scanning |
2 | 2 |
|
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: |
4 | 16 |
|
5 |
| -## Skip Files |
6 | 17 | | Scanner | Supported |
|
7 | 18 | |:----------------:|:---------:|
|
8 | 19 | | Vulnerability | ✓ |
|
9 | 20 | | Misconfiguration | ✓ |
|
10 | 21 | | Secret | ✓ |
|
11 | 22 | | License | ✓ |
|
12 | 23 |
|
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: |
15 | 27 |
|
16 |
| -Using the `--skip-files` flag: |
17 | 28 | ```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" . |
19 | 31 | ```
|
20 | 32 |
|
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" . |
27 | 36 | ```
|
28 | 37 |
|
29 |
| -It's possible to specify globs as part of the value. |
30 |
| -
|
31 | 38 | ```bash
|
32 |
| -$ trivy image --skip-files "./testdata/*/bar" . |
| 39 | +# skip all subdirectories of the testdata directory. |
| 40 | +trivy image --skip-dirs "./testdata/*" . |
33 | 41 | ```
|
34 | 42 |
|
35 |
| -This will skip any file named `bar` in the subdirectories of testdata. |
36 |
| - |
37 | 43 | ```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" . |
39 | 47 | ```
|
40 | 48 |
|
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. |
53 | 50 |
|
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: |
58 | 52 |
|
59 |
| -Using the Trivy YAML configuration: |
60 | 53 | ```yaml
|
61 | 54 | image:
|
| 55 | + skip-files: |
| 56 | + - foo |
| 57 | + - "testdata/*/bar" |
62 | 58 | skip-dirs:
|
63 | 59 | - foo/bar/
|
64 | 60 | - "**/.terraform"
|
65 | 61 | ```
|
66 | 62 |
|
67 |
| -It's possible to specify globs as part of the value. |
| 63 | +## Customizing file handling |
68 | 64 |
|
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>`. |
72 | 67 |
|
73 |
| -This will skip all subdirectories of the testdata directory. |
| 68 | +For example: |
74 | 69 |
|
75 | 70 | ```bash
|
76 |
| -$ trivy config --skip-dirs "**/.terraform" . |
| 71 | +trivy fs --file-patterns "pip:.requirements-test.txt ." |
77 | 72 | ```
|
78 | 73 |
|
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: |
84 | 75 |
|
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 |
96 | 76 | | Scanner | Supported |
|
97 | 77 | |:----------------:|:---------:|
|
98 | 78 | | Vulnerability | ✓ |
|
99 | 79 | | Misconfiguration | ✓ |
|
100 | 80 | | Secret | |
|
101 | 81 | | License | ✓[^1] |
|
102 | 82 |
|
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. |
105 | 85 |
|
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) |
108 | 87 |
|
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: |
110 | 89 |
|
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 . |
112 | 93 | ```
|
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 ." |
114 | 100 | ```
|
115 | 101 |
|
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 |
117 | 103 |
|
| 104 | +## Avoid full filesystem traversal |
118 | 105 |
|
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) |
0 commit comments