Skip to content

Commit d45e5f1

Browse files
committed
Intentions module input configuration added
1 parent 86e55d5 commit d45e5f1

File tree

7 files changed

+1280
-4
lines changed

7 files changed

+1280
-4
lines changed

config/intentions_service.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
)
7+
8+
// IntentionsServicesConfig configures regexp/list of names of services.
9+
type IntentionsServicesConfig struct {
10+
// Regexp configures the source services to monitor by matching on the service name.
11+
// Either Regexp or Names must be configured, not both.
12+
Regexp *string `mapstructure:"regexp"`
13+
14+
// Names configures the services to monitor by listing the service name.
15+
// Either Regexp or Names must be configured, not both.
16+
Names []string `mapstructure:"names"`
17+
}
18+
19+
// Copy returns a deep copy of IntentionsServicesConfig
20+
func (c *IntentionsServicesConfig) Copy() *IntentionsServicesConfig {
21+
if c == nil {
22+
return nil
23+
}
24+
25+
var o IntentionsServicesConfig
26+
o.Regexp = StringCopy(c.Regexp)
27+
28+
if c.Names != nil {
29+
o.Names = make([]string, 0, len(c.Names))
30+
o.Names = append(o.Names, c.Names...)
31+
}
32+
return &o
33+
}
34+
35+
// Merge combines all values in this IntentionsServicesConfig with the values in the other
36+
// configuration, with values in the other configuration taking precedence.
37+
// Maps and slices are merged, most other values are overwritten. Complex
38+
// structs define their own merge functionality.
39+
func (c *IntentionsServicesConfig) Merge(o *IntentionsServicesConfig) *IntentionsServicesConfig {
40+
if c == nil {
41+
if o == nil {
42+
return nil
43+
}
44+
return o.Copy()
45+
}
46+
47+
if o == nil {
48+
return c.Copy()
49+
}
50+
51+
r := c.Copy()
52+
53+
if o.Regexp != nil {
54+
r.Regexp = StringCopy(o.Regexp)
55+
}
56+
57+
r.Names = mergeSlices(c.Names, o.Names)
58+
59+
return r
60+
}
61+
62+
// Finalize ensures there no nil pointers.
63+
// Exception: `Regexp` is never finalized and can potentially be nil.
64+
func (c *IntentionsServicesConfig) Finalize() {
65+
if c == nil { // config not required, return early
66+
return
67+
}
68+
69+
if c.Names == nil {
70+
c.Names = []string{}
71+
}
72+
}
73+
74+
func (c *IntentionsServicesConfig) Validate() error {
75+
if c == nil { // config not required, return early
76+
return nil
77+
}
78+
79+
// Check that either regex or names is configured but not both
80+
namesConfigured := c.Names != nil && len(c.Names) > 0
81+
regexConfigured := c.Regexp != nil
82+
83+
if namesConfigured && regexConfigured {
84+
return fmt.Errorf("regexp and names fields cannot both be " +
85+
"configured. If both are needed, consider including the list of " +
86+
"names as part of the regex or creating separate tasks")
87+
}
88+
if !namesConfigured && !regexConfigured {
89+
return fmt.Errorf("either the regexp or names field must be configured")
90+
}
91+
92+
// Validate regex
93+
if regexConfigured {
94+
if _, err := regexp.Compile(StringVal(c.Regexp)); err != nil {
95+
return fmt.Errorf("unable to compile intentions service regexp: %s", err)
96+
}
97+
}
98+
99+
// Check that names does not contain empty strings
100+
if namesConfigured {
101+
for _, name := range c.Names {
102+
if name == "" {
103+
return fmt.Errorf("names field includes empty string(s). " +
104+
"intentions service names cannot be empty")
105+
}
106+
}
107+
}
108+
109+
return nil
110+
}
111+
112+
func (c *IntentionsServicesConfig) GoString() string {
113+
if c == nil {
114+
return ": (*IntentionsServicesConfig)(nil)"
115+
}
116+
117+
if len(c.Names) > 0 {
118+
return fmt.Sprintf(
119+
"Names:%s",
120+
c.Names,
121+
)
122+
123+
} else {
124+
return fmt.Sprintf(
125+
"Regexp:%s",
126+
StringVal(c.Regexp),
127+
)
128+
129+
}
130+
}

