Skip to content

Commit 34a63d5

Browse files
Mark Soltersmsolters
authored andcommitted
S3 Plugin: Implement SSE option
- Introduces a server_side_encryption parameter for the S3 [OUTPUT] plugin. Possible values are AES256 and aws:kms, as per AWS API documentation: https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html#API_PutObject_ResponseSyntax - If either value is provided, the x-amz-server-side-encryption header will be included with S3 requests with the corresponding value set. Signed-off-by: Mark Solters <[email protected]>
1 parent 20c461a commit 34a63d5

File tree

5 files changed

+138
-1
lines changed

5 files changed

+138
-1
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2+
3+
/* Fluent Bit
4+
* ==========
5+
* Copyright (C) 2019-2021 The Fluent Bit Authors
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
#ifndef FLB_AWS_SSE
21+
#define FLB_AWS_SSE
22+
23+
// Per https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html#API_PutObject_ResponseSyntax
24+
#include <sys/types.h>
25+
#define FLB_AWS_SSE_NONE 0
26+
#define FLB_AWS_SSE_AWSKMS 1
27+
#define FLB_AWS_SSE_AES256 2
28+
29+
/*
30+
* Get sse type from sse keyword. The return value is used to identify
31+
* what sse option to utilize.
32+
*
33+
* Returns int sse type id - FLB_AWS_SSE_<sse-type>
34+
*/
35+
int flb_aws_sse_get_type(const char *sse_keyword);
36+
37+
#endif

plugins/out_s3/s3.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <fluent-bit/flb_config_map.h>
2626
#include <fluent-bit/flb_aws_util.h>
2727
#include <fluent-bit/aws/flb_aws_compress.h>
28+
#include <fluent-bit/aws/flb_aws_sse.h>
2829
#include <fluent-bit/flb_hash.h>
2930
#include <fluent-bit/flb_crypto.h>
3031
#include <fluent-bit/flb_signv4.h>
@@ -98,6 +99,13 @@ static struct flb_aws_header storage_class_header = {
9899
.val_len = 0,
99100
};
100101

102+
static struct flb_aws_header server_side_encryption_header = {
103+
.key = "x-amz-server-side-encryption",
104+
.key_len = 28,
105+
.val = "",
106+
.val_len = 0,
107+
};
108+
101109
static char *mock_error_response(char *error_env_var)
102110
{
103111
char *err_val = NULL;
@@ -150,6 +158,9 @@ int create_headers(struct flb_s3 *ctx, char *body_md5,
150158
if (body_md5 != NULL && strlen(body_md5) && multipart_upload == FLB_FALSE) {
151159
headers_len++;
152160
}
161+
if (strlen(ctx->sse)) {
162+
headers_len++;
163+
}
153164
if (ctx->storage_class != NULL) {
154165
headers_len++;
155166
}
@@ -187,6 +198,12 @@ int create_headers(struct flb_s3 *ctx, char *body_md5,
187198
s3_headers[n].val_len = strlen(body_md5);
188199
n++;
189200
}
201+
if (strlen(ctx->sse)) {
202+
s3_headers[n] = server_side_encryption_header;
203+
s3_headers[n].val = ctx->sse;
204+
s3_headers[n].val_len = strlen(ctx->sse);
205+
n++;
206+
}
190207
if (ctx->storage_class != NULL) {
191208
s3_headers[n] = storage_class_header;
192209
s3_headers[n].val = ctx->storage_class;
@@ -756,6 +773,16 @@ static int cb_s3_init(struct flb_output_instance *ins,
756773
}
757774
}
758775

776+
tmp = flb_output_get_property("server_side_encryption", ins);
777+
if (tmp) {
778+
ret = flb_aws_sse_get_type(tmp);
779+
if (ret == -1) {
780+
flb_plg_error(ctx->ins, "unknown server-side encryption type: %s", tmp);
781+
return -1;
782+
}
783+
ctx->sse = tmp;
784+
}
785+
759786
tmp = flb_output_get_property("sts_endpoint", ins);
760787
if (tmp) {
761788
ctx->sts_endpoint = (char *) tmp;
@@ -2376,7 +2403,12 @@ static struct flb_config_map config_map[] = {
23762403
"A standard MIME type for the S3 object; this will be set "
23772404
"as the Content-Type HTTP header."
23782405
},
2379-
2406+
{
2407+
FLB_CONFIG_MAP_STR, "server_side_encryption", NULL,
2408+
0, FLB_FALSE, 0,
2409+
"Optional serve-side encryption type to use"
2410+
"Defaults to no encryption header. "
2411+
},
23802412
{
23812413
FLB_CONFIG_MAP_STR, "store_dir", "/tmp/fluent-bit/s3",
23822414
0, FLB_TRUE, offsetof(struct flb_s3, store_dir),

plugins/out_s3/s3.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ struct flb_s3 {
113113
char *log_key;
114114
char *external_id;
115115
char *profile;
116+
char *sse;
116117
int free_endpoint;
117118
int retry_requests;
118119
int use_put_object;

src/aws/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ set(src
1515
"flb_aws_imds.c"
1616
"flb_aws_credentials_http.c"
1717
"flb_aws_credentials_profile.c"
18+
"flb_aws_sse.c"
1819
)
1920

2021
if(FLB_HAVE_AWS_CREDENTIAL_PROCESS)

src/aws/flb_aws_sse.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2+
3+
/* Fluent Bit
4+
* ==========
5+
* Copyright (C) 2019-2021 The Fluent Bit Authors
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
#include <fluent-bit/flb_log.h>
21+
22+
#include <fluent-bit/aws/flb_aws_sse.h>
23+
24+
#include <stdint.h>
25+
26+
struct sse_option {
27+
int sse_type;
28+
char *sse_keyword;
29+
};
30+
31+
/*
32+
* Library of sse options
33+
* AWS plugins that support sse will have these options.
34+
* Referenced function should return -1 on error and 0 on success.
35+
*/
36+
static const struct sse_option sse_options[] = {
37+
/* FLB_AWS_SSE_NONE which is 0 is reserved for array footer */
38+
{
39+
FLB_AWS_SSE_AWSKMS,
40+
"aws:kms"
41+
},
42+
{
43+
FLB_AWS_SSE_AES256,
44+
"AES256"
45+
},
46+
{ 0 }
47+
};
48+
49+
int flb_aws_sse_get_type(const char *sse_keyword)
50+
{
51+
int ret;
52+
const struct sse_option *o;
53+
54+
o = sse_options;
55+
56+
while (o->sse_type != 0) {
57+
ret = strcmp(o->sse_keyword, sse_keyword);
58+
if (ret == 0) {
59+
return o->sse_type;
60+
}
61+
++o;
62+
}
63+
64+
flb_error("[aws_compress] unknown sse type: %s", sse_keyword);
65+
return -1;
66+
}

0 commit comments

Comments
 (0)