Files
beelzebub/main.go

134 lines
3.6 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-21 11:43:10 +02:00
"encoding/json"
"fmt"
2022-05-21 12:38:08 +02:00
amqp "github.com/rabbitmq/amqp091-go"
2022-05-08 20:49:53 +02:00
log "github.com/sirupsen/logrus"
"io"
"os"
)
var quit = make(chan struct{})
2022-05-21 12:24:07 +02:00
var channel *amqp.Channel
2022-05-17 00:17:39 +02:00
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-06-04 17:34:03 +02:00
failOnError(err, fmt.Sprintf("Error during ReadConfigurationsCore: "))
2022-05-08 20:49:53 +02:00
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-21 11:43:10 +02:00
failOnError(err, fmt.Sprintf("Error during ReadConfigurationsServices: "))
if coreConfigurations.Core.Tracing.RabbitMQEnabled {
2022-06-01 23:52:01 +02:00
rabbitMQURI, configured := os.LookupEnv("RABBITMQ_URI")
if !configured {
rabbitMQURI = coreConfigurations.Core.Tracing.RabbitMQURI
}
2022-06-04 17:44:15 +02:00
conn, err := amqp.Dial(rabbitMQURI)
failOnError(err, "Failed to connect to RabbitMQ")
2022-06-04 17:34:03 +02:00
defer conn.Close()
2022-06-04 17:44:15 +02:00
channel, err = conn.Channel()
failOnError(err, "Failed to open a channel")
defer channel.Close()
2022-05-08 20:49:53 +02:00
}
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-07-03 17:15:38 +02:00
transmissionControlProtocolStrategy := &protocols.TransmissionControlProtocolStrategy{}
2022-05-08 20:49:53 +02:00
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(traceStrategyStdoutAndRabbitMQ, hypertextTransferProtocolStrategy)
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
2022-07-03 17:15:38 +02:00
case "tcp":
protocolManager.SetProtocolStrategy(transmissionControlProtocolStrategy)
break
2022-05-08 20:49:53 +02:00
default:
log.Fatalf("Protocol %s not managed", beelzebubServiceConfiguration.Protocol)
continue
}
2022-05-12 22:46:15 +02:00
err := protocolManager.InitService(beelzebubServiceConfiguration)
2022-05-21 11:43:10 +02:00
failOnError(err, fmt.Sprintf("Error during init protocol: %s, ", beelzebubServiceConfiguration.Protocol))
2022-05-08 20:49:53 +02:00
}
<-quit
}
2022-05-21 11:43:10 +02:00
func failOnError(err error, msg string) {
if err != nil {
log.Fatalf("%s: %s", msg, err)
}
}
func traceStrategyStdoutAndRabbitMQ(event tracer.Event) {
log.WithFields(log.Fields{
"status": event.Status,
"event": event,
}).Info("New Event")
2022-05-17 00:17:39 +02:00
2022-05-21 12:24:07 +02:00
if channel != nil {
2022-06-04 17:44:15 +02:00
log.Debug("Push Event on queue")
2022-05-21 11:43:10 +02:00
eventJSON, err := json.Marshal(event)
2022-05-21 12:24:07 +02:00
failOnError(err, "Failed to Marshal Event")
queue, err := channel.QueueDeclare(
"event",
false,
false,
false,
false,
nil,
)
failOnError(err, "Failed to declare a queue")
2022-05-21 11:43:10 +02:00
err = channel.Publish(
"",
queue.Name,
false,
false,
amqp.Publishing{
ContentType: "application/json",
Body: eventJSON,
})
failOnError(err, "Failed to publish a message")
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
}