Skip to content

Commit 9bb7576

Browse files
committed
Add documentation for code-security.datadog.yaml configuration file
1 parent 67491f7 commit 9bb7576

4 files changed

Lines changed: 195 additions & 141 deletions

File tree

FAQ.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,18 @@ You can learn more about diff-aware scanning in our [dedicated documentation](do
6161

6262
## How to scan generated files?
6363

64-
By default, the static analyzer skip generated files (like files generated by protobuf).
64+
By default, the static analyzer skips generated files (like files generated by protobuf).
6565
This is implemented in [generated_content.rs](crates/static-analysis-kernel/src/analysis/generated_content.rs).
6666

6767
If you want to force the analysis of generated code, add the directive
68-
`ignore-generated-files` in your `static-analysis.datadog.yml` file as shown below.
68+
`ignore-generated-files` to your `code-security.datadog.yaml` file as shown below.
6969

7070
```yaml
71-
rulesets:
72-
- ruleset1
73-
ignore-generated-files: false
71+
schema-version: v1.0
72+
73+
sast:
74+
global-config:
75+
ignore-generated-files: false
7476
```
7577
7678

README.md

Lines changed: 179 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,23 @@ the solution to your problem.
4343

4444
### Advanced Usage
4545

46-
You can choose the rules to use to scan your repository by creating a `static-analysis.datadog.yml` file.
46+
You can choose the rules to use to scan your repository by creating a `code-security.datadog.yaml` file.
4747

4848
First, make sure you follow the [documentation](https://docs.datadoghq.com/code_analysis/static_analysis)
49-
and create a `static-analysis.datadog.yml` file at the root of your project with the rulesets you want to use.
50-
51-
All the rules can be found on the [Datadog documentation](https://docs.datadoghq.com/security/code_security/static_analysis/static_analysis_rules/). Your `static-analysis.datadog.yml` may only contain rulesets available from the [Datadog documentation](https://docs.datadoghq.com/security/code_security/static_analysis/static_analysis_rules/)
49+
and create a `code-security.datadog.yaml` file at the root of your project with the rulesets you want to use.
5250

5351
Example of YAML file
5452

5553
```yaml
56-
schema-version: v1
57-
rulesets:
58-
- python-code-style
59-
- python-best-practices
60-
- python-inclusive
61-
ignore:
62-
- tests
54+
schema-version: v1.0
55+
sast:
56+
use-rulesets:
57+
- python-code-style
58+
- python-best-practices
59+
- python-inclusive
60+
global-config:
61+
ignore-paths:
62+
- src/experiments
6363
```
6464
6565
### CI/CD Integration
@@ -74,15 +74,15 @@ If you use it in your own CI/CD pipeline, you can integrate the tool directly: s
7474
### IntelliJ JetBrains products
7575
7676
The [Datadog IntelliJ extension](https://plugins.jetbrains.com/plugin/19495-datadog) allows you to use the static analyzer directly from all JetBrains products.
77-
Create a `static-analysis.datadog.yml` file, download the extension and you can start using it. You can see below an example of a suggestion to add a timeout
78-
when fetching data with Python with the requests module.
77+
Create a configuration file ([reference here](doc/legacy_config.md)), download the extension, and you can start using it.
78+
You can see below an example of a suggestion to add a timeout when fetching data with Python with the requests module.
7979
8080
![Datadog Static Analysis JetBrains](misc/imgs/jetbrains.gif)
8181
8282
### VS Code
8383
8484
The [Datadog VS Code extension](https://marketplace.visualstudio.com/items?itemName=Datadog.datadog-vscode) allows you to use the static analyzer directly from VS Code.
85-
Create a `static-analysis.datadog.yml` file, download the extension and you can start using it.
85+
Create a configuration file ([reference here](doc/legacy_config.md)), download the extension, and you can start using it.
8686
8787
![Datadog Static Analysis JetBrains](misc/imgs/vscode.gif)
8888
@@ -117,10 +117,6 @@ curl -L -O https://www.github.com/DataDog/datadog-static-analyzer/releases/lates
117117
datadog-static-analyzer -i <directory> -o <output-file>
118118
```
119119

120-
For the tool to work, you must have a `<directory>/static-analysis.datadog.yml` file that defines the configuration of the analyzer. This file will indicate the rules you will use for your project.
121-
122-
You can get more information about the configuration on [Datadog documentation](https://docs.datadoghq.com/security/code_security/static_analysis/setup).
123-
124120
### Mac OS X users
125121

126122
If you installed via Homebrew (`brew install datadog-static-analyzer`), you can skip this section.
@@ -151,141 +147,193 @@ Set the following variables to configure an analysis:
151147

152148
## Configuration file
153149

154-
The static analyzer can be configured using a `static-analysis.datadog.yml` file
155-
at the root directory of the repository. This is a YAML file with the following entries:
150+
The static analyzer can be configured using a `code-security.datadog.yaml` file at the root directory of the repository.
151+
The file must begin with `schema-version: v1.0` and should have a `sast` object specifying the configuration.
152+
153+
```yaml
154+
schema-version: v1.0
155+
sast:
156+
# ... configuration goes here
157+
```
158+
159+
The **sast object** supports the following fields:
160+
161+
| **Property** | **Type** | **Description** | **Default** |
162+
| --- | --- | --- | --- |
163+
| `use-default-rulesets` | Boolean | Whether to enable Datadog default rulesets. | `true` |
164+
| `use-rulesets` | Array | A list of ruleset names to enable ([custom rulesets](https://docs.datadoghq.com/security/code_security/static_analysis/custom_rules/tutorial/) or [Datadog default rulesets](https://docs.datadoghq.com/security/code_security/static_analysis/static_analysis_rules/)). Enabled in addition to the default rulesets if `use-default-rulesets` is `true`. | None |
165+
| `ignore-rulesets` | Array | A list of ruleset names to disable. Takes precedence over `use-rulesets` and `use-default-rulesets`. | None |
166+
| `ruleset-configs` | Object | A map from ruleset name to its configuration. | None |
167+
| `global-config` | Object | Global settings for the repository. | None |
168+
169+
### Ruleset configuration
156170

157-
- `rulesets`: (required) a list with all the rulesets to use for this repository (see [Datadog Documentation](https://docs.datadoghq.com/security/code_security/static_analysis/static_analysis_rules/) for a full list). The elements of this list must be strings or maps containing a configuration for a ruleset (described below.)
158-
- `ignore`: (optional) a list of path prefixes and glob patterns to ignore. A file that matches any of its entries will not be analyzed.
159-
- `only`: (optional) a list of path prefixes and glob patterns to analyze. If `only` is specified, only files that match one of its entries will be analyzed.
160-
- `ignore-gitignore`: (optional) by default, any entries found in the `.gitignore` file are added to the `ignore` list. If the `ignore-gitignore` option is true, the `.gitignore` file is not read.
161-
- `max-file-size-kb`: (optional) files larger than this size, in kilobytes, will be ignored. The default value is 200 kB.
162-
- `schema-version`: (optional) the version of the schema that this configuration file follows. If specified, it must be `v1`.
171+
Each entry in the `ruleset-configs` map configures a specific ruleset. A ruleset does not need to be listed in `use-rulesets` for its configuration to apply; the configuration is used whenever the ruleset is enabled, including through `use-default-rulesets`.
163172

164-
The entries of the `rulesets` list must be strings that contain the name of a ruleset to enable, or a map that contains the configuration for a ruleset. This map contains the following fields:
173+
| **Property** | **Type** | **Description** | **Default** |
174+
| --- | --- | --- | --- |
175+
| `only-paths` | Array | File paths or glob patterns. Only files matching these patterns are processed for this ruleset. | None |
176+
| `ignore-paths` | Array | File paths or glob patterns to exclude from analysis for this ruleset. | None |
177+
| `rule-configs` | Object | A map from rule name to its configuration. | None |
165178

166-
- the first field (required) gives the ruleset name as its key, with an empty value.
167-
- `ignore`: (optional) a list of path prefixes and glob patterns to ignore _for this ruleset_. Rules in this ruleset will not be evaluated for any files that match any of the entries in the `ignore` list.
168-
- `only`: (optional) a list of path prefixes and glob patterns to analyze _for this ruleset_. If `only` is specified, rules in this ruleset will only be evaluated for files that match one of the entries.
169-
- `rules`: (optional) a map of rule configurations. Rules not specified in this map will still be evaluated, but with their default configuration.
179+
### Rule configuration
170180

171-
The map in the `rules` field uses the rule's name as its key, and the values are maps with the following fields:
181+
Each entry in the `rule-configs` map configures a specific rule:
172182

173-
- `ignore` (optional) a list of path prefixes and glob patterns to ignore _for this rule_. This rule will not be evaluated for any files that match any of the entries in the `ignore` list.
174-
- `only`: (optional) a list of path prefixes and glob patterns to analyze _for this rule_. If `only` is specified, this rule will only be evaluated for files that match one of the entries.
175-
- `severity`: (optional) if provided, override the severity of violations produced by this rule. The valid severities are `ERROR`, `WARNING`, `NOTICE`, and `NONE`.
176-
- `category`: (optional) if provided, override this rule's category. The valid categories are `BEST_PRACTICES`, `CODE_STYLE`, `ERROR_PRONE`, `PERFORMANCE`, and `SECURITY`.
177-
- `arguments`: (optional) a map of values for the rule's arguments.
183+
| **Property** | **Type** | **Description** | **Default** |
184+
| --- | --- | --- | --- |
185+
| `only-paths` | Array | File paths or glob patterns. The rule is applied only to files matching these patterns. | None |
186+
| `ignore-paths` | Array | File paths or glob patterns to exclude. The rule is not applied to files matching these patterns. | None |
187+
| `arguments` | Object | Parameters and values for the rule. Values can be scalars or defined per path. | None |
188+
| `severity` | String or Object | The rule severity. Valid values: `ERROR`, `WARNING`, `NOTICE`, `NONE`. Can be a single value or defined per path. | None |
189+
| `category` | String | The rule category. Valid values: `BEST_PRACTICES`, `CODE_STYLE`, `ERROR_PRONE`, `PERFORMANCE`, `SECURITY`. | None |
178190

179-
The map in the `arguments` field uses an argument's name as its key, and the values are either strings or maps:
191+
## Argument and severity configuration
180192

181-
- if you want to set a value for the whole repository, you can specify it as a string;
182-
- if you want to set different values for different subtrees in the repository, you can specify them as a map from a subtree prefix to the value that the argument will have within that subtree. See the example for more details.
193+
Arguments and severity can be defined in one of two formats:
194+
195+
1. **Single value:** Applies to the whole repository.
196+
197+
```yaml
198+
arguments:
199+
argument-name: value
200+
severity: ERROR
201+
```
202+
203+
2. **Per-path mapping:** Different values for different subtrees. The longest matching path prefix applies. Use `/` as a catch-all default.
204+
205+
```yaml
206+
arguments:
207+
argument-name:
208+
/: value_default
209+
path/example: value_specific
210+
severity:
211+
/: WARNING
212+
path/example: ERROR
213+
```
214+
215+
The `category` field takes a single string value for the whole repository.
216+
217+
### Global configuration
218+
219+
The `global-config` object controls repository-wide settings and has the following optional fields:
220+
221+
| **Property** | **Type** | **Description** | **Default** |
222+
| --- | --- | --- | --- |
223+
| `only-paths` | Array | File paths or glob patterns. Only matching files are analyzed. | None |
224+
| `ignore-paths` | Array | File paths or glob patterns to exclude. Matching files are not analyzed. | None |
225+
| `use-gitignore` | Boolean | Whether to include entries from the `.gitignore` file in `ignore-paths`. | `true` |
226+
| `ignore-generated-files` | Boolean | Whether to include common generated file patterns in `ignore-paths`. | `true` |
227+
| `max-file-size-kb` | Number | Maximum file size (in kB) to analyze. Larger files are ignored. | `200` |
183228

184229
An annotated example of a configuration file:
185230

186231
```yaml
187-
# This is a legacy "v1" configuration file.
188-
schema-version: v1
189-
# The list of rulesets to enable for this repository.
190-
rulesets:
191-
# Enable the `python-inclusive` ruleset with the default configuration.
192-
- python-inclusive
193-
# Enable the `python-best-practices` ruleset with a custom configuration.
194-
- python-best-practices:
195-
# Do not apply any of the rules in this ruleset to files that match `src/**/*.generated.py`.
196-
ignore:
197-
- src/**/*.generated.py
198-
rules:
199-
# Special configuration for the `python-best-practices/no-generic-exception` rule.
200-
no-generic-exception:
201-
# Treat violations of this rule as errors (normally "notice").
202-
severity: ERROR
203-
# Classify violations of this rule under the "code style" category.
204-
category: CODE_STYLE
205-
# Only apply this rule to files under the `src/new-code` subtree.
206-
only:
207-
- src/new-code
208-
# Enable the `python-code-style ruleset` with a custom configuration.
209-
- python-code-style:
210-
rules:
211-
max-function-lines:
212-
# Set arguments for the `python-code-style/max-function-lines` rule.
213-
arguments:
214-
# Set the `max-lines` argument to 150 in the whole repository.
215-
max-lines: 150
216-
max-class-lines:
217-
# Set arguments for the `python-code-style/max-class-lines` rule.
218-
arguments:
219-
# Set different values for the `max-lines` argument in different subtrees.
220-
max-lines:
221-
# Set the `max-lines` argument to 100 by default
222-
/: 100
223-
# Set the `max-lines` argument to 75 under the `src/new-code` subtree.
224-
src/new-code: 75
225-
# Analyze only files in the `src` and `imported` subtrees.
226-
only:
227-
- src
228-
- imported
229-
# Do not analyze any files in the `src/tests` subtree.
230-
ignore:
231-
- src/tests
232-
# Do not add the content of the `.gitignore` file to the `ignore` list.
233-
ignore-gitignore: true
234-
# Do not analyze files larger than 100 kB.
235-
max-file-size-kb: 100
232+
schema-version: v1.0
233+
sast:
234+
# Always ensure the following rulesets are run (in addition to the Datadog defaults).
235+
use-rulesets:
236+
- python-inclusive
237+
- my-custom-python-rules
238+
# Never use the following rulesets (even if they are in the Datadog defaults).
239+
ignore-rulesets:
240+
- python-pandas
241+
ruleset-configs:
242+
# Configuration for the `python-best-practices` ruleset.
243+
python-best-practices:
244+
# Do not apply any of the rules in this ruleset to files that match `**/*_model.py`.
245+
ignore-paths:
246+
- "**/*_model.py"
247+
rule-configs:
248+
# Special configuration for the `python-best-practices/no-generic-exception` rule.
249+
no-generic-exception:
250+
# Treat violations of this rule as errors.
251+
severity: ERROR
252+
# Classify violations of this rule under the "code style" category.
253+
category: CODE_STYLE
254+
# Only apply this rule to files under the `src/new-code` subtree.
255+
only-paths:
256+
- src/new-code
257+
# Configuration for the `python-code-style` ruleset.
258+
python-code-style:
259+
rule-configs:
260+
max-function-lines:
261+
# Set arguments for the `python-code-style/max-function-lines` rule.
262+
arguments:
263+
# Set the `max-lines` argument to 150 in the whole repository.
264+
max-lines: 150
265+
max-class-lines:
266+
# Set arguments for the `python-code-style/max-class-lines` rule.
267+
arguments:
268+
# Set different values for the `max-lines` argument in different subtrees.
269+
max-lines:
270+
# 100 lines by default.
271+
/: 100
272+
# 75 lines under the `src/new-code` subtree.
273+
src/new-code: 75
274+
# Repository-wide settings.
275+
global-config:
276+
# Analyze only files in the `src` and `imported` subtrees.
277+
only-paths:
278+
- src
279+
- imported
280+
# Do not analyze any files in the `src/third_party` subtree.
281+
ignore-paths:
282+
- src/third_party
283+
# Do not analyze files larger than 100 kB.
284+
max-file-size-kb: 100
236285
```
237286
238287
Another example that shows every option being used:
239288
240289
```yaml
241-
schema-version: v1
242-
rulesets:
243-
- python-best-practices
244-
- python-code-style:
245-
ignore:
246-
- src/generated
247-
- src/**/*_test.py
248-
only:
290+
schema-version: v1.0
291+
sast:
292+
use-default-rulesets: false
293+
use-rulesets:
294+
- python-code-style
295+
ignore-rulesets:
296+
- python-pandas
297+
ruleset-configs:
298+
python-code-style:
299+
ignore-paths:
300+
- src/third_party
301+
- src/**/*_test.py
302+
only-paths:
303+
- src
304+
- imported/**/new/**
305+
rule-configs:
306+
max-function-lines:
307+
severity: WARNING
308+
category: PERFORMANCE
309+
ignore-paths:
310+
- src/new-code
311+
- src/new/*_gen.py
312+
only-paths:
313+
- src/new
314+
- src/**/new-code/**
315+
arguments:
316+
max-lines: 150
317+
min-lines:
318+
/: 10
319+
src/new-code: 0
320+
global-config:
321+
ignore-paths:
322+
- dist
323+
- lib/**/*.py
324+
only-paths:
249325
- src
250-
- imported/**/new/**
251-
rules:
252-
max-function-lines:
253-
severity: WARNING
254-
category: PERFORMANCE
255-
ignore:
256-
- src/new-code
257-
- src/new/*.gen.py
258-
only:
259-
- src/new
260-
- src/**/new-code/**
261-
arguments:
262-
max-lines: 150
263-
min-lines:
264-
/: 10
265-
src/new-code: 0
266-
ignore:
267-
- dist
268-
- lib/**/*.py
269-
only:
270-
- src
271-
- imported/**/*.py
272-
ignore-gitignore: true
273-
max-file-size-kb: 256
326+
- imported/**/*.py
327+
use-gitignore: false
328+
ignore-generated-files: false
329+
max-file-size-kb: 256
274330
```
275331
276332
## Configuration file schema
277333
278-
There is a JSON Schema definition for the `static-analysis.datadog.yml` in the `schema/legacy` subdirectory.
279-
280-
You can use it to check the syntax of your configuration file:
281-
282-
1. Execute `npx --yes [email protected] validate -s schema/legacy/schema.json -r 'schema/legacy/examples/*/*.json' -d path/to/your/static-analysis.datadog.yml`
283-
284-
There are some examples of valid and invalid configuration files in the [`schema/legacy/examples/valid`](schema/legacy/examples/valid)
285-
and [`schema/legacy/examples/invalid`](schema/legacy/examples/invalid) subdirectories, respectively. If you make changes to the JSON
286-
Schema, you can test them against our examples:
334+
You can use the included JSON schema definition to check the syntax of your configuration file:
287335
288-
1. Execute `make -C schema`
336+
1. Execute `npx --yes [email protected] validate --spec=draft2020 -s schema/sast/v1.0/validation.schema.json -r schema/sast/v1.0/schema.json -d path/to/your/code-security.datadog.yaml`
289337

290338
## Diff-Aware Scanning
291339

0 commit comments

Comments
 (0)