Skip to content

Commit c9e6053

Browse files
committed
docs: how to use w/ examples
1 parent 47a54a9 commit c9e6053

File tree

1 file changed

+104
-1
lines changed

1 file changed

+104
-1
lines changed

README.md

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ NGINX AWS Signature Library to authenticate AWS services such as S3 and Lambda v
44

55
![](./docs/img/nginx-aws-signature.png)
66

7+
**TABLE OF CONTENTS:**
8+
9+
- [Getting Started](#getting-started)
10+
- [Directory Structure and File Descriptions](#directory-structure-and-file-descriptions)
11+
- [How to Use](#how-to-use)
12+
- [Contributing](#contributing)
13+
- [Authors and acknowledgment](#authors-and-acknowledgment)
14+
- [License](#license)
15+
716
## Getting Started
817

918
![](./docs/img/nginx-aws-signature-flow.png)
@@ -38,7 +47,101 @@ nginx-aws-signature
3847

3948
## How to Use
4049

41-
TBD
50+
### Sparse Checkouts of Submodules
51+
52+
Create or update git submodule when using this lib in your repository. Otherwise, copy [`core/*.js`](./core/) into the prefered directory on your NGINX instance.
53+
54+
#### Step 1. Choose one of the following options
55+
- Option 1. Clone this repo with a depth of 1 for the first time
56+
```bash
57+
git clone --depth=1 --no-checkout [email protected]:nginxinc/nginx-aws-signature.git <path/to/submodule>
58+
```
59+
60+
- Option 2. Update a submodule when using the latest lib after cloning
61+
```bash
62+
git submodule update --init <path/to/submodule>
63+
```
64+
65+
#### Step 2. Sparse checkouts of submodules
66+
67+
```bash
68+
git submodule absorbgitdirs
69+
git -C <path/to/submodule> config core.sparseCheckout true
70+
echo 'core/*' >>.git/modules/<path/to/submodule>/info/sparse-checkout
71+
git submodule update --force --checkout <path/to/submodule>
72+
```
73+
74+
### Configure NGINX
75+
76+
```nginx
77+
js_import /etc/nginx/awssig/awscredentials.js;
78+
js_import /etc/nginx/awssig/awssig4.js;
79+
js_import /etc/nginx/serverless/lambdagateway.js;
80+
81+
js_set $awsDate awssig4.awsHeaderDate;
82+
js_set $awsPayloadHash awssig4.awsHeaderPayloadHash;
83+
js_set $awsSessionToken awscredentials.sessionToken;
84+
js_set $lambdaFunctionARNAuth lambdagateway.lambdaFunctionARNAuth;
85+
86+
map $request_uri $lambda_url {
87+
default https://lambda.us-east-1.amazonaws.com;
88+
}
89+
90+
server {
91+
listen 80; # Use SSL/TLS in production
92+
93+
location /2015-03-31/functions/foo/invocations {
94+
auth_request /aws/credentials/retrieval;
95+
proxy_set_header x-amz-date $awsDate;
96+
proxy_set_header x-amz-content-sha256 $awsPayloadHash;
97+
proxy_set_header x-amz-security-token $awsSessionToken;
98+
proxy_set_header Authorization $lambdaFunctionARNAuth;
99+
proxy_pass $lambda_url$request_uri;
100+
}
101+
102+
location /aws/credentials/retrieval {
103+
internal;
104+
js_content awscredentials.fetchCredentials;
105+
}
106+
}
107+
```
108+
109+
### Integrate AWS Signature Lib To Your Custom NJS
110+
111+
Import library files of `nginx-aws-signature`, and implement a function to generate `Authorization` header by using the lib with the proper parameters in your custom NJS.
112+
113+
`/etc/nginx/<custom-njs-path>/<your-njs>.js`:
114+
115+
```njs
116+
import awscred from "../awssig/awscredentials.js";
117+
import awssig4 from "../awssig/awssig4.js";
118+
import utils from "../awssig/utils.js";
119+
120+
const SERVICE = 'lambda';
121+
122+
utils.requireEnvVar('LAMBDA_SERVER');
123+
utils.requireEnvVar('LAMBDA_REGION');
124+
125+
function lambdaFunctionARNAuth(r) {
126+
const host = process.env['LAMBDA_SERVER'];
127+
const region = process.env['LAMBDA_REGION'];
128+
const queryParams = '';
129+
const credentials = awscred.readCredentials(r);
130+
131+
const signature = awssig4.signatureV4(
132+
r, awscred.getNow(), region, SERVICE,
133+
r.variables.request_uri, queryParams, host, credentials
134+
);
135+
return signature;
136+
}
137+
```
138+
139+
**Examples:**
140+
141+
| Project | NJS example |
142+
|------------------------|------------------------------------------|
143+
| `nginx-s3-gateway` | [`s3gateway.js`](https://github.com/nginxinc/nginx-s3-gateway/blob/master/common/etc/nginx/include/s3gateway.js) |
144+
| `nginx-lambda-gateway` | [`lambdagateway.js`](https://github.com/nginx-serverless/nginx-lambda-gateway/blob/main/common/lambda-core/lambdagateway.js) |
42145

43146
## Contributing
44147

0 commit comments

Comments
 (0)