Skip to content

Commit ab04b1d

Browse files
chore: Update Trivy Diagnostics (#293)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 6fd9cc6 commit ab04b1d

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed

doc/BUILTINS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,7 @@ local sources = { null_ls.builtins.diagnostics.trivy }
17791779

17801780
#### Defaults
17811781

1782-
- Filetypes: `{ "terraform", "tf", "terraform-vars" }`
1782+
- Filetypes: `{ "terraform", "tf", "terraform-vars", "helmfile", "dockerfile" }`
17831783
- Method: `diagnostics_on_save`
17841784
- Command: `trivy`
17851785
- Args: dynamically resolved (see [source](https://github.com/nvimtools/none-ls.nvim/blob/main/lua/null-ls/builtins/diagnostics/trivy.lua))

doc/builtins.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,9 @@
499499
"filetypes": [
500500
"terraform",
501501
"tf",
502-
"terraform-vars"
502+
"terraform-vars",
503+
"helmfile",
504+
"dockerfile"
503505
]
504506
},
505507
"twigcs": {

lua/null-ls/builtins/_meta/diagnostics.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ return {
242242
filetypes = {}
243243
},
244244
trivy = {
245-
filetypes = { "terraform", "tf", "terraform-vars" }
245+
filetypes = { "terraform", "tf", "terraform-vars", "helm", "dockerfile" }
246246
},
247247
twigcs = {
248248
filetypes = { "twig" }

lua/null-ls/builtins/_meta/filetype_map.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ return {
207207
diagnostics = { "terragrunt_validate" },
208208
formatting = { "atlas_fmt", "hclfmt", "packer", "terragrunt_fmt" }
209209
},
210+
helm = {
211+
diagnostics = { "trivy" }
212+
},
210213
html = {
211214
diagnostics = { "markuplint", "tidy" },
212215
formatting = { "prettier", "prettierd", "rustywind", "tidy" }
@@ -494,7 +497,7 @@ return {
494497
formatting = { "tidy", "xmllint" }
495498
},
496499
yaml = {
497-
diagnostics = { "actionlint", "cfn_lint", "spectral", "vacuum", "yamllint" },
500+
diagnostics = { "actionlint", "cfn_lint", "spectral", "vacuum", "yamllint", "trivy" },
498501
formatting = { "prettier", "prettierd", "yamlfix", "yamlfmt" }
499502
},
500503
["yaml.ansible"] = {

lua/null-ls/builtins/diagnostics/trivy.lua

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
local h = require("null-ls.helpers")
22
local methods = require("null-ls.methods")
3-
local u = require("null-ls.utils")
43

54
local DIAGNOSTICS_ON_SAVE = methods.internal.DIAGNOSTICS_ON_SAVE
65

@@ -12,14 +11,35 @@ local severities = {
1211
UNKNOWN = h.diagnostics.severities["information"],
1312
}
1413

14+
-- NOTE: (vkhitrin) custom logic to derive the directory name for trivy execution:
15+
-- * If buffer is inside a helm chart, attempt to set the directory to the directory
16+
-- containing Chart.yaml.
17+
-- * Otherwise, set the directory to none-ls' '$DIRNAME'.
18+
local trivy_working_dir = function()
19+
local filetype = vim.bo.filetype
20+
if filetype == "helm" then
21+
local dir = vim.fn.expand("%:p:h")
22+
while dir ~= "/" do
23+
local chart_path = dir .. "/Chart.yaml"
24+
if vim.fn.filereadable(chart_path) == 1 then
25+
return dir
26+
end
27+
dir = vim.fn.fnamemodify(dir, ":h")
28+
end
29+
return dir
30+
else
31+
return "$DIRNAME"
32+
end
33+
end
34+
1535
return h.make_builtin({
1636
name = "trivy",
1737
meta = {
1838
url = "https://github.com/aquasecurity/trivy",
1939
description = "Find misconfigurations and vulnerabilities",
2040
},
2141
method = DIAGNOSTICS_ON_SAVE,
22-
filetypes = { "terraform", "tf", "terraform-vars" },
42+
filetypes = { "terraform", "tf", "terraform-vars", "helm", "dockerfile" },
2343
generator_opts = {
2444
command = "trivy",
2545
timeout = 30000, -- Trivy can be slow, so increase timeout
@@ -29,7 +49,7 @@ return h.make_builtin({
2949
"--format",
3050
"json",
3151
"--quiet",
32-
"$DIRNAME",
52+
trivy_working_dir(),
3353
}
3454

3555
local config_file_path = vim.fs.find("trivy.yaml", {
@@ -55,8 +75,8 @@ return h.make_builtin({
5575
cwd = h.cache.by_bufnr(function(params)
5676
return vim.fs.dirname(params.bufname)
5777
end),
58-
from_stderr = false, -- Trivy outputs logs to stderr that even --quiet doesn't silence
59-
ignore_stderr = true,
78+
from_stderr = true, -- https://github.com/aquasecurity/trivy/pull/2289
79+
ignore_stderr = false,
6080
to_stdin = false,
6181
multiple_files = true,
6282
format = "json",
@@ -82,12 +102,14 @@ return h.make_builtin({
82102
for _, result in pairs(params.output.Results or {}) do
83103
for _, misconfiguration in ipairs(result.Misconfigurations or {}) do
84104
local rewritten_diagnostic = {
85-
message = misconfiguration.ID .. " - " .. misconfiguration.Title,
105+
code = misconfiguration.ID,
106+
message = misconfiguration.Title,
86107
row = misconfiguration.CauseMetadata.StartLine,
108+
end_row = misconfiguration.CauseMetadata.EndLine,
87109
col = 0,
88110
source = "trivy",
89111
severity = severities[misconfiguration.Severity],
90-
filename = u.path.join(params.cwd, result.Target),
112+
filename = result.Target,
91113
}
92114
table.insert(combined_diagnostics, rewritten_diagnostic)
93115
end

0 commit comments

Comments
 (0)