-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmain.go
225 lines (199 loc) · 5.92 KB
/
main.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//go:generate go run pkg/codegen/cleanup/main.go
//go:generate /bin/rm -rf pkg/generated
//go:generate go run pkg/codegen/main.go
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"time"
"github.com/rancher/wrangler/v3/pkg/kubeconfig"
"github.com/rancher/wrangler/v3/pkg/signals"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
cisoperatorapiv1 "github.com/rancher/cis-operator/pkg/apis/cis.cattle.io/v1"
cisoperator "github.com/rancher/cis-operator/pkg/securityscan"
// Automatically sets fallback trusted x509 roots, in case they are
// not available at runtime. This is required to establish trust
// when deployed into a scratch container.
_ "golang.org/x/crypto/x509roots/fallback"
// Embed a copy of the timezone database, so that it does not depend
// on it being available at runtime.
_ "time/tzdata"
corev1 "k8s.io/api/core/v1"
)
var (
Version = "v0.0.0-dev"
GitCommit = "HEAD"
kubeConfig string
threads int
name string
metricsPort string
alertSeverity string
debug bool
securityScanImage string
securityScanImageTag string
sonobuoyImage string
sonobuoyImageTag string
clusterName string
securityScanJobTolerationsVal string
)
func main() {
app := cli.NewApp()
app.Name = "cis-operator"
app.Version = fmt.Sprintf("%s (%s)", Version, GitCommit)
app.Usage = "cis-operator needs help!"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "kubeconfig",
EnvVar: "KUBECONFIG",
Destination: &kubeConfig,
},
cli.IntFlag{
Name: "threads",
EnvVar: "CIS_OPERATOR_THREADS",
Value: 2,
Destination: &threads,
},
cli.StringFlag{
Name: "name",
EnvVar: "CIS_OPERATOR_NAME",
Value: "cis-operator",
Destination: &name,
},
cli.StringFlag{
Name: "security-scan-image",
EnvVar: "SECURITY_SCAN_IMAGE",
Value: "rancher/security-scan",
Destination: &securityScanImage,
},
cli.StringFlag{
Name: "security-scan-image-tag",
EnvVar: "SECURITY_SCAN_IMAGE_TAG",
Value: "latest",
Destination: &securityScanImageTag,
},
cli.StringFlag{
Name: "sonobuoy-image",
EnvVar: "SONOBUOY_IMAGE",
Value: "rancher/sonobuoy-sonobuoy",
Destination: &sonobuoyImage,
},
cli.StringFlag{
Name: "sonobuoy-image-tag",
EnvVar: "SONOBUOY_IMAGE_TAG",
Value: "latest",
Destination: &sonobuoyImageTag,
},
cli.StringFlag{
Name: "cis_metrics_port",
EnvVar: "CIS_METRICS_PORT",
Value: "8080",
Destination: &metricsPort,
},
cli.BoolFlag{
Name: "debug",
EnvVar: "CIS_OPERATOR_DEBUG",
Destination: &debug,
},
cli.StringFlag{
Name: "alertSeverity",
EnvVar: "CIS_ALERTS_SEVERITY",
Value: "warning",
Destination: &alertSeverity,
},
cli.StringFlag{
Name: "clusterName",
EnvVar: "CLUSTER_NAME",
Value: "",
Destination: &clusterName,
},
cli.StringFlag{
Name: "security-scan-job-tolerations",
EnvVar: "SECURITY_SCAN_JOB_TOLERATIONS",
Value: "",
Destination: &securityScanJobTolerationsVal,
},
cli.BoolFlag{
Name: "alertEnabled",
EnvVar: "CIS_ALERTS_ENABLED",
},
}
app.Action = run
if err := app.Run(os.Args); err != nil {
logrus.Fatal(err)
}
}
func run(c *cli.Context) {
logrus.Info("Starting CIS-Operator")
ctx := context.Background()
handler := signals.SetupSignalHandler()
go func() {
<-handler
ctx.Done()
}()
if debug {
logrus.SetLevel(logrus.DebugLevel)
}
kubeConfig = c.String("kubeconfig")
threads = c.Int("threads")
securityScanImage = c.String("security-scan-image")
securityScanImageTag = c.String("security-scan-image-tag")
sonobuoyImage = c.String("sonobuoy-image")
sonobuoyImageTag = c.String("sonobuoy-image-tag")
name = c.String("name")
securityScanJobTolerations := []corev1.Toleration{{
Operator: corev1.TolerationOpExists,
}}
securityScanJobTolerationsVal = c.String("security-scan-job-tolerations")
if securityScanJobTolerationsVal != "" {
err := json.Unmarshal([]byte(securityScanJobTolerationsVal), &securityScanJobTolerations)
if err != nil {
logrus.Fatalf("invalid value received for security-scan-job-tolerations flag:%s", err.Error())
}
}
kubeConfig, err := kubeconfig.GetNonInteractiveClientConfig(kubeConfig).ClientConfig()
if err != nil {
logrus.Fatalf("failed to find kubeconfig: %v", err)
}
imgConfig := &cisoperatorapiv1.ScanImageConfig{
SecurityScanImage: securityScanImage,
SecurityScanImageTag: securityScanImageTag,
SonobuoyImage: sonobuoyImage,
SonobuoyImageTag: sonobuoyImageTag,
AlertSeverity: alertSeverity,
ClusterName: clusterName,
AlertEnabled: c.Bool("alertEnabled"),
}
if err := validateConfig(imgConfig); err != nil {
logrus.Fatalf("Error starting CIS-Operator: %v", err)
}
ctl, err := cisoperator.NewController(ctx, kubeConfig, cisoperatorapiv1.ClusterScanNS, name, imgConfig, securityScanJobTolerations)
if err != nil {
logrus.Fatalf("Error building controller: %s", err.Error())
}
if err := ctl.Start(ctx, threads, 2*time.Hour); err != nil {
logrus.Fatalf("Error starting: %v", err)
}
http.Handle("/metrics", promhttp.Handler())
if err := http.ListenAndServe(":"+metricsPort, nil); err != nil {
log.Fatal(err)
}
<-handler
ctx.Done()
logrus.Info("Registered CIS controller")
}
func validateConfig(imgConfig *cisoperatorapiv1.ScanImageConfig) error {
if imgConfig.SecurityScanImage == "" {
return errors.New("No Security-Scan Image specified")
}
if imgConfig.SonobuoyImage == "" {
return errors.New("No Sonobuoy tool Image specified")
}
return nil
}