2021-08-04 11:04:01 +00:00
|
|
|
package config
|
|
|
|
|
2023-10-28 20:17:05 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
2021-08-06 13:27:42 +00:00
|
|
|
// The struct that represents the config.json file
|
2021-08-04 11:04:01 +00:00
|
|
|
type Config struct {
|
|
|
|
ListenAddress string
|
|
|
|
Services map[string]struct {
|
2021-08-14 00:02:36 +00:00
|
|
|
Script Command
|
|
|
|
Secret string
|
|
|
|
SignaturePrefix string
|
|
|
|
SignatureHeader string
|
|
|
|
DisableSignatureVerification bool
|
|
|
|
Tests []Command
|
2021-08-04 11:04:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-06 13:27:42 +00:00
|
|
|
// Check that all required fields are filled in
|
2021-08-04 11:04:01 +00:00
|
|
|
func (c Config) Validate() error {
|
|
|
|
if c.ListenAddress == "" {
|
|
|
|
return requiredFieldError{"ListenAddress", ""}
|
|
|
|
}
|
|
|
|
|
|
|
|
for serviceName, service := range c.Services {
|
|
|
|
if service.Script.Program == "" {
|
|
|
|
return requiredFieldError{"Script.Program", serviceName}
|
|
|
|
}
|
2021-08-14 00:02:36 +00:00
|
|
|
if !service.DisableSignatureVerification && service.SignatureHeader == "" {
|
2021-08-04 11:04:01 +00:00
|
|
|
return requiredFieldError{"SignatureHeader", serviceName}
|
|
|
|
}
|
2021-08-14 00:02:36 +00:00
|
|
|
if !service.DisableSignatureVerification && service.Secret == "" {
|
2021-08-04 11:04:01 +00:00
|
|
|
return requiredFieldError{"Secret", serviceName}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-10-28 20:17:05 +00:00
|
|
|
|
|
|
|
func (c *Config) Load(config_filename string) error {
|
|
|
|
|
|
|
|
raw_config, err := ioutil.ReadFile(config_filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(raw_config, &c)
|
|
|
|
if err == nil {
|
|
|
|
return c.Validate()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(raw_config, &c)
|
|
|
|
if err == nil {
|
|
|
|
return c.Validate()
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|