config/module_input.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ func moduleInputToTypeFunc() mapstructure.DecodeHookFunc {
6464
return decodeModuleInputToType(c, &config)
6565
}
6666

67+
if c, ok := moduleInputs[intentionsType]; ok {
68+
var config IntentionsModuleInputConfig
69+
return decodeModuleInputToType(c, &config)
70+
}
71+
6772
return nil, fmt.Errorf("unsupported module_input type: %v", data)
6873
}
6974
}

config/module_input_intentions.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
var _ ModuleInputConfig = (*IntentionsModuleInputConfig)(nil)
8+
9+
// IntentionsModuleInputConfig configures a module_input configuration block of
10+
// type 'intentions'. Data about the intentions monitored will be used as input
11+
// for the module variables.
12+
type IntentionsModuleInputConfig struct {
13+
IntentionsMonitorConfig `mapstructure:",squash"`
14+
}
15+
16+
// Copy returns a deep copy of this configuration.
17+
func (c *IntentionsModuleInputConfig) Copy() MonitorConfig {
18+
if c == nil {
19+
return nil
20+
}
21+
22+
conf, ok := c.IntentionsMonitorConfig.Copy().(*IntentionsMonitorConfig)
23+
if !ok {
24+
return nil
25+
}
26+
return &IntentionsModuleInputConfig{
27+
IntentionsMonitorConfig: *conf,
28+
}
29+
}
30+
31+
// Merge combines all values in this configuration `c` with the values in the other
32+
// configuration `o`, with values in the other configuration taking precedence.
33+
// Maps and slices are merged, most other values are overwritten. Complex
34+
// structs define their own merge functionality.
35+
func (c *IntentionsModuleInputConfig) Merge(o MonitorConfig) MonitorConfig {
36+
if c == nil {
37+
if isModuleInputNil(o) { // o is interface, use isConditionNil()
38+
return nil
39+
}
40+
return o.Copy()
41+
}
42+
43+
if isModuleInputNil(o) {
44+
return c.Copy()
45+
}
46+
47+
o2, ok := o.(*IntentionsModuleInputConfig)
48+
if !ok {
49+
return nil
50+
}
51+
52+
merged, ok := c.IntentionsMonitorConfig.Merge(&o2.IntentionsMonitorConfig).(*IntentionsMonitorConfig)
53+
if !ok {
54+
return nil
55+
}
56+
57+
return &IntentionsModuleInputConfig{
58+
IntentionsMonitorConfig: *merged,
59+
}
60+
}
61+
62+
// Finalize ensures there are no nil pointers.
63+
func (c *IntentionsModuleInputConfig) Finalize() {
64+
if c == nil { // config not required, return early
65+
return
66+
}
67+
c.IntentionsMonitorConfig.Finalize()
68+
}
69+
70+
// Validate validates the values and required options. This method is recommended
71+
// to run after Finalize() to ensure the configuration is safe to proceed.
72+
func (c *IntentionsModuleInputConfig) Validate() error {
73+
if c == nil { // config not required, return early
74+
return nil
75+
}
76+
if err := c.IntentionsMonitorConfig.Validate(); err != nil {
77+
return fmt.Errorf("error validating `module_input \"intentions\"`: %s", err)
78+
}
79+
return nil
80+
}
81+
82+
// GoString defines the printable version of this struct.
83+
func (c *IntentionsModuleInputConfig) GoString() string {
84+
if c == nil {
85+
return "(*IntentionsModuleInputConfig)(nil)"
86+
}
87+
88+
return fmt.Sprintf("&IntentionsModuleInputConfig{"+
89+
"%s"+
90+
"}",
91+
c.IntentionsMonitorConfig.GoString(),
92+
)
93+
}

0 commit comments

Comments
 (0)