Fix update webhook and use secret for webhook authentication (#42)

* Fix update repository webhooks and extend webhook tests

* Use secret to authenticate the webhook payload and extend the tests with the secret

Co-authored-by: Raul Barreto <raul.barreto@redbull.com>
This commit is contained in:
raulbarreto-delivion
2021-12-15 01:52:58 +01:00
committed by GitHub
parent bee3f62908
commit 1323abe664
3 changed files with 98 additions and 12 deletions

View File

@@ -9,14 +9,19 @@ import (
"strings"
)
type WebhookConfiguration struct {
Secret string `json:"secret,omitempty"`
}
type Webhook struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
CreatedDate jsonTime `json:"createdDate,omitempty"`
UpdatedDate jsonTime `json:"updatedDate,omitempty"`
URL string `json:"url,omitempty"`
Active bool `json:"active,omitempty"`
Events []interface{} `json:"events"`
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
CreatedDate jsonTime `json:"createdDate,omitempty"`
UpdatedDate jsonTime `json:"updatedDate,omitempty"`
URL string `json:"url,omitempty"`
Active bool `json:"active,omitempty"`
Events []interface{} `json:"events"`
Configuration WebhookConfiguration `json:"configuration"`
}
type WebhookListResponse struct {
@@ -64,6 +69,12 @@ func resourceRepositoryWebhook() *schema.Resource {
ForceNew: false,
Elem: &schema.Schema{Type: schema.TypeString},
},
"secret": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Default: "",
},
"active": {
Type: schema.TypeBool,
Optional: true,
@@ -101,7 +112,7 @@ func resourceRepositoryWebhookUpdate(d *schema.ResourceData, m interface{}) erro
return err
}
return resourceRepositoryHookRead(d, m)
return resourceRepositoryWebhookRead(d, m)
}
func resourceRepositoryWebhookCreate(d *schema.ResourceData, m interface{}) error {
@@ -183,11 +194,16 @@ func resourceRepositoryWebhookDelete(d *schema.ResourceData, m interface{}) erro
}
func newWebhookFromResource(d *schema.ResourceData) (Hook *Webhook) {
configuration := &WebhookConfiguration{
Secret: d.Get("secret").(string),
}
webhook := &Webhook{
Name: d.Get("name").(string),
URL: d.Get("webhook_url").(string),
Active: d.Get("active").(bool),
Events: d.Get("events").([]interface{}),
Name: d.Get("name").(string),
URL: d.Get("webhook_url").(string),
Active: d.Get("active").(bool),
Events: d.Get("events").([]interface{}),
Configuration: *configuration,
}
return webhook
@@ -224,6 +240,7 @@ func getRepositoryWebhookFromId(d *schema.ResourceData, m interface{}) error {
_ = d.Set("webhook_url", webhook.URL)
_ = d.Set("active", webhook.Active)
_ = d.Set("events", webhook.Events)
_ = d.Set("secret", webhook.Configuration.Secret)
return nil
}
@@ -260,6 +277,7 @@ func getRepositoryWebhookFromList(d *schema.ResourceData, m interface{}) error {
_ = d.Set("webhook_url", webhook.URL)
_ = d.Set("active", webhook.Active)
_ = d.Set("events", webhook.Events)
_ = d.Set("secret", webhook.Configuration.Secret)
return nil
}
}