Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

notif: support webhook url as secret #1302

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/notif/discord.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ Allow sending notifications to your Discord channel.

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

!!! abstract "Environment variables"
* `DIUN_NOTIF_DISCORD_WEBHOOKURL`
* `DIUN_NOTIF_DISCORD_WEBHOOKURLFILE`
* `DIUN_NOTIF_DISCORD_MENTIONS` (comma separated)
* `DIUN_NOTIF_DISCORD_RENDERFIELDS`
* `DIUN_NOTIF_DISCORD_TIMEOUT`
Expand Down
4 changes: 3 additions & 1 deletion docs/notif/slack.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ You can send notifications to your Slack channel using an [incoming webhook URL]

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

!!! abstract "Environment variables"
* `DIUN_NOTIF_SLACK_WEBHOOKURL`
* `DIUN_NOTIF_SLACK_WEBHOOKURLFILE`
* `DIUN_NOTIF_SLACK_RENDERFIELDS`
* `DIUN_NOTIF_SLACK_TEMPLATEBODY`

Expand Down
4 changes: 3 additions & 1 deletion docs/notif/teams.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ You can send notifications to your Teams team-channel using an [incoming webhook

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

!!! abstract "Environment variables"
* `DIUN_NOTIF_TEAMS_WEBHOOKURL`
* `DIUN_NOTIF_TEAMS_WEBHOOKURLFILE`
* `DIUN_NOTIF_TEAMS_RENDERFACTS`
* `DIUN_NOTIF_TEAMS_TEMPLATEBODY`

Expand Down
11 changes: 6 additions & 5 deletions internal/model/notif_discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (

// NotifDiscord holds Discord notification configuration details
type NotifDiscord struct {
WebhookURL string `yaml:"webhookURL,omitempty" json:"webhookURL,omitempty" validate:"required"`
Mentions []string `yaml:"mentions,omitempty" json:"mentions,omitempty"`
RenderFields *bool `yaml:"renderFields,omitempty" json:"renderFields,omitempty" validate:"required"`
Timeout *time.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty" validate:"required"`
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
WebhookURL string `yaml:"webhookURL,omitempty" json:"webhookURL,omitempty" validate:"omitempty"`
WebhookURLFile string `yaml:"webhookURLFile,omitempty" json:"webhookURLFile,omitempty" validate:"omitempty,file"`
Mentions []string `yaml:"mentions,omitempty" json:"mentions,omitempty"`
RenderFields *bool `yaml:"renderFields,omitempty" json:"renderFields,omitempty" validate:"required"`
Timeout *time.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty" validate:"required"`
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
}

// GetDefaults gets the default values
Expand Down
7 changes: 4 additions & 3 deletions internal/model/notif_slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ const NotifSlackDefaultTemplateBody = "<!channel> Docker tag {{ if .Entry.Image.

// NotifSlack holds slack notification configuration details
type NotifSlack struct {
WebhookURL string `yaml:"webhookURL,omitempty" json:"webhookURL,omitempty" validate:"required"`
RenderFields *bool `yaml:"renderFields,omitempty" json:"renderFields,omitempty" validate:"required"`
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
WebhookURL string `yaml:"webhookURL,omitempty" json:"webhookURL,omitempty" validate:"omitempty"`
WebhookURLFile string `yaml:"webhookURLFile,omitempty" json:"webhookURLFile,omitempty" validate:"omitempty,file"`
RenderFields *bool `yaml:"renderFields,omitempty" json:"renderFields,omitempty" validate:"required"`
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
}

// GetDefaults gets the default values
Expand Down
7 changes: 4 additions & 3 deletions internal/model/notif_teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ const NotifTeamsDefaultTemplateBody = "Docker tag {{ if .Entry.Image.HubLink }}[

// NotifTeams holds Teams notification configuration details
type NotifTeams struct {
WebhookURL string `yaml:"webhookURL,omitempty" json:"webhookURL,omitempty" validate:"required"`
RenderFacts *bool `yaml:"renderFacts,omitempty" json:"renderFacts,omitempty" validate:"required"`
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
WebhookURL string `yaml:"webhookURL,omitempty" json:"webhookURL,omitempty" validate:"omitempty"`
WebhookURLFile string `yaml:"webhookURLFile,omitempty" json:"webhookURLFile,omitempty" validate:"omitempty,file"`
RenderFacts *bool `yaml:"renderFacts,omitempty" json:"renderFacts,omitempty" validate:"required"`
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
}

// GetDefaults gets the default values
Expand Down
8 changes: 7 additions & 1 deletion internal/notif/discord/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/crazy-max/diun/v4/internal/model"
"github.com/crazy-max/diun/v4/internal/msg"
"github.com/crazy-max/diun/v4/internal/notif/notifier"
"github.com/crazy-max/diun/v4/pkg/utl"
"github.com/pkg/errors"
)

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

webhookURL, err := utl.GetSecret(c.cfg.WebhookURL, c.cfg.WebhookURLFile)
if err != nil {
return errors.Wrap(err, "cannot retrieve webhook URL for Discord notifier")
}

message, err := msg.New(msg.Options{
Meta: c.meta,
Entry: entry,
Expand Down Expand Up @@ -117,7 +123,7 @@ func (c *Client) Send(entry model.NotifEntry) error {
return err
}

u, err := url.Parse(c.cfg.WebhookURL)
u, err := url.Parse(webhookURL)
if err != nil {
return err
}
Expand Down
9 changes: 8 additions & 1 deletion internal/notif/slack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"github.com/crazy-max/diun/v4/internal/model"
"github.com/crazy-max/diun/v4/internal/msg"
"github.com/crazy-max/diun/v4/internal/notif/notifier"
"github.com/crazy-max/diun/v4/pkg/utl"
"github.com/nlopes/slack"
"github.com/pkg/errors"
)

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

// Send creates and sends a slack notification with an entry
func (c *Client) Send(entry model.NotifEntry) error {
webhookURL, err := utl.GetSecret(c.cfg.WebhookURL, c.cfg.WebhookURLFile)
if err != nil {
return errors.Wrap(err, "cannot retrieve webhook URL for Slack notifier")
}

message, err := msg.New(msg.Options{
Meta: c.meta,
Entry: entry,
Expand Down Expand Up @@ -93,7 +100,7 @@ func (c *Client) Send(entry model.NotifEntry) error {
color = "#0054ca"
}

return slack.PostWebhook(c.cfg.WebhookURL, &slack.WebhookMessage{
return slack.PostWebhook(webhookURL, &slack.WebhookMessage{
Attachments: []slack.Attachment{
{
Color: color,
Expand Down
9 changes: 8 additions & 1 deletion internal/notif/teams/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/crazy-max/diun/v4/internal/model"
"github.com/crazy-max/diun/v4/internal/msg"
"github.com/crazy-max/diun/v4/internal/notif/notifier"
"github.com/crazy-max/diun/v4/pkg/utl"
"github.com/pkg/errors"
)

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

// Send creates and sends a webhook notification with an entry
func (c *Client) Send(entry model.NotifEntry) error {
webhookURL, err := utl.GetSecret(c.cfg.WebhookURL, c.cfg.WebhookURLFile)
if err != nil {
return errors.Wrap(err, "cannot retrieve webhook URL for Teams notifier")
}

message, err := msg.New(msg.Options{
Meta: c.meta,
Entry: entry,
Expand Down Expand Up @@ -107,7 +114,7 @@ func (c *Client) Send(entry model.NotifEntry) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(10)*time.Second)
defer cancel()

req, err := http.NewRequestWithContext(ctx, "POST", c.cfg.WebhookURL, bytes.NewBuffer(jsonBody))
req, err := http.NewRequestWithContext(ctx, "POST", webhookURL, bytes.NewBuffer(jsonBody))
if err != nil {
return err
}
Expand Down
Loading