mirror of
https://github.com/mariocandela/beelzebub.git
synced 2025-07-01 18:47:26 -04:00
first commit
This commit is contained in:
119
parser/configurationsParser.go
Normal file
119
parser/configurationsParser.go
Normal file
@ -0,0 +1,119 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gopkg.in/yaml.v3"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type BeelzebubCoreConfigurations struct {
|
||||
Core struct {
|
||||
Logging Logging `yaml:"logging"`
|
||||
}
|
||||
}
|
||||
|
||||
type Logging struct {
|
||||
Debug bool `yaml:"debug"`
|
||||
DebugReportCaller bool `yaml:"debugReportCaller"`
|
||||
LogDisableTimestamp bool `yaml:"logDisableTimestamp"`
|
||||
LogsPath string `yaml:"logsPath,omitempty"`
|
||||
}
|
||||
|
||||
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"`
|
||||
}
|
||||
|
||||
type Command struct {
|
||||
Regex string `yaml:"regex"`
|
||||
Handler string `yaml:"handler"`
|
||||
Headers []string `yaml:"headers"`
|
||||
StatusCode int `yaml:"statusCode"`
|
||||
}
|
||||
|
||||
type configurationsParser struct {
|
||||
configurationsCorePath string
|
||||
configurationsServicesDirectory string
|
||||
readFileBytes ReadFileBytes
|
||||
readDir ReadDir
|
||||
}
|
||||
|
||||
type ReadFileBytes func(filePath string) ([]byte, error)
|
||||
|
||||
type ReadDir func(dirName string) ([]string, error)
|
||||
|
||||
// Init Parser, return a configurationsParser and use the DI Pattern to inject the dependencies
|
||||
func Init(configurationsCorePath, configurationsServicesDirectory string) *configurationsParser {
|
||||
return &configurationsParser{
|
||||
configurationsCorePath: configurationsCorePath,
|
||||
configurationsServicesDirectory: configurationsServicesDirectory,
|
||||
readFileBytes: readFileBytes,
|
||||
readDir: readDir,
|
||||
}
|
||||
}
|
||||
|
||||
func (bp configurationsParser) ReadConfigurationsCore() (*BeelzebubCoreConfigurations, error) {
|
||||
buf, err := bp.readFileBytes(bp.configurationsCorePath)
|
||||
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
|
||||
}
|
||||
|
||||
func (bp configurationsParser) ReadConfigurationsServices() ([]BeelzebubServiceConfiguration, error) {
|
||||
services, err := bp.readDir(bp.configurationsServicesDirectory)
|
||||
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)
|
||||
buf, err := bp.readFileBytes(filePath)
|
||||
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
|
||||
}
|
||||
|
||||
func readDir(dirName string) ([]string, error) {
|
||||
var filesName []string
|
||||
files, err := ioutil.ReadDir(dirName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
filesName = append(filesName, file.Name())
|
||||
}
|
||||
return filesName, nil
|
||||
}
|
||||
|
||||
func readFileBytes(filePath string) ([]byte, error) {
|
||||
return os.ReadFile(filePath)
|
||||
}
|
113
parser/configurationsParser_test.go
Normal file
113
parser/configurationsParser_test.go
Normal file
@ -0,0 +1,113 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func mockReadfilebytesConfigurationsCore(filePath string) ([]byte, error) {
|
||||
configurationsCoreBytes := []byte(`
|
||||
core:
|
||||
logging:
|
||||
debug: false
|
||||
debugReportCaller: false
|
||||
logDisableTimestamp: true
|
||||
logsPath: ./logs`)
|
||||
return configurationsCoreBytes, nil
|
||||
}
|
||||
|
||||
func mockReadfilebytesFormatError(filePath string) ([]byte, error) {
|
||||
configurationsCoreBytes := []byte(`{{}`)
|
||||
return configurationsCoreBytes, nil
|
||||
}
|
||||
|
||||
func mockReadfilebytesError(filePath string) ([]byte, error) {
|
||||
return nil, errors.New("mockErrorReadFileBytes")
|
||||
}
|
||||
|
||||
func mockReadDirError(dirPath string) ([]string, error) {
|
||||
return nil, errors.New("mockErrorReadFileBytes")
|
||||
}
|
||||
|
||||
func mockReadDirValid(dirPath string) ([]string, error) {
|
||||
return []string{""}, nil
|
||||
}
|
||||
|
||||
func mockReadfilebytesBeelzebubServiceConfiguration(filePath string) ([]byte, error) {
|
||||
beelzebubServiceConfiguration := []byte(`
|
||||
apiVersion: "v1"
|
||||
protocol: "http"
|
||||
address: ":8080"
|
||||
commands:
|
||||
- regex: "wp-admin"
|
||||
handler: "login"
|
||||
headers:
|
||||
- "Content-Type: text/html"`)
|
||||
return beelzebubServiceConfiguration, nil
|
||||
}
|
||||
|
||||
func TestReadConfigurationsCoreError(t *testing.T) {
|
||||
configurationsParser := Init("mockConfigurationsCorePath", "mockConfigurationsServicesDirectory")
|
||||
|
||||
configurationsParser.readFileBytes = mockReadfilebytesError
|
||||
beelzebubCoreConfigurations, err := configurationsParser.ReadConfigurationsCore()
|
||||
|
||||
assert.Nil(t, beelzebubCoreConfigurations)
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, "in file mockConfigurationsCorePath: mockErrorReadFileBytes", err.Error())
|
||||
|
||||
configurationsParser.readFileBytes = mockReadfilebytesFormatError
|
||||
|
||||
beelzebubCoreConfigurations, err = configurationsParser.ReadConfigurationsCore()
|
||||
assert.Nil(t, beelzebubCoreConfigurations)
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, "in file mockConfigurationsCorePath: yaml: line 1: did not find expected ',' or '}'", err.Error())
|
||||
}
|
||||
|
||||
func TestReadConfigurationsCoreValid(t *testing.T) {
|
||||
configurationsParser := Init("", "")
|
||||
configurationsParser.readFileBytes = mockReadfilebytesConfigurationsCore
|
||||
|
||||
coreConfigurations, err := configurationsParser.ReadConfigurationsCore()
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, coreConfigurations.Core)
|
||||
assert.NotNil(t, coreConfigurations.Core.Logging)
|
||||
assert.Equal(t, coreConfigurations.Core.Logging.Debug, false)
|
||||
assert.Equal(t, coreConfigurations.Core.Logging.LogDisableTimestamp, true)
|
||||
assert.Equal(t, coreConfigurations.Core.Logging.DebugReportCaller, false)
|
||||
assert.Equal(t, coreConfigurations.Core.Logging.LogsPath, "./logs")
|
||||
}
|
||||
|
||||
func TestReadConfigurationsServicesFail(t *testing.T) {
|
||||
configurationsParser := Init("", "")
|
||||
|
||||
configurationsParser.readFileBytes = mockReadfilebytesError
|
||||
configurationsParser.readDir = mockReadDirError
|
||||
|
||||
beelzebubServiceConfiguration, err := configurationsParser.ReadConfigurationsServices()
|
||||
assert.Nil(t, beelzebubServiceConfiguration)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestReadConfigurationsServicesValid(t *testing.T) {
|
||||
configurationsParser := Init("", "")
|
||||
|
||||
configurationsParser.readFileBytes = mockReadfilebytesBeelzebubServiceConfiguration
|
||||
configurationsParser.readDir = mockReadDirValid
|
||||
|
||||
beelzebubServicesConfiguration, err := configurationsParser.ReadConfigurationsServices()
|
||||
|
||||
firstBeelzebubServiceConfiguration := beelzebubServicesConfiguration[0]
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, firstBeelzebubServiceConfiguration.Protocol, "http")
|
||||
assert.Equal(t, firstBeelzebubServiceConfiguration.ApiVersion, "v1")
|
||||
assert.Equal(t, firstBeelzebubServiceConfiguration.Address, ":8080")
|
||||
assert.Equal(t, len(firstBeelzebubServiceConfiguration.Commands), 1)
|
||||
assert.Equal(t, len(firstBeelzebubServiceConfiguration.Commands), 1)
|
||||
assert.Equal(t, firstBeelzebubServiceConfiguration.Commands[0].Regex, "wp-admin")
|
||||
assert.Equal(t, firstBeelzebubServiceConfiguration.Commands[0].Handler, "login")
|
||||
assert.Equal(t, len(firstBeelzebubServiceConfiguration.Commands[0].Headers), 1)
|
||||
assert.Equal(t, firstBeelzebubServiceConfiguration.Commands[0].Headers[0], "Content-Type: text/html")
|
||||
}
|
Reference in New Issue
Block a user