added playground validator to validate the input more.
This commit is contained in:
+22
-13
@@ -4,7 +4,9 @@ import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/goccy/go-yaml"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"slices"
|
||||
@@ -41,42 +43,42 @@ func (c CIDR) MarshalYAML() ([]byte, error) {
|
||||
|
||||
type Port struct {
|
||||
Port string `yaml:"port"`
|
||||
Protocol string `yaml:"protocol"`
|
||||
Protocol string `yaml:"protocol,omitempty" validate:"omitempty,oneof=TCP UDP"`
|
||||
}
|
||||
|
||||
// Network represents each network entry in the YAML
|
||||
type Network struct {
|
||||
Name string `yaml:"name"`
|
||||
Name string `yaml:"name" validate:"required"`
|
||||
CIDR CIDR `yaml:"cidr"`
|
||||
Except []CIDR `yaml:"except,omitempty"`
|
||||
Ports []Port `yaml:"ports,omitempty"`
|
||||
Except []CIDR `yaml:"except,omitempty" validate:"dive,required"`
|
||||
Ports []Port `yaml:"ports,omitempty" validate:"dive,required"`
|
||||
}
|
||||
|
||||
type Application struct {
|
||||
Name string `yaml:"name"`
|
||||
Ports []Port `yaml:"ports,omitempty"`
|
||||
MatchLabels map[string]string `yaml:"matchLabels"`
|
||||
Namespace *Namespace `yaml:"-"`
|
||||
Namespace *Namespace `yaml:"-" validate:"-"`
|
||||
}
|
||||
|
||||
type Namespace struct {
|
||||
Name string `yaml:"name"`
|
||||
Open bool `yaml:"open"`
|
||||
Capabilities []string `yaml:"capabilities"`
|
||||
Applications []*Application `yaml:"applications"`
|
||||
Applications []*Application `yaml:"applications" validate:"dive,required"`
|
||||
}
|
||||
|
||||
type Communication struct {
|
||||
From []string `yaml:"from"`
|
||||
To []string `yaml:"to"`
|
||||
Ports []Port `yaml:"ports"`
|
||||
From []string `yaml:"from" validate:"dive,required"`
|
||||
To []string `yaml:"to" validate:"dive,required"`
|
||||
Ports []Port `yaml:"ports" validate:"dive,required"`
|
||||
}
|
||||
|
||||
// Config represents the top-level YAML structure
|
||||
type Config struct {
|
||||
Networks []*Network `yaml:"networks,omitempty"`
|
||||
Namespaces []*Namespace `yaml:"namespaces,omitempty"`
|
||||
Communications []*Communication `yaml:"communications,omitempty"`
|
||||
Networks []*Network `yaml:"networks,omitempty" validate:"dive,required"`
|
||||
Namespaces []*Namespace `yaml:"namespaces,omitempty" validate:"dive,required"`
|
||||
Communications []*Communication `yaml:"communications,omitempty" validate:"dive,required"`
|
||||
}
|
||||
|
||||
func (c *Config) Update(config *Config) {
|
||||
@@ -85,6 +87,11 @@ func (c *Config) Update(config *Config) {
|
||||
c.Communications = append(c.Communications, config.Communications...)
|
||||
}
|
||||
|
||||
func (c Config) ValidateSchema() error {
|
||||
validate := validator.New(validator.WithRequiredStructEnabled())
|
||||
return validate.Struct(c)
|
||||
}
|
||||
|
||||
func (c Config) Validate() error {
|
||||
|
||||
errs := make([]error, 0)
|
||||
@@ -100,7 +107,8 @@ func (c Config) Validate() error {
|
||||
|
||||
// network names mus tbe unique
|
||||
networks := make(map[string]bool)
|
||||
for _, network := range c.Networks {
|
||||
for nn, network := range c.Networks {
|
||||
log.Printf("Network %+v %v %v\n", network, nn, c.Networks)
|
||||
if networks[network.Name] {
|
||||
errs = append(errs, fmt.Errorf("Duplicate network name %s", network.Name))
|
||||
}
|
||||
@@ -174,6 +182,7 @@ func LoadConfig(file string) (*Config, error) {
|
||||
yaml.UseJSONUnmarshaler(),
|
||||
yaml.DisallowUnknownField(),
|
||||
yaml.UseOrderedMap(),
|
||||
yaml.Strict(),
|
||||
)
|
||||
var config Config
|
||||
err = dec.Decode(&config)
|
||||
|
||||
@@ -22,7 +22,10 @@ func execute(files []string, options *Options) error {
|
||||
log.Printf("LOADING %s\n", file)
|
||||
configNew, err := LoadConfig(file)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("%s: %w", file, err)
|
||||
}
|
||||
if err = configNew.ValidateSchema(); err != nil {
|
||||
return fmt.Errorf("%s: %w", file, err)
|
||||
}
|
||||
config.Update(configNew)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user