Files
beelzebub/parser/configurations_parser.go

174 lines
5.7 KiB
Go
Raw Normal View History

// Package parser is responsible for parsing the configurations of the core and honeypot service
2022-05-08 20:49:53 +02:00
package parser
import (
"fmt"
"github.com/mariocandela/beelzebub/v3/plugins"
2022-05-08 20:49:53 +02:00
"os"
"path/filepath"
2023-06-16 23:32:49 +02:00
"strings"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
2022-05-08 20:49:53 +02:00
)
// BeelzebubCoreConfigurations is the struct that contains the configurations of the core
2022-05-08 20:49:53 +02:00
type BeelzebubCoreConfigurations struct {
Core struct {
Logging Logging `yaml:"logging"`
Tracings Tracings `yaml:"tracings"`
Prometheus Prometheus `yaml:"prometheus"`
2022-05-08 20:49:53 +02:00
}
}
// Logging is the struct that contains the configurations of the logging
2022-05-08 20:49:53 +02:00
type Logging struct {
Debug bool `yaml:"debug"`
DebugReportCaller bool `yaml:"debugReportCaller"`
LogDisableTimestamp bool `yaml:"logDisableTimestamp"`
LogsPath string `yaml:"logsPath,omitempty"`
}
// Tracings is the struct that contains the configurations of the tracings
type Tracings struct {
RabbitMQ `yaml:"rabbit-mq"`
BeelzebubCloud `yaml:"beelzebub-cloud"`
}
type BeelzebubCloud struct {
Enabled bool `yaml:"enabled"`
URI string `yaml:"uri"`
AuthToken string `yaml:"auth-token"`
}
type RabbitMQ struct {
Enabled bool `yaml:"enabled"`
URI string `yaml:"uri"`
}
type Prometheus struct {
Path string `yaml:"path"`
Port string `yaml:"port"`
}
type Plugin struct {
OpenAISecretKey string `yaml:"openAISecretKey"`
Host string `yaml:"host"`
LLMModel string `yaml:"llmModel"`
}
func FromString(llmModel string) (plugins.LLMModel, error) {
switch llmModel {
case "llama3":
return plugins.LLAMA3, nil
case "gpt4-o":
return plugins.GPT4O, nil
default:
return -1, fmt.Errorf("model %s not found", llmModel)
}
}
// BeelzebubServiceConfiguration is the struct that contains the configurations of the honeypot service
2022-05-08 20:49:53 +02:00
type BeelzebubServiceConfiguration struct {
ApiVersion string `yaml:"apiVersion"`
Protocol string `yaml:"protocol"`
Address string `yaml:"address"`
Commands []Command `yaml:"commands"`
ServerVersion string `yaml:"serverVersion"`
ServerName string `yaml:"serverName"`
DeadlineTimeoutSeconds int `yaml:"deadlineTimeoutSeconds"`
PasswordRegex string `yaml:"passwordRegex"`
Description string `yaml:"description"`
2022-07-03 12:39:27 +02:00
Banner string `yaml:"banner"`
Plugin Plugin `yaml:"plugin"`
2022-05-08 20:49:53 +02:00
}
// Command is the struct that contains the configurations of the commands
2022-05-08 20:49:53 +02:00
type Command struct {
Regex string `yaml:"regex"`
Handler string `yaml:"handler"`
Headers []string `yaml:"headers"`
StatusCode int `yaml:"statusCode"`
Plugin string `yaml:"plugin"`
2022-05-08 20:49:53 +02:00
}
type configurationsParser struct {
2022-05-12 22:46:15 +02:00
configurationsCorePath string
configurationsServicesDirectory string
readFileBytesByFilePathDependency ReadFileBytesByFilePath
2022-05-14 16:51:00 +02:00
gelAllFilesNameByDirNameDependency GelAllFilesNameByDirName
2022-05-08 20:49:53 +02:00
}
2022-05-12 22:46:15 +02:00
type ReadFileBytesByFilePath func(filePath string) ([]byte, error)
2022-05-08 20:49:53 +02:00
2022-05-14 16:51:00 +02:00
type GelAllFilesNameByDirName func(dirName string) ([]string, error)
2022-05-08 20:49:53 +02:00
2022-06-04 17:14:46 +02:00
// Init Parser, return a configurationsParser and use the D.I. Pattern to inject the dependencies
2022-05-08 20:49:53 +02:00
func Init(configurationsCorePath, configurationsServicesDirectory string) *configurationsParser {
return &configurationsParser{
2022-05-12 22:46:15 +02:00
configurationsCorePath: configurationsCorePath,
configurationsServicesDirectory: configurationsServicesDirectory,
readFileBytesByFilePathDependency: readFileBytesByFilePath,
2022-05-14 16:51:00 +02:00
gelAllFilesNameByDirNameDependency: gelAllFilesNameByDirName,
2022-05-08 20:49:53 +02:00
}
}
// ReadConfigurationsCore is the method that reads the configurations of the core from files
2022-05-08 20:49:53 +02:00
func (bp configurationsParser) ReadConfigurationsCore() (*BeelzebubCoreConfigurations, error) {
2022-05-12 22:46:15 +02:00
buf, err := bp.readFileBytesByFilePathDependency(bp.configurationsCorePath)
2022-05-08 20:49:53 +02:00
if err != nil {
return nil, fmt.Errorf("in file %s: %v", bp.configurationsCorePath, err)
}
beelzebubConfiguration := &BeelzebubCoreConfigurations{}
err = yaml.Unmarshal(buf, beelzebubConfiguration)
if err != nil {
return nil, fmt.Errorf("in file %s: %v", bp.configurationsCorePath, err)
}
return beelzebubConfiguration, nil
}
// ReadConfigurationsServices is the method that reads the configurations of the honeypot services from files
2022-05-08 20:49:53 +02:00
func (bp configurationsParser) ReadConfigurationsServices() ([]BeelzebubServiceConfiguration, error) {
2022-05-14 16:51:00 +02:00
services, err := bp.gelAllFilesNameByDirNameDependency(bp.configurationsServicesDirectory)
2022-05-08 20:49:53 +02:00
if err != nil {
return nil, fmt.Errorf("in directory %s: %v", bp.configurationsServicesDirectory, err)
}
var servicesConfiguration []BeelzebubServiceConfiguration
for _, servicesName := range services {
filePath := filepath.Join(bp.configurationsServicesDirectory, servicesName)
2022-05-12 22:46:15 +02:00
buf, err := bp.readFileBytesByFilePathDependency(filePath)
2022-05-08 20:49:53 +02:00
if err != nil {
return nil, fmt.Errorf("in file %s: %v", filePath, err)
}
beelzebubServiceConfiguration := &BeelzebubServiceConfiguration{}
err = yaml.Unmarshal(buf, beelzebubServiceConfiguration)
if err != nil {
return nil, fmt.Errorf("in file %s: %v", filePath, err)
}
log.Debug(beelzebubServiceConfiguration)
servicesConfiguration = append(servicesConfiguration, *beelzebubServiceConfiguration)
}
return servicesConfiguration, nil
}
2022-05-14 16:51:00 +02:00
func gelAllFilesNameByDirName(dirName string) ([]string, error) {
files, err := os.ReadDir(dirName)
2022-05-08 20:49:53 +02:00
if err != nil {
return nil, err
}
2023-06-16 23:57:35 +02:00
var filesName []string
2022-05-08 20:49:53 +02:00
for _, file := range files {
2023-06-16 23:57:35 +02:00
if !file.IsDir() && strings.HasSuffix(file.Name(), ".yaml") {
2023-06-16 23:32:49 +02:00
filesName = append(filesName, file.Name())
}
2022-05-08 20:49:53 +02:00
}
return filesName, nil
}
2022-05-12 22:46:15 +02:00
func readFileBytesByFilePath(filePath string) ([]byte, error) {
2022-05-08 20:49:53 +02:00
return os.ReadFile(filePath)
}