Skip to content

Commit 9ea52e6

Browse files
committed
Complete rewrite to support AWS V4 style signature
The rewrite also includes a lot of changes to the coding style. The core logic of generating signatures now resides in a separate file from the nginx module source. The nginx module code i.e. ngx_http_aws_auth now only containts lines that glues the signature generation code to the nginx lifecycle. We also also introduced unit tests based on cmocka. Documentation on usage of the unit tests is sparse. It however serves as a great mechanism to both test correctness of any new functionality and also as a way to detect regressions.
1 parent 64891ff commit 9ea52e6

15 files changed

+1314
-924
lines changed

Makefile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
CC=gcc
2+
CFLAGS=-g -I${NGX_PATH}/src/os/unix -I${NGX_PATH}/src/core -I${NGX_PATH}/src/http -I${NGX_PATH}/src/http/modules -I${NGX_PATH}/src/event -I${NGX_PATH}/objs/ -I.
3+
4+
5+
all:
6+
7+
%.o: %.c
8+
$(CC) -c -o $@ $< $(CFLAGS)
9+
10+
.PHONY: all clean test nginx
11+
12+
13+
NGX_OBJS := $(shell find ${NGX_PATH}/objs -name \*.o)
14+
15+
nginx:
16+
cd ${NGX_PATH} && rm -rf ${NGX_PATH}/objs/src/core/nginx.o && make
17+
18+
test: | nginx
19+
strip -N main -o ${NGX_PATH}/objs/src/core/nginx_without_main.o ${NGX_PATH}/objs/src/core/nginx.o
20+
mv ${NGX_PATH}/objs/src/core/nginx_without_main.o ${NGX_PATH}/objs/src/core/nginx.o
21+
$(CC) test_suite.c $(CFLAGS) -o test_suite -lcmocka ${NGX_OBJS} -ldl -lpthread -lcrypt -lssl -lpcre -lcrypto -lz $<
22+
./test_suite
23+
24+
clean:
25+
rm -f *.o test_suite
26+
27+
# vim: ft=make ts=8 sw=8 noet

README.md

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
# AWS proxy module
22

3-
This branch contains the legacy code based on the deprecated AWS V2 authentication protocol.
4-
The code here is well tested in production. You can find the experimental support for the
5-
V4 authentication protocol that will work with all new regions of AWS under the branch named
6-
*AuthV4*.
3+
This nginx module can proxy requests to authenticated S3 backends using Amazon's
4+
V4 authentication API. The first version of this module was written for the V2
5+
authentication protocol and can be found in the *AuthV2* branch.
6+
7+
## License
8+
This project uses the same license as ngnix does i.e. the 2 clause BSD / simplified BSD / FreeBSD license
9+
10+
## Usage example
711

812
Implements proxying of authenticated requests to S3.
913

1014
```nginx
1115
server {
1216
listen 8000;
1317
18+
aws_access_key your_aws_access_key;
19+
aws_key_scope scope_of_generated_signing_key;
20+
aws_signing_key signing_key_generated_using_script;
21+
aws_s3_bucket your_s3_bucket;
22+
1423
location / {
24+
aws_sign;
1525
proxy_pass http://your_s3_bucket.s3.amazonaws.com;
16-
17-
aws_access_key your_aws_access_key;
18-
aws_secret_key the_secret_associated_with_the_above_access_key;
19-
s3_bucket your_s3_bucket;
20-
21-
proxy_set_header Authorization $s3_auth_token;
22-
proxy_set_header x-amz-date $aws_date;
2326
}
2427
2528
# This is an example that does not use the server root for the proxy root
@@ -28,49 +31,66 @@ Implements proxying of authenticated requests to S3.
2831
rewrite /myfiles/(.*) /$1 break;
2932
proxy_pass http://your_s3_bucket.s3.amazonaws.com/$1;
3033
31-
aws_access_key your_aws_access_key;
32-
aws_secret_key the_secret_associated_with_the_above_access_key;
33-
s3_bucket your_s3_bucket;
34-
chop_prefix /myfiles; # Take out this part of the URL before signing it, since '/myfiles' will not be part of the URI sent to Amazon
3534
36-
37-
proxy_set_header Authorization $s3_auth_token;
38-
proxy_set_header x-amz-date $aws_date;
35+
aws_access_key your_aws_access_key;
36+
aws_key_scope scope_of_generated_signing_key;
37+
aws_signing_key signing_key_generated_using_script;
3938
}
4039
4140
}
4241
```
4342

