refactor: Improve Go docs (#74)

* add go docs, package: parser
* add go docs, package: protocols
* add go docs, package: tracer
This commit is contained in:
Mario Candela
2023-10-15 20:54:53 +02:00
committed by GitHub
parent d77aa0c8a0
commit 5e5d0494a9
3 changed files with 19 additions and 5 deletions

View File

@ -1,3 +1,4 @@
// Package parser is responsible for parsing the configurations of the core and honeypot service
package parser package parser
import ( import (
@ -10,6 +11,7 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
// BeelzebubCoreConfigurations is the struct that contains the configurations of the core
type BeelzebubCoreConfigurations struct { type BeelzebubCoreConfigurations struct {
Core struct { Core struct {
Logging Logging `yaml:"logging"` Logging Logging `yaml:"logging"`
@ -18,6 +20,7 @@ type BeelzebubCoreConfigurations struct {
} }
} }
// Logging is the struct that contains the configurations of the logging
type Logging struct { type Logging struct {
Debug bool `yaml:"debug"` Debug bool `yaml:"debug"`
DebugReportCaller bool `yaml:"debugReportCaller"` DebugReportCaller bool `yaml:"debugReportCaller"`
@ -25,6 +28,7 @@ type Logging struct {
LogsPath string `yaml:"logsPath,omitempty"` LogsPath string `yaml:"logsPath,omitempty"`
} }
// Tracings is the struct that contains the configurations of the tracings
type Tracings struct { type Tracings struct {
RabbitMQ `yaml:"rabbit-mq"` RabbitMQ `yaml:"rabbit-mq"`
} }
@ -42,6 +46,7 @@ type Plugin struct {
OpenAPIChatGPTSecretKey string `yaml:"openAPIChatGPTSecretKey"` OpenAPIChatGPTSecretKey string `yaml:"openAPIChatGPTSecretKey"`
} }
// BeelzebubServiceConfiguration is the struct that contains the configurations of the honeypot service
type BeelzebubServiceConfiguration struct { type BeelzebubServiceConfiguration struct {
ApiVersion string `yaml:"apiVersion"` ApiVersion string `yaml:"apiVersion"`
Protocol string `yaml:"protocol"` Protocol string `yaml:"protocol"`
@ -56,6 +61,7 @@ type BeelzebubServiceConfiguration struct {
Plugin Plugin `yaml:"plugin"` Plugin Plugin `yaml:"plugin"`
} }
// Command is the struct that contains the configurations of the commands
type Command struct { type Command struct {
Regex string `yaml:"regex"` Regex string `yaml:"regex"`
Handler string `yaml:"handler"` Handler string `yaml:"handler"`
@ -85,6 +91,7 @@ func Init(configurationsCorePath, configurationsServicesDirectory string) *confi
} }
} }
// ReadConfigurationsCore is the method that reads the configurations of the core from files
func (bp configurationsParser) ReadConfigurationsCore() (*BeelzebubCoreConfigurations, error) { func (bp configurationsParser) ReadConfigurationsCore() (*BeelzebubCoreConfigurations, error) {
buf, err := bp.readFileBytesByFilePathDependency(bp.configurationsCorePath) buf, err := bp.readFileBytesByFilePathDependency(bp.configurationsCorePath)
if err != nil { if err != nil {
@ -100,6 +107,7 @@ func (bp configurationsParser) ReadConfigurationsCore() (*BeelzebubCoreConfigura
return beelzebubConfiguration, nil return beelzebubConfiguration, nil
} }
// ReadConfigurationsServices is the method that reads the configurations of the honeypot services from files
func (bp configurationsParser) ReadConfigurationsServices() ([]BeelzebubServiceConfiguration, error) { func (bp configurationsParser) ReadConfigurationsServices() ([]BeelzebubServiceConfiguration, error) {
services, err := bp.gelAllFilesNameByDirNameDependency(bp.configurationsServicesDirectory) services, err := bp.gelAllFilesNameByDirNameDependency(bp.configurationsServicesDirectory)
if err != nil { if err != nil {

View File

@ -1,3 +1,4 @@
// Package protocols is responsible for managing the different protocols
package protocols package protocols
import ( import (
@ -5,6 +6,7 @@ import (
"github.com/mariocandela/beelzebub/v3/tracer" "github.com/mariocandela/beelzebub/v3/tracer"
) )
// ServiceStrategy is the common interface that each protocol honeypot implements
type ServiceStrategy interface { type ServiceStrategy interface {
Init(beelzebubServiceConfiguration parser.BeelzebubServiceConfiguration, tracer tracer.Tracer) error Init(beelzebubServiceConfiguration parser.BeelzebubServiceConfiguration, tracer tracer.Tracer) error
} }
@ -14,10 +16,11 @@ type ProtocolManager struct {
tracer tracer.Tracer tracer tracer.Tracer
} }
func InitProtocolManager(tracerStrategy tracer.Strategy, strategy ServiceStrategy) *ProtocolManager { // InitProtocolManager is the method that initializes the protocol manager, receving the concrete tracer and the concrete service
func InitProtocolManager(tracerStrategy tracer.Strategy, serviceStrategy ServiceStrategy) *ProtocolManager {
return &ProtocolManager{ return &ProtocolManager{
tracer: tracer.GetInstance(tracerStrategy), tracer: tracer.GetInstance(tracerStrategy),
strategy: strategy, strategy: serviceStrategy,
} }
} }
@ -25,6 +28,7 @@ func (pm *ProtocolManager) SetProtocolStrategy(strategy ServiceStrategy) {
pm.strategy = strategy pm.strategy = strategy
} }
// InitService is the method that initializes the honeypot
func (pm *ProtocolManager) InitService(beelzebubServiceConfiguration parser.BeelzebubServiceConfiguration) error { func (pm *ProtocolManager) InitService(beelzebubServiceConfiguration parser.BeelzebubServiceConfiguration) error {
return pm.strategy.Init(beelzebubServiceConfiguration, pm.tracer) return pm.strategy.Init(beelzebubServiceConfiguration, pm.tracer)
} }

View File

@ -1,3 +1,4 @@
// Package tracer is responsible for tracing the events that occur in the honeypots
package tracer package tracer
import ( import (
@ -9,6 +10,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promauto"
) )
// Workers is the number of workers that will
const Workers = 5 const Workers = 5
type Event struct { type Event struct {
@ -78,14 +80,14 @@ type tracer struct {
var lock = &sync.Mutex{} var lock = &sync.Mutex{}
var singleton *tracer var singleton *tracer
func GetInstance(strategy Strategy) *tracer { func GetInstance(defaultStrategy Strategy) *tracer {
if singleton == nil { if singleton == nil {
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
// This is to prevent expensive lock operations every time the GetInstance method is called // This is to prevent expensive lock operations every time the GetInstance method is called
if singleton == nil { if singleton == nil {
singleton = &tracer{ singleton = &tracer{
strategy: strategy, strategy: defaultStrategy,
eventsChan: make(chan Event, Workers), eventsChan: make(chan Event, Workers),
eventsTotal: promauto.NewCounter(prometheus.CounterOpts{ eventsTotal: promauto.NewCounter(prometheus.CounterOpts{
Namespace: "beelzebub", Namespace: "beelzebub",
@ -111,7 +113,7 @@ func GetInstance(strategy Strategy) *tracer {
for i := 0; i < Workers; i++ { for i := 0; i < Workers; i++ {
go func(i int) { go func(i int) {
log.Debug("GetInstance trace worker: ", i) log.Debug("Trace worker: ", i)
for event := range singleton.eventsChan { for event := range singleton.eventsChan {
singleton.strategy(event) singleton.strategy(event)
} }