mirror of
https://github.com/aquasecurity/trivy.git
synced 2026-12-02 01:40:33 +00:00
refactor(crypto): clean up crypto code and docs (#11485)
This commit is contained in:
1 parent
559ee38654
commit
1d1ab074ed
21 files changed
+300
-290
No files matched your search
@@ -9,6 +9,7 @@ Supported values:
|
||||
- misconfig
|
||||
- secret
|
||||
- license
|
||||
- crypto[^1]
|
||||
|
||||
For example, container image scanning enables vulnerability and secret scanners by default.
|
||||
If you don't need secret scanning, it can be disabled.
|
||||
@@ -24,7 +25,7 @@ $ trivy image --scanners vuln alpine:3.15
|
||||
| Misconfiguration | ✓ |
|
||||
| Secret | ✓ |
|
||||
| License | ✓ |
|
||||
| Crypto | ✓ |
|
||||
| Crypto | ✓[^1] |
|
||||
|
||||
By default, `Trivy` exits with code 0 even when security issues are detected.
|
||||
Use the `--exit-code` option if you want to exit with a non-zero exit code.
|
||||
@@ -180,3 +181,5 @@ You can disable telemetry collection using the `--disable-telemetry` flag.
|
||||
!!! note
|
||||
The [version check](#check-for-updates) and telemetry share a single request to `check.trivy.dev`, so the request is still sent unless the version check is disabled as well.
|
||||
See [Check updates service](../advanced/air-gap.md#check-updates-service) for details.
|
||||
|
||||
[^1]: Experimental. Available for container image scanning only. See [Cryptographic Asset Scanning](../scanner/crypto.md).
|
||||
@@ -18,7 +18,7 @@ Trivy supports the following formats:
|
||||
| Misconfiguration | ✓ |
|
||||
| Secret | ✓ |
|
||||
| License | ✓ |
|
||||
| Crypto | ✓ |
|
||||
| Crypto | ✓[^2] |
|
||||
|
||||
```bash
|
||||
$ trivy image -f table golang:1.22.11-alpine3.20
|
||||
@@ -268,6 +268,7 @@ Then, you can try to update **axios@0.21.4** and **cra-append-sw@2.7.0** to reso
|
||||
| Misconfiguration | ✓ |
|
||||
| Secret | ✓ |
|
||||
| License | ✓ |
|
||||
| Crypto | ✓[^2] |
|
||||
|
||||
```
|
||||
$ trivy image -f json -o results.json alpine:latest
|
||||
@@ -460,6 +461,7 @@ $ trivy image -f json -o results.json alpine:latest
|
||||
| Misconfiguration | ✓ |
|
||||
| Secret | ✓ |
|
||||
| License | ✓ |
|
||||
| Crypto | |
|
||||
|
||||
[SARIF][sarif-home] (Static Analysis Results Interchange Format) complying with [SARIF 2.1.0 OASIS standard][sarif-spec] can be generated with the `--format sarif` flag.
|
||||
|
||||
@@ -494,6 +496,7 @@ This snapshot file can be [submitted][github-sbom-submit] to your GitHub reposit
|
||||
| Misconfiguration | ✓ |
|
||||
| Secret | ✓ |
|
||||
| License | ✓ |
|
||||
| Crypto | |
|
||||
|
||||
#### Custom Template
|
||||
|
||||
@@ -556,6 +559,7 @@ If Trivy is installed using rpm then default templates can be found at `/usr/loc
|
||||
| Misconfiguration | ✓ |
|
||||
| Secret | ✓ |
|
||||
| License | ✓ |
|
||||
| Crypto | |
|
||||
|
||||
In the following example using the template `junit.tpl` XML can be generated.
|
||||
```
|
||||
@@ -569,6 +573,7 @@ $ trivy image --format template --template "@contrib/junit.tpl" -o junit-report.
|
||||
| Misconfiguration | ✓ |
|
||||
| Secret | ✓ |
|
||||
| License | |
|
||||
| Crypto | |
|
||||
|
||||
Trivy also supports an [ASFF template for reporting findings to AWS Security Hub][asff]
|
||||
|
||||
@@ -579,6 +584,7 @@ Trivy also supports an [ASFF template for reporting findings to AWS Security Hub
|
||||
| Misconfiguration | ✓ |
|
||||
| Secret | |
|
||||
| License | |
|
||||
| Crypto | |
|
||||
|
||||
```
|
||||
$ trivy image --format template --template "@contrib/html.tpl" -o report.html golang:1.12-alpine
|
||||
@@ -638,6 +644,8 @@ $ trivy image --format json -o result.json debian:11
|
||||
$ trivy convert --format table --severity CRITICAL result.json
|
||||
```
|
||||
|
||||
The findings of the [crypto scanner](../scanner/crypto.md#findings) are kept only for the formats that show them (`table` and `json`).
|
||||
|
||||
!!! note
|
||||
JSON reports from "trivy k8s" are not yet supported.
|
||||
|
||||
@@ -670,4 +678,5 @@ $ trivy convert --format table --severity CRITICAL result.json
|
||||
[pubspec-lock]: ../coverage/language/dart.md#dart
|
||||
[cargo-binaries]: ../coverage/language/rust.md#binaries
|
||||
|
||||
[^1]: To show summary table in `convert` mode - you need to enable the scanners used during JSON report generation.
|
||||
[^1]: To show summary table in `convert` mode - you need to enable the scanners used during JSON report generation.
|
||||
[^2]: Experimental. Available for container image scanning only. See [Cryptographic Asset Scanning](../scanner/crypto.md).
|
||||
@@ -23,6 +23,7 @@ This feature is relevant for the following scanners:
|
||||
| Misconfiguration | ✓ |
|
||||
| Secret | ✓ |
|
||||
| License | ✓ |
|
||||
| Crypto | ✓[^2] |
|
||||
|
||||
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/v4@v4.8.1#readme-patterns).
|
||||
|
||||
@@ -85,7 +86,7 @@ This feature is relevant for the following scanners:
|
||||
| Misconfiguration | ✓ |
|
||||
| Secret | |
|
||||
| License | ✓[^1] |
|
||||
| Crypto | ✓ |
|
||||
| Crypto | ✓[^2] |
|
||||
|
||||
The list of analyzers can be found [here](https://github.com/aquasecurity/trivy/tree/{{ git.commit }}/pkg/fanal/analyzer/const.go).
|
||||
Note that this flag is not applicable for parsers that accepts files of different extensions, for example the Terraform file parser which handles .tf and .tf.json files.
|
||||
@@ -106,6 +107,7 @@ trivy fs --scanners misconfig,vuln --file-patterns "dockerfile:.production.docke
|
||||
```
|
||||
|
||||
[^1]: Only work with the [license-full](../scanner/license.md) flag
|
||||
[^2]: Experimental. Available for container image scanning only. See [Cryptographic Asset Scanning](../scanner/crypto.md).
|
||||
|
||||
## Avoid full filesystem traversal
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ Some scanners run on the client side, even in client/server mode.
|
||||
| Misconfiguration | Client[^1] |
|
||||
| Secret | Client[^2] |
|
||||
| License | Server |
|
||||
| Crypto | Server[^3] |
|
||||
|
||||
!!! note
|
||||
Scanning of misconfigurations and secrets is performed on the client side (as in standalone mode).
|
||||
@@ -350,4 +351,5 @@ Returns the `200 OK` status if the request was successful.
|
||||

|
||||
|
||||
[^1]: The checks bundle is also downloaded on the client side.
|
||||
[^2]: The scan result with masked secrets is sent to the server
|
||||
[^2]: The scan result with masked secrets is sent to the server
|
||||
[^3]: See [Cryptographic Asset Scanning](../../scanner/crypto.md#clientserver-mode).
|
||||
@@ -129,7 +129,7 @@ A small RSA key or a small EC private key gets the finding on its key asset. The
|
||||
|
||||
A certificate signed with RSA-PSS gives no SHA-1 finding, because Trivy does not read the hash from the RSA-PSS parameters.
|
||||
|
||||
Every entry of `CryptoAssets` in JSON output has an `AssetID`, and a finding refers to its asset by this ID. The ID depends on the asset identity, the file path, the layer and the format. Two scans of one image with the same Trivy version give the same IDs. `Evidence` holds the values the check was evaluated on.
|
||||
Every entry of `CryptoAssets` in JSON output has an `AssetID`, and a finding refers to its asset by this ID. The ID depends on the asset identity, the file path and the format. Two scans of one image with the same Trivy version give the same IDs. `Evidence` holds the values the check was evaluated on.
|
||||
|
||||
```json
|
||||
{
|
||||
@@ -189,7 +189,15 @@ Total: 4 (UNKNOWN: 0, LOW: 0, MEDIUM: 1, HIGH: 3, CRITICAL: 0)
|
||||
└──────────┴─────────────┴───────────────────────────────────┴──────────────────────┴─────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
Findings are evaluated only for JSON and table output, so they do not affect the exit code of CycloneDX output. `trivy convert` keeps the findings of a JSON report only when converting to JSON or table, and keeps the cryptographic assets for any format.
|
||||
Each format contains the findings, the assets or both, as the table shows. Findings are evaluated only for the formats that contain them, so they do not affect the exit code of CycloneDX output. `trivy convert` can write the findings of a JSON report only to JSON and table, while the assets can be converted only to JSON and CycloneDX.
|
||||
|
||||
| Format | Findings | Assets |
|
||||
| --- | :---: | :---: |
|
||||
| Table | ✓ | |
|
||||
| JSON | ✓ | ✓ |
|
||||
| CycloneDX | | ✓ |
|
||||
|
||||
`--severity` filters only the findings. `--skip-files` and `--skip-dirs` exclude the assets.
|
||||
|
||||
A rule ID in an [ignore file](../configuration/filtering.md#by-finding-ids) suppresses its findings, and the `crypto` section of `.trivyignore.yaml` can limit that to some file paths. The assets of suppressed findings stay in the inventory. `--ignore-policy` and `--show-suppressed` do not apply to crypto findings.
|
||||
|
||||
@@ -209,7 +217,7 @@ Two attributes in the report separate them. The path says whether a certificate
|
||||
|
||||
## Client/server mode
|
||||
|
||||
The scanner works the same way in [client/server mode](../references/modes/client-server.md).
|
||||
The scanner also works in [client/server mode](../references/modes/client-server.md). The client extracts the cryptographic assets from the image. The server assesses their security levels and runs the checks, and it uses its own clock to decide whether a certificate has expired.
|
||||
|
||||
## Limitations
|
||||
|
||||
|
||||
@@ -96,11 +96,14 @@ func TestConvertCrypto(t *testing.T) {
|
||||
name string
|
||||
format types.Format
|
||||
golden string
|
||||
args []string
|
||||
}{
|
||||
{
|
||||
name: "cyclonedx",
|
||||
format: types.FormatCycloneDX,
|
||||
golden: goldenCryptoCDX,
|
||||
// Fails if the findings of the JSON input are not dropped before the exit code check
|
||||
args: []string{"--exit-code", "1"},
|
||||
},
|
||||
{
|
||||
name: "json",
|
||||
@@ -120,6 +123,7 @@ func TestConvertCrypto(t *testing.T) {
|
||||
"--format",
|
||||
string(tt.format),
|
||||
}
|
||||
osArgs = append(osArgs, tt.args...)
|
||||
|
||||
runTest(t, osArgs, tt.golden, tt.format, runOptions{
|
||||
fakeUUID: "3ff14136-e09f-4df9-80ea-%012d",
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/mitchellh/hashstructure/v2"
|
||||
|
||||
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
|
||||
"github.com/aquasecurity/trivy/pkg/log"
|
||||
)
|
||||
|
||||
// AssignIDs sets the ID of every asset. The ID is derived from the descriptor, the file path
|
||||
// and the format, so the same material found in different places gets different IDs.
|
||||
func AssignIDs(assets []ftypes.CryptoAsset) {
|
||||
for i := range assets {
|
||||
assets[i].AssetID = assetID(assets[i])
|
||||
}
|
||||
}
|
||||
|
||||
func assetID(asset ftypes.CryptoAsset) string {
|
||||
v := map[string]any{
|
||||
"descriptor": asset.Descriptor().String(),
|
||||
"filePath": asset.FilePath,
|
||||
"format": asset.Format,
|
||||
}
|
||||
hash, err := hashstructure.Hash(v, hashstructure.FormatV2, &hashstructure.HashOptions{
|
||||
ZeroNil: true,
|
||||
IgnoreZeroValue: true,
|
||||
})
|
||||
if err != nil {
|
||||
log.Warn("Failed to calculate the cryptographic asset hash",
|
||||
log.String("asset", asset.Descriptor().String()), log.FilePath(asset.FilePath), log.Err(err))
|
||||
}
|
||||
return strconv.FormatUint(hash, 16)
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
package crypto_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/aquasecurity/trivy/internal/cryptotest"
|
||||
"github.com/aquasecurity/trivy/pkg/crypto"
|
||||
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
|
||||
)
|
||||
|
||||
func TestAssignIDs(t *testing.T) {
|
||||
base := cryptotest.PublicKeyAsset(cryptotest.WithMutate(func(asset *ftypes.CryptoAsset) {
|
||||
asset.Layer = ftypes.Layer{DiffID: "sha256:9a4f"}
|
||||
}))
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
mutate func(*ftypes.CryptoAsset)
|
||||
same bool
|
||||
}{
|
||||
{
|
||||
name: "same asset",
|
||||
mutate: func(*ftypes.CryptoAsset) {},
|
||||
same: true,
|
||||
},
|
||||
{
|
||||
name: "different name and encoding",
|
||||
mutate: func(asset *ftypes.CryptoAsset) {
|
||||
asset.Name = "RSA-2048 public key"
|
||||
asset.Encoding = ftypes.CryptoEncodingDER
|
||||
},
|
||||
same: true,
|
||||
},
|
||||
{
|
||||
name: "different identity",
|
||||
mutate: func(asset *ftypes.CryptoAsset) {
|
||||
asset.Identity.Value = "4f1c"
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "different file path",
|
||||
mutate: func(asset *ftypes.CryptoAsset) {
|
||||
asset.FilePath = "/etc/other.pem"
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "same file in a different layer",
|
||||
mutate: func(asset *ftypes.CryptoAsset) {
|
||||
asset.Layer.DiffID = "sha256:3c1d"
|
||||
},
|
||||
same: true,
|
||||
},
|
||||
{
|
||||
name: "key without a container of its own",
|
||||
mutate: func(asset *ftypes.CryptoAsset) {
|
||||
asset.Format = ""
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
other := cryptotest.PublicKeyAsset(cryptotest.WithMutate(func(asset *ftypes.CryptoAsset) {
|
||||
asset.Layer = base.Layer
|
||||
tt.mutate(asset)
|
||||
}))
|
||||
assets := []ftypes.CryptoAsset{base, other}
|
||||
crypto.AssignIDs(assets)
|
||||
assert.NotEmpty(t, assets[0].AssetID)
|
||||
assert.Equal(t, tt.same, assets[0].AssetID == assets[1].AssetID)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -113,40 +113,6 @@ func DescribeEncryptedKey(
|
||||
}
|
||||
}
|
||||
|
||||
// LinkKeyPairs points every private key at the public key derived from it, when both were
|
||||
// found. A public key states nothing about the existence of a private one, so the
|
||||
// reference runs in one direction only.
|
||||
//
|
||||
// The two halves are told apart by the key type and share an identity, since a key is
|
||||
// identified by the digest of its SubjectPublicKeyInfo. An encrypted container is left
|
||||
// alone, because its identity is the digest of the container, which never equals the
|
||||
// digest of a SubjectPublicKeyInfo.
|
||||
func LinkKeyPairs(assets []ftypes.CryptoAsset) {
|
||||
public := make(map[ftypes.CryptoIdentity]ftypes.CryptoDescriptor)
|
||||
for _, asset := range assets {
|
||||
if asset.Kind == ftypes.CryptoKindKey && asset.KeyType == ftypes.CryptoKeyTypePublic {
|
||||
public[asset.Identity] = asset.Descriptor()
|
||||
}
|
||||
}
|
||||
if len(public) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
for i, asset := range assets {
|
||||
if asset.Kind != ftypes.CryptoKindKey || asset.KeyType != ftypes.CryptoKeyTypePrivate {
|
||||
continue
|
||||
}
|
||||
descriptor, found := public[asset.Identity]
|
||||
if !found {
|
||||
continue
|
||||
}
|
||||
assets[i].Relationships = append(assets[i].Relationships, ftypes.CryptoRelationship{
|
||||
Type: ftypes.CryptoRelationshipCorrespondsTo,
|
||||
RelatedAsset: descriptor,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// keyDetails reports the size in bits and the curve of a public key, and for DSA the
|
||||
// subgroup size in bits.
|
||||
func keyDetails(pub stdcrypto.PublicKey) (size, subgroupSize int, curve string) {
|
||||
|
||||
@@ -13,13 +13,11 @@ import (
|
||||
"encoding/asn1"
|
||||
"math/big"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/aquasecurity/trivy/internal/cryptotest"
|
||||
"github.com/aquasecurity/trivy/pkg/crypto"
|
||||
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
|
||||
)
|
||||
@@ -629,95 +627,6 @@ func TestMarshalPublicKey(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLinkKeyPairs(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
publicKey := cryptotest.PublicKeyAsset()
|
||||
otherPublicKey := cryptotest.PublicKeyAsset(cryptotest.WithMutate(func(asset *ftypes.CryptoAsset) {
|
||||
asset.Identity.Value = strings.Repeat("d", 64)
|
||||
asset.FilePath = "etc/ssl/certs/other.pem"
|
||||
}))
|
||||
correspondsTo := ftypes.CryptoRelationship{
|
||||
Type: ftypes.CryptoRelationshipCorrespondsTo,
|
||||
RelatedAsset: publicKey.Descriptor(),
|
||||
}
|
||||
privateKeyAt := func(path string, relationships ...ftypes.CryptoRelationship) ftypes.CryptoAsset {
|
||||
return cryptotest.PrivateKeyAsset(cryptotest.WithMutate(func(asset *ftypes.CryptoAsset) {
|
||||
asset.FilePath = path
|
||||
asset.Relationships = relationships
|
||||
}))
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
assets []ftypes.CryptoAsset
|
||||
want []ftypes.CryptoAsset
|
||||
}{
|
||||
{
|
||||
name: "a private key found without its public half",
|
||||
assets: []ftypes.CryptoAsset{privateKeyAt("etc/ssl/private/server.key")},
|
||||
want: []ftypes.CryptoAsset{privateKeyAt("etc/ssl/private/server.key")},
|
||||
},
|
||||
{
|
||||
name: "a private key and the public key derived from it",
|
||||
assets: []ftypes.CryptoAsset{
|
||||
privateKeyAt("etc/ssl/private/server.key"),
|
||||
publicKey,
|
||||
},
|
||||
want: []ftypes.CryptoAsset{
|
||||
privateKeyAt("etc/ssl/private/server.key", correspondsTo),
|
||||
publicKey,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "another public key of the same size",
|
||||
assets: []ftypes.CryptoAsset{
|
||||
privateKeyAt("etc/ssl/private/server.key"),
|
||||
otherPublicKey,
|
||||
publicKey,
|
||||
},
|
||||
want: []ftypes.CryptoAsset{
|
||||
privateKeyAt("etc/ssl/private/server.key", correspondsTo),
|
||||
otherPublicKey,
|
||||
publicKey,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "the same private key found in two files",
|
||||
assets: []ftypes.CryptoAsset{
|
||||
privateKeyAt("etc/ssl/private/server.key"),
|
||||
privateKeyAt("opt/app/server.key"),
|
||||
publicKey,
|
||||
},
|
||||
want: []ftypes.CryptoAsset{
|
||||
privateKeyAt("etc/ssl/private/server.key", correspondsTo),
|
||||
privateKeyAt("opt/app/server.key", correspondsTo),
|
||||
publicKey,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "an encrypted container is identified by the container itself",
|
||||
assets: []ftypes.CryptoAsset{
|
||||
cryptotest.EncryptedPrivateKeyAsset(),
|
||||
publicKey,
|
||||
},
|
||||
want: []ftypes.CryptoAsset{
|
||||
cryptotest.EncryptedPrivateKeyAsset(),
|
||||
publicKey,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
crypto.LinkKeyPairs(tt.assets)
|
||||
assert.Equal(t, tt.want, tt.assets)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func newKeyFixtures(t *testing.T) keyFixtures {
|
||||
t.Helper()
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package check
|
||||
package scanner
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
@@ -1,5 +1,5 @@
|
||||
// Package check evaluates the built-in checks of cryptographic assets.
|
||||
package check
|
||||
// Package scanner scans cryptographic assets with the built-in checks.
|
||||
package scanner
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
@@ -41,9 +41,9 @@ type match struct {
|
||||
finding types.CryptoFinding
|
||||
}
|
||||
|
||||
// Evaluate returns the findings of the built-in checks for the given assets. The assets must
|
||||
// Scan returns the findings of the built-in checks for the given assets. The assets must
|
||||
// already have their IDs and the security levels of algorithm assets.
|
||||
func Evaluate(assets []ftypes.CryptoAsset, now time.Time) []types.CryptoFinding {
|
||||
func Scan(assets []ftypes.CryptoAsset, now time.Time) []types.CryptoFinding {
|
||||
var matches []match
|
||||
for _, asset := range assets {
|
||||
for _, r := range rules {
|
||||
@@ -1,4 +1,4 @@
|
||||
package check_test
|
||||
package scanner_test
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/aquasecurity/trivy/pkg/crypto"
|
||||
"github.com/aquasecurity/trivy/pkg/crypto/check"
|
||||
"github.com/aquasecurity/trivy/pkg/crypto/scanner"
|
||||
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
|
||||
"github.com/aquasecurity/trivy/pkg/types"
|
||||
)
|
||||
@@ -39,8 +39,8 @@ func algorithmDescriptor(oid, parameters string) ftypes.CryptoDescriptor {
|
||||
|
||||
// withID sets the ID the scan assigns to the asset.
|
||||
func withID(asset ftypes.CryptoAsset) ftypes.CryptoAsset {
|
||||
assets := []ftypes.CryptoAsset{asset}
|
||||
crypto.AssignIDs(assets)
|
||||
assets := ftypes.CryptoAssets{asset}
|
||||
assets.Finalize()
|
||||
return assets[0]
|
||||
}
|
||||
|
||||
@@ -173,7 +173,7 @@ const (
|
||||
oidSHA256WithRSA = "1.2.840.113549.1.1.11"
|
||||
)
|
||||
|
||||
func TestEvaluate(t *testing.T) {
|
||||
func TestScan(t *testing.T) {
|
||||
valid := now.AddDate(1, 0, 0)
|
||||
|
||||
tests := []struct {
|
||||
@@ -348,7 +348,7 @@ func TestEvaluate(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := check.Evaluate(tt.assets, now)
|
||||
got := scanner.Scan(tt.assets, now)
|
||||
assert.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
@@ -416,7 +416,7 @@ func TestMessage(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert.Equal(t, tt.want, check.Message(tt.finding))
|
||||
assert.Equal(t, tt.want, scanner.Message(tt.finding))
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"github.com/package-url/packageurl-go"
|
||||
"github.com/samber/lo"
|
||||
|
||||
"github.com/aquasecurity/trivy/pkg/crypto"
|
||||
"github.com/aquasecurity/trivy/pkg/dependency"
|
||||
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
|
||||
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
|
||||
@@ -214,11 +213,9 @@ func ApplyLayers(layers []ftypes.BlobInfo) ftypes.ArtifactDetail {
|
||||
return nil
|
||||
})
|
||||
|
||||
// A key file states nothing about where the other half of the pair was found, so the
|
||||
// two are linked here, once every layer has been applied.
|
||||
crypto.LinkKeyPairs(mergedLayer.CryptoAssets)
|
||||
// IDs are assigned after merging, when the final list of assets is known.
|
||||
crypto.AssignIDs(mergedLayer.CryptoAssets)
|
||||
// IDs are assigned and key pairs are linked after merging, when the final list of assets
|
||||
// is known. A key file states nothing about where the other half of the pair was found.
|
||||
mergedLayer.CryptoAssets.Finalize()
|
||||
|
||||
for _, s := range secretsMap {
|
||||
mergedLayer.Secrets = append(mergedLayer.Secrets, s)
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/aquasecurity/trivy/internal/cryptotest"
|
||||
"github.com/aquasecurity/trivy/pkg/crypto"
|
||||
"github.com/aquasecurity/trivy/pkg/fanal/applier"
|
||||
"github.com/aquasecurity/trivy/pkg/fanal/types"
|
||||
)
|
||||
@@ -1616,10 +1615,6 @@ func TestApplyLayers(t *testing.T) {
|
||||
Digest: "sha256:24df0d4e20c0f42d3703bf1f1db2bdd77346c7956f74f423603d651e8e5ae8a7",
|
||||
DiffID: "sha256:aad63a9339440e7c3e1fff2b988991b9bfb81280042fa7f39a5e327023056819",
|
||||
}
|
||||
asset.Relationships = []types.CryptoRelationship{{
|
||||
Type: types.CryptoRelationshipCorrespondsTo,
|
||||
RelatedAsset: cryptotest.PublicKeyAsset().Descriptor(),
|
||||
}}
|
||||
})),
|
||||
cryptotest.PublicKeyAsset(cryptotest.WithMutate(func(asset *types.CryptoAsset) {
|
||||
asset.FilePath = "etc/ssl/certs/server.pem"
|
||||
@@ -1824,7 +1819,7 @@ func TestApplyLayers(t *testing.T) {
|
||||
}
|
||||
return got.CustomResources[i].FilePath < got.CustomResources[j].FilePath
|
||||
})
|
||||
crypto.AssignIDs(tt.want.CryptoAssets)
|
||||
tt.want.CryptoAssets.Finalize()
|
||||
assert.Equal(t, tt.want, got, tt.name)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -230,7 +230,7 @@ type ArtifactDetail struct {
|
||||
Misconfigurations []Misconfiguration `json:",omitempty"`
|
||||
Secrets Secrets `json:",omitempty"`
|
||||
Licenses LicenseFiles `json:",omitempty"`
|
||||
CryptoAssets []CryptoAsset `json:",omitempty"`
|
||||
CryptoAssets CryptoAssets `json:",omitempty"`
|
||||
|
||||
// ImageConfig has information from container image config
|
||||
ImageConfig ImageConfigDetail
|
||||
|
||||
@@ -5,9 +5,13 @@ import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"slices"
|
||||
"strconv"
|
||||
|
||||
"github.com/mitchellh/hashstructure/v2"
|
||||
"github.com/samber/lo"
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"github.com/aquasecurity/trivy/pkg/log"
|
||||
)
|
||||
|
||||
// CryptoKind identifies the category of a cryptographic asset.
|
||||
@@ -281,3 +285,66 @@ func (a CryptoAsset) Clone() CryptoAsset {
|
||||
a.CryptoAssetInfo = a.CryptoAssetInfo.Clone()
|
||||
return a
|
||||
}
|
||||
|
||||
// CryptoAssets is a list of cryptographic assets.
|
||||
type CryptoAssets []CryptoAsset
|
||||
|
||||
// Finalize sets the ID of every asset and links key pairs.
|
||||
func (assets CryptoAssets) Finalize() {
|
||||
assets.assignIDs()
|
||||
assets.linkKeyPairs()
|
||||
}
|
||||
|
||||
func (assets CryptoAssets) assignIDs() {
|
||||
for i := range assets {
|
||||
assets[i].AssetID = assetID(assets[i])
|
||||
}
|
||||
}
|
||||
|
||||
// linkKeyPairs points every private key at the public key derived from it, when both were
|
||||
// found. An encrypted container is never linked, because its identity is the digest of the
|
||||
// container and not of a SubjectPublicKeyInfo.
|
||||
func (assets CryptoAssets) linkKeyPairs() {
|
||||
public := make(map[CryptoIdentity]CryptoDescriptor)
|
||||
for _, asset := range assets {
|
||||
if asset.Kind == CryptoKindKey && asset.KeyType == CryptoKeyTypePublic {
|
||||
public[asset.Identity] = asset.Descriptor()
|
||||
}
|
||||
}
|
||||
if len(public) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
for i, asset := range assets {
|
||||
if asset.Kind != CryptoKindKey || asset.KeyType != CryptoKeyTypePrivate {
|
||||
continue
|
||||
}
|
||||
descriptor, found := public[asset.Identity]
|
||||
if !found {
|
||||
continue
|
||||
}
|
||||
assets[i].Relationships = append(assets[i].Relationships, CryptoRelationship{
|
||||
Type: CryptoRelationshipCorrespondsTo,
|
||||
RelatedAsset: descriptor,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// assetID derives the ID from the descriptor, the file path and the format, so the same
|
||||
// material found in different files gets different IDs.
|
||||
func assetID(asset CryptoAsset) string {
|
||||
v := map[string]any{
|
||||
"descriptor": asset.Descriptor().String(),
|
||||
"filePath": asset.FilePath,
|
||||
"format": asset.Format,
|
||||
}
|
||||
hash, err := hashstructure.Hash(v, hashstructure.FormatV2, &hashstructure.HashOptions{
|
||||
ZeroNil: true,
|
||||
IgnoreZeroValue: true,
|
||||
})
|
||||
if err != nil {
|
||||
log.Warn("Failed to calculate the cryptographic asset hash",
|
||||
log.String("asset", asset.Descriptor().String()), log.FilePath(asset.FilePath), log.Err(err))
|
||||
}
|
||||
return strconv.FormatUint(hash, 16)
|
||||
}
|
||||
@@ -518,3 +518,156 @@ func TestCryptoAssetClone(t *testing.T) {
|
||||
assert.Equal(t, 0, *source.Algorithm.NISTQuantumSecurityLevel)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCryptoAssetsFinalize_AssetID(t *testing.T) {
|
||||
base := cryptotest.PublicKeyAsset(cryptotest.WithMutate(func(asset *types.CryptoAsset) {
|
||||
asset.Layer = types.Layer{DiffID: "sha256:9a4f"}
|
||||
}))
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
mutate func(*types.CryptoAsset)
|
||||
same bool
|
||||
}{
|
||||
{
|
||||
name: "same asset",
|
||||
mutate: func(*types.CryptoAsset) {},
|
||||
same: true,
|
||||
},
|
||||
{
|
||||
name: "different name and encoding",
|
||||
mutate: func(asset *types.CryptoAsset) {
|
||||
asset.Name = "RSA-2048 public key"
|
||||
asset.Encoding = types.CryptoEncodingDER
|
||||
},
|
||||
same: true,
|
||||
},
|
||||
{
|
||||
name: "different identity",
|
||||
mutate: func(asset *types.CryptoAsset) {
|
||||
asset.Identity.Value = "4f1c"
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "different file path",
|
||||
mutate: func(asset *types.CryptoAsset) {
|
||||
asset.FilePath = "/etc/other.pem"
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "same file in a different layer",
|
||||
mutate: func(asset *types.CryptoAsset) {
|
||||
asset.Layer.DiffID = "sha256:3c1d"
|
||||
},
|
||||
same: true,
|
||||
},
|
||||
{
|
||||
name: "key without a container of its own",
|
||||
mutate: func(asset *types.CryptoAsset) {
|
||||
asset.Format = ""
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
other := cryptotest.PublicKeyAsset(cryptotest.WithMutate(func(asset *types.CryptoAsset) {
|
||||
asset.Layer = base.Layer
|
||||
tt.mutate(asset)
|
||||
}))
|
||||
assets := types.CryptoAssets{base, other}
|
||||
assets.Finalize()
|
||||
assert.NotEmpty(t, assets[0].AssetID)
|
||||
assert.Equal(t, tt.same, assets[0].AssetID == assets[1].AssetID)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCryptoAssetsFinalize_KeyPairs(t *testing.T) {
|
||||
publicKey := cryptotest.PublicKeyAsset()
|
||||
otherPublicKey := cryptotest.PublicKeyAsset(cryptotest.WithMutate(func(asset *types.CryptoAsset) {
|
||||
asset.Identity.Value = strings.Repeat("d", 64)
|
||||
asset.FilePath = "etc/ssl/certs/other.pem"
|
||||
}))
|
||||
correspondsTo := types.CryptoRelationship{
|
||||
Type: types.CryptoRelationshipCorrespondsTo,
|
||||
RelatedAsset: publicKey.Descriptor(),
|
||||
}
|
||||
privateKeyAt := func(path string, relationships ...types.CryptoRelationship) types.CryptoAsset {
|
||||
return cryptotest.PrivateKeyAsset(cryptotest.WithMutate(func(asset *types.CryptoAsset) {
|
||||
asset.FilePath = path
|
||||
asset.Relationships = relationships
|
||||
}))
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
assets types.CryptoAssets
|
||||
want types.CryptoAssets
|
||||
}{
|
||||
{
|
||||
name: "a private key found without its public half",
|
||||
assets: types.CryptoAssets{privateKeyAt("etc/ssl/private/server.key")},
|
||||
want: types.CryptoAssets{privateKeyAt("etc/ssl/private/server.key")},
|
||||
},
|
||||
{
|
||||
name: "a private key and the public key derived from it",
|
||||
assets: types.CryptoAssets{
|
||||
privateKeyAt("etc/ssl/private/server.key"),
|
||||
publicKey,
|
||||
},
|
||||
want: types.CryptoAssets{
|
||||
privateKeyAt("etc/ssl/private/server.key", correspondsTo),
|
||||
publicKey,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "another public key of the same size",
|
||||
assets: types.CryptoAssets{
|
||||
privateKeyAt("etc/ssl/private/server.key"),
|
||||
otherPublicKey,
|
||||
publicKey,
|
||||
},
|
||||
want: types.CryptoAssets{
|
||||
privateKeyAt("etc/ssl/private/server.key", correspondsTo),
|
||||
otherPublicKey,
|
||||
publicKey,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "the same private key found in two files",
|
||||
assets: types.CryptoAssets{
|
||||
privateKeyAt("etc/ssl/private/server.key"),
|
||||
privateKeyAt("opt/app/server.key"),
|
||||
publicKey,
|
||||
},
|
||||
want: types.CryptoAssets{
|
||||
privateKeyAt("etc/ssl/private/server.key", correspondsTo),
|
||||
privateKeyAt("opt/app/server.key", correspondsTo),
|
||||
publicKey,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "an encrypted container is identified by the container itself",
|
||||
assets: types.CryptoAssets{
|
||||
cryptotest.EncryptedPrivateKeyAsset(),
|
||||
publicKey,
|
||||
},
|
||||
want: types.CryptoAssets{
|
||||
cryptotest.EncryptedPrivateKeyAsset(),
|
||||
publicKey,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tt.assets.Finalize()
|
||||
// IDs are covered by the other test
|
||||
for i := range tt.assets {
|
||||
tt.assets[i].AssetID = ""
|
||||
}
|
||||
assert.Equal(t, tt.want, tt.assets)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
|
||||
"github.com/aquasecurity/tml"
|
||||
dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
|
||||
"github.com/aquasecurity/trivy/pkg/crypto/check"
|
||||
"github.com/aquasecurity/trivy/pkg/crypto/scanner"
|
||||
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
|
||||
"github.com/aquasecurity/trivy/pkg/types"
|
||||
)
|
||||
@@ -51,7 +51,7 @@ func (r *cryptoRenderer) Render(result types.Result) {
|
||||
cmp.Compare(a.RuleID, b.RuleID),
|
||||
cmp.Compare(assetA.FilePath, assetB.FilePath),
|
||||
cmp.Compare(cryptoAssetLabel(assetA), cryptoAssetLabel(assetB)),
|
||||
cmp.Compare(check.Message(a), check.Message(b)),
|
||||
cmp.Compare(scanner.Message(a), scanner.Message(b)),
|
||||
)
|
||||
})
|
||||
|
||||
@@ -75,7 +75,7 @@ func (r *cryptoRenderer) Render(result types.Result) {
|
||||
if row.count > 1 {
|
||||
label = fmt.Sprintf("%s x%d", label, row.count)
|
||||
}
|
||||
message := tml.Sprintf("%s\n<blue>%s</blue>", check.Message(f), cryptoRuleURL(f.RuleID))
|
||||
message := tml.Sprintf("%s\n<blue>%s</blue>", scanner.Message(f), cryptoRuleURL(f.RuleID))
|
||||
tw.AddRow(severity, f.RuleID, label, row.asset.FilePath, message)
|
||||
}
|
||||
tw.Render()
|
||||
@@ -98,7 +98,7 @@ func groupCryptoFindings(findings []types.CryptoFinding, assets map[string]ftype
|
||||
prev := rows[n-1]
|
||||
if prev.finding.Severity == f.Severity && prev.finding.RuleID == f.RuleID &&
|
||||
prev.asset.FilePath == asset.FilePath && cryptoAssetLabel(prev.asset) == cryptoAssetLabel(asset) &&
|
||||
check.Message(prev.finding) == check.Message(f) {
|
||||
scanner.Message(prev.finding) == scanner.Message(f) {
|
||||
rows[n-1].count++
|
||||
continue
|
||||
}
|
||||
|
||||
+21
-15
@@ -15,7 +15,7 @@ import (
|
||||
dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
|
||||
"github.com/aquasecurity/trivy/pkg/clock"
|
||||
"github.com/aquasecurity/trivy/pkg/crypto"
|
||||
"github.com/aquasecurity/trivy/pkg/crypto/check"
|
||||
"github.com/aquasecurity/trivy/pkg/crypto/scanner"
|
||||
ospkgDetector "github.com/aquasecurity/trivy/pkg/detector/ospkg"
|
||||
"github.com/aquasecurity/trivy/pkg/extension"
|
||||
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
|
||||
@@ -165,20 +165,7 @@ func (s Service) ScanTarget(ctx context.Context, target types.ScanTarget, option
|
||||
// Scan licenses
|
||||
results = append(results, s.scanLicenses(target, options)...)
|
||||
|
||||
if options.Scanners.Enabled(types.CryptoScanner) && len(target.CryptoAssets) != 0 {
|
||||
// The security levels are assessed here rather than during analysis, so that a change
|
||||
// to their basis applies to cached assets without analyzing them again.
|
||||
crypto.AssessStrength(target.CryptoAssets)
|
||||
result := types.Result{
|
||||
Target: target.Name,
|
||||
Class: types.ClassCrypto,
|
||||
CryptoAssets: target.CryptoAssets,
|
||||
}
|
||||
if options.CryptoChecks {
|
||||
result.CryptoFindings = check.Evaluate(target.CryptoAssets, clock.Now(ctx))
|
||||
}
|
||||
results = append(results, result)
|
||||
}
|
||||
results = append(results, s.scanCrypto(ctx, target, options)...)
|
||||
|
||||
// For WASM plugins and custom analyzers
|
||||
if len(target.CustomResources) != 0 {
|
||||
@@ -410,6 +397,25 @@ func (s Service) scanFileLicenses(licenses []ftypes.LicenseFile, scanner licensi
|
||||
}
|
||||
}
|
||||
|
||||
func (s Service) scanCrypto(ctx context.Context, target types.ScanTarget, options types.ScanOptions) types.Results {
|
||||
if !options.Scanners.Enabled(types.CryptoScanner) || len(target.CryptoAssets) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// The security levels are assessed here rather than during analysis, so that a change
|
||||
// to their basis applies to cached assets without analyzing them again.
|
||||
crypto.AssessStrength(target.CryptoAssets)
|
||||
result := types.Result{
|
||||
Target: target.Name,
|
||||
Class: types.ClassCrypto,
|
||||
CryptoAssets: target.CryptoAssets,
|
||||
}
|
||||
if options.CryptoChecks {
|
||||
result.CryptoFindings = scanner.Scan(target.CryptoAssets, clock.Now(ctx))
|
||||
}
|
||||
return types.Results{result}
|
||||
}
|
||||
|
||||
func toDetectedMisconfiguration(res ftypes.MisconfResult, defaultSeverity dbTypes.Severity,
|
||||
status types.MisconfStatus, layer ftypes.Layer) types.DetectedMisconfiguration {
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@ import (
|
||||
"github.com/aquasecurity/trivy/internal/dbtest"
|
||||
"github.com/aquasecurity/trivy/internal/hooktest"
|
||||
"github.com/aquasecurity/trivy/pkg/cache"
|
||||
"github.com/aquasecurity/trivy/pkg/crypto"
|
||||
"github.com/aquasecurity/trivy/pkg/fanal/applier"
|
||||
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
|
||||
"github.com/aquasecurity/trivy/pkg/scan/langpkg"
|
||||
@@ -174,8 +173,8 @@ func TestScanner_Scan(t *testing.T) {
|
||||
}))
|
||||
scannedCertificate := cachedCertificate
|
||||
scannedCertificate.Layer = cryptoLayer
|
||||
scannedAssets := []ftypes.CryptoAsset{scannedAlgorithm, scannedCertificate}
|
||||
crypto.AssignIDs(scannedAssets)
|
||||
scannedAssets := ftypes.CryptoAssets{scannedAlgorithm, scannedCertificate}
|
||||
scannedAssets.Finalize()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
Reference in new issue
Block a user