mirror of
https://github.com/mariocandela/beelzebub.git
synced 2025-07-01 18:47:26 -04:00
refactor:Added Integration test and tiny refactoring (#23)
* Refactoring name convention * Added integration test * Added Makefile * Bump golang.org/x/crypto from 0.0.0-20220826181053-bd7e27e6170d to 0.6.0 Bumps [golang.org/x/crypto](https://github.com/golang/crypto) from 0.0.0-20220826181053-bd7e27e6170d to 0.6.0. - [Release notes](https://github.com/golang/crypto/releases) - [Commits](https://github.com/golang/crypto/commits/v0.6.0) --- updated-dependencies: - dependency-name: golang.org/x/crypto dependency-type: direct:production update-type: version-update:semver-minor ... * Upgrade go from 1.16 to 1.20 * Added integration test: HTTP, TCP, SSH * Added Makefile Improve README.md * Fixed unit test CI * Fixed go-version * Added integration test into C.I. actions --------- Signed-off-by: Mario Candela <m4r10.php@gmail.com> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
This commit is contained in:
124
builder/builder.go
Normal file
124
builder/builder.go
Normal file
@ -0,0 +1,124 @@
|
||||
package builder
|
||||
|
||||
import (
|
||||
"beelzebub/parser"
|
||||
"beelzebub/protocols"
|
||||
"beelzebub/protocols/strategies"
|
||||
"beelzebub/tracer"
|
||||
"errors"
|
||||
"fmt"
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
const RabbitmqQueueName = "event"
|
||||
|
||||
type Builder struct {
|
||||
beelzebubServicesConfiguration []parser.BeelzebubServiceConfiguration
|
||||
traceStrategy tracer.Strategy
|
||||
rabbitMQChannel *amqp.Channel
|
||||
rabbitMQConnection *amqp.Connection
|
||||
logsFile *os.File
|
||||
}
|
||||
|
||||
func (b *Builder) setTraceStrategy(traceStrategy tracer.Strategy) {
|
||||
b.traceStrategy = traceStrategy
|
||||
}
|
||||
|
||||
func (b *Builder) buildLogger(configurations parser.Logging) error {
|
||||
logsFile, err := os.OpenFile(configurations.LogsPath, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.SetOutput(io.MultiWriter(os.Stdout, logsFile))
|
||||
|
||||
log.SetFormatter(&log.JSONFormatter{
|
||||
DisableTimestamp: configurations.LogDisableTimestamp,
|
||||
})
|
||||
log.SetReportCaller(configurations.DebugReportCaller)
|
||||
if configurations.Debug {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
} else {
|
||||
log.SetLevel(log.InfoLevel)
|
||||
}
|
||||
b.logsFile = logsFile
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Builder) buildRabbitMQ(rabbitMQURI string) error {
|
||||
rabbitMQConnection, err := amqp.Dial(rabbitMQURI)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
b.rabbitMQChannel, err = rabbitMQConnection.Channel()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//creates a queue if it doesn't already exist, or ensures that an existing queue matches the same parameters.
|
||||
if _, err = b.rabbitMQChannel.QueueDeclare(RabbitmqQueueName, false, false, false, false, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
b.rabbitMQConnection = rabbitMQConnection
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Builder) Close() error {
|
||||
if b.rabbitMQConnection != nil {
|
||||
if err := b.rabbitMQChannel.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := b.rabbitMQConnection.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Builder) Run() error {
|
||||
// Init Protocol strategies
|
||||
secureShellStrategy := &strategies.SecureShellStrategy{}
|
||||
hypertextTransferProtocolStrategy := &strategies.HypertextTransferProtocolStrategy{}
|
||||
transmissionControlProtocolStrategy := &strategies.TransmissionControlProtocolStrategy{}
|
||||
|
||||
// Init Tracer strategies, and set the trace strategy default HTTP
|
||||
protocolManager := protocols.InitProtocolManager(b.traceStrategy, hypertextTransferProtocolStrategy)
|
||||
|
||||
for _, beelzebubServiceConfiguration := range b.beelzebubServicesConfiguration {
|
||||
switch beelzebubServiceConfiguration.Protocol {
|
||||
case "http":
|
||||
protocolManager.SetProtocolStrategy(hypertextTransferProtocolStrategy)
|
||||
break
|
||||
case "ssh":
|
||||
protocolManager.SetProtocolStrategy(secureShellStrategy)
|
||||
break
|
||||
case "tcp":
|
||||
protocolManager.SetProtocolStrategy(transmissionControlProtocolStrategy)
|
||||
break
|
||||
default:
|
||||
log.Fatalf("Protocol %s not managed", beelzebubServiceConfiguration.Protocol)
|
||||
continue
|
||||
}
|
||||
|
||||
if err := protocolManager.InitService(beelzebubServiceConfiguration); err != nil {
|
||||
return errors.New(fmt.Sprintf("Error during init protocol: %s, %s", beelzebubServiceConfiguration.Protocol, err.Error()))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Builder) build() *Builder {
|
||||
return &Builder{
|
||||
beelzebubServicesConfiguration: b.beelzebubServicesConfiguration,
|
||||
traceStrategy: b.traceStrategy,
|
||||
}
|
||||
}
|
||||
|
||||
func NewBuilder() *Builder {
|
||||
return &Builder{}
|
||||
}
|
Reference in New Issue
Block a user