-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathdestawss3.go
More file actions
371 lines (320 loc) · 10.5 KB
/
Copy pathdestawss3.go
File metadata and controls
371 lines (320 loc) · 10.5 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package destawss3
import (
"bytes"
"context"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"strings"
"time"
awssdk "github.com/aws/aws-sdk-go-v2/aws"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
awscreds "github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/hookdeck/outpost/internal/destregistry"
"github.com/hookdeck/outpost/internal/destregistry/metadata"
"github.com/hookdeck/outpost/internal/models"
"github.com/jmespath/go-jmespath"
)
// Default template that generates keys with timestamp and event ID
// Using join to concatenate the timestamp, underscore, event-id, and .json extension
const defaultKeyTemplate = `join('', [time.rfc3339_nano, '_', metadata."event-id", '.json'])`
// AWSS3Config is the configuration for an S3 destination
type AWSS3Config struct {
Bucket string
Region string
KeyTemplate string // JMESPath expression for generating S3 keys
StorageClass string
Endpoint string // Optional endpoint for testing
}
// AWSS3Credentials is the credentials for an S3 destination
type AWSS3Credentials struct {
Key string
Secret string
Session string
}
// AWSS3Provider is the S3 Provider implementation
type AWSS3Provider struct {
*destregistry.BaseProvider
}
var _ destregistry.Provider = (*AWSS3Provider)(nil)
// New creates a new AWSS3Provider
func New(loader metadata.MetadataLoader, basePublisherOpts []destregistry.BasePublisherOption) (*AWSS3Provider, error) {
base, err := destregistry.NewBaseProvider(loader, "aws_s3", basePublisherOpts...)
if err != nil {
return nil, err
}
return &AWSS3Provider{BaseProvider: base}, nil
}
// Validate checks if the destination configuration is valid
func (p *AWSS3Provider) Validate(ctx context.Context, destination *models.Destination) error {
_, _, err := p.resolveConfig(ctx, destination)
return err
}
// createS3Client creates a new S3 client using the provided configuration and credentials.
func (p *AWSS3Provider) createS3Client(ctx context.Context, cfg *AWSS3Config, creds *AWSS3Credentials) (*s3.Client, error) {
sdkConfig, err := awsconfig.LoadDefaultConfig(ctx,
awsconfig.WithCredentialsProvider(awscreds.NewStaticCredentialsProvider(
creds.Key,
creds.Secret,
creds.Session,
)),
awsconfig.WithRegion(cfg.Region),
)
if err != nil {
return nil, fmt.Errorf("failed to load AWS config: %w", err)
}
s3Options := []func(*s3.Options){}
if cfg.Endpoint != "" {
s3Options = append(s3Options, func(o *s3.Options) {
o.BaseEndpoint = awssdk.String(cfg.Endpoint)
o.UsePathStyle = true // Required for LocalStack
})
}
return s3.NewFromConfig(sdkConfig, s3Options...), nil
}
// CreatePublisher creates a new S3 publisher for the given destination
func (p *AWSS3Provider) CreatePublisher(ctx context.Context, destination *models.Destination) (destregistry.Publisher, error) {
cfg, creds, err := p.resolveConfig(ctx, destination)
if err != nil {
return nil, err
}
client, err := p.createS3Client(ctx, cfg, creds)
if err != nil {
return nil, fmt.Errorf("failed to create S3 client: %w", err)
}
return NewAWSS3Publisher(
p.BaseProvider.NewPublisher(),
client,
cfg.Bucket,
cfg.KeyTemplate,
cfg.StorageClass,
), nil
}
// resolveConfig resolves the configuration and credentials for the S3 destination
func (p *AWSS3Provider) resolveConfig(ctx context.Context, destination *models.Destination) (*AWSS3Config, *AWSS3Credentials, error) {
if err := p.BaseProvider.Validate(ctx, destination); err != nil {
return nil, nil, err
}
sc := destination.Config["storage_class"]
_, err := parseStorageClass(sc)
if err != nil {
return nil, nil, destregistry.NewErrDestinationValidation([]destregistry.ValidationErrorDetail{
{
Field: "config.storage_class",
Type: "enum",
},
})
}
// Use custom template if provided, otherwise use default
keyTemplate := destination.Config["key_template"]
if keyTemplate == "" {
keyTemplate = defaultKeyTemplate
}
// Validate the JMESPath expression by compiling it
if _, err := jmespath.Compile(keyTemplate); err != nil {
return nil, nil, destregistry.NewErrDestinationValidation([]destregistry.ValidationErrorDetail{
{
Field: "config.key_template",
Type: "pattern",
},
})
}
return &AWSS3Config{
Bucket: destination.Config["bucket"],
Region: destination.Config["region"],
KeyTemplate: keyTemplate,
StorageClass: sc,
Endpoint: destination.Config["endpoint"],
}, &AWSS3Credentials{
Key: destination.Credentials["key"],
Secret: destination.Credentials["secret"],
Session: destination.Credentials["session"],
}, nil
}
// ComputeTarget returns a human-readable target description for the S3 destination
func (p *AWSS3Provider) ComputeTarget(destination *models.Destination) destregistry.DestinationTarget {
bucket := destination.Config["bucket"]
region := destination.Config["region"]
return destregistry.DestinationTarget{
Target: fmt.Sprintf("%s in %s", bucket, region),
TargetURL: fmt.Sprintf("https://s3.console.aws.amazon.com/s3/buckets/%s?region=%s", bucket, region),
}
}
// Publisher implementation
// AWSS3Publisher is the S3 publisher implementation
type AWSS3Publisher struct {
*destregistry.BasePublisher
client *s3.Client
bucket string
keyTemplate *jmespath.JMESPath
storageClass string
}
func (p *AWSS3Publisher) Close() error {
p.BasePublisher.StartClose()
return nil
}
// parseTimeFields converts an event time to pre-parsed time components
func parseTimeFields(t time.Time) map[string]interface{} {
utc := t.UTC()
return map[string]interface{}{
"year": fmt.Sprintf("%04d", utc.Year()),
"month": fmt.Sprintf("%02d", utc.Month()),
"day": fmt.Sprintf("%02d", utc.Day()),
"hour": fmt.Sprintf("%02d", utc.Hour()),
"minute": fmt.Sprintf("%02d", utc.Minute()),
"second": fmt.Sprintf("%02d", utc.Second()),
"date": utc.Format("2006-01-02"),
"datetime": utc.Format("2006-01-02T15:04:05"),
"unix": fmt.Sprintf("%d", utc.Unix()),
"rfc3339": utc.Format(time.RFC3339),
"rfc3339_nano": utc.Format(time.RFC3339Nano),
}
}
func (p *AWSS3Publisher) makeKey(event *models.Event, metadata map[string]string) (string, error) {
// Convert event data to map[string]interface{}
dataMap := make(map[string]interface{})
for k, v := range event.Data {
dataMap[k] = v
}
// Convert metadata to map[string]interface{} for JMESPath
metadataMap := make(map[string]interface{})
for k, v := range metadata {
metadataMap[k] = v
}
// Build the data structure for JMESPath evaluation
templateData := map[string]interface{}{
"data": dataMap,
"metadata": metadataMap,
"time": parseTimeFields(event.Time),
}
// Evaluate the JMESPath expression
result, err := p.keyTemplate.Search(templateData)
if err != nil {
return "", fmt.Errorf("failed to evaluate key template: %w", err)
}
// Convert result to string
var key string
switch v := result.(type) {
case string:
key = v
case float64:
key = fmt.Sprintf("%g", v)
case int:
key = fmt.Sprintf("%d", v)
case bool:
key = fmt.Sprintf("%t", v)
default:
// For complex types or nil, try to convert to string
if v == nil {
return "", fmt.Errorf("key template produced nil result")
}
key = fmt.Sprintf("%v", v)
}
if key == "" {
return "", fmt.Errorf("key template produced empty string")
}
return key, nil
}
func (p *AWSS3Publisher) getStorageClass() (types.StorageClass, error) {
return parseStorageClass(p.storageClass)
}
func (p *AWSS3Publisher) getChecksums(payload []byte) (string, error) {
checksum := sha256.Sum256(payload)
return base64.StdEncoding.EncodeToString(checksum[:]), nil
}
func (p *AWSS3Publisher) Format(_ context.Context, event *models.Event) (*s3.PutObjectInput, error) {
data, err := json.Marshal(event.Data)
if err != nil {
return nil, err
}
// Get merged metadata (system + event metadata)
metadata := p.BasePublisher.MakeMetadata(event, time.Now())
key, err := p.makeKey(event, metadata)
if err != nil {
return nil, fmt.Errorf("failed to create S3 key: %w", err)
}
storageClass, err := p.getStorageClass()
if err != nil {
return nil, fmt.Errorf("failed to get S3 storage class: %w", err)
}
checksumSha256, err := p.getChecksums(data)
if err != nil {
return nil, fmt.Errorf("failed to compute checksum: %w", err)
}
return &s3.PutObjectInput{
Bucket: awssdk.String(p.bucket),
Key: awssdk.String(key),
Body: bytes.NewReader(data),
Metadata: metadata,
StorageClass: storageClass,
ContentType: awssdk.String("application/json"),
ChecksumSHA256: awssdk.String(checksumSha256),
ChecksumAlgorithm: types.ChecksumAlgorithmSha256,
}, nil
}
func (p *AWSS3Publisher) Publish(ctx context.Context, event *models.Event) (*destregistry.Delivery, error) {
if err := p.BasePublisher.StartPublish(); err != nil {
return nil, err
}
defer p.BasePublisher.FinishPublish()
input, err := p.Format(ctx, event)
if err != nil {
return nil, err
}
_, err = p.client.PutObject(ctx, input)
if err != nil {
return &destregistry.Delivery{
Status: "failed",
Code: "ERR",
Response: map[string]interface{}{
"error": err.Error(),
},
}, destregistry.NewErrDestinationPublishAttempt(err, "aws_s3", map[string]interface{}{
"error": err.Error(),
})
}
return &destregistry.Delivery{
Status: "success",
Code: "OK",
Response: map[string]interface{}{
"bucket": p.bucket,
"key": *input.Key,
},
}, nil
}
func parseStorageClass(storageClass string) (types.StorageClass, error) {
if storageClass == "" {
return types.StorageClassStandard, nil
}
// Just here so we can call .Values() on the types.ObjectStorageClass type
var sc types.StorageClass
for _, val := range sc.Values() {
if strings.EqualFold(string(val), storageClass) {
return val, nil
}
}
return "", fmt.Errorf("invalid S3 storage class: %q", storageClass)
}
// NewAWSS3Publisher exposed for testing
func NewAWSS3Publisher(
basePublisher *destregistry.BasePublisher,
client *s3.Client,
bucket, keyTemplateStr, storageClass string,
) *AWSS3Publisher {
// Compile the JMESPath expression (we assume it's already validated)
tmpl, err := jmespath.Compile(keyTemplateStr)
if err != nil {
// This should not happen as template is validated in resolveConfig
panic(fmt.Sprintf("invalid key template: %v", err))
}
return &AWSS3Publisher{
BasePublisher: basePublisher,
client: client,
bucket: bucket,
keyTemplate: tmpl,
storageClass: storageClass,
}
}