-
Notifications
You must be signed in to change notification settings - Fork 565
Expand file tree
/
Copy pathasset.go
More file actions
98 lines (88 loc) · 2.56 KB
/
Copy pathasset.go
File metadata and controls
98 lines (88 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Package cryptotest provides cryptographic asset fixtures for tests.
package cryptotest
import (
"strings"
"github.com/aquasecurity/trivy/pkg/crypto"
)
// Option customizes an Asset fixture.
type Option func(*crypto.Asset)
// WithMutate applies mutate after constructing a complete Asset fixture.
func WithMutate(mutate func(*crypto.Asset)) Option {
return mutate
}
// CertificateAsset returns a valid certificate asset.
func CertificateAsset(opts ...Option) crypto.Asset {
asset := crypto.Asset{
Kind: crypto.KindCertificate,
Identity: crypto.Identity{
Method: crypto.MethodSHA256,
Value: strings.Repeat("a", 64),
},
Name: "example.test",
FilePath: "/etc/example.pem",
Certificate: &crypto.Certificate{
Subject: "CN=example.test",
Issuer: "CN=Example Test CA",
SerialNumber: "1",
Format: crypto.CertificateFormatX509,
},
}
return applyOptions(asset, opts)
}
// PublicKeyAsset returns a valid public key asset.
func PublicKeyAsset(opts ...Option) crypto.Asset {
asset := crypto.Asset{
Kind: crypto.KindKey,
KeyType: crypto.KeyTypePublic,
Identity: crypto.Identity{
Method: crypto.MethodSPKISHA256,
Value: strings.Repeat("b", 64),
},
FilePath: "/etc/example-public.pem",
Key: &crypto.Key{
Size: 2048,
Format: crypto.KeyFormatPKIX,
Encoding: crypto.EncodingPEM,
},
}
return applyOptions(asset, opts)
}
// PrivateKeyAsset returns a valid private key asset.
func PrivateKeyAsset(opts ...Option) crypto.Asset {
asset := PublicKeyAsset()
asset.KeyType = crypto.KeyTypePrivate
asset.FilePath = "/etc/example-private.pem"
asset.Key.Format = crypto.KeyFormatPKCS8
return applyOptions(asset, opts)
}
// EncryptedPrivateKeyAsset returns a valid encrypted private key asset.
func EncryptedPrivateKeyAsset(opts ...Option) crypto.Asset {
asset := PrivateKeyAsset()
asset.Identity.Method = crypto.MethodEncryptedPKCS8SHA256
asset.FilePath = "/etc/example-encrypted-private.pem"
asset.Key.Encrypted = true
return applyOptions(asset, opts)
}
// AlgorithmAsset returns a valid algorithm asset.
func AlgorithmAsset(opts ...Option) crypto.Asset {
asset := crypto.Asset{
Kind: crypto.KindAlgorithm,
Identity: crypto.Identity{
Method: crypto.MethodOID,
Value: "1.2.840.113549.1.1.1",
},
Name: "RSA",
FilePath: "/etc/example-algorithm.pem",
Algorithm: &crypto.Algorithm{
Family: "RSA",
Primitive: crypto.PrimitivePKE,
},
}
return applyOptions(asset, opts)
}
func applyOptions(asset crypto.Asset, opts []Option) crypto.Asset {
for _, opt := range opts {
opt(&asset)
}
return asset
}