Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit c699b45

Browse files
authored
cmd/bosun: define rule vars in sysconfig
1 parent a76718c commit c699b45

File tree

13 files changed

+60
-23
lines changed

13 files changed

+60
-23
lines changed

cmd/bosun/conf/conf.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type SystemConfProvider interface {
3434
GetTLSCertFile() string
3535
GetTLSKeyFile() string
3636

37+
GetRuleVars() map[string]string
38+
3739
GetSMTPHost() string
3840
GetSMTPUsername() string // SMTP username
3941
GetSMTPPassword() string // SMTP password

cmd/bosun/conf/rule/modify.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
// will not be saved. If the savehook fails to run or returns an error thaen the orginal config
1616
// will be restored and the reload will not take place.
1717
func (c *Conf) SaveRawText(rawConfig, diff, user, message string, args ...string) error {
18-
newConf, err := NewConf(c.Name, c.backends, rawConfig)
18+
newConf, err := NewConf(c.Name, c.backends, c.sysVars, rawConfig)
1919
if err != nil {
2020
return err
2121
}
@@ -101,7 +101,7 @@ func (c *Conf) BulkEdit(edits conf.BulkEditRequest) error {
101101
} else {
102102
rawConf = writeSection(l, newConf.RawText, edit.Text)
103103
}
104-
newConf, err = NewConf(c.Name, c.backends, rawConf)
104+
newConf, err = NewConf(c.Name, c.backends, c.sysVars, rawConf)
105105
if err != nil {
106106
return fmt.Errorf("could not create new conf: failed on step %v:%v : %v", edit.Type, edit.Name, err)
107107
}

cmd/bosun/conf/rule/rule.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ type Conf struct {
4545
reload func() error
4646
backends conf.EnabledBackends
4747

48+
sysVars map[string]string
49+
4850
tree *parse.Tree
4951
node parse.Node
5052
unknownTemplate string
@@ -125,19 +127,19 @@ func (c *Conf) parseNotifications(v string) (map[string]*conf.Notification, erro
125127
return ns, nil
126128
}
127129

128-
func ParseFile(fname string, backends conf.EnabledBackends) (*Conf, error) {
130+
func ParseFile(fname string, backends conf.EnabledBackends, sysVars map[string]string) (*Conf, error) {
129131
f, err := ioutil.ReadFile(fname)
130132
if err != nil {
131133
return nil, err
132134
}
133-
return NewConf(fname, backends, string(f))
135+
return NewConf(fname, backends, sysVars, string(f))
134136
}
135137

136138
func (c *Conf) SaveConf(newConf *Conf) error {
137139
return ioutil.WriteFile(c.Name, []byte(newConf.RawText), os.FileMode(int(0640)))
138140
}
139141

140-
func NewConf(name string, backends conf.EnabledBackends, text string) (c *Conf, err error) {
142+
func NewConf(name string, backends conf.EnabledBackends, sysVars map[string]string, text string) (c *Conf, err error) {
141143
defer errRecover(&err)
142144
c = &Conf{
143145
Name: name,
@@ -153,6 +155,7 @@ func NewConf(name string, backends conf.EnabledBackends, text string) (c *Conf,
153155
writeLock: make(chan bool, 1),
154156
deferredSections: make(map[string][]deferredSection),
155157
backends: backends,
158+
sysVars: sysVars,
156159
}
157160
c.tree, err = parse.Parse(name, text)
158161
if err != nil {
@@ -776,6 +779,8 @@ func (c *Conf) Expand(v string, vars map[string]string, ignoreBadExpand bool) st
776779
n = _n
777780
} else if strings.HasPrefix(s, "$env.") {
778781
n = os.Getenv(s[5:])
782+
} else if strings.HasPrefix(s, "$sys.") {
783+
n = c.sysVars[s[5:]]
779784
} else if ignoreBadExpand {
780785
return s
781786
} else {

cmd/bosun/conf/rule/rule_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestPrint(t *testing.T) {
1818
if err := os.Setenv("env", "1"); err != nil {
1919
t.Fatal(err)
2020
}
21-
c, err := NewConf(fname, conf.EnabledBackends{OpenTSDB: true}, string(b))
21+
c, err := NewConf(fname, conf.EnabledBackends{OpenTSDB: true}, nil, string(b))
2222
if err != nil {
2323
t.Fatal(err)
2424
}
@@ -79,7 +79,7 @@ func TestInvalid(t *testing.T) {
7979
if err != nil {
8080
t.Fatal(err)
8181
}
82-
_, err = NewConf(fname, conf.EnabledBackends{OpenTSDB: true}, string(b))
82+
_, err = NewConf(fname, conf.EnabledBackends{OpenTSDB: true}, nil, string(b))
8383
if err == nil {
8484
t.Error("expected error in", path)
8585
continue

cmd/bosun/conf/system.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ type SystemConf struct {
3939

4040
SMTPConf SMTPConf
4141

42+
RuleVars map[string]string
43+
4244
OpenTSDBConf OpenTSDBConf
4345
GraphiteConf GraphiteConf
4446
InfluxConf InfluxConf
@@ -333,6 +335,12 @@ func (sc *SystemConf) GetAuthConf() *AuthConf {
333335
return sc.AuthConf
334336
}
335337

338+
// GetRuleVars user defined variables that will be available to the rule configuration
339+
// under "$sys.". This is so values with secrets can be defined in the system configuration
340+
func (sc *SystemConf) GetRuleVars() map[string]string {
341+
return sc.RuleVars
342+
}
343+
336344
// GetTimeAndDate returns the http://www.timeanddate.com/ that should be available to the UI
337345
// so it can show links to translate UTC times to various timezones. This feature is only
338346
// for creating UI Links as Bosun is expected to be running on a machine that is set to UTC

cmd/bosun/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func main() {
114114
if err != nil {
115115
slog.Fatal(err)
116116
}
117-
ruleConf, err := rule.ParseFile(sysProvider.GetRuleFilePath(), systemConf.EnabledBackends())
117+
ruleConf, err := rule.ParseFile(sysProvider.GetRuleFilePath(), systemConf.EnabledBackends(), systemConf.GetRuleVars())
118118
if err != nil {
119119
slog.Fatalf("couldn't read rules: %v", err)
120120
}
@@ -219,7 +219,7 @@ func main() {
219219
defer func() {
220220
<-reloading
221221
}()
222-
newConf, err := rule.ParseFile(sysProvider.GetRuleFilePath(), sysProvider.EnabledBackends())
222+
newConf, err := rule.ParseFile(sysProvider.GetRuleFilePath(), sysProvider.EnabledBackends(), sysProvider.GetRuleVars())
223223
if err != nil {
224224
return err
225225
}

cmd/bosun/sched/check_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
func TestCheckFlapping(t *testing.T) {
1919
defer setup()()
20-
c, err := rule.NewConf("", conf.EnabledBackends{}, `
20+
c, err := rule.NewConf("", conf.EnabledBackends{}, nil, `
2121
template t {
2222
subject = 1
2323
}
@@ -106,7 +106,7 @@ func TestCheckSilence(t *testing.T) {
106106
if err != nil {
107107
t.Fatal(err)
108108
}
109-
c, err := rule.NewConf("", conf.EnabledBackends{}, fmt.Sprintf(`
109+
c, err := rule.NewConf("", conf.EnabledBackends{}, nil, fmt.Sprintf(`
110110
template t {
111111
subject = "test"
112112
body = "test"
@@ -143,7 +143,7 @@ func TestCheckSilence(t *testing.T) {
143143

144144
func TestIncidentIds(t *testing.T) {
145145
defer setup()()
146-
c, err := rule.NewConf("", conf.EnabledBackends{}, `
146+
c, err := rule.NewConf("", conf.EnabledBackends{}, nil, `
147147
alert a {
148148
crit = 1
149149
}
@@ -201,7 +201,7 @@ func TestCheckNotify(t *testing.T) {
201201
if err != nil {
202202
t.Fatal(err)
203203
}
204-
c, err := rule.NewConf("", conf.EnabledBackends{}, fmt.Sprintf(`
204+
c, err := rule.NewConf("", conf.EnabledBackends{}, nil, fmt.Sprintf(`
205205
template t {
206206
subject = {{.Last.Status}}
207207
}
@@ -245,7 +245,7 @@ func TestCheckNotifyUnknown(t *testing.T) {
245245
if err != nil {
246246
t.Fatal(err)
247247
}
248-
c, err := rule.NewConf("", conf.EnabledBackends{}, fmt.Sprintf(`
248+
c, err := rule.NewConf("", conf.EnabledBackends{}, nil, fmt.Sprintf(`
249249
template t {
250250
subject = {{.Name}}: {{.Group | len}} unknown alerts
251251
}
@@ -308,7 +308,7 @@ func TestCheckNotifyUnknownDefault(t *testing.T) {
308308
if err != nil {
309309
t.Fatal(err)
310310
}
311-
c, err := rule.NewConf("", conf.EnabledBackends{}, fmt.Sprintf(`
311+
c, err := rule.NewConf("", conf.EnabledBackends{}, nil, fmt.Sprintf(`
312312
template t {
313313
subject = template
314314
}
@@ -369,7 +369,7 @@ func TestCheckNotifyLog(t *testing.T) {
369369
if err != nil {
370370
t.Fatal(err)
371371
}
372-
c, err := rule.NewConf("", conf.EnabledBackends{}, fmt.Sprintf(`
372+
c, err := rule.NewConf("", conf.EnabledBackends{}, nil, fmt.Sprintf(`
373373
template t {
374374
subject = {{.Alert.Name}}
375375
}

cmd/bosun/sched/notification_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func TestActionNotificationTemplates(t *testing.T) {
14-
c, err := rule.NewConf("", conf.EnabledBackends{}, ``)
14+
c, err := rule.NewConf("", conf.EnabledBackends{}, nil, ``)
1515
if err != nil {
1616
t.Fatal(err)
1717
}
@@ -53,7 +53,7 @@ func TestActionNotificationTemplates(t *testing.T) {
5353

5454
func TestActionNotificationGrouping(t *testing.T) {
5555
defer setup()()
56-
c, err := rule.NewConf("", conf.EnabledBackends{}, `
56+
c, err := rule.NewConf("", conf.EnabledBackends{}, nil, `
5757
template t{
5858
subject = 2
5959
}

cmd/bosun/sched/sched_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func testSched(t *testing.T, st *schedTest) (s *Schedule) {
9898
t.Fatal(err)
9999
}
100100
//confs := "tsdbHost = " + u.Host + "\n" + st.conf
101-
c, err := rule.NewConf("testconf", conf.EnabledBackends{OpenTSDB: true}, st.conf)
101+
c, err := rule.NewConf("testconf", conf.EnabledBackends{OpenTSDB: true}, nil, st.conf)
102102
if err != nil {
103103
t.Error(err)
104104
t.Logf("conf:\n%s", st.conf)

cmd/bosun/web/expr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ func buildConfig(r *http.Request) (c conf.RuleConfProvider, a *conf.Alert, hash
448448
if err != nil {
449449
return nil, nil, "", err
450450
}
451-
c, err = rule.NewConf("Test Config", schedule.SystemConf.EnabledBackends(), string(config))
451+
c, err = rule.NewConf("Test Config", schedule.SystemConf.EnabledBackends(), schedule.SystemConf.GetRuleVars(), string(config))
452452
if err != nil {
453453
return nil, nil, "", err
454454
}

0 commit comments

Comments
 (0)