mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-21 14:50:53 -08:00
Added test and support of ASFF template (#594)
* Added test and support of ASFF template * Improve test coverage * Fixed/Improved tests * Removed extra space * Added NVD score/vectors, Added logic to trim description due to file size restriction * Included quotations around AccountID
This commit is contained in:
@@ -900,6 +900,12 @@ In the following example using the template `sarif.tpl` [Sarif](https://docs.git
|
||||
$ trivy image --format template --template "@contrib/sarif.tpl" -o report.sarif golang:1.12-alpine
|
||||
```
|
||||
|
||||
In the following example using the template `asff.tpl` [ASFF](https://docs.aws.amazon.com/securityhub/latest/userguide/securityhub-findings-format.html) can be generated.
|
||||
```
|
||||
$ AWS_REGION=us-west AWS_ACCOUNT_ID=123456789012 trivy image --format template --template "@contrib/asff.tpl" -o report.asff golang:1.12-alpine
|
||||
```
|
||||
ASFF template needs AWS_REGION and AWS_ACCOUNT_ID from environment variables.
|
||||
|
||||
### Filter the vulnerabilities by severities
|
||||
|
||||
```
|
||||
|
||||
78
contrib/asff.tpl
Normal file
78
contrib/asff.tpl
Normal file
@@ -0,0 +1,78 @@
|
||||
[
|
||||
{{- $t_first := true -}}
|
||||
{{- range . -}}
|
||||
{{- $target := .Target -}}
|
||||
{{- range .Vulnerabilities -}}
|
||||
{{- if $t_first -}}
|
||||
{{- $t_first = false -}}
|
||||
{{- else -}}
|
||||
,
|
||||
{{- end -}}
|
||||
{{- $trivyProductSev := 0 -}}
|
||||
{{- $trivyNormalizedSev := 0 -}}
|
||||
{{- if eq .Severity "LOW" -}}
|
||||
{{- $trivyProductSev = 1 -}}
|
||||
{{- $trivyNormalizedSev = 10 -}}
|
||||
{{- else if eq .Severity "MEDIUM" -}}
|
||||
{{- $trivyProductSev = 4 -}}
|
||||
{{- $trivyNormalizedSev = 40 -}}
|
||||
{{- else if eq .Severity "HIGH" -}}
|
||||
{{- $trivyProductSev = 7 -}}
|
||||
{{- $trivyNormalizedSev = 70 -}}
|
||||
{{- else if eq .Severity "CRITICAL" -}}
|
||||
{{- $trivyProductSev = 9 -}}
|
||||
{{- $trivyNormalizedSev = 90 -}}
|
||||
{{- end }}
|
||||
{{- $description := .Description -}}
|
||||
{{- if gt (len $description ) 1021 -}}
|
||||
{{- $description = (slice $description 0 1021) | printf "%v .." -}}
|
||||
{{- end}}
|
||||
{
|
||||
"SchemaVersion": "2018-10-08",
|
||||
"Id": "{{ $target }}/{{ .VulnerabilityID }}",
|
||||
"ProductArn": "arn:aws:securityhub:{{ getEnv "AWS_REGION" }}::product/aquasecurity/aquasecurity",
|
||||
"GeneratorId": "Trivy",
|
||||
"AwsAccountId": "{{ getEnv "AWS_ACCOUNT_ID" }}",
|
||||
"Types": [ "Software and Configuration Checks/Vulnerabilities/CVE" ],
|
||||
"CreatedAt": "{{ getCurrentTime }}",
|
||||
"UpdatedAt": "{{ getCurrentTime }}",
|
||||
"Severity": {
|
||||
"Product": {{ $trivyProductSev }},
|
||||
"Normalized": {{ $trivyNormalizedSev }}
|
||||
},
|
||||
"Title": "Trivy found a vulnerability to {{ .VulnerabilityID }} in container {{ $target }}",
|
||||
"Description": {{ escapeString $description | printf "%q" }},
|
||||
"Remediation": {
|
||||
"Recommendation": {
|
||||
"Text": "More information on this vulnerability is provided in the hyperlink",
|
||||
"Url": "{{ index .References 0 }}"
|
||||
}
|
||||
},
|
||||
"ProductFields": { "Product Name": "Trivy" },
|
||||
"Resources": [
|
||||
{
|
||||
"Type": "Container",
|
||||
"Id": "{{ $target }}",
|
||||
"Partition": "aws",
|
||||
"Region": "{{ getEnv "AWS_REGION" }}",
|
||||
"Details": {
|
||||
"Container": { "ImageName": "{{ $target }}" },
|
||||
"Other": {
|
||||
"CVE ID": "{{ .VulnerabilityID }}",
|
||||
"CVE Title": {{ .Title | printf "%q" }},
|
||||
"PkgName": "{{ .PkgName }}",
|
||||
"Installed Package": "{{ .InstalledVersion }}",
|
||||
"Patched Package": "{{ .FixedVersion }}",
|
||||
"NvdCvssScoreV3": "{{ (index .CVSS "nvd").V3Score }}",
|
||||
"NvdCvssVectorV3": "{{ (index .CVSS "nvd").V3Vector }}",
|
||||
"NvdCvssScoreV2": "{{ (index .CVSS "nvd").V2Score }}",
|
||||
"NvdCvssVectorV2": "{{ (index .CVSS "nvd").V2Vector }}"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"RecordState": "ACTIVE"
|
||||
}
|
||||
{{- end -}}
|
||||
{{- end }}
|
||||
]
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/urfave/cli/v2"
|
||||
|
||||
"github.com/aquasecurity/trivy/internal"
|
||||
"github.com/aquasecurity/trivy/pkg/report"
|
||||
)
|
||||
|
||||
type args struct {
|
||||
@@ -309,12 +310,27 @@ func TestClientServer(t *testing.T) {
|
||||
},
|
||||
golden: "testdata/busybox-with-lockfile.json.golden",
|
||||
},
|
||||
{
|
||||
name: "alpine 3.10 integration with ASFF template",
|
||||
testArgs: args{
|
||||
Format: "template",
|
||||
TemplatePath: "@../contrib/asff.tpl",
|
||||
Version: "dev",
|
||||
Input: "testdata/fixtures/alpine-310.tar.gz",
|
||||
},
|
||||
golden: "testdata/alpine-310.asff.golden",
|
||||
},
|
||||
}
|
||||
|
||||
app, addr, cacheDir := setup(t, "", "")
|
||||
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
report.Now = func() time.Time {
|
||||
return time.Date(2020, 8, 10, 7, 28, 17, 958601, time.UTC)
|
||||
}
|
||||
os.Setenv("AWS_REGION", "test-region")
|
||||
os.Setenv("AWS_ACCOUNT_ID", "123456789012")
|
||||
osArgs, outputFile, cleanup := setupClient(t, c.testArgs, addr, cacheDir, c.golden)
|
||||
defer cleanup()
|
||||
|
||||
|
||||
186
integration/testdata/alpine-310.asff.golden
vendored
Normal file
186
integration/testdata/alpine-310.asff.golden
vendored
Normal file
@@ -0,0 +1,186 @@
|
||||
[
|
||||
{
|
||||
"SchemaVersion": "2018-10-08",
|
||||
"Id": "testdata/fixtures/alpine-310.tar.gz (alpine 3.10.2)/CVE-2019-1549",
|
||||
"ProductArn": "arn:aws:securityhub:test-region::product/aquasecurity/aquasecurity",
|
||||
"GeneratorId": "Trivy",
|
||||
"AwsAccountId": "123456789012",
|
||||
"Types": [ "Software and Configuration Checks/Vulnerabilities/CVE" ],
|
||||
"CreatedAt": "2020-08-10T07:28:17.000958601Z",
|
||||
"UpdatedAt": "2020-08-10T07:28:17.000958601Z",
|
||||
"Severity": {
|
||||
"Product": 4,
|
||||
"Normalized": 40
|
||||
},
|
||||
"Title": "Trivy found a vulnerability to CVE-2019-1549 in container testdata/fixtures/alpine-310.tar.gz (alpine 3.10.2)",
|
||||
"Description": "OpenSSL 1.1.1 introduced a rewritten random number generator (RNG). This was intended to include protection in the event of a fork() system call in order to ensure that the parent and child processes did not share the same RNG state. However this protection was not being used in the default case. A partial mitigation for this issue is that the output from a high precision timer is mixed into the RNG state so the likelihood of a parent and child process sharing state is significantly reduced. If an application already calls OPENSSL_init_crypto() explicitly using OPENSSL_INIT_ATFORK then this problem does not occur at all. Fixed in OpenSSL 1.1.1d (Affected 1.1.1-1.1.1c).",
|
||||
"Remediation": {
|
||||
"Recommendation": {
|
||||
"Text": "More information on this vulnerability is provided in the hyperlink",
|
||||
"Url": "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2019-1549"
|
||||
}
|
||||
},
|
||||
"ProductFields": { "Product Name": "Trivy" },
|
||||
"Resources": [
|
||||
{
|
||||
"Type": "Container",
|
||||
"Id": "testdata/fixtures/alpine-310.tar.gz (alpine 3.10.2)",
|
||||
"Partition": "aws",
|
||||
"Region": "test-region",
|
||||
"Details": {
|
||||
"Container": { "ImageName": "testdata/fixtures/alpine-310.tar.gz (alpine 3.10.2)" },
|
||||
"Other": {
|
||||
"CVE ID": "CVE-2019-1549",
|
||||
"CVE Title": "openssl: information disclosure in fork()",
|
||||
"PkgName": "openssl",
|
||||
"Installed Package": "1.1.1c-r0",
|
||||
"Patched Package": "1.1.1d-r0",
|
||||
"NvdCvssScoreV3": "0",
|
||||
"NvdCvssVectorV3": "",
|
||||
"NvdCvssScoreV2": "0",
|
||||
"NvdCvssVectorV2": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"RecordState": "ACTIVE"
|
||||
},
|
||||
{
|
||||
"SchemaVersion": "2018-10-08",
|
||||
"Id": "testdata/fixtures/alpine-310.tar.gz (alpine 3.10.2)/CVE-2019-1551",
|
||||
"ProductArn": "arn:aws:securityhub:test-region::product/aquasecurity/aquasecurity",
|
||||
"GeneratorId": "Trivy",
|
||||
"AwsAccountId": "123456789012",
|
||||
"Types": [ "Software and Configuration Checks/Vulnerabilities/CVE" ],
|
||||
"CreatedAt": "2020-08-10T07:28:17.000958601Z",
|
||||
"UpdatedAt": "2020-08-10T07:28:17.000958601Z",
|
||||
"Severity": {
|
||||
"Product": 4,
|
||||
"Normalized": 40
|
||||
},
|
||||
"Title": "Trivy found a vulnerability to CVE-2019-1551 in container testdata/fixtures/alpine-310.tar.gz (alpine 3.10.2)",
|
||||
"Description": "There is an overflow bug in the x64_64 Montgomery squaring procedure used in exponentiation with 512-bit moduli. No EC algorithms are affected. Analysis suggests that attacks against 2-prime RSA1024, 3-prime RSA1536, and DSA1024 as a result of this defect would be very difficult to perform and are not believed likely. Attacks against DH512 are considered just feasible. However, for an attack the target would have to re-use the DH512 private key, which is not recommended anyway. Also applications directly using the low level API BN_mod_exp may be affected if they use BN_FLG_CONSTTIME. Fixed in OpenSSL 1.1.1e (Affected 1.1.1-1.1.1d). Fixed in OpenSSL 1.0.2u (Affected 1.0.2-1.0.2t).",
|
||||
"Remediation": {
|
||||
"Recommendation": {
|
||||
"Text": "More information on this vulnerability is provided in the hyperlink",
|
||||
"Url": "http://lists.opensuse.org/opensuse-security-announce/2020-01/msg00030.html"
|
||||
}
|
||||
},
|
||||
"ProductFields": { "Product Name": "Trivy" },
|
||||
"Resources": [
|
||||
{
|
||||
"Type": "Container",
|
||||
"Id": "testdata/fixtures/alpine-310.tar.gz (alpine 3.10.2)",
|
||||
"Partition": "aws",
|
||||
"Region": "test-region",
|
||||
"Details": {
|
||||
"Container": { "ImageName": "testdata/fixtures/alpine-310.tar.gz (alpine 3.10.2)" },
|
||||
"Other": {
|
||||
"CVE ID": "CVE-2019-1551",
|
||||
"CVE Title": "openssl: Integer overflow in RSAZ modular exponentiation on x86_64",
|
||||
"PkgName": "openssl",
|
||||
"Installed Package": "1.1.1c-r0",
|
||||
"Patched Package": "1.1.1d-r2",
|
||||
"NvdCvssScoreV3": "0",
|
||||
"NvdCvssVectorV3": "",
|
||||
"NvdCvssScoreV2": "0",
|
||||
"NvdCvssVectorV2": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"RecordState": "ACTIVE"
|
||||
},
|
||||
{
|
||||
"SchemaVersion": "2018-10-08",
|
||||
"Id": "testdata/fixtures/alpine-310.tar.gz (alpine 3.10.2)/CVE-2019-1563",
|
||||
"ProductArn": "arn:aws:securityhub:test-region::product/aquasecurity/aquasecurity",
|
||||
"GeneratorId": "Trivy",
|
||||
"AwsAccountId": "123456789012",
|
||||
"Types": [ "Software and Configuration Checks/Vulnerabilities/CVE" ],
|
||||
"CreatedAt": "2020-08-10T07:28:17.000958601Z",
|
||||
"UpdatedAt": "2020-08-10T07:28:17.000958601Z",
|
||||
"Severity": {
|
||||
"Product": 4,
|
||||
"Normalized": 40
|
||||
},
|
||||
"Title": "Trivy found a vulnerability to CVE-2019-1563 in container testdata/fixtures/alpine-310.tar.gz (alpine 3.10.2)",
|
||||
"Description": "In situations where an attacker receives automated notification of the success or failure of a decryption attempt an attacker, after sending a very large number of messages to be decrypted, can recover a CMS/PKCS7 transported encryption key or decrypt any RSA encrypted message that was encrypted with the public RSA key, using a Bleichenbacher padding oracle attack. Applications are not affected if they use a certificate together with the private RSA key to the CMS_decrypt or PKCS7_decrypt functions to select the correct recipient info to decrypt. Fixed in OpenSSL 1.1.1d (Affected 1.1.1-1.1.1c). Fixed in OpenSSL 1.1.0l (Affected 1.1.0-1.1.0k). Fixed in OpenSSL 1.0.2t (Affected 1.0.2-1.0.2s).",
|
||||
"Remediation": {
|
||||
"Recommendation": {
|
||||
"Text": "More information on this vulnerability is provided in the hyperlink",
|
||||
"Url": "http://packetstormsecurity.com/files/154467/Slackware-Security-Advisory-openssl-Updates.html"
|
||||
}
|
||||
},
|
||||
"ProductFields": { "Product Name": "Trivy" },
|
||||
"Resources": [
|
||||
{
|
||||
"Type": "Container",
|
||||
"Id": "testdata/fixtures/alpine-310.tar.gz (alpine 3.10.2)",
|
||||
"Partition": "aws",
|
||||
"Region": "test-region",
|
||||
"Details": {
|
||||
"Container": { "ImageName": "testdata/fixtures/alpine-310.tar.gz (alpine 3.10.2)" },
|
||||
"Other": {
|
||||
"CVE ID": "CVE-2019-1563",
|
||||
"CVE Title": "openssl: information disclosure in PKCS7_dataDecode and CMS_decrypt_set1_pkey",
|
||||
"PkgName": "openssl",
|
||||
"Installed Package": "1.1.1c-r0",
|
||||
"Patched Package": "1.1.1d-r0",
|
||||
"NvdCvssScoreV3": "0",
|
||||
"NvdCvssVectorV3": "",
|
||||
"NvdCvssScoreV2": "0",
|
||||
"NvdCvssVectorV2": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"RecordState": "ACTIVE"
|
||||
},
|
||||
{
|
||||
"SchemaVersion": "2018-10-08",
|
||||
"Id": "testdata/fixtures/alpine-310.tar.gz (alpine 3.10.2)/CVE-2019-1547",
|
||||
"ProductArn": "arn:aws:securityhub:test-region::product/aquasecurity/aquasecurity",
|
||||
"GeneratorId": "Trivy",
|
||||
"AwsAccountId": "123456789012",
|
||||
"Types": [ "Software and Configuration Checks/Vulnerabilities/CVE" ],
|
||||
"CreatedAt": "2020-08-10T07:28:17.000958601Z",
|
||||
"UpdatedAt": "2020-08-10T07:28:17.000958601Z",
|
||||
"Severity": {
|
||||
"Product": 1,
|
||||
"Normalized": 10
|
||||
},
|
||||
"Title": "Trivy found a vulnerability to CVE-2019-1547 in container testdata/fixtures/alpine-310.tar.gz (alpine 3.10.2)",
|
||||
"Description": "Normally in OpenSSL EC groups always have a co-factor present and this is used in side channel resistant code paths. However, in some cases, it is possible to construct a group using explicit parameters (instead of using a named curve). In those cases it is possible that such a group does not have the cofactor present. This can occur even where all the parameters match a known named curve. If such a curve is used then OpenSSL falls back to non-side channel resistant code paths which may result in full key recovery during an ECDSA signature operation. In order to be vulnerable an attacker would have to have the ability to time the creation of a large number of signatures where explicit parameters with no co-factor present are in use by an application using libcrypto. For the avoidance of doubt libssl is not vulnerable because explicit parameters are never used. Fixed in OpenSSL 1.1.1d (Affected 1.1.1-1.1.1c). Fixed in OpenSSL 1.1.0l (Affected 1.1.0-1.1.0k). Fixed in OpenSSL 1.0.2t (Affected 1.0.2-1.0.2s).",
|
||||
"Remediation": {
|
||||
"Recommendation": {
|
||||
"Text": "More information on this vulnerability is provided in the hyperlink",
|
||||
"Url": "http://packetstormsecurity.com/files/154467/Slackware-Security-Advisory-openssl-Updates.html"
|
||||
}
|
||||
},
|
||||
"ProductFields": { "Product Name": "Trivy" },
|
||||
"Resources": [
|
||||
{
|
||||
"Type": "Container",
|
||||
"Id": "testdata/fixtures/alpine-310.tar.gz (alpine 3.10.2)",
|
||||
"Partition": "aws",
|
||||
"Region": "test-region",
|
||||
"Details": {
|
||||
"Container": { "ImageName": "testdata/fixtures/alpine-310.tar.gz (alpine 3.10.2)" },
|
||||
"Other": {
|
||||
"CVE ID": "CVE-2019-1547",
|
||||
"CVE Title": "openssl: side-channel weak encryption vulnerability",
|
||||
"PkgName": "openssl",
|
||||
"Installed Package": "1.1.1c-r0",
|
||||
"Patched Package": "1.1.1d-r0",
|
||||
"NvdCvssScoreV3": "0",
|
||||
"NvdCvssVectorV3": "",
|
||||
"NvdCvssScoreV2": "0",
|
||||
"NvdCvssVectorV2": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"RecordState": "ACTIVE"
|
||||
}
|
||||
]
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
@@ -21,6 +22,8 @@ import (
|
||||
"github.com/olekukonko/tablewriter"
|
||||
)
|
||||
|
||||
var Now = time.Now
|
||||
|
||||
type Results []Result
|
||||
|
||||
type Result struct {
|
||||
@@ -66,6 +69,12 @@ func WriteResults(format string, output io.Writer, severities []dbTypes.Severity
|
||||
"escapeString": func(input string) string {
|
||||
return html.EscapeString(input)
|
||||
},
|
||||
"getEnv": func(key string) string {
|
||||
return os.Getenv(key)
|
||||
},
|
||||
"getCurrentTime": func() string {
|
||||
return Now().UTC().Format(time.RFC3339Nano)
|
||||
},
|
||||
}).Parse(outputTemplate)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("error parsing template: %w", err)
|
||||
|
||||
@@ -3,7 +3,9 @@ package report_test
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
@@ -303,9 +305,19 @@ func TestReportWriter_Template(t *testing.T) {
|
||||
template: `{{ range . }}{{ range .Vulnerabilities}}{{.VulnerabilityID}} {{ endWithPeriod (escapeString .Description) | printf "%q" }}{{ end }}{{ end }}`,
|
||||
expected: `CVE-2019-0000 "without period."CVE-2019-0000 "with period."CVE-2019-0000 "with period and unescaped string curl: Use-after-free when closing 'easy' handle in Curl_close()."`,
|
||||
},
|
||||
{
|
||||
name: "happy path: env var parsing and getCurrentTime",
|
||||
detectedVulns: []types.DetectedVulnerability{},
|
||||
template: `{{ toLower (getEnv "AWS_ACCOUNT_ID") }} {{ getCurrentTime }}`,
|
||||
expected: `123456789012 2020-08-10T07:28:17.000958601Z`,
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
report.Now = func() time.Time {
|
||||
return time.Date(2020, 8, 10, 7, 28, 17, 958601, time.UTC)
|
||||
}
|
||||
os.Setenv("AWS_ACCOUNT_ID", "123456789012")
|
||||
tmplWritten := bytes.Buffer{}
|
||||
inputResults := report.Results{
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user