forked from anomalizer/ngx_aws_auth
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ngx_http_aws_auth.c
252 lines (204 loc) · 7.76 KB
/
ngx_http_aws_auth.c
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
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include "aws_functions.h"
#define AWS_S3_VARIABLE "s3_auth_token"
#define AWS_DATE_VARIABLE "aws_date"
static void* ngx_http_aws_auth_create_loc_conf(ngx_conf_t *cf);
static char* ngx_http_aws_auth_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
static ngx_int_t ngx_aws_auth_req_init(ngx_conf_t *cf);
static char * ngx_http_aws_endpoint(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char* ngx_http_aws_sign(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
typedef struct {
ngx_str_t access_key;
ngx_str_t key_scope;
ngx_str_t signing_key;
ngx_str_t signing_key_decoded;
ngx_str_t endpoint;
ngx_str_t bucket_name;
ngx_str_t security_token;
ngx_uint_t enabled;
} ngx_http_aws_auth_conf_t;
static ngx_command_t ngx_http_aws_auth_commands[] = {
{ ngx_string("aws_access_key"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_aws_auth_conf_t, access_key),
NULL },
{ ngx_string("aws_key_scope"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_aws_auth_conf_t, key_scope),
NULL },
{ ngx_string("aws_signing_key"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_aws_auth_conf_t, signing_key),
NULL },
{ ngx_string("aws_endpoint"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_aws_endpoint,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_aws_auth_conf_t, endpoint),
NULL },
{ ngx_string("aws_s3_bucket"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_aws_auth_conf_t, bucket_name),
NULL },
{ ngx_string("aws_security_token"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_aws_auth_conf_t, security_token),
NULL },
{ ngx_string("aws_sign"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
ngx_http_aws_sign,
0,
0,
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_aws_auth_module_ctx = {
NULL, /* preconfiguration */
ngx_aws_auth_req_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_aws_auth_create_loc_conf, /* create location configuration */
ngx_http_aws_auth_merge_loc_conf /* merge location configuration */
};
ngx_module_t ngx_http_aws_auth_module = {
NGX_MODULE_V1,
&ngx_http_aws_auth_module_ctx, /* module context */
ngx_http_aws_auth_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static void *
ngx_http_aws_auth_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_aws_auth_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_aws_auth_conf_t));
conf->enabled = 0;
ngx_str_set(&conf->endpoint, "s3.amazonaws.com");
if (conf == NULL) {
return NGX_CONF_ERROR;
}
return conf;
}
static char *
ngx_http_aws_auth_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_aws_auth_conf_t *prev = parent;
ngx_http_aws_auth_conf_t *conf = child;
ngx_conf_merge_str_value(conf->access_key, prev->access_key, "");
ngx_conf_merge_str_value(conf->key_scope, prev->key_scope, "");
ngx_conf_merge_str_value(conf->signing_key, prev->signing_key, "");
ngx_conf_merge_str_value(conf->endpoint, prev->endpoint, "s3.amazonaws.com");
ngx_conf_merge_str_value(conf->bucket_name, prev->bucket_name, "");
ngx_conf_merge_str_value(conf->security_token, prev->security_token, "");
if(conf->signing_key_decoded.data == NULL)
{
conf->signing_key_decoded.data = ngx_pcalloc(cf->pool, 100);
if(conf->signing_key_decoded.data == NULL)
{
return NGX_CONF_ERROR;
}
}
if(conf->signing_key.len > 64) {
return NGX_CONF_ERROR;
} else {
ngx_decode_base64(&conf->signing_key_decoded, &conf->signing_key);
}
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_aws_proxy_sign(ngx_http_request_t *r)
{
ngx_http_aws_auth_conf_t *conf = ngx_http_get_module_loc_conf(r, ngx_http_aws_auth_module);
if(!conf->enabled) {
/* return directly if module is not enabled */
return NGX_DECLINED;
}
ngx_table_elt_t *h;
header_pair_t *hv;
if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
/* We do not wish to support anything with a body as signing for a body is unimplemented */
return NGX_HTTP_NOT_ALLOWED;
}
const ngx_array_t* headers_out = ngx_aws_auth__sign(r->pool, r,
&conf->access_key, &conf->signing_key_decoded, &conf->key_scope, &conf->bucket_name, &conf->endpoint, &conf->security_token);
ngx_uint_t i;
for(i = 0; i < headers_out->nelts; i++)
{
hv = (header_pair_t*)((u_char *) headers_out->elts + headers_out->size * i);
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"header name %s, value %s", hv->key.data, hv->value.data);
if(ngx_strncmp(hv->key.data, HOST_HEADER.data, hv->key.len) == 0) {
/* host header is controlled by proxy pass directive and hence
cannot be set by our module */
continue;
}
h = ngx_list_push(&r->headers_in.headers);
if (h == NULL) {
return NGX_ERROR;
}
h->hash = 1;
h->key = hv->key;
h->lowcase_key = hv->key.data; /* We ensure that header names are already lowercased */
h->value = hv->value;
}
return NGX_OK;
}
static char *
ngx_http_aws_endpoint(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
char *p = conf;
ngx_str_t *field, *value;
field = (ngx_str_t *)(p + cmd->offset);
value = cf->args->elts;
*field = value[1];
return NGX_CONF_OK;
}
static char *
ngx_http_aws_sign(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
/*
ngx_http_core_loc_conf_t *clcf;
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_aws_proxy_sign;
*/
ngx_http_aws_auth_conf_t *mconf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_aws_auth_module);
mconf->enabled = 1;
return NGX_CONF_OK;
}
static ngx_int_t
ngx_aws_auth_req_init(ngx_conf_t *cf)
{
ngx_http_handler_pt *h;
ngx_http_core_main_conf_t *cmcf;
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = ngx_http_aws_proxy_sign;
return NGX_OK;
}
/*
* vim: ts=4 sw=4 et
*/