|
1 |
| -# Skipping Artifacts |
| 1 | +# Selecting files for scanning |
2 | 2 |
|
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. |
4 | 4 |
|
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) |
28 | 7 |
|
29 |
| -It's possible to specify globs as part of the value. |
| 8 | +## Skip Files and Directories |
30 | 9 |
|
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. |
34 | 11 |
|
35 |
| -This will skip any file named `bar` in the subdirectories of testdata. |
| 12 | +For example: |
36 | 13 |
|
37 | 14 | ```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 |
39 | 16 | ```
|
40 | 17 |
|
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: |
42 | 19 |
|
43 |
| -## Skip Directories |
44 | 20 | | Scanner | Supported |
|
45 | 21 | |:----------------:|:---------:|
|
46 | 22 | | Vulnerability | ✓ |
|
47 | 23 | | Misconfiguration | ✓ |
|
48 | 24 | | Secret | ✓ |
|
49 | 25 | | License | ✓ |
|
50 | 26 |
|
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: |
53 | 30 |
|
54 |
| -Using the `--skip-dirs` flag: |
55 | 31 | ```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" . |
57 | 34 | ```
|
58 | 35 |
|
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" . |
65 | 39 | ```
|
66 | 40 |
|
67 |
| -It's possible to specify globs as part of the value. |
68 |
| -
|
69 | 41 | ```bash
|
70 |
| -$ trivy image --skip-dirs "./testdata/*" . |
| 42 | +# skip all subdirectories of the testdata directory. |
| 43 | +trivy image --skip-dirs "./testdata/*" . |
71 | 44 | ```
|
72 | 45 |
|
73 |
| -This will skip all subdirectories of the testdata directory. |
74 |
| - |
75 | 46 | ```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" . |
77 | 50 | ```
|
78 | 51 |
|
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. |
81 | 53 |
|
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: |
84 | 55 |
|
| 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 |
85 | 67 |
|
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: |
88 | 72 |
|
89 | 73 | ```bash
|
90 |
| -$ trivy image --skip-files "**/foo" image:tag |
| 74 | +trivy fs --file-patterns "pip:.requirements-test.txt ." |
91 | 75 | ```
|
92 | 76 |
|
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: |
94 | 78 |
|
95 |
| -## File patterns |
96 | 79 | | Scanner | Supported |
|
97 | 80 | |:----------------:|:---------:|
|
98 | 81 | | Vulnerability | ✓ |
|
99 | 82 | | Misconfiguration | ✓ |
|
100 | 83 | | Secret | |
|
101 | 84 | | License | ✓[^1] |
|
102 | 85 |
|
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. |
105 | 88 |
|
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) |
108 | 90 |
|
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: |
110 | 92 |
|
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 . |
114 | 96 | ```
|
115 | 97 |
|
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: |
117 | 99 |
|
| 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 | +``` |
118 | 104 |
|
119 |
| -[^1]: Only work with the [license-full](../scanner/license.md) flag) |
| 105 | +[^1]: Only work with the [license-full](../scanner/license.md) flag |
120 | 106 |
|
121 |
| -## Skip Resources |
| 107 | +## Avoid full filesystem traversal |
122 | 108 |
|
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) |
0 commit comments