-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathconfig.go
253 lines (213 loc) · 7.46 KB
/
config.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package config
import (
"fmt"
"os"
"time"
utilsmetadata "github.com/armosec/utils-k8s-go/armometadata"
"github.com/kubescape/backend/pkg/servicediscovery"
"github.com/kubescape/backend/pkg/servicediscovery/schema"
v2 "github.com/kubescape/backend/pkg/servicediscovery/v2"
"github.com/kubescape/backend/pkg/utils"
"github.com/kubescape/go-logger"
"github.com/kubescape/go-logger/helpers"
"github.com/spf13/viper"
)
type Component struct {
Enabled bool `json:"enabled"`
}
type Capabilities struct {
ConfigurationScan string `json:"configurationScan"`
ContinuousScan string `json:"continuousScan"`
NetworkGenerator string `json:"networkGenerator"`
NodeScan string `json:"nodeScan"`
Otel string `json:"otel"`
Relevancy string `json:"relevancy"`
RuntimeObservability string `json:"runtimeObservability"`
Seccomp string `json:"seccomp"`
VulnerabilityScan string `json:"vulnerabilityScan"`
}
type Components struct {
Gateway Component `mapstructure:"gateway"`
HostScanner Component `mapstructure:"hostScanner"`
Kollector Component `mapstructure:"kollector"`
Kubescape Component `mapstructure:"kubescape"`
KubescapeScheduler Component `mapstructure:"kubescapeScheduler"`
Kubevuln Component `mapstructure:"kubevuln"`
KubevulnScheduler Component `mapstructure:"kubevulnScheduler"`
NodeAgent Component `mapstructure:"nodeAgent"`
Operator Component `mapstructure:"operator"`
OtelCollector Component `mapstructure:"otelCollector"`
Persistence Component `mapstructure:"persistence"`
ServiceDiscovery Component `mapstructure:"serviceDiscovery"`
Storage Component `mapstructure:"storage"`
}
type ServiceScanConfig struct {
Enabled bool `json:"enabled"`
Interval time.Duration `json:"interval"`
}
type Server struct {
Account string `json:"account"`
DiscoveryURL string `json:"discoveryUrl"`
OtelURL string `json:"otelUrl"`
}
type Configurations struct {
Persistence string `json:"persistence"`
Server Server `json:"server"`
}
type CapabilitiesConfig struct {
Capabilities Capabilities `mapstructure:"capabilities"`
Components Components `mapstructure:"components"`
Configurations Configurations `mapstructure:"configurations"`
ServiceScanConfig ServiceScanConfig `mapstructure:"serviceScanConfig"`
}
func LoadCapabilitiesConfig(path string) (CapabilitiesConfig, error) {
viper.AddConfigPath(path)
viper.SetConfigName("capabilities")
viper.SetConfigType("json")
viper.AutomaticEnv()
err := viper.ReadInConfig()
if err != nil {
return CapabilitiesConfig{}, err
}
var c CapabilitiesConfig
err = viper.Unmarshal(&c)
return c, err
}
type Config struct {
Namespace string `mapstructure:"namespace"`
RestAPIPort string `mapstructure:"port"`
CleanUpRoutineInterval time.Duration `mapstructure:"cleanupDelay"`
ConcurrencyWorkers int `mapstructure:"workerConcurrency"`
TriggerSecurityFramework bool `mapstructure:"triggerSecurityFramework"`
MatchingRulesFilename string `mapstructure:"matchingRulesFilename"`
// EventDeduplicationInterval is the interval during which duplicate events will be silently dropped from processing via continuous scanning
EventDeduplicationInterval time.Duration `mapstructure:"eventDeduplicationInterval"`
}
// IConfig is an interface for all config types used in the operator
type IConfig interface {
Namespace() string
AccountID() string
AccessKey() string
ClusterName() string
EventReceiverURL() string
GatewayWebsocketURL() string
ConcurrencyWorkers() int
Components() Components
ContinuousScanEnabled() bool
CleanUpRoutineInterval() time.Duration
MatchingRulesFilename() string
TriggerSecurityFramework() bool
KubescapeURL() string
KubevulnURL() string
}
// OperatorConfig implements IConfig
type OperatorConfig struct {
serviceConfig Config
components CapabilitiesConfig
clusterConfig utilsmetadata.ClusterConfig
accountId string
accessKey string
eventReceiverRestURL string
}
func NewOperatorConfig(components CapabilitiesConfig, clusterConfig utilsmetadata.ClusterConfig, creds *utils.Credentials, eventReceiverRestURL string, serviceConfig Config) *OperatorConfig {
return &OperatorConfig{
components: components,
serviceConfig: serviceConfig,
clusterConfig: clusterConfig,
accountId: creds.Account,
accessKey: creds.AccessKey,
eventReceiverRestURL: eventReceiverRestURL,
}
}
func (c *OperatorConfig) ContinuousScanEnabled() bool {
return c.components.Capabilities.ContinuousScan == "enable"
}
func (c *OperatorConfig) KubevulnURL() string {
return c.clusterConfig.KubevulnURL
}
func (c *OperatorConfig) KubescapeURL() string {
return c.clusterConfig.KubescapeURL
}
func (c *OperatorConfig) TriggerSecurityFramework() bool {
return c.serviceConfig.TriggerSecurityFramework
}
func (c *OperatorConfig) Namespace() string {
return c.serviceConfig.Namespace
}
func (c *OperatorConfig) CleanUpRoutineInterval() time.Duration {
return c.serviceConfig.CleanUpRoutineInterval
}
func (c *OperatorConfig) MatchingRulesFilename() string {
return c.serviceConfig.MatchingRulesFilename
}
func (c *OperatorConfig) GatewayWebsocketURL() string {
return c.clusterConfig.GatewayWebsocketURL
}
func (c *OperatorConfig) ConcurrencyWorkers() int {
return c.serviceConfig.ConcurrencyWorkers
}
func (c *OperatorConfig) Components() Components {
return c.components.Components
}
func (c *OperatorConfig) AccountID() string {
return c.accountId
}
func (c *OperatorConfig) AccessKey() string {
return c.accessKey
}
func (c *OperatorConfig) ClusterName() string {
return c.clusterConfig.ClusterName
}
func (c *OperatorConfig) EventReceiverURL() string {
return c.eventReceiverRestURL
}
func LoadConfig(path string) (Config, error) {
viper.AddConfigPath(path)
viper.SetConfigName("config")
viper.SetConfigType("json")
viper.SetDefault("namespace", "kubescape")
viper.SetDefault("port", "4002")
viper.SetDefault("cleanupDelay", 10*time.Minute)
viper.SetDefault("workerConcurrency", 3)
viper.SetDefault("triggerSecurityFramework", false)
viper.SetDefault("matchingRulesFilename", "/etc/config/matchingRules.json")
viper.SetDefault("eventDeduplicationInterval", 2*time.Minute)
viper.AutomaticEnv()
err := viper.ReadInConfig()
if err != nil {
return Config{}, err
}
var c Config
err = viper.Unmarshal(&c)
return c, err
}
func LoadClusterConfig() (utilsmetadata.ClusterConfig, error) {
pathAndFileName, present := os.LookupEnv("CONFIG")
if !present {
pathAndFileName = "/etc/config/clusterData.json"
}
clusterConfig, err := utilsmetadata.LoadConfig(pathAndFileName)
if err != nil {
return utilsmetadata.ClusterConfig{}, err
}
return *clusterConfig, err
}
func GetServiceURLs(filePath string) (schema.IBackendServices, error) {
pathAndFileName, present := os.LookupEnv("SERVICES")
if !present {
pathAndFileName = filePath
}
logger.L().Debug("discovery service URLs from file", helpers.String("path", pathAndFileName))
return servicediscovery.GetServices(
v2.NewServiceDiscoveryFileV2(pathAndFileName),
)
}
func ValidateConfig(config IConfig) error {
if config.AccountID() == "" && config.Components().ServiceDiscovery.Enabled {
return fmt.Errorf("missing account id")
}
if config.ClusterName() == "" {
return fmt.Errorf("missing cluster name in config")
}
return nil
}