Files
beelzebub/parser/configurations_parser.go
Mario Candela 6468b5aa6f 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>
2023-02-26 18:04:05 +01:00

134 lines
4.2 KiB
Go

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"`
Tracing Tracing `yaml:"tracing"`
}
}
type Logging struct {
Debug bool `yaml:"debug"`
DebugReportCaller bool `yaml:"debugReportCaller"`
LogDisableTimestamp bool `yaml:"logDisableTimestamp"`
LogsPath string `yaml:"logsPath,omitempty"`
}
type Tracing struct {
RabbitMQEnabled bool `yaml:"rabbitMQEnabled"`
RabbitMQURI string `yaml:"rabbitMQURI"`
}
type Plugin struct {
OpenAPIChatGPTSecretKey string `yaml:"openAPIChatGPTSecretKey"`
}
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"`
Banner string `yaml:"banner"`
Plugin Plugin `yaml:"plugin"`
}
type Command struct {
Regex string `yaml:"regex"`
Handler string `yaml:"handler"`
Headers []string `yaml:"headers"`
StatusCode int `yaml:"statusCode"`
Plugin string `yaml:"plugin"`
}
type configurationsParser struct {
configurationsCorePath string
configurationsServicesDirectory string
readFileBytesByFilePathDependency ReadFileBytesByFilePath
gelAllFilesNameByDirNameDependency GelAllFilesNameByDirName
}
type ReadFileBytesByFilePath func(filePath string) ([]byte, error)
type GelAllFilesNameByDirName func(dirName string) ([]string, error)
// Init Parser, return a configurationsParser and use the D.I. Pattern to inject the dependencies
func Init(configurationsCorePath, configurationsServicesDirectory string) *configurationsParser {
return &configurationsParser{
configurationsCorePath: configurationsCorePath,
configurationsServicesDirectory: configurationsServicesDirectory,
readFileBytesByFilePathDependency: readFileBytesByFilePath,
gelAllFilesNameByDirNameDependency: gelAllFilesNameByDirName,
}
}
func (bp configurationsParser) ReadConfigurationsCore() (*BeelzebubCoreConfigurations, error) {
buf, err := bp.readFileBytesByFilePathDependency(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.gelAllFilesNameByDirNameDependency(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.readFileBytesByFilePathDependency(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 gelAllFilesNameByDirName(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 readFileBytesByFilePath(filePath string) ([]byte, error) {
return os.ReadFile(filePath)
}