/
enrich_http_get.go
156 lines (128 loc) · 4.08 KB
/
enrich_http_get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//go:build !wasm
package transform
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/brexhq/substation/config"
iconfig "github.com/brexhq/substation/internal/config"
"github.com/brexhq/substation/internal/errors"
"github.com/brexhq/substation/internal/http"
"github.com/brexhq/substation/internal/secrets"
"github.com/brexhq/substation/message"
)
type enrichHTTPGetConfig struct {
// URL is the HTTP(S) endpoint that data is retrieved from.
//
// If the substring ${DATA} is in the URL, then the URL is interpolated with
// data (either the value from Object.SourceKey or the raw data). URLs may be optionally
// interpolated with secrets (e.g., ${SECRET:FOO}).
URL string `json:"url"`
// Headers are an array of objects that contain HTTP headers sent in the request.
// Values may be optionally interpolated with secrets (e.g., ${SECRET:FOO}).
//
// This is optional and has no default.
Headers map[string]string `json:"headers"`
Object iconfig.Object `json:"object"`
}
func (c *enrichHTTPGetConfig) Decode(in interface{}) error {
return iconfig.Decode(in, c)
}
func (c *enrichHTTPGetConfig) Validate() error {
if c.Object.SourceKey == "" && c.Object.TargetKey != "" {
return fmt.Errorf("object_source_key: %v", errors.ErrMissingRequiredOption)
}
if c.Object.SourceKey != "" && c.Object.TargetKey == "" {
return fmt.Errorf("object_target_key: %v", errors.ErrMissingRequiredOption)
}
if c.URL == "" {
return fmt.Errorf("url: %v", errors.ErrMissingRequiredOption)
}
return nil
}
func newEnrichHTTPGet(ctx context.Context, cfg config.Config) (*enrichHTTPGet, error) {
conf := enrichHTTPGetConfig{}
if err := conf.Decode(cfg.Settings); err != nil {
return nil, fmt.Errorf("transform: enrich_http_get: %v", err)
}
if err := conf.Validate(); err != nil {
return nil, fmt.Errorf("transform: enrich_http_get: %v", err)
}
tf := enrichHTTPGet{
conf: conf,
}
tf.client.Setup()
for k, v := range conf.Headers {
// Retrieve secret and interpolate with header value.
v, err := secrets.Interpolate(ctx, v)
if err != nil {
return nil, fmt.Errorf("transform: enrich_http_get: %v", err)
}
tf.headers = append(tf.headers, http.Header{
Key: k,
Value: v,
})
}
return &tf, nil
}
type enrichHTTPGet struct {
conf enrichHTTPGetConfig
// client is safe for concurrent use.
client http.HTTP
headers []http.Header
}
func (tf *enrichHTTPGet) Transform(ctx context.Context, msg *message.Message) ([]*message.Message, error) {
if msg.IsControl() {
return []*message.Message{msg}, nil
}
// The URL can exist in three states:
//
// - No interpolation, the URL is unchanged.
//
// - Object-based interpolation, the URL is interpolated
// using the object handling pattern.
//
// - Data-based interpolation, the URL is interpolated
// using the data handling pattern.
//
// The URL is always interpolated with the substring ${data}.
url := tf.conf.URL
if strings.Contains(url, enrichHTTPInterp) {
if tf.conf.Object.SourceKey != "" {
value := msg.GetValue(tf.conf.Object.SourceKey)
if !value.Exists() {
return []*message.Message{msg}, nil
}
url = strings.ReplaceAll(url, enrichHTTPInterp, value.String())
} else {
url = strings.ReplaceAll(url, enrichHTTPInterp, string(msg.Data()))
}
}
// Retrieve secret and interpolate with URL
url, err := secrets.Interpolate(ctx, url)
if err != nil {
return nil, fmt.Errorf("transform: enrich_http_get: %v", err)
}
// resp.Body is closed by enrichHTTPParseResponse.
resp, err := tf.client.Get(ctx, url, tf.headers...)
if err != nil {
return nil, fmt.Errorf("transform: enrich_http_get: %v", err)
}
parsed, err := enrichHTTPParseResponse(resp)
if err != nil {
return nil, fmt.Errorf("transform: enrich_http_get: %v", err)
}
if tf.conf.Object.TargetKey != "" {
if err := msg.SetValue(tf.conf.Object.TargetKey, parsed); err != nil {
return nil, fmt.Errorf("transform: enrich_http_get: %v", err)
}
return []*message.Message{msg}, nil
}
msg.SetData(parsed)
return []*message.Message{msg}, nil
}
func (tf *enrichHTTPGet) String() string {
b, _ := json.Marshal(tf.conf)
return string(b)
}