Skip to content

Commit 2cb582b

Browse files
authored
feat: support updating config ignores (#248)
* feat: support updating config ignores * feat: use 2 spaces for YAML indenting * docs: add section about new flag * test: add case for updating configs with nested configs * fix: ensure that config updating output is consistently ordered * feat: account for existing ignores in configs * fix: color config file paths consistently * test: don't fail if we can't clean up test config files * test: add case when there are a lot of different files and a single config
1 parent 65e9e51 commit 2cb582b

File tree

10 files changed

+904
-9
lines changed

10 files changed

+904
-9
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,11 @@ pids
3232

3333
# Dependency directories (remove the comment below to include it)
3434
vendor/
35+
36+
# These files are generated during tests
37+
fixtures/locks-insecure-nested/.osv-detector.yml
38+
fixtures/locks-insecure-nested/nested/.osv-detector.yml
39+
fixtures/locks-insecure-many/.osv-detector.yml
40+
fixtures/existing-config-with-ignored-ignores.yml
41+
fixtures/existing-config-with-ignores.yml
42+
fixtures/existing-config.yml

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,12 @@ osv-detector --ignore GHSA-896r-f27r-55mw --ignore GHSA-74fj-2j2h-c42q package-l
241241
Ignores provided via the flag will be combined with any ignores specified in the
242242
loaded config file.
243243
244-
You can use `jq` to generate a list of OSV ids if you want to ignore all current
245-
known vulnerabilities found by the detector:
244+
You can use `--update-config-ignores` to have the detector update configs being
245+
used for lockfiles to ignore any vulnerabilities that were found; it will also
246+
remove ignores for vulnerabilities that are no longer present.
247+
248+
Alternatively, you can use `jq` to generate a list of OSV ids if you want to
249+
ignore all current known vulnerabilities found by the detector:
246250
247251
```shell
248252
osv-detector --json . | jq -r '[.results[].packages | map("- " + .vulnerabilities[].id)] | flatten | unique | sort | .[]'

fixtures/locks-insecure-many/my-package-lock.json

Lines changed: 72 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fixtures/locks-insecure-nested/my-package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
{
2+
"_readme": [
3+
"This file locks the dependencies of your project to a known state",
4+
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
5+
"This file is @generated automatically"
6+
],
7+
"content-hash": "36f1605a5dac03350d3c7d40eafc8477",
8+
"packages": [
9+
{
10+
"name": "guzzlehttp/psr7",
11+
"version": "1.8.2",
12+
"source": {
13+
"type": "git",
14+
"url": "https://github.com/guzzle/psr7.git",
15+
"reference": "dc960a912984efb74d0a90222870c72c87f10c91"
16+
},
17+
"dist": {
18+
"type": "zip",
19+
"url": "https://api.github.com/repos/guzzle/psr7/zipball/dc960a912984efb74d0a90222870c72c87f10c91",
20+
"reference": "dc960a912984efb74d0a90222870c72c87f10c91",
21+
"shasum": ""
22+
},
23+
"require": {
24+
"php": ">=5.4.0",
25+
"psr/http-message": "~1.0",
26+
"ralouphie/getallheaders": "^2.0.5 || ^3.0.0"
27+
},
28+
"provide": {
29+
"psr/http-message-implementation": "1.0"
30+
},
31+
"require-dev": {
32+
"ext-zlib": "*",
33+
"phpunit/phpunit": "~4.8.36 || ^5.7.27 || ^6.5.14 || ^7.5.20 || ^8.5.8 || ^9.3.10"
34+
},
35+
"suggest": {
36+
"laminas/laminas-httphandlerrunner": "Emit PSR-7 responses"
37+
},
38+
"type": "library",
39+
"extra": {
40+
"branch-alias": {
41+
"dev-master": "1.7-dev"
42+
}
43+
},
44+
"autoload": {
45+
"psr-4": {
46+
"GuzzleHttp\\Psr7\\": "src/"
47+
},
48+
"files": ["src/functions_include.php"]
49+
},
50+
"notification-url": "https://packagist.org/downloads/",
51+
"license": ["MIT"],
52+
"authors": [
53+
{
54+
"name": "Michael Dowling",
55+
"email": "[email protected]",
56+
"homepage": "https://github.com/mtdowling"
57+
},
58+
{
59+
"name": "Tobias Schultze",
60+
"homepage": "https://github.com/Tobion"
61+
}
62+
],
63+
"description": "PSR-7 message implementation that also provides common utility methods",
64+
"keywords": [
65+
"http",
66+
"message",
67+
"psr-7",
68+
"request",
69+
"response",
70+
"stream",
71+
"uri",
72+
"url"
73+
],
74+
"time": "2021-04-26T09:17:50+00:00"
75+
}
76+
],
77+
"packages-dev": [],
78+
"aliases": [],
79+
"minimum-stability": "dev",
80+
"stability-flags": {
81+
"cwp/cwp-recipe-cms": 0,
82+
"cwp/cwp-recipe-core": 0,
83+
"innoweb/silverstripe-mailchimp-signup": 20,
84+
"silverstripe/recipe-blog": 0,
85+
"silverstripe/redirectedurls": 20
86+
},
87+
"prefer-stable": true,
88+
"prefer-lowest": false,
89+
"platform": {
90+
"php": ">=7.4.0"
91+
},
92+
"platform-dev": [],
93+
"plugin-api-version": "1.1.0"
94+
}

internal/configer/load.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ import (
1414
)
1515

1616
type rawDatabaseConfig struct {
17-
Name string `yaml:"name"`
18-
Type string `yaml:"type"`
1917
URL string `yaml:"url"`
20-
WorkingDirectory string `yaml:"working-directory"`
18+
Name string `yaml:"name,omitempty"`
19+
Type string `yaml:"type,omitempty"`
20+
WorkingDirectory string `yaml:"working-directory,omitempty"`
2121
}
2222

2323
type rawConfig struct {
2424
FilePath string `yaml:"-"`
2525
Ignore []string `yaml:"ignore"`
26-
Databases []rawDatabaseConfig `yaml:"extra-databases"`
26+
Databases []rawDatabaseConfig `yaml:"extra-databases,omitempty"`
2727
}
2828

2929
type Config struct {
@@ -130,7 +130,7 @@ func Find(r *reporter.Reporter, pathToDirectory string) (Config, error) {
130130
return Config{}, nil
131131
}
132132

133-
func Load(r *reporter.Reporter, pathToConfig string) (Config, error) {
133+
func load(pathToConfig string) (rawConfig, error) {
134134
var raw rawConfig
135135

136136
pathToConfig = filepath.Clean(pathToConfig)
@@ -140,13 +140,23 @@ func Load(r *reporter.Reporter, pathToConfig string) (Config, error) {
140140
configContents, err := os.ReadFile(pathToConfig)
141141

142142
if err != nil {
143-
return Config{FilePath: pathToConfig}, fmt.Errorf("could not read %s: %w", pathToConfig, err)
143+
return raw, fmt.Errorf("could not read %s: %w", pathToConfig, err)
144144
}
145145

146146
err = yaml.Unmarshal(configContents, &raw)
147147

148148
if err != nil {
149-
return Config{FilePath: pathToConfig}, fmt.Errorf("could not read %s: %w", pathToConfig, err)
149+
return raw, fmt.Errorf("could not read %s: %w", pathToConfig, err)
150+
}
151+
152+
return raw, nil
153+
}
154+
155+
func Load(r *reporter.Reporter, pathToConfig string) (Config, error) {
156+
raw, err := load(pathToConfig)
157+
158+
if err != nil {
159+
return Config{FilePath: raw.FilePath}, err
150160
}
151161

152162
return newConfig(r, raw)

internal/configer/update.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package configer
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"gopkg.in/yaml.v3"
8+
)
9+
10+
func UpdateWithIgnores(pathToConfig string, ignores []string) error {
11+
raw, err := load(pathToConfig)
12+
13+
if err != nil {
14+
return fmt.Errorf("%w", err)
15+
}
16+
17+
raw.Ignore = ignores
18+
19+
f, err := os.OpenFile(pathToConfig, os.O_TRUNC|os.O_WRONLY, os.ModePerm)
20+
21+
if err != nil {
22+
return fmt.Errorf("%w", err)
23+
}
24+
25+
encoder := yaml.NewEncoder(f)
26+
encoder.SetIndent(2)
27+
err = encoder.Encode(raw)
28+
29+
if err != nil {
30+
return fmt.Errorf("%w", err)
31+
}
32+
33+
return nil
34+
}

0 commit comments

Comments
 (0)