-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtrivy.go
144 lines (125 loc) · 3.39 KB
/
trivy.go
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"encoding/json"
"fmt"
"log"
"time"
"github.com/aquasecurity/trivy-kubernetes/pkg/artifacts"
"github.com/aquasecurity/trivy-kubernetes/pkg/k8s"
tk "github.com/aquasecurity/trivy-kubernetes/pkg/trivyk8s"
corev1 "k8s.io/api/core/v1"
"k8s.io/utils/ptr"
"context"
)
func main() {
ctx := context.Background()
cluster, err := k8s.GetCluster(k8s.WithBurst(100), k8s.WithQPS(100))
if err != nil {
log.Fatal(err)
}
fmt.Println("Current namespace:", cluster.GetCurrentNamespace())
trivyk8s := tk.New(cluster, tk.WithExcludeOwned(true))
fmt.Println("Scanning cluster")
//trivy k8s #cluster
start := time.Now()
artifacts, err := trivyk8s.ListArtifacts(ctx)
end := time.Now()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Scan took %v\n", end.Sub(start))
printArtifacts(artifacts)
fmt.Println("Scanning kind 'pods' with exclude-owned=true")
artifacts, err = trivyk8s.Resources("pod").AllNamespaces().ListArtifacts(ctx)
if err != nil {
log.Fatal(err)
}
printArtifacts(artifacts)
fmt.Println("Scanning namespace 'default'")
//trivy k8s --namespace default
artifacts, err = trivyk8s.Namespace("default").ListArtifacts(ctx)
if err != nil {
log.Fatal(err)
}
printArtifacts(artifacts)
fmt.Println("Scanning all namespaces ")
artifacts, err = trivyk8s.AllNamespaces().ListArtifacts(ctx)
if err != nil {
log.Fatal(err)
}
printArtifacts(artifacts)
fmt.Println("Scanning 'deployments'")
//trivy k8s deployment
artifacts, err = trivyk8s.Namespace("default").Resources("deployment").ListArtifacts(ctx)
if err != nil {
log.Fatal(err)
}
printArtifacts(artifacts)
fmt.Println("Scanning 'cm,pods'")
//trivy k8s clusterroles,pods
artifacts, err = trivyk8s.Namespace("default").Resources("cm,pods").ListArtifacts(ctx)
if err != nil {
log.Fatal(err)
}
printArtifacts(artifacts)
tolerations := []corev1.Toleration{
{
Effect: corev1.TaintEffectNoSchedule,
Operator: corev1.TolerationOperator(corev1.NodeSelectorOpExists),
},
{
Effect: corev1.TaintEffectNoExecute,
Operator: corev1.TolerationOperator(corev1.NodeSelectorOpExists),
},
{
Effect: corev1.TaintEffectNoExecute,
Key: "node.kubernetes.io/not-ready",
Operator: corev1.TolerationOperator(corev1.NodeSelectorOpExists),
TolerationSeconds: ptr.To[int64](300),
},
{
Effect: corev1.TaintEffectNoExecute,
Key: "node.kubernetes.io/unreachable",
Operator: corev1.TolerationOperator(corev1.NodeSelectorOpExists),
TolerationSeconds: ptr.To[int64](300),
},
}
// collect node info
ar, err := trivyk8s.ListArtifactAndNodeInfo(ctx, []tk.NodeCollectorOption{
tk.WithScanJobNamespace("trivy-temp"),
tk.WithIgnoreLabels(map[string]string{"chen": "test"}),
tk.WithTolerations(tolerations),
}...)
if err != nil {
log.Fatal(err)
}
for _, a := range ar {
if a.Kind != "NodeInfo" {
continue
}
fmt.Println(a.RawResource)
}
bi, err := trivyk8s.ListClusterBomInfo(ctx)
if err != nil {
log.Fatal(err)
}
bb, err := json.Marshal(bi)
if err != nil {
log.Fatal(err)
}
fmt.Print(string(bb))
}
func printArtifacts(artifacts []*artifacts.Artifact) {
for _, artifact := range artifacts {
printArtifact(artifact)
}
}
func printArtifact(artifact *artifacts.Artifact) {
fmt.Printf(
"Name: %s, Kind: %s, Namespace: %s, Images: %v\n",
artifact.Name,
artifact.Kind,
artifact.Namespace,
artifact.Images,
)
}