first commit

This commit is contained in:
Mario
2022-05-08 20:49:53 +02:00
commit 7b7eeb3e15
21 changed files with 1314 additions and 0 deletions

View 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)
}

View 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")
}