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

feat: Add custom headers support for Webhook AlertMethod #383

Merged
merged 4 commits into from
Jan 13, 2025
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
2 changes: 2 additions & 0 deletions docs/resources/alert_method_webhook.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ resource "nobl9_alert_method_webhook" "this" {

- `description` (String) Optional description of the resource. Here, you can add details about who is responsible for the integration (team/owner) or the purpose of creating it.
- `display_name` (String) User-friendly display name of the resource.
- `headers` (Map of String) Custom HTTP headers to include in the webhook request.
- `sensitive_headers` (Map of String, Sensitive) Custom HTTP headers to include in the webhook request. The values are treated as sensitive, will not be displayed in the Terraform state and will be encrypted by Nobl9 platform.
- `template` (String) Webhook message template. See documentation for template format and samples.
- `template_fields` (List of String) Webhook message fields. The message contains JSON payload with specified fields. See documentation for allowed fields.
- `url` (String, Sensitive) URL of the webhook endpoint.
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
github.com/MicahParks/jwkset v0.5.20 // indirect
github.com/MicahParks/jwkset v0.7.0 // indirect
github.com/MicahParks/keyfunc/v3 v3.3.5 // indirect
github.com/ProtonMail/go-crypto v1.1.0-alpha.2 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
Expand Down Expand Up @@ -92,7 +92,7 @@ require (
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.8.0 // indirect
golang.org/x/time v0.9.0 // indirect
golang.org/x/tools v0.28.0 // indirect
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da // indirect
google.golang.org/appengine v1.6.8 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj
github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM=
github.com/MicahParks/jwkset v0.5.20 h1:gTIKx9AofTqQJ0srd8AL7ty9NeadP5WUXSPOZadTpOI=
github.com/MicahParks/jwkset v0.5.20/go.mod h1:q8ptTGn/Z9c4MwbcfeCDssADeVQb3Pk7PnVxrvi+2QY=
github.com/MicahParks/jwkset v0.7.0 h1:CXWuiYBk5NuTl+N/3UI3UcYNH79yWuKAZWZkc/y+7Ok=
github.com/MicahParks/jwkset v0.7.0/go.mod h1:fVrj6TmG1aKlJEeceAz7JsXGTXEn72zP1px3us53JrA=
github.com/MicahParks/keyfunc/v3 v3.3.5 h1:7ceAJLUAldnoueHDNzF8Bx06oVcQ5CfJnYwNt1U3YYo=
github.com/MicahParks/keyfunc/v3 v3.3.5/go.mod h1:SdCCyMJn/bYqWDvARspC6nCT8Sk74MjuAY22C7dCST8=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
Expand Down Expand Up @@ -307,6 +309,8 @@ golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/time v0.8.0 h1:9i3RxcPv3PZnitoVGMPDKZSq1xW1gK1Xy3ArNOGZfEg=
golang.org/x/time v0.8.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/time v0.9.0 h1:EsRrnYcQiGH+5FfbgvV4AP7qEZstoyrHB0DzarOQ4ZY=
golang.org/x/time v0.9.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
Expand Down
44 changes: 44 additions & 0 deletions nobl9/resource_alertmethod.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,20 @@ func (i alertMethodWebhook) GetSchema() map[string]*schema.Schema {
Type: schema.TypeString,
},
},
"headers": {
Type: schema.TypeMap,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "Custom HTTP headers to include in the webhook request.",
},
"sensitive_headers": {
Type: schema.TypeMap,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Sensitive: true,
// nolint:lll
Description: "Custom HTTP headers to include in the webhook request. The values are treated as sensitive, will not be displayed in the Terraform state and will be encrypted by Nobl9 platform.",
},
}
}

Expand All @@ -205,12 +219,31 @@ func (i alertMethodWebhook) MarshalSpec(r resourceInterface) v1alphaAlertMethod.
templateFields = nil
}

headers := make([]v1alphaAlertMethod.WebhookHeader, 0)
for key, value := range r.Get("headers").(map[string]interface{}) {
header := v1alphaAlertMethod.WebhookHeader{
Name: key,
Value: value.(string),
IsSecret: false,
}
headers = append(headers, header)
}
for key, value := range r.Get("sensitive_headers").(map[string]interface{}) {
header := v1alphaAlertMethod.WebhookHeader{
Name: key,
Value: value.(string),
IsSecret: true,
}
headers = append(headers, header)
}

return v1alphaAlertMethod.Spec{
Description: r.Get("description").(string),
Webhook: &v1alphaAlertMethod.WebhookAlertMethod{
URL: r.Get("url").(string),
Template: template,
TemplateFields: templateFields,
Headers: headers,
},
}
}
Expand All @@ -224,6 +257,17 @@ func (i alertMethodWebhook) UnmarshalSpec(d *schema.ResourceData, spec v1alphaAl
err = d.Set("template_fields", config.TemplateFields)
diags = appendError(diags, err)

headers := make(map[string]interface{})

for _, header := range config.Headers {
if !header.IsSecret { // secrets will be returned from the API as [hidden] values
headers[header.Name] = header.Value
}
}

err = d.Set("headers", headers)
diags = appendError(diags, err)
nieomylnieja marked this conversation as resolved.
Show resolved Hide resolved

return diags
}

Expand Down
19 changes: 19 additions & 0 deletions nobl9/resource_alertmethod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestAcc_Nobl9AlertMethod(t *testing.T) {
}{
{"test-webhook", "webhook", testWebhookTemplateConfig},
{"test-webhook-fields", "webhook", testWebhookTemplateFieldsConfig},
{"test-webhook-headers", "webhook", testWebhookHeadersConfig},
{"test-pagerduty", "pagerduty", testPagerDutyConfig},
{"test-pagerduty-send-resolution", "pagerduty", testPagerDutyWithSendResolutionConfig},
{"test-pagerduty-send-resolution-message", "pagerduty", testPagerDutyWithSendResolutionWithMessageConfig},
Expand Down Expand Up @@ -72,6 +73,24 @@ resource "nobl9_alert_method_webhook" "%s" {
`, name, name, testProject)
}

func testWebhookHeadersConfig(name string) string {
return fmt.Sprintf(`
resource "nobl9_alert_method_webhook" "%s" {
name = "%s"
project = "%s"
description = "WebHook"
url = "http://web.net"
template = "SLO needs attention $slo_name"
headers = {
"X-Custom-Header" = "custom value"
}
sensitive_headers = {
"Authorization" = "Bearer xyz"
}
}
`, name, name, testProject)
}

func testPagerDutyConfig(name string) string {
return fmt.Sprintf(`
resource "nobl9_alert_method_pagerduty" "%s" {
Expand Down
Loading