-
Notifications
You must be signed in to change notification settings - Fork 13
/
ruleset.go
87 lines (67 loc) · 2.06 KB
/
ruleset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package validate
import (
"context"
"os"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/dadrus/heimdall/internal/config"
"github.com/dadrus/heimdall/internal/rules"
"github.com/dadrus/heimdall/internal/rules/event"
"github.com/dadrus/heimdall/internal/rules/mechanisms"
"github.com/dadrus/heimdall/internal/rules/provider/filesystem"
)
// NewValidateRulesCommand represents the "validate rules" command.
func NewValidateRulesCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "rules [path to ruleset]",
Short: "Validates heimdall's ruleset",
Args: cobra.ExactArgs(1),
Example: "heimdall validate rules -c myconfig.yaml myruleset.yaml",
Run: func(cmd *cobra.Command, args []string) {
if err := validateRuleSet(cmd, args); err != nil {
cmd.PrintErrf("%v\n", err)
os.Exit(1)
}
cmd.Println("Rule set is valid")
},
}
cmd.PersistentFlags().Bool("proxy-mode", false,
"If specified, rule set validation considers usage in proxy operation mode")
return cmd
}
func validateRuleSet(cmd *cobra.Command, args []string) error {
const queueSize = 50
envPrefix, _ := cmd.Flags().GetString("env-config-prefix")
logger := zerolog.Nop()
configPath, _ := cmd.Flags().GetString("config")
if len(configPath) == 0 {
return ErrNoConfigFile
}
opMode := config.DecisionMode
if proxyMode, _ := cmd.Flags().GetBool("proxy-mode"); proxyMode {
opMode = config.ProxyMode
}
conf, err := config.NewConfiguration(
config.EnvVarPrefix(envPrefix),
config.ConfigurationPath(configPath),
)
if err != nil {
return err
}
conf.Providers.FileSystem = map[string]any{"src": args[0]}
mFactory, err := mechanisms.NewFactory(conf, logger)
if err != nil {
return err
}
rFactory, err := rules.NewRuleFactory(mFactory, conf, opMode, logger)
if err != nil {
return err
}
queue := make(event.RuleSetChangedEventQueue, queueSize)
defer close(queue)
provider, err := filesystem.NewProvider(conf, rules.NewRuleSetProcessor(queue, rFactory, logger), logger)
if err != nil {
return err
}
return provider.Start(context.Background())
}