Skip to content

Commit d5a8a89

Browse files
authored
Merge pull request #1302 from crazy-max/webhookurl-file
notif: support webhook url as secret
2 parents 503460f + 2fde654 commit d5a8a89

File tree

9 files changed

+46
-17
lines changed

9 files changed

+46
-17
lines changed

docs/notif/discord.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ Allow sending notifications to your Discord channel.
2323

2424
| Name | Default | Description |
2525
|--------------------|------------------------------------|-----------------------------------------------------------------------------------------------------------|
26-
| `webhookURL`[^1] | | Discord [incoming webhook URL](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks) |
26+
| `webhookURL` | | Discord [incoming webhook URL](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks) |
27+
| `webhookURLFile` | | Use content of secret file as webhook URL if `webhookURL` is not defined |
2728
| `mentions` | | List of users or roles to notify |
2829
| `renderFields` | `true` | Render [field objects](https://discordjs.guide/popular-topics/embeds.html) |
2930
| `timeout` | `10s` | Timeout specifies a time limit for the request to be made |
3031
| `templateBody`[^1] | See [below](#default-templatebody) | [Notification template](../faq.md#notification-template) for message body |
3132

3233
!!! abstract "Environment variables"
3334
* `DIUN_NOTIF_DISCORD_WEBHOOKURL`
35+
* `DIUN_NOTIF_DISCORD_WEBHOOKURLFILE`
3436
* `DIUN_NOTIF_DISCORD_MENTIONS` (comma separated)
3537
* `DIUN_NOTIF_DISCORD_RENDERFIELDS`
3638
* `DIUN_NOTIF_DISCORD_TIMEOUT`

docs/notif/slack.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ You can send notifications to your Slack channel using an [incoming webhook URL]
1919

2020
| Name | Default | Description |
2121
|--------------------|------------------------------------|-------------------------------------------------------------------------------------------|
22-
| `webhookURL`[^1] | | Slack [incoming webhook URL](https://api.slack.com/messaging/webhooks) |
22+
| `webhookURL` | | Slack [incoming webhook URL](https://api.slack.com/messaging/webhooks) |
23+
| `webhookURLFile` | | Use content of secret file as webhook URL if `webhookURL` is not defined |
2324
| `renderFields` | `true` | Render [field objects](https://api.slack.com/messaging/composing/layouts#stack_of_blocks) |
2425
| `templateBody`[^1] | See [below](#default-templatebody) | [Notification template](../faq.md#notification-template) for message body |
2526

2627
!!! abstract "Environment variables"
2728
* `DIUN_NOTIF_SLACK_WEBHOOKURL`
29+
* `DIUN_NOTIF_SLACK_WEBHOOKURLFILE`
2830
* `DIUN_NOTIF_SLACK_RENDERFIELDS`
2931
* `DIUN_NOTIF_SLACK_TEMPLATEBODY`
3032

docs/notif/teams.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ You can send notifications to your Teams team-channel using an [incoming webhook
1616

1717
| Name | Default | Description |
1818
|--------------------|------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------|
19-
| `webhookURL`[^1] | | Teams [incoming webhook URL](https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/what-are-webhooks-and-connectors) |
19+
| `webhookURL` | | Teams [incoming webhook URL](https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/what-are-webhooks-and-connectors) |
20+
| `webhookURLFile` | | Use content of secret file as webhook URL if `webhookURL` is not defined |
2021
| `renderFacts` | `true` | Render fact objects |
2122
| `templateBody`[^1] | See [below](#default-templatebody) | [Notification template](../faq.md#notification-template) for message body |
2223

2324
!!! abstract "Environment variables"
2425
* `DIUN_NOTIF_TEAMS_WEBHOOKURL`
26+
* `DIUN_NOTIF_TEAMS_WEBHOOKURLFILE`
2527
* `DIUN_NOTIF_TEAMS_RENDERFACTS`
2628
* `DIUN_NOTIF_TEAMS_TEMPLATEBODY`
2729

internal/model/notif_discord.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import (
88

99
// NotifDiscord holds Discord notification configuration details
1010
type NotifDiscord struct {
11-
WebhookURL string `yaml:"webhookURL,omitempty" json:"webhookURL,omitempty" validate:"required"`
12-
Mentions []string `yaml:"mentions,omitempty" json:"mentions,omitempty"`
13-
RenderFields *bool `yaml:"renderFields,omitempty" json:"renderFields,omitempty" validate:"required"`
14-
Timeout *time.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty" validate:"required"`
15-
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
11+
WebhookURL string `yaml:"webhookURL,omitempty" json:"webhookURL,omitempty" validate:"omitempty"`
12+
WebhookURLFile string `yaml:"webhookURLFile,omitempty" json:"webhookURLFile,omitempty" validate:"omitempty,file"`
13+
Mentions []string `yaml:"mentions,omitempty" json:"mentions,omitempty"`
14+
RenderFields *bool `yaml:"renderFields,omitempty" json:"renderFields,omitempty" validate:"required"`
15+
Timeout *time.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty" validate:"required"`
16+
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
1617
}
1718

1819
// GetDefaults gets the default values

internal/model/notif_slack.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ const NotifSlackDefaultTemplateBody = "<!channel> Docker tag {{ if .Entry.Image.
77

88
// NotifSlack holds slack notification configuration details
99
type NotifSlack struct {
10-
WebhookURL string `yaml:"webhookURL,omitempty" json:"webhookURL,omitempty" validate:"required"`
11-
RenderFields *bool `yaml:"renderFields,omitempty" json:"renderFields,omitempty" validate:"required"`
12-
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
10+
WebhookURL string `yaml:"webhookURL,omitempty" json:"webhookURL,omitempty" validate:"omitempty"`
11+
WebhookURLFile string `yaml:"webhookURLFile,omitempty" json:"webhookURLFile,omitempty" validate:"omitempty,file"`
12+
RenderFields *bool `yaml:"renderFields,omitempty" json:"renderFields,omitempty" validate:"required"`
13+
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
1314
}
1415

1516
// GetDefaults gets the default values

internal/model/notif_teams.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ const NotifTeamsDefaultTemplateBody = "Docker tag {{ if .Entry.Image.HubLink }}[
77

88
// NotifTeams holds Teams notification configuration details
99
type NotifTeams struct {
10-
WebhookURL string `yaml:"webhookURL,omitempty" json:"webhookURL,omitempty" validate:"required"`
11-
RenderFacts *bool `yaml:"renderFacts,omitempty" json:"renderFacts,omitempty" validate:"required"`
12-
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
10+
WebhookURL string `yaml:"webhookURL,omitempty" json:"webhookURL,omitempty" validate:"omitempty"`
11+
WebhookURLFile string `yaml:"webhookURLFile,omitempty" json:"webhookURLFile,omitempty" validate:"omitempty,file"`
12+
RenderFacts *bool `yaml:"renderFacts,omitempty" json:"renderFacts,omitempty" validate:"required"`
13+
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
1314
}
1415

1516
// GetDefaults gets the default values

internal/notif/discord/client.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/crazy-max/diun/v4/internal/model"
1313
"github.com/crazy-max/diun/v4/internal/msg"
1414
"github.com/crazy-max/diun/v4/internal/notif/notifier"
15+
"github.com/crazy-max/diun/v4/pkg/utl"
1516
"github.com/pkg/errors"
1617
)
1718

@@ -42,6 +43,11 @@ func (c *Client) Name() string {
4243
func (c *Client) Send(entry model.NotifEntry) error {
4344
var content bytes.Buffer
4445

46+
webhookURL, err := utl.GetSecret(c.cfg.WebhookURL, c.cfg.WebhookURLFile)
47+
if err != nil {
48+
return errors.Wrap(err, "cannot retrieve webhook URL for Discord notifier")
49+
}
50+
4551
message, err := msg.New(msg.Options{
4652
Meta: c.meta,
4753
Entry: entry,
@@ -117,7 +123,7 @@ func (c *Client) Send(entry model.NotifEntry) error {
117123
return err
118124
}
119125

120-
u, err := url.Parse(c.cfg.WebhookURL)
126+
u, err := url.Parse(webhookURL)
121127
if err != nil {
122128
return err
123129
}

internal/notif/slack/client.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"github.com/crazy-max/diun/v4/internal/model"
1010
"github.com/crazy-max/diun/v4/internal/msg"
1111
"github.com/crazy-max/diun/v4/internal/notif/notifier"
12+
"github.com/crazy-max/diun/v4/pkg/utl"
1213
"github.com/nlopes/slack"
14+
"github.com/pkg/errors"
1315
)
1416

1517
// Client represents an active slack notification object
@@ -36,6 +38,11 @@ func (c *Client) Name() string {
3638

3739
// Send creates and sends a slack notification with an entry
3840
func (c *Client) Send(entry model.NotifEntry) error {
41+
webhookURL, err := utl.GetSecret(c.cfg.WebhookURL, c.cfg.WebhookURLFile)
42+
if err != nil {
43+
return errors.Wrap(err, "cannot retrieve webhook URL for Slack notifier")
44+
}
45+
3946
message, err := msg.New(msg.Options{
4047
Meta: c.meta,
4148
Entry: entry,
@@ -93,7 +100,7 @@ func (c *Client) Send(entry model.NotifEntry) error {
93100
color = "#0054ca"
94101
}
95102

96-
return slack.PostWebhook(c.cfg.WebhookURL, &slack.WebhookMessage{
103+
return slack.PostWebhook(webhookURL, &slack.WebhookMessage{
97104
Attachments: []slack.Attachment{
98105
{
99106
Color: color,

internal/notif/teams/client.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/crazy-max/diun/v4/internal/model"
1212
"github.com/crazy-max/diun/v4/internal/msg"
1313
"github.com/crazy-max/diun/v4/internal/notif/notifier"
14+
"github.com/crazy-max/diun/v4/pkg/utl"
15+
"github.com/pkg/errors"
1416
)
1517

1618
// Client represents an active webhook notification object
@@ -50,6 +52,11 @@ type Fact struct {
5052

5153
// Send creates and sends a webhook notification with an entry
5254
func (c *Client) Send(entry model.NotifEntry) error {
55+
webhookURL, err := utl.GetSecret(c.cfg.WebhookURL, c.cfg.WebhookURLFile)
56+
if err != nil {
57+
return errors.Wrap(err, "cannot retrieve webhook URL for Teams notifier")
58+
}
59+
5360
message, err := msg.New(msg.Options{
5461
Meta: c.meta,
5562
Entry: entry,
@@ -107,7 +114,7 @@ func (c *Client) Send(entry model.NotifEntry) error {
107114
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(10)*time.Second)
108115
defer cancel()
109116

110-
req, err := http.NewRequestWithContext(ctx, "POST", c.cfg.WebhookURL, bytes.NewBuffer(jsonBody))
117+
req, err := http.NewRequestWithContext(ctx, "POST", webhookURL, bytes.NewBuffer(jsonBody))
111118
if err != nil {
112119
return err
113120
}

0 commit comments

Comments
 (0)