2022-05-08 20:49:53 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"beelzebub/parser"
|
|
|
|
"beelzebub/protocols"
|
2022-05-11 22:58:03 +02:00
|
|
|
"beelzebub/tracer"
|
2022-05-21 11:43:10 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-05-08 20:49:53 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-05-21 11:43:10 +02:00
|
|
|
"github.com/streadway/amqp"
|
2022-05-08 20:49:53 +02:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
var quit = make(chan struct{})
|
|
|
|
|
2022-05-21 11:43:10 +02:00
|
|
|
var queue amqp.Queue
|
|
|
|
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-05-21 11:43:10 +02:00
|
|
|
failOnError(err, fmt.Sprintf("Error during coreConfigurations: "))
|
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 {
|
|
|
|
conn, err := amqp.Dial(coreConfigurations.Core.Tracing.RabbitMQURI)
|
|
|
|
failOnError(err, "Failed to connect to RabbitMQ")
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
channel, err := conn.Channel()
|
|
|
|
failOnError(err, "Failed to open a channel")
|
|
|
|
defer channel.Close()
|
|
|
|
|
|
|
|
queue, err = channel.QueueDeclare(
|
|
|
|
"event", // name
|
|
|
|
false, // durable
|
|
|
|
false, // delete when unused
|
|
|
|
false, // exclusive
|
|
|
|
false, // no-wait
|
|
|
|
nil, // arguments
|
|
|
|
)
|
|
|
|
failOnError(err, "Failed to declare a queue")
|
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-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
|
|
|
|
|
|
|
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-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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-12 22:46:15 +02:00
|
|
|
func traceStrategyStdout(event tracer.Event) {
|
2022-05-11 22:58:03 +02:00
|
|
|
log.WithFields(log.Fields{
|
2022-05-17 23:31:20 +02:00
|
|
|
"status": event.Status,
|
2022-05-11 22:58:03 +02:00
|
|
|
"event": event,
|
|
|
|
}).Info("New Event")
|
2022-05-17 00:17:39 +02:00
|
|
|
|
2022-05-21 11:43:10 +02:00
|
|
|
//TODO check amqp.Channe
|
|
|
|
if queue != (amqp.Queue{}) {
|
|
|
|
eventJSON, err := json.Marshal(event)
|
|
|
|
failOnError(err, "Failed to publish a message")
|
|
|
|
|
|
|
|
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-11 22:58:03 +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)
|
|
|
|
}
|
|
|
|
|
2022-05-11 22:58:03 +02:00
|
|
|
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
|
|
|
|
}
|