-
Notifications
You must be signed in to change notification settings - Fork 572
/
directory.go
306 lines (275 loc) · 7.5 KB
/
directory.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package directory
import (
"context"
"os"
"strconv"
"sync"
"github.com/deepfence/ThreatMapper/deepfence_utils/log"
)
const (
GlobalDirKey = NamespaceID("global")
NonSaaSDirKey = NamespaceID("default")
DatabaseDirKey = NamespaceID("database")
NamespaceKey = "namespace"
LicenseActiveKey = "license_active"
)
type NamespaceID string
type RedisConfig struct {
Endpoint string
Password string
Database int
}
type Neo4jConfig struct {
Endpoint string
Username string
Password string
}
type PostgresqlConfig struct {
Host string
Port int
Username string
Password string
Database string
SslMode string
}
type FileServerConfig struct {
Endpoint string
Username string
Password string
BucketName string
Secure bool
Region string
}
type DBConfigs struct {
Redis *RedisConfig
Neo4j *Neo4jConfig
Postgres *PostgresqlConfig
FileServer *FileServerConfig
}
type namespaceDirectory struct {
Directory map[NamespaceID]DBConfigs
sync.RWMutex
}
var directory namespaceDirectory
func init() {
directory = namespaceDirectory{
Directory: map[NamespaceID]DBConfigs{},
}
fileServerCfg := initFileServer()
saasMode := false
saasModeOn, has := os.LookupEnv("DEEPFENCE_SAAS_MODE")
if !has {
log.Warn().Msg("DEEPFENCE_SAAS_MODE defaults to: off")
} else if saasModeOn == "on" {
saasMode = true
}
directory.Lock()
if !saasMode {
redisCfg := initRedis()
neo4jCfg := initNeo4j()
postgresqlCfg := initPosgresql()
directory.Directory[NonSaaSDirKey] = DBConfigs{
Redis: &redisCfg,
Neo4j: &neo4jCfg,
Postgres: &postgresqlCfg,
FileServer: nil,
}
}
directory.Directory[GlobalDirKey] = DBConfigs{
Redis: nil,
Neo4j: nil,
Postgres: nil,
FileServer: &fileServerCfg,
}
directory.Unlock()
}
func GetAllNamespaces() []NamespaceID {
directory.RLock()
defer directory.RUnlock()
var namespaces []NamespaceID
for k := range directory.Directory {
if k != GlobalDirKey {
namespaces = append(namespaces, k)
}
}
return namespaces
}
func GetDatabaseConfig(ctx context.Context) (*DBConfigs, error) {
ns, err := ExtractNamespace(ctx)
if err != nil {
return nil, err
}
directory.RLock()
defer directory.RUnlock()
cfg, found := directory.Directory[ns]
if !found {
return nil, ErrNamespaceNotFound
}
return &cfg, nil
}
func ForEachNamespace(applyFn func(ctx context.Context) (string, error)) {
namespaces := GetAllNamespaces()
var err error
var msg string
for _, ns := range namespaces {
msg, err = applyFn(NewContextWithNameSpace(ns))
if err != nil {
log.Error().Err(err).Msg(msg)
}
}
}
func FetchNamespace(email string) NamespaceID {
namespaces := GetAllNamespaces()
if len(namespaces) == 1 && namespaces[0] == NonSaaSDirKey {
return NonSaaSDirKey
} else { //nolint:staticcheck
// TODO: Fetch namespace for SaaS tenant
}
return ""
}
func IsNonSaaSDeployment() bool {
namespaces := GetAllNamespaces()
if len(namespaces) == 1 && namespaces[0] == NonSaaSDirKey {
return true
}
return false
}
func initRedis() RedisConfig {
redisHost, has := os.LookupEnv("DEEPFENCE_REDIS_HOST")
if !has {
redisHost = "localhost"
log.Warn().Msgf("DEEPFENCE_REDIS_HOST defaults to: %v", redisHost)
}
redisPort, has := os.LookupEnv("DEEPFENCE_REDIS_PORT")
if !has {
redisPort = "6379"
log.Warn().Msgf("DEEPFENCE_REDIS_PORT defaults to: %v", redisPort)
}
redisEndpoint := redisHost + ":" + redisPort
redisPassword := os.Getenv("DEEPFENCE_REDIS_PASSWORD")
redisDBNumber := 0
var err error
redisDBNumberStr := os.Getenv("DEEPFENCE_REDIS_DB_NUMBER")
if redisDBNumberStr != "" {
redisDBNumber, err = strconv.Atoi(redisDBNumberStr)
if err != nil {
redisDBNumber = 0
}
}
return RedisConfig{
Endpoint: redisEndpoint,
Password: redisPassword,
Database: redisDBNumber,
}
}
func initFileServer() FileServerConfig {
fileServerHost, has := os.LookupEnv("DEEPFENCE_FILE_SERVER_HOST")
if !has {
fileServerHost = "deepfence-file-server"
log.Warn().Msgf("DEEPFENCE_FILE_SERVER_HOST defaults to: %v", fileServerHost)
}
fileServerPort, has := os.LookupEnv("DEEPFENCE_FILE_SERVER_PORT")
if !has {
fileServerPort = "9000"
log.Warn().Msgf("DEEPFENCE_FILE_SERVER_PORT defaults to: %v", fileServerPort)
}
fileServerUser := os.Getenv("DEEPFENCE_FILE_SERVER_USER")
if fileServerUser == "" {
fileServerUser = "deepfence"
log.Warn().Msgf("DEEPFENCE_FILE_SERVER_USER defaults to: %v", fileServerUser)
}
fileServerPassword := os.Getenv("DEEPFENCE_FILE_SERVER_PASSWORD")
if fileServerPassword == "" {
fileServerPassword = "deepfence"
log.Warn().Msg("using default file server password")
}
fileServerBucket := os.Getenv("DEEPFENCE_FILE_SERVER_BUCKET")
fileServerRegion := os.Getenv("DEEPFENCE_FILE_SERVER_REGION")
fileServerSecure := os.Getenv("DEEPFENCE_FILE_SERVER_SECURE")
fileServerEndpoint := fileServerHost
if fileServerHost != "s3.amazonaws.com" {
fileServerEndpoint = fileServerHost + ":" + fileServerPort
}
if fileServerSecure == "" {
fileServerSecure = "false"
}
isSecure, err := strconv.ParseBool(fileServerSecure)
if err != nil {
isSecure = false
log.Warn().Msgf("DEEPFENCE_FILE_SERVER_SECURE defaults to: %v (%v)", isSecure, err.Error())
}
return FileServerConfig{
Endpoint: fileServerEndpoint,
Username: fileServerUser,
Password: fileServerPassword,
BucketName: fileServerBucket,
Secure: isSecure,
Region: fileServerRegion,
}
}
func initPosgresql() PostgresqlConfig {
var err error
postgresHost, has := os.LookupEnv("DEEPFENCE_POSTGRES_USER_DB_HOST")
if !has {
postgresHost = "localhost"
log.Warn().Msgf("DEEPFENCE_POSTGRES_USER_DB_HOST defaults to: %v", postgresHost)
}
postgresPort := 5432
postgresPortStr := os.Getenv("DEEPFENCE_POSTGRES_USER_DB_PORT")
if postgresPortStr == "" {
log.Warn().Msgf("DEEPFENCE_POSTGRES_USER_DB_PORT defaults to: %d", postgresPort)
} else {
postgresPort, err = strconv.Atoi(postgresPortStr)
if err != nil {
postgresPort = 5432
}
}
postgresUsername := os.Getenv("DEEPFENCE_POSTGRES_USER_DB_USER")
if postgresUsername == "" {
postgresUsername = "deepfence"
log.Warn().Msgf("DEEPFENCE_POSTGRES_USER_DB_USER defaults to: %v", postgresUsername)
}
postgresPassword := os.Getenv("DEEPFENCE_POSTGRES_USER_DB_PASSWORD")
if postgresPassword == "" {
postgresPassword = "deepfence"
log.Warn().Msg("using default postgres password")
}
postgresDatabase := os.Getenv("DEEPFENCE_POSTGRES_USER_DB_NAME")
postgresSslMode := os.Getenv("DEEPFENCE_POSTGRES_USER_DB_SSLMODE")
return PostgresqlConfig{
Host: postgresHost,
Port: postgresPort,
Username: postgresUsername,
Password: postgresPassword,
Database: postgresDatabase,
SslMode: postgresSslMode,
}
}
func initNeo4j() Neo4jConfig {
neo4jHost, has := os.LookupEnv("DEEPFENCE_NEO4J_HOST")
if !has {
neo4jHost = "localhost"
log.Warn().Msgf("DEEPFENCE_NEO4J_HOST defaults to: %v", neo4jHost)
}
neo4jBoltPort, has := os.LookupEnv("DEEPFENCE_NEO4J_BOLT_PORT")
if !has {
neo4jBoltPort = "7687"
log.Warn().Msgf("DEEPFENCE_NEO4J_BOLT_PORT defaults to: %v", neo4jBoltPort)
}
neo4jEndpoint := "bolt://" + neo4jHost + ":" + neo4jBoltPort
neo4jUsername := os.Getenv("DEEPFENCE_NEO4J_USER")
if neo4jUsername == "" {
neo4jUsername = "neo4j"
log.Warn().Msgf("DEEPFENCE_NEO4J_USER defaults to: %v", neo4jUsername)
}
neo4jPassword := os.Getenv("DEEPFENCE_NEO4J_PASSWORD")
if neo4jPassword == "" {
neo4jPassword = "e16908ffa5b9f8e9d4ed"
log.Warn().Msg("using default neo4j password")
}
return Neo4jConfig{
Endpoint: neo4jEndpoint,
Username: neo4jUsername,
Password: neo4jPassword,
}
}