This repository has been archived by the owner on Mar 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
/
global.go
453 lines (377 loc) · 10.3 KB
/
global.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package global
import (
"context"
"crypto/tls"
"database/sql"
"fmt"
"github.com/seknox/trasa/server/utils"
"github.com/spf13/viper"
"net"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/minio/minio-go"
"github.com/sirupsen/logrus"
_ "github.com/lib/pq"
"github.com/oschwald/geoip2-golang"
firebase "firebase.google.com/go"
"github.com/go-redis/redis/v8"
vault "github.com/hashicorp/vault/api"
//"go.etcd.io/etcd/clientv3"
"github.com/seknox/trasa/server/models/migrations"
"google.golang.org/api/option"
)
var DBVersion string = "2020-07-31-rc"
var LogToFile *bool
var config Config
func GetConfig() Config {
return config
}
func SetOrgID(orgID string) {
config.Trasa.OrgId = orgID
}
type State struct {
DB *sql.DB
Geoip *geoip2.Reader
FirebaseClient *firebase.App
MinioClient *minio.Client
VaultClient *vault.Client
// EtcdClient *clientv3.Client
//Config Config
RedisClient *redis.Client
VaultRootToken string
TsxvKey tsxKey
TsxCPxyKey string
}
type tsxKey struct {
Key *[32]byte
State bool
}
// ECDHKexDerivedKey stores kexDerivedKey with sessionID as key.
// For dashboard based login, key becomes sessionID of dashboard session.
// For enrol new device, key becomes trasaID of user
// For http session recording, key becomse sessionID of http session.
// It is responsibility of caller to delete keys after usage.
var ECDHKexDerivedKey = make(map[string]KexDerivedKey)
// KexDerivedKey stores secretkey derived from Kex, deviceID of device with which key was exchanged and timestamp recording kex operation.
type KexDerivedKey struct {
// device id of client
DeviceID string
// secret key
Secretkey []byte
// Time of when secret key was derived
Timestamp int64
}
func InitDBSTORE() *State {
checkInitDirsAndFiles()
viper.AutomaticEnv()
conf := ParseConfig()
if conf.Trasa.DashboardAddr == "" {
conf.Trasa.DashboardAddr = fmt.Sprintf("https://%s", conf.Trasa.ListenAddr)
}
return InitDBSTOREWithConfig(conf)
}
func InitDBSTOREWithConfig(conf Config) *State {
config = conf
level, _ := logrus.ParseLevel(config.Logging.Level)
OxyLog = logrus.New()
OxyLog.SetLevel(logrus.ErrorLevel)
if LogToFile != nil && *LogToFile {
f, err := os.OpenFile(filepath.Join(utils.GetVarDir(), "log", "trasa.log"), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
panic(err)
}
OxyLog.SetOutput(f)
logrus.SetOutput(f)
} else {
logrus.SetOutput(os.Stdout)
}
logrus.SetLevel(level)
logrus.SetReportCaller(true)
logrusFormatter := new(logrus.TextFormatter)
logrusFormatter.QuoteEmptyFields = true
logrusFormatter.TimestampFormat = time.RFC3339
logrusFormatter.DisableTimestamp = false
logrusFormatter.FullTimestamp = true
logrusFormatter.CallerPrettyfier = func(frame *runtime.Frame) (function string, file string) {
dir, filename := filepath.Split(frame.File)
filename = filepath.Join(filepath.Base(dir), filename)
return frame.Function, fmt.Sprintf("%s:%d", filename, frame.Line)
}
logrus.SetFormatter(logrusFormatter)
// we start trasa-server dependencies:
// initialize cockroachdb connection
db := DBconn(config)
// DbCon = db
redisClient := newRedisClient(config)
//elasticUrl, authUser, authPass := elasticon()
// initialize geoIP connection
absPath, err := filepath.Abs(filepath.Join(utils.GetETCDir(), "trasa", "static", "GeoLite2-City.mmdb"))
if err != nil {
panic("geodb file not found: " + err.Error())
}
geodb, err := geoip2.Open(absPath)
if err != nil {
panic(err)
}
absPath, err = filepath.Abs(filepath.Join(utils.GetETCDir(), "trasa", "config", "key.json"))
if err != nil {
logrus.Errorf("firebase key not found: %v", err)
}
opt := option.WithCredentialsFile(absPath)
app, err := firebase.NewApp(context.Background(), nil, opt)
if err != nil {
logrus.Errorf("firebase key not found: %v", err)
//panic(err)
}
var minioClient *minio.Client
if config.Minio.Status {
minioClient, err = getMinioClient(config)
if err != nil {
panic(err)
}
}
err = migrate(db)
if err != nil {
//fmt.Println(err)
panic(err)
}
if config.Proxy.GuacdEnabled {
guacdAddr := config.Proxy.GuacdAddr
if guacdAddr == "" {
guacdAddr = "127.0.0.1:4822"
}
c, err := net.Dial("tcp", guacdAddr)
if err != nil {
panic("guacd is down")
}
c.Close()
}
return &State{
DB: db,
Geoip: geodb,
FirebaseClient: app,
MinioClient: minioClient,
TsxvKey: tsxKey{
Key: new([32]byte),
State: false,
},
RedisClient: redisClient,
}
//return
}
func migrate(conn *sql.DB) error {
for _, v := range migrations.PrimaryMigration {
_, e := conn.Exec(v)
if e != nil {
logrus.Error(e)
return e
}
logrus.Tracef("%s migrated", strings.Split(v, " ")[5])
}
return nil
}
func DBconn(config Config) *sql.DB {
dbuser := config.Database.Dbuser
dbpass := config.Database.Dbpass
dbhost := config.Database.Server
dbport := config.Database.Port
dbname := config.Database.Dbname
sslEnabled := config.Database.Sslenabled
var str string
if sslEnabled {
caCertPath := config.Database.Cacert
userCertPath := config.Database.Usercert
userKeyPath := config.Database.Userkey
caCert, _ := filepath.Abs(caCertPath)
userCert, _ := filepath.Abs(userCertPath)
userKey, _ := filepath.Abs(userKeyPath)
str = fmt.Sprintf("postgresql://%s:%s@%s:%s/%s?sslmode=verify-full&sslrootcert=%s&sslcert=%s&sslkey=%s", dbuser, dbpass, dbhost, dbport, dbname, caCert, userCert, userKey)
} else {
str = fmt.Sprintf("postgresql://%s:%s@%s:%s/%s?sslmode=disable", dbuser, dbpass, dbhost, dbport, dbname)
}
db, err := sql.Open("postgres", str)
if err != nil {
panic(err.Error())
}
//Check if the connection is successful by establishing a connection.
//Retry upto 10 times if connection is not successful
for retryCount := 0; retryCount < 10; retryCount++ {
err = db.Ping()
if err == nil {
logrus.Info("database connection successful")
return db
}
logrus.Info("could not connect to database: retrying...")
time.Sleep(time.Second)
}
panic("could not connect to database")
}
func getMinioClient(config Config) (*minio.Client, error) {
endpoint := config.Minio.Server
accessKeyID := config.Minio.Key
secretAccessKey := config.Minio.Secret
useSSL := config.Minio.Usessl
insecureSkipVerify := config.Security.InsecureSkipVerify
// Initialize minio client object.
minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
if err != nil {
return nil, err
}
t := http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: insecureSkipVerify,
},
}
minioClient.SetCustomTransport(&t)
//Check connection and if bucket exists
bucketExists, err := minioClient.BucketExists("trasa-ssh-logs")
if err != nil {
panic(err)
}
if !bucketExists {
err = minioClient.MakeBucket("trasa-ssh-logs", "")
if err != nil {
panic(err)
}
}
bucketExists, err = minioClient.BucketExists("trasa-guac-logs")
if err != nil {
panic(err)
}
if !bucketExists {
err = minioClient.MakeBucket("trasa-guac-logs", "")
if err != nil {
panic(err)
}
}
bucketExists, err = minioClient.BucketExists("trasa-https-logs")
if err != nil {
panic(err)
}
if !bucketExists {
err = minioClient.MakeBucket("trasa-https-logs", "")
if err != nil {
panic(err)
}
}
bucketExists, err = minioClient.BucketExists("trasa-db-logs")
if err != nil {
panic(err)
}
if !bucketExists {
err = minioClient.MakeBucket("trasa-db-logs", "")
if err != nil {
panic(err)
}
}
return minioClient, nil
}
func newRedisClient(config Config) *redis.Client {
cl := redis.NewClient(&redis.Options{
Addr: config.Redis.Server[0],
Password: "", // no password set
DB: 0, // use default DB
})
_, err := cl.Ping(context.Background()).Result()
if err != nil {
panic(err)
}
return cl
}
func checkInitDirsAndFiles() {
err := os.MkdirAll(filepath.Join(utils.GetTmpDir(), "trasa", "accessproxy", "guac", "shared"), 0600)
if err != nil {
panic(err)
}
err = os.MkdirAll(filepath.Join(utils.GetTmpDir(), "trasa", "accessproxy", "ssh"), 0600)
if err != nil {
panic(err)
}
err = os.MkdirAll(filepath.Join(utils.GetTmpDir(), "trasa", "accessproxy", "http"), 0600)
if err != nil {
panic(err)
}
err = os.MkdirAll(filepath.Join(utils.GetVarDir(), "trasa", "crdb"), 0600)
if err != nil {
panic(err)
}
err = os.MkdirAll(filepath.Join(utils.GetVarDir(), "trasa", "sessions"), 0600)
if err != nil {
panic(err)
}
err = os.MkdirAll(filepath.Join(utils.GetETCDir(), "trasa", "certs"), 0600)
if err != nil {
panic(err)
}
err = os.MkdirAll(filepath.Join(utils.GetETCDir(), "trasa", "config"), 0600)
if err != nil {
panic(err)
}
err = os.MkdirAll(filepath.Join(utils.GetETCDir(), "trasa", "static"), 0600)
if err != nil {
panic(err)
}
//create config file if no exist
_, err = os.Stat(filepath.Join(utils.GetETCDir(), "trasa", "config", "config.toml"))
if err != nil {
f, err := os.OpenFile(filepath.Join(utils.GetETCDir(), "trasa", "config", "config.toml"), os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
panic(err)
}
defer f.Close()
f.WriteString(
`[backup]
backupdir = "/var/trasa/backup"
[database]
cacert = "/etc/trasa/certs/ca.crt"
dbname = "trasadb"
dbtype = "postgres"
dbuser = "trasauser"
dbpass = "trasauser"
port = "5432"
server = "localhost"
usercert = "/etc/trasa/certs/client.trasauser.crt"
userkey = "/etc/trasa/certs/client.trasauser.key"
[logging]
level = "INFO"
[minio]
status = false
key = "minioadmin"
secret = "minioadmin"
server = "127.0.0.1:9000"
usessl = false
[platform]
base = "private"
[redis]
server = "localhost:6379"
[security]
insecureSkipVerify = true
[proxy]
sshlistenAddr = "127.0.0.1:8022"
dbListenAddr = "127.0.0.1:3333"
guacdEnabled = false
guacdAddr = "127.0.0.1:4822"
[trasa]
autoCert = true
proxyDashboard = false
dashboardAddr = "http://localhost:3000"
cloudServer = "https://sg.cpxy.trasa.io"
listenAddr = "app.trasa"
`)
}
}
//Gstate is a global state struct which contains database connections, configurations etc
type Gstate struct {
db *sql.DB
geoip *geoip2.Reader
firebaseClient *firebase.App
minioClient *minio.Client
config Config
redisClient *redis.Client
}
// OxyLog is logging instance for oxy with default error level.
var OxyLog *logrus.Logger