Skip to content

Commit 5e158d2

Browse files
Updated to use non-pointer version
Signed-off-by: Anand Rajagopal <anrajag@amazon.com>
1 parent 5babfa7 commit 5e158d2

15 files changed

+1531
-147
lines changed

cmd/alertmanager/main.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import (
1717
"context"
1818
"errors"
1919
"fmt"
20-
"github.com/prometheus/alertmanager/secrets"
21-
"github.com/prometheus/alertmanager/secrets/providers"
2220
"log/slog"
2321
"net"
2422
"net/http"
@@ -32,6 +30,9 @@ import (
3230
"syscall"
3331
"time"
3432

33+
"github.com/prometheus/alertmanager/secrets"
34+
"github.com/prometheus/alertmanager/secrets/providers"
35+
3536
"github.com/KimMachineGun/automemlimit/memlimit"
3637
"github.com/alecthomas/kingpin/v2"
3738
"github.com/prometheus/client_golang/prometheus"
@@ -160,10 +161,10 @@ func run() int {
160161
httpTimeout = kingpin.Flag("web.timeout", "Timeout for HTTP requests. If negative or zero, no timeout is set.").Default("0").Duration()
161162

162163
memlimitRatio = kingpin.Flag("auto-gomemlimit.ratio", "The ratio of reserved GOMEMLIMIT memory to the detected maximum container or system memory. The value must be greater than 0 and less than or equal to 1.").
163-
Default("0.9").Float64()
164+
Default("0.9").Float64()
164165

165166
clusterBindAddr = kingpin.Flag("cluster.listen-address", "Listen address for cluster. Set to empty string to disable HA mode.").
166-
Default(defaultClusterAddr).String()
167+
Default(defaultClusterAddr).String()
167168
clusterAdvertiseAddr = kingpin.Flag("cluster.advertise-address", "Explicit address to advertise in cluster.").String()
168169
peers = kingpin.Flag("cluster.peer", "Initial peers (may be repeated).").Strings()
169170
peerTimeout = kingpin.Flag("cluster.peer-timeout", "Time to wait between peers to send notifications.").Default("15s").Duration()
@@ -435,8 +436,10 @@ func run() int {
435436
})
436437

437438
spRegistry := secrets.NewSecretsProviderRegistry(logger, prometheus.NewRegistry())
438-
// currently only one secrets providers is supported
439-
spRegistry.Register(providers.AWSSecretsManagerSecretProviderDiscoveryConfig{})
439+
// currently only one secrets provider is registered. Inline secrets provider is always available
440+
if spRegistry.Register(providers.AWSSecretsManagerSecretProviderDiscoveryConfig{}) != nil {
441+
configLogger.Error("failed to register secrets provider", "err", err)
442+
}
440443
spRegistry.Init()
441444
// Build the map of receiver to integrations.
442445
receivers := make(map[string][]notify.Integration, len(activeReceivers))

config/config_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"testing"
2424
"time"
2525

26+
"github.com/prometheus/alertmanager/secrets"
27+
2628
commoncfg "github.com/prometheus/common/config"
2729
"github.com/prometheus/common/model"
2830
"github.com/prometheus/common/promslog"
@@ -528,6 +530,8 @@ func TestHideConfigSecrets(t *testing.T) {
528530
func TestShowMarshalSecretValues(t *testing.T) {
529531
MarshalSecretValue = true
530532
defer func() { MarshalSecretValue = false }()
533+
secrets.MarshalSecretValue = true
534+
defer func() { secrets.MarshalSecretValue = false }()
531535

532536
c, err := LoadFile("testdata/conf.good.yml")
533537
if err != nil {

config/notifiers.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ package config
1616
import (
1717
"errors"
1818
"fmt"
19-
"github.com/prometheus/alertmanager/secrets"
2019
"net/textproto"
2120
"regexp"
2221
"strings"
2322
"text/template"
2423
"time"
2524

25+
"github.com/prometheus/alertmanager/secrets"
26+
2627
commoncfg "github.com/prometheus/common/config"
2728
"github.com/prometheus/common/model"
2829
"github.com/prometheus/sigv4"
@@ -329,22 +330,22 @@ type PagerdutyConfig struct {
329330

330331
HTTPConfig *commoncfg.HTTPClientConfig `yaml:"http_config,omitempty" json:"http_config,omitempty"`
331332

332-
ServiceKey *secrets.GenericSecret `yaml:"service_key,omitempty" json:"service_key,omitempty"`
333-
ServiceKeyFile string `yaml:"service_key_file,omitempty" json:"service_key_file,omitempty"`
334-
RoutingKey *secrets.GenericSecret `yaml:"routing_key,omitempty" json:"routing_key,omitempty"`
335-
RoutingKeyFile string `yaml:"routing_key_file,omitempty" json:"routing_key_file,omitempty"`
336-
URL *URL `yaml:"url,omitempty" json:"url,omitempty"`
337-
Client string `yaml:"client,omitempty" json:"client,omitempty"`
338-
ClientURL string `yaml:"client_url,omitempty" json:"client_url,omitempty"`
339-
Description string `yaml:"description,omitempty" json:"description,omitempty"`
340-
Details map[string]string `yaml:"details,omitempty" json:"details,omitempty"`
341-
Images []PagerdutyImage `yaml:"images,omitempty" json:"images,omitempty"`
342-
Links []PagerdutyLink `yaml:"links,omitempty" json:"links,omitempty"`
343-
Source string `yaml:"source,omitempty" json:"source,omitempty"`
344-
Severity string `yaml:"severity,omitempty" json:"severity,omitempty"`
345-
Class string `yaml:"class,omitempty" json:"class,omitempty"`
346-
Component string `yaml:"component,omitempty" json:"component,omitempty"`
347-
Group string `yaml:"group,omitempty" json:"group,omitempty"`
333+
ServiceKey secrets.GenericSecret `yaml:"service_key,omitempty" json:"service_key,omitempty"`
334+
ServiceKeyFile string `yaml:"service_key_file,omitempty" json:"service_key_file,omitempty"`
335+
RoutingKey secrets.GenericSecret `yaml:"routing_key,omitempty" json:"routing_key,omitempty"`
336+
RoutingKeyFile string `yaml:"routing_key_file,omitempty" json:"routing_key_file,omitempty"`
337+
URL *URL `yaml:"url,omitempty" json:"url,omitempty"`
338+
Client string `yaml:"client,omitempty" json:"client,omitempty"`
339+
ClientURL string `yaml:"client_url,omitempty" json:"client_url,omitempty"`
340+
Description string `yaml:"description,omitempty" json:"description,omitempty"`
341+
Details map[string]string `yaml:"details,omitempty" json:"details,omitempty"`
342+
Images []PagerdutyImage `yaml:"images,omitempty" json:"images,omitempty"`
343+
Links []PagerdutyLink `yaml:"links,omitempty" json:"links,omitempty"`
344+
Source string `yaml:"source,omitempty" json:"source,omitempty"`
345+
Severity string `yaml:"severity,omitempty" json:"severity,omitempty"`
346+
Class string `yaml:"class,omitempty" json:"class,omitempty"`
347+
Component string `yaml:"component,omitempty" json:"component,omitempty"`
348+
Group string `yaml:"group,omitempty" json:"group,omitempty"`
348349
}
349350

350351
// PagerdutyLink is a link.
@@ -360,20 +361,24 @@ type PagerdutyImage struct {
360361
Href string `yaml:"href,omitempty" json:"href,omitempty"`
361362
}
362363

364+
func (c *PagerdutyConfig) isKeyZero() bool {
365+
return c.ServiceKey.IsZero() && c.RoutingKey.IsZero()
366+
}
367+
363368
// UnmarshalYAML implements the yaml.Unmarshaler interface.
364369
func (c *PagerdutyConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
365370
*c = DefaultPagerdutyConfig
366371
type plain PagerdutyConfig
367372
if err := unmarshal((*plain)(c)); err != nil {
368373
return err
369374
}
370-
if c.RoutingKey == nil && c.ServiceKey == nil && c.RoutingKeyFile == "" && c.ServiceKeyFile == "" {
375+
if c.isKeyZero() && c.RoutingKeyFile == "" && c.ServiceKeyFile == "" {
371376
return errors.New("missing service or routing key in PagerDuty config")
372377
}
373-
if c.RoutingKey != nil && len(c.RoutingKeyFile) > 0 {
378+
if !c.RoutingKey.IsZero() && len(c.RoutingKeyFile) > 0 {
374379
return errors.New("at most one of routing_key & routing_key_file must be configured")
375380
}
376-
if c.ServiceKey != nil && len(c.ServiceKeyFile) > 0 {
381+
if !c.ServiceKey.IsZero() && len(c.ServiceKeyFile) > 0 {
377382
return errors.New("at most one of service_key & service_key_file must be configured")
378383
}
379384
if c.Details == nil {

config/notifiers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ routing_key_file: 'xyz'
142142
func TestPagerdutyServiceKey(t *testing.T) {
143143
t.Run("error if no service key or key file", func(t *testing.T) {
144144
in := `
145-
service_key: ''
145+
service_key:
146146
`
147147
var cfg PagerdutyConfig
148148
err := yaml.UnmarshalStrict([]byte(in), &cfg)

config/receiver/receiver.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
package receiver
1515

1616
import (
17-
"github.com/prometheus/alertmanager/secrets"
1817
"log/slog"
1918

19+
"github.com/prometheus/alertmanager/secrets"
20+
2021
commoncfg "github.com/prometheus/common/config"
2122
"github.com/prometheus/common/promslog"
2223

config/receiver/receiver_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ package receiver
1616
import (
1717
"testing"
1818

19+
"github.com/prometheus/client_golang/prometheus"
20+
"github.com/prometheus/common/promslog"
21+
22+
"github.com/prometheus/alertmanager/secrets"
23+
1924
commoncfg "github.com/prometheus/common/config"
2025
"github.com/stretchr/testify/require"
2126

@@ -71,7 +76,8 @@ func TestBuildReceiverIntegrations(t *testing.T) {
7176
} {
7277
tc := tc
7378
t.Run("", func(t *testing.T) {
74-
integrations, err := BuildReceiverIntegrations(tc.receiver, nil, nil)
79+
sp := secrets.NewSecretsProviderRegistry(promslog.NewNopLogger(), prometheus.DefaultRegisterer)
80+
integrations, err := BuildReceiverIntegrations(tc.receiver, nil, nil, sp)
7581
if tc.err {
7682
require.Error(t, err)
7783
return

notify/pagerduty/pagerduty.go

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ import (
1919
"encoding/json"
2020
"errors"
2121
"fmt"
22-
"github.com/prometheus/alertmanager/secrets"
2322
"io"
2423
"log/slog"
2524
"net/http"
2625
"os"
2726
"strings"
2827

28+
"github.com/prometheus/alertmanager/secrets"
29+
2930
"github.com/alecthomas/units"
3031
commoncfg "github.com/prometheus/common/config"
3132
"github.com/prometheus/common/model"
@@ -62,14 +63,21 @@ func New(c *config.PagerdutyConfig, t *template.Template, l *slog.Logger, spRegi
6263
return nil, err
6364
}
6465
n := &Notifier{conf: c, tmpl: t, logger: l, client: client}
65-
if c.ServiceKey != nil || c.ServiceKeyFile != "" {
66+
67+
if !c.ServiceKey.IsZero() {
6668
n.secretsFetcher, err = spRegistry.RegisterSecret(c.ServiceKey)
69+
} else if !c.RoutingKey.IsZero() {
70+
n.secretsFetcher, err = spRegistry.RegisterSecret(c.RoutingKey)
71+
}
72+
if err != nil {
73+
l.Error("error registering secret", "err", err)
74+
}
75+
if !c.ServiceKey.IsZero() || c.ServiceKeyFile != "" {
6776
n.apiV1 = "https://events.pagerduty.com/generic/2010-04-15/create_event.json"
6877
// Retrying can solve the issue on 403 (rate limiting) and 5xx response codes.
6978
// https://v2.developer.pagerduty.com/docs/trigger-events
7079
n.retrier = &notify.Retrier{RetryCodes: []int{http.StatusForbidden}, CustomDetailsFunc: errDetails}
7180
} else {
72-
n.secretsFetcher, err = spRegistry.RegisterSecret(c.RoutingKey)
7381
// Retrying can solve the issue on 429 (rate limiting) and 5xx response codes.
7482
// https://v2.developer.pagerduty.com/docs/events-api-v2#api-response-codes--retry-logic
7583
n.retrier = &notify.Retrier{RetryCodes: []int{http.StatusTooManyRequests}, CustomDetailsFunc: errDetails}
@@ -148,19 +156,22 @@ func (n *Notifier) encodeMessage(msg *pagerDutyMessage) (bytes.Buffer, error) {
148156
}
149157

150158
func (n *Notifier) getSecret(ctx context.Context) string {
151-
var secret *secrets.GenericSecret
152-
if n.conf.ServiceKey != nil {
159+
var secret secrets.GenericSecret
160+
if !n.conf.ServiceKey.IsZero() {
153161
secret = n.conf.ServiceKey
154-
} else {
162+
} else if !n.conf.RoutingKey.IsZero() {
155163
secret = n.conf.RoutingKey
156164
}
165+
if secret.IsZero() || n.secretsFetcher == nil {
166+
return ""
167+
}
157168

158-
if sec, err := n.secretsFetcher.FetchSecret(ctx, secret); err != nil {
159-
n.logger.Error("unable to fetch secret", err)
169+
sec, err := n.secretsFetcher.FetchSecret(ctx, secret)
170+
if err != nil {
171+
n.logger.Error("unable to fetch secret", "error", err)
160172
return ""
161-
} else {
162-
return sec
163173
}
174+
return sec
164175
}
165176

166177
func (n *Notifier) notifyV1(
@@ -179,9 +190,8 @@ func (n *Notifier) notifyV1(
179190
n.logger.Warn("Truncated description", "key", key, "max_runes", maxV1DescriptionLenRunes)
180191
}
181192

182-
//serviceKey := string(n.conf.ServiceKey)
183193
serviceKey := n.getSecret(ctx)
184-
if serviceKey == "" {
194+
if serviceKey == "" && n.conf.ServiceKeyFile != "" {
185195
content, fileErr := os.ReadFile(n.conf.ServiceKeyFile)
186196
if fileErr != nil {
187197
return false, fmt.Errorf("failed to read service key from file: %w", fileErr)
@@ -220,6 +230,9 @@ func (n *Notifier) notifyV1(
220230
if err != nil {
221231
return true, fmt.Errorf("failed to post message to PagerDuty v1: %w", err)
222232
}
233+
if resp.StatusCode == 403 {
234+
n.secretsFetcher.RefreshCredentialsAsync()
235+
}
223236
defer notify.Drain(resp)
224237

225238
return n.retrier.Check(resp.StatusCode, resp.Body)
@@ -245,9 +258,8 @@ func (n *Notifier) notifyV2(
245258
n.logger.Warn("Truncated summary", "key", key, "max_runes", maxV2SummaryLenRunes)
246259
}
247260

248-
//routingKey := string(n.conf.RoutingKey)
249261
routingKey := n.getSecret(ctx)
250-
if routingKey == "" {
262+
if routingKey == "" && n.conf.RoutingKeyFile != "" {
251263
content, fileErr := os.ReadFile(n.conf.RoutingKeyFile)
252264
if fileErr != nil {
253265
return false, fmt.Errorf("failed to read routing key from file: %w", fileErr)
@@ -317,6 +329,9 @@ func (n *Notifier) notifyV2(
317329
}
318330
defer notify.Drain(resp)
319331

332+
if resp.StatusCode == 403 {
333+
n.secretsFetcher.RefreshCredentialsAsync()
334+
}
320335
retry, err := n.retrier.Check(resp.StatusCode, resp.Body)
321336
if err != nil {
322337
return retry, notify.NewErrorWithReason(notify.GetFailureReasonFromStatusCode(resp.StatusCode), err)

0 commit comments

Comments
 (0)