Skip to content
This repository has been archived by the owner on Aug 31, 2022. It is now read-only.

Add XML payload to Webhooks/HTTP #182

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
74 changes: 73 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ receivers:
### Webhooks/HTTP

Webhooks are the easiest way of integrating this tool to external systems. It allows templating & custom headers which
allows you to push events to many possible sources out there. See [Customizing Payload] for more information.
allows you to push events to many possible sources out there. See [Customizing Payload](#customizing-payload) for more information.
In addition Webhooks/HTTP support XML payload, specifying header `Content-Type: application/xml`. See [XML Payload](#xml-payload) for more information.

```yaml
# ...
Expand All @@ -89,6 +90,77 @@ receivers:
User-Agent: kube-event-exporter 1.0
layout: # Optional
```
#### XML payload
```yaml
# ...
receivers:
- name: "alerts"
webhook:
endpoint: "https://my-super-secret-service.com"
headers:
X-API-KEY: "123"
User-Agent: kube-event-exporter 1.0
Content-Type: "application/xml"
layout: # Optional
```

Resulting XML payload differ if there is a root key or not. Root key is used as root tag in XML payload, otherwise root tag is `<doc>`.

With root key
```yaml
# ...
receivers:
- name: "alerts"
webhook:
endpoint: "https://my-super-secret-service.com"
headers:
Content-Type: "application/xml"
layout:
event:
region: "us-west-2"
eventType: "kubernetes-event"
type: "{{ .Type }}"
count: "{{ .Count }}"
```

XML payload
```xml
<?xml version="1.0" encoding="UTF-8"?>
<event>
<count>23</count>
<eventType>kubernetes-event</eventType>
<region>us-west-2</region>
<type>Warning</type>
</event>
```

Without root key
```yaml
# ...
receivers:
- name: "alerts"
webhook:
endpoint: "https://my-super-secret-service.com"
headers:
Content-Type: "application/xml"
layout:
region: "us-west-2"
eventType: "kubernetes-event"
type: "{{ .Type }}"
count: "{{ .Count }}"
```

XML payload
```xml
<?xml version="1.0" encoding="UTF-8"?>
<doc>
<count>23</count>
<eventType>kubernetes-event</eventType>
<region>us-west-2</region>
<type>Warning</type>
</doc>
```


### Elasticsearch

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
cloud.google.com/go/bigquery v1.9.0
cloud.google.com/go/pubsub v1.3.1
github.com/Masterminds/sprig v2.22.0+incompatible
github.com/clbanning/mxj v1.8.4
github.com/Shopify/sarama v1.24.1
github.com/aws/aws-sdk-go v1.41.19
github.com/elastic/go-elasticsearch/v7 v7.4.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/clbanning/mxj v1.8.4 h1:HuhwZtbyvyOw+3Z1AowPkU87JkJUSv751ELWaiTpj8I=
github.com/clbanning/mxj v1.8.4/go.mod h1:BVjHeAH+rl9rs6f+QIpeRl0tfu10SXn1pUSa5PVGJng=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
Expand Down
53 changes: 52 additions & 1 deletion pkg/sinks/tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package sinks
import (
"bytes"
"encoding/json"
"encoding/xml"
"text/template"

"github.com/Masterminds/sprig"
"github.com/clbanning/mxj"
"github.com/opsgenie/kubernetes-event-exporter/pkg/kube"
"text/template"
)

func GetString(event *kube.EnhancedEvent, text string) (string, error) {
Expand Down Expand Up @@ -98,3 +101,51 @@ func serializeEventWithLayout(layout map[string]interface{}, ev *kube.EnhancedEv
}
return toSend, nil
}

func serializeXMLEventWithLayout(layout map[string]interface{}, ev *kube.EnhancedEvent) ([]byte, error) {
var (
err error
toSend []byte
obj interface{}
rootTag string = mxj.DefaultRootTag
)

if layout != nil {
res, err := convertLayoutTemplate(layout, ev)
if err != nil {
return nil, err
}

if len(res) == 1 {

for k, v := range res {
rootTag = k
obj = v
}
} else {

obj = res
}

} else {
// convert event to map
// TODO: create benchmark to check if reflect version is more performant
mrv := make(map[string]interface{})
jsonRepr, err := json.Marshal(ev)
if err != nil {
return nil, err
}
err = json.Unmarshal(jsonRepr, &mrv)
if err != nil {
return nil, err
}
obj = mrv
}

toSend, err = mxj.AnyXml(obj, rootTag)
if err != nil {
return nil, err
}

return []byte(xml.Header + string(toSend)), nil
}
85 changes: 82 additions & 3 deletions pkg/sinks/tmpl_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package sinks

import (
"testing"
"time"

"github.com/clbanning/mxj"
"github.com/opsgenie/kubernetes-event-exporter/pkg/kube"
"github.com/stretchr/testify/require"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"
"time"
)

func TestLayoutConvert(t *testing.T) {
func newTestEvent() *kube.EnhancedEvent {
ev := &kube.EnhancedEvent{}
ev.Namespace = "default"
ev.Type = "Warning"
ev.Count = 23
ev.InvolvedObject.Kind = "Pod"
ev.InvolvedObject.Name = "nginx-server-123abc-456def"
ev.Message = "Successfully pulled image \"nginx:latest\""
ev.FirstTimestamp = v1.Time{Time: time.Now()}
return ev
}

func TestLayoutConvert(t *testing.T) {
ev := newTestEvent()

// Because Go, when parsing yaml, its []interface, not []string
var tagz interface{}
Expand Down Expand Up @@ -50,3 +58,74 @@ func TestLayoutConvert(t *testing.T) {

require.Equal(t, val2, ev.Message)
}

var xmltests = []struct {
name string
layout map[string]interface{}
expectedRoot string
}{
{
"XML with root node",
map[string]interface{}{
"event": map[interface{}]interface{}{
"title": "{{ .InvolvedObject.Name }}",
"severity": "{{ .Type }}",
"Count": "{{ .Count }}",
"eventType": "kube-event",
"region": "us-west-2",
"createdAt": "{{ .GetTimestampMs }}",
}},
"event",
},
{
"XML without root node",
map[string]interface{}{
"title": "{{ .InvolvedObject.Name }}",
"severity": "{{ .Type }}",
"Count": "{{ .Count }}",
"eventType": "kube-event",
"region": "us-west-2",
"createdAt": "{{ .GetTimestampMs }}",
},
"doc",
},
{
"XML format k8s event (no layout)",
nil,
"doc",
},
}

func TestXMLEventWithLayout(t *testing.T) {
ev := newTestEvent()

for _, tt := range xmltests {
t.Run(tt.name, func(t *testing.T) {
res, err := serializeXMLEventWithLayout(tt.layout, ev)

require.NoError(t, err)

//t.Logf("res: %v", string(res))

m, err := mxj.NewMapXml(res, true)

require.NoError(t, err)

root, ok := m[tt.expectedRoot].(map[string]interface{})

require.True(t, ok)

if tt.layout != nil {
require.Equal(t, root["title"], "nginx-server-123abc-456def")
require.Equal(t, root["severity"], "Warning")
require.Equal(t, root["region"], "us-west-2")
require.Equal(t, root["Count"], 23.0) // mxj unmarshal try to cast only to boolean or float64
} else {
require.Equal(t, root["involvedObject"].(map[string]interface{})["name"], "nginx-server-123abc-456def")
require.Equal(t, root["type"], "Warning")
require.Equal(t, root["count"], 23.0) // mxj unmarshal try to cast only to boolean or float64
}

})
}
}
55 changes: 51 additions & 4 deletions pkg/sinks/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strings"

"github.com/opsgenie/kubernetes-event-exporter/pkg/kube"
)
Expand All @@ -18,20 +19,65 @@ type WebhookConfig struct {
Headers map[string]string `yaml:"headers"`
}

type serializeEvent func(layout map[string]interface{}, ev *kube.EnhancedEvent) ([]byte, error)

var mimetypeConverter = map[string]serializeEvent{
"application/json": serializeEventWithLayout,
"application/xml": serializeXMLEventWithLayout,
}

func NewWebhook(cfg *WebhookConfig) (Sink, error) {
return &Webhook{cfg: cfg}, nil

var contentType = ""
var converter serializeEvent

for k, v := range cfg.Headers {

if strings.Contains(strings.ToLower(k), "content-type") {
contentType = v
break
}
}

if contentType == "" {
// default
contentType = "application/json"
cfg.Headers["Content-Type"] = "application/json"
}

for k, v := range mimetypeConverter {
if strings.Contains(contentType, k) {
converter = v
break
}
}

if converter == nil {

errMsg := "Wrong Content-Type in headers: only %s are supported"
mimetypes := ""
for k := range mimetypeConverter {
mimetypes = mimetypes + ", " + k
}

return nil, fmt.Errorf(errMsg, mimetypes)
}

return &Webhook{cfg: cfg, serializer: converter}, nil
}

type Webhook struct {
cfg *WebhookConfig
cfg *WebhookConfig
serializer serializeEvent
}

func (w *Webhook) Close() {
// No-op
}

func (w *Webhook) Send(ctx context.Context, ev *kube.EnhancedEvent) error {
reqBody, err := serializeEventWithLayout(w.cfg.Layout, ev)

reqBody, err := w.serializer(w.cfg.Layout, ev)
if err != nil {
return err
}
Expand All @@ -40,10 +86,11 @@ func (w *Webhook) Send(ctx context.Context, ev *kube.EnhancedEvent) error {
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")

for k, v := range w.cfg.Headers {
req.Header.Add(k, v)
}

tlsClientConfig, err := setupTLS(&w.cfg.TLS)
if err != nil {
return fmt.Errorf("failed to setup TLS: %w", err)
Expand Down
Loading