43+
## Security considerations
44+
The V4 protocol does not need access to the actual secret keys that one obtains
45+
from the IAM service. The correct way to use the IAM key is to actually generate
46+
a scoped signing key and use this signing key to access S3. This nginx module
47+
requires the signing key and not the actual secret key. It is an insecure practise
48+
to let the secret key reside on your nginx server.
4449

45-
# Community
50+
Note that signing keys have a validity of just one week. Hence, they need to
51+
be refreshed constantly. Please useyour favourite configuration management
52+
system such as saltstack, puppet, chef, etc. etc. to distribute the signing
53+
keys to your nginx clusters. Do not forget to HUP the server after placing the new
54+
signing key as nginx reads the configuration only at startup time.
4655

47-
The project uses google groups for discussions. The group name is nginx-aws-auth. You can visit the web forum [here](https://groups.google.com/forum/#!forum/nginx-aws-auth)
48-
49-
50-
Request signing & Amazon Cloudfront Service
51-
-------------------------------------------
52-
53-
54-
If Nginx is behind Amazon's CloudFront CDN service, you need to add this setting :
56+
A standalone python script has been provided to generate the signing key
57+
```
58+
./generate_signing_key -h
59+
usage: generate_signing_key [-h] -k ACCESS_KEY -r REGION [-s SERVICE]
60+
[-d DATE] [--no-base64] [-v]
61+
62+
Generate AWS S3 signing key in it's base64 encoded form
63+
64+
optional arguments:
65+
-h, --help show this help message and exit
66+
-k SECRET_KEY, --secret-key SECRET_KEY
67+
The secret key generated using AWS IAM. Do not confuse
68+
this with the access key id
69+
-r REGION, --region REGION
70+
The AWS region where this key would be used. Example:
71+
us-east-1
72+
-s SERVICE, --service SERVICE
73+
The AWS service for which this key would be used.
74+
Example: s3
75+
-d DATE, --date DATE The date on which this key is generated in yyyymmdd
76+
format
77+
--no-base64 Disable output as a base64 encoded string. This NOT
78+
recommended
79+
-v, --verbose Produce verbose output on stderr
5580
56-
```nginx
57-
proxy_set_header x-amz-cf-id "";
5881
```
5982

60-
into nginx.conf in order to clear X-Amz-Cf-Id header before signing the request to Amazon S3 bucket.
83+
## Known limitations
84+
The 2.x version of the module currently only has support for GET and HEAD calls. This is because
85+
signing request body is complex and has not yet been implemented.
6186

6287

63-
More info here :
88+
## Community
6489

65-
http://s3.amazonaws.com/doc/s3-developer-guide/RESTAuthentication.html
90+
The project uses google groups for discussions. The group name is nginx-aws-auth. You can visit the web forum [here](https://groups.google.com/forum/#!forum/nginx-aws-auth)
6691

6792

68-
Credits:
69-
========
93+
## Credits
7094
Original idea based on http://nginx.org/pipermail/nginx/2010-February/018583.html and suggestion of moving to variables rather than patching the proxy module.
7195

7296
Subsequent contributions can be found in the commit logs of the project.
73-
74-
License
75-
-------
76-
This project uses the same license as ngnix does i.e. the 2 clause BSD / simplified BSD / FreeBSD license

VERSION

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## Version 2.0.0
4+
Move to AWS V4 signatures
5+
36
## Version 1.1.1
47
AWS dat header is computed unconditionallt. See #11
58

0 commit comments

Comments
 (0)