Files
beelzebub/main.go

128 lines
3.4 KiB
Go
Raw Normal View History

2022-05-08 20:49:53 +02:00
package main
import (
"beelzebub/parser"
"beelzebub/protocols"
"beelzebub/tracer"
2022-05-17 00:17:39 +02:00
"context"
2022-05-08 20:49:53 +02:00
log "github.com/sirupsen/logrus"
2022-05-17 00:17:39 +02:00
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
2022-05-08 20:49:53 +02:00
"io"
"os"
"time"
2022-05-08 20:49:53 +02:00
)
var quit = make(chan struct{})
2022-05-17 00:17:39 +02:00
var mongoClient *mongo.Client
2022-05-08 20:49:53 +02:00
func main() {
2022-05-12 22:46:15 +02:00
parser := parser.Init("./configurations/beelzebub.yaml", "./configurations/services/")
2022-05-08 20:49:53 +02:00
2022-05-12 22:46:15 +02:00
coreConfigurations, err := parser.ReadConfigurationsCore()
2022-05-08 20:49:53 +02:00
if err != nil {
log.Fatal(err)
}
2022-05-12 22:46:15 +02:00
fileLogs := configureLoggingByConfigurations(coreConfigurations.Core.Logging)
2022-05-08 20:49:53 +02:00
defer fileLogs.Close()
2022-05-12 22:46:15 +02:00
beelzebubServicesConfiguration, err := parser.ReadConfigurationsServices()
2022-05-08 20:49:53 +02:00
if err != nil {
log.Fatal(err)
}
2022-05-12 22:46:15 +02:00
// Init Protocol strategies
2022-05-08 20:49:53 +02:00
secureShellStrategy := &protocols.SecureShellStrategy{}
hypertextTransferProtocolStrategy := &protocols.HypertextTransferProtocolStrategy{}
2022-05-12 22:46:15 +02:00
// Init protocol manager, with simple log on stout trace strategy and default protocol HTTP
protocolManager := protocols.InitProtocolManager(traceStrategyStdout, hypertextTransferProtocolStrategy)
2022-05-08 20:49:53 +02:00
if coreConfigurations.Core.Tracing.MongoEnabled {
mongoClient = buildMongoClient(coreConfigurations.Core.Tracing.MongoURI)
defer func(mongoClient *mongo.Client, ctx context.Context) {
err := mongoClient.Disconnect(ctx)
if err != nil {
log.Error(err)
}
}(mongoClient, context.TODO())
}
2022-05-17 00:17:39 +02:00
2022-05-08 20:49:53 +02:00
for _, beelzebubServiceConfiguration := range beelzebubServicesConfiguration {
switch beelzebubServiceConfiguration.Protocol {
case "http":
2022-05-12 22:46:15 +02:00
protocolManager.SetProtocolStrategy(hypertextTransferProtocolStrategy)
2022-05-08 20:49:53 +02:00
break
case "ssh":
2022-05-12 22:46:15 +02:00
protocolManager.SetProtocolStrategy(secureShellStrategy)
2022-05-08 20:49:53 +02:00
break
default:
log.Fatalf("Protocol %s not managed", beelzebubServiceConfiguration.Protocol)
continue
}
2022-05-12 22:46:15 +02:00
err := protocolManager.InitService(beelzebubServiceConfiguration)
2022-05-08 20:49:53 +02:00
if err != nil {
log.Errorf("Error during init protocol: %s, %s", beelzebubServiceConfiguration.Protocol, err.Error())
}
}
<-quit
}
2022-05-12 22:46:15 +02:00
func traceStrategyStdout(event tracer.Event) {
log.WithFields(log.Fields{
"status": event.Status,
"event": event,
}).Info("New Event")
2022-05-17 00:17:39 +02:00
if mongoClient != nil {
coll := mongoClient.Database("beelzebub").Collection("event")
data, err := bson.Marshal(event)
if err != nil {
log.Fatal(err)
}
2022-05-17 00:17:39 +02:00
_, err = coll.InsertOne(context.TODO(), data)
if err != nil {
log.Fatal(err)
}
2022-05-17 00:17:39 +02:00
}
}
2022-05-08 20:49:53 +02:00
2022-05-12 22:46:15 +02:00
func configureLoggingByConfigurations(configurations parser.Logging) *os.File {
2022-05-08 20:49:53 +02:00
file, err := os.OpenFile(configurations.LogsPath, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
log.SetOutput(io.MultiWriter(os.Stdout, file))
2022-05-08 20:49:53 +02:00
log.SetFormatter(&log.JSONFormatter{
DisableTimestamp: configurations.LogDisableTimestamp,
})
log.SetReportCaller(configurations.DebugReportCaller)
if configurations.Debug {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
return file
}
2022-05-17 00:17:39 +02:00
func buildMongoClient(uri string) *mongo.Client {
// Create a new client and connect to the server
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri).SetServerSelectionTimeout(time.Second*2))
2022-05-17 00:17:39 +02:00
if err != nil {
log.Fatal(err)
}
// Ping the primary
if err := client.Ping(context.TODO(), readpref.Primary()); err != nil {
log.Fatal(err)
2022-05-17 00:17:39 +02:00
}
log.Println("Successfully connected and pinged.")
return client
}