github.com/Axway/agent-sdk@v1.1.101/pkg/config/webhookconfig.go (about) 1 package config 2 3 import ( 4 "errors" 5 "net/url" 6 "strings" 7 8 log "github.com/Axway/agent-sdk/pkg/util/log" 9 ) 10 11 // WebhookConfig - Interface for webhook config 12 type WebhookConfig interface { 13 GetURL() string 14 GetWebhookHeaders() map[string]string 15 GetSecret() string 16 IsConfigured() bool 17 ValidateConfig() error 18 } 19 20 // WebhookConfiguration - do NOT make this an IConfigValidator, as it is validated as part of subscriptionConfig 21 type WebhookConfiguration struct { 22 WebhookConfig 23 URL string `config:"url"` 24 Headers string `config:"headers"` 25 Secret string `config:"secret"` 26 webhookHeaders map[string]string 27 } 28 29 // NewWebhookConfig - 30 func NewWebhookConfig() WebhookConfig { 31 return &WebhookConfiguration{} 32 } 33 34 // GetURL - Returns the URL 35 func (c *WebhookConfiguration) GetURL() string { 36 return c.URL 37 } 38 39 // IsConfigured - bool 40 func (c *WebhookConfiguration) IsConfigured() bool { 41 return c.URL != "" 42 } 43 44 // GetWebhookHeaders - Returns the webhook headers 45 func (c *WebhookConfiguration) GetWebhookHeaders() map[string]string { 46 return c.webhookHeaders 47 } 48 49 // GetSecret - Returns the secret 50 func (c *WebhookConfiguration) GetSecret() string { 51 return c.Secret 52 } 53 54 // ValidateConfig - Validate the config. Do NOT make this ValidateCfg IConfigValidator. It is called directly from the subscriptionconfig 55 // validator. But, it is ONLY called if the approvalMode is "webhook" 56 func (c *WebhookConfiguration) ValidateConfig() error { 57 if c.IsConfigured() { 58 webhookURL := c.GetURL() 59 if _, err := url.ParseRequestURI(webhookURL); err != nil { 60 return errors.New("central.subscriptions.approvalWebhook.URL is not a valid URL") 61 } 62 63 // headers are allowed to be empty, so only validate if there is a configured value 64 if c.Headers != "" { 65 // (example header) Header=contentType,Value=application/json, Header=Elements-Formula-Instance-Id,Value=440874, Header=Authorization,Value=User F+rYQSfu0w5yIa5q7uNs2MKYcIok8pYpgAUwJtXFnzc=, Organization a1713018bbde8f54f4f55ff8c3bd8bfe 66 c.webhookHeaders = map[string]string{} 67 c.Headers = strings.Replace(c.Headers, ", ", ",", -1) 68 headersValues := strings.Split(c.Headers, ",Header=") 69 for _, headerValue := range headersValues { 70 hvArray := strings.Split(headerValue, ",Value=") 71 if len(hvArray) != 2 { 72 return errors.New("could not parse value of central.subscriptions.approvalWebhook.headers") 73 } 74 hvArray[0] = strings.TrimPrefix(hvArray[0], "Header=") // handle the first header in the list 75 c.webhookHeaders[hvArray[0]] = hvArray[1] 76 } 77 } 78 log.Trace("Subscription approval webhook configuration set") 79 } 80 81 return nil 82 }