Skip to content

Commit 3be1a5c

Browse files
ardatanhasparus
authored andcommitted
Documentation: Incoming AWS Sigv4 validation (#6587)
1 parent 93ebfd1 commit 3be1a5c

File tree

1 file changed

+105
-40
lines changed
  • packages/web/docs/src/content/gateway/other-features/security

1 file changed

+105
-40
lines changed

packages/web/docs/src/content/gateway/other-features/security/aws-sigv4.mdx

Lines changed: 105 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Hive Gateway allows you to sign subgraph requests with
88
[AWS Signature Version 4 (SigV4)](https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html)
99
for secure communication between the Gateway and the subgraphs.
1010

11+
## Signing outgoing requests
12+
1113
```mermaid
1214
flowchart TD
1315
A[Consumer] -->|GraphQL Request| B(Hive Gateway)
@@ -22,20 +24,22 @@ flowchart TD
2224
J --> |HTTP Request| K[Users Subgraph]
2325
```
2426

25-
## How to use?
27+
### How to use?
2628

27-
You can enable AWS SigV4 signing by setting the `awsSigV4` option to `true` in the Gateway
29+
You can enable AWS SigV4 signing by setting the `awsSigV4.outgoing` option to `true` in the Gateway
2830
configuration.
2931

3032
```ts filename="gateway.config.ts"
3133
import { defineConfig } from '@graphql-hive/gateway'
3234

3335
export const gatewayConfig = defineConfig({
34-
awsSigV4: true
36+
awsSigV4: {
37+
outgoing: true
38+
}
3539
})
3640
```
3741

38-
## Credentials
42+
### Credentials
3943

4044
By default, Hive Gateway will use the standard environment variables to get the AWS credentials. But
4145
you can also provide the credentials directly in the configuration.
@@ -45,14 +49,16 @@ import { defineConfig } from '@graphql-hive/gateway'
4549

4650
export const gatewayConfig = defineConfig({
4751
awsSigV4: {
48-
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
49-
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
50-
region: process.env.AWS_REGION
52+
outgoing: {
53+
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
54+
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
55+
region: process.env.AWS_REGION
56+
}
5157
}
5258
})
5359
```
5460

55-
### Assume Role (IAM)
61+
#### Assume Role (IAM)
5662

5763
You can provide the `roleArn` and `roleSessionName` to assume a role using the provided credentials.
5864

@@ -61,15 +67,17 @@ import { defineConfig } from '@graphql-hive/gateway'
6167

6268
export const gatewayConfig = defineConfig({
6369
awsSigV4: {
64-
region: process.env.AWS_REGION,
65-
// By default it takes the credentials from the environment variables
66-
roleArn: 'arn:aws:iam::123456789012:role/role-name', // process.env.AWS_ROLE_ARN
67-
roleSessionName: 'session-name' // process.env.AWS_ROLE_SESSION_NAME
70+
outgoing: {
71+
region: process.env.AWS_REGION,
72+
// By default it takes the credentials from the environment variables
73+
roleArn: 'arn:aws:iam::123456789012:role/role-name', // process.env.AWS_ROLE_ARN
74+
roleSessionName: 'session-name' // process.env.AWS_ROLE_SESSION_NAME
75+
}
6876
}
6977
})
7078
```
7179

72-
## Service and region configuration
80+
### Service and region configuration
7381

7482
By default, the plugin extracts the service and region from the URL of the subgraph. But you can
7583
also provide the service and region directly in the configuration.
@@ -79,16 +87,18 @@ import { defineConfig } from '@graphql-hive/gateway'
7987

8088
export const gatewayConfig = defineConfig({
8189
awsSigV4: {
82-
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
83-
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
84-
region: process.env.AWS_REGION,
85-
serviceName: 'lambda',
86-
region: 'us-east-1'
90+
outgoing: {
91+
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
92+
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
93+
region: process.env.AWS_REGION,
94+
serviceName: 'lambda',
95+
region: 'us-east-1'
96+
}
8797
}
8898
})
8999
```
90100

91-
## Subgraph-specific configuration
101+
### Subgraph-specific configuration
92102

93103
You can also configure the SigV4 signing for specific subgraphs by setting the `awsSigV4` option in
94104
the subgraph configuration.
@@ -97,8 +107,10 @@ the subgraph configuration.
97107
import { defineConfig } from '@graphql-hive/gateway'
98108

99109
export const gatewayConfig = defineConfig({
100-
// Allowing SigV4 signing for only the 'products' subgraph
101-
awsSigV4: subgraph => subgraph === 'products'
110+
awsSigV4: {
111+
// Allowing SigV4 signing for only the 'products' subgraph
112+
outgoing: subgraph => subgraph === 'products'
113+
}
102114
})
103115
```
104116

@@ -108,28 +120,81 @@ or you can provide the credentials directly per subgraph.
108120
import { defineConfig } from '@graphql-hive/gateway'
109121

110122
export const gatewayConfig = defineConfig({
111-
// Providing AWS SigV4 credentials for the 'products' and 'users' subgraphs separately
112-
// And do not allow SigV4 signing for any other subgraph
113-
awsSigV4(subgraph) {
114-
// You can use hardcoded credentials for the 'products' subgraph
115-
if (subgraph === 'products') {
116-
return {
117-
accessKeyId: process.env.PRODUCTS_AWS_ACCESS_KEY_ID,
118-
secretAccessKey: process.env.PRODUCTS_AWS_SECRET_ACCESS_KEY,
119-
serviceName: 'lambda',
120-
region: 'eu-west-1'
123+
awsSigV4: {
124+
// Providing AWS SigV4 credentials for the 'products' and 'users' subgraphs separately
125+
// And do not allow SigV4 signing for any other subgraph
126+
outgoing(subgraph) {
127+
// You can use hardcoded credentials for the 'products' subgraph
128+
if (subgraph === 'products') {
129+
return {
130+
accessKeyId: process.env.PRODUCTS_AWS_ACCESS_KEY_ID,
131+
secretAccessKey: process.env.PRODUCTS_AWS_SECRET_ACCESS_KEY,
132+
serviceName: 'lambda',
133+
region: 'eu-west-1'
134+
}
121135
}
122-
}
123-
// You can use Assume Role for the 'users' subgraph
124-
if (subgraph === 'users') {
125-
return {
126-
roleArn: 'arn:aws:iam::123456789012:role/role-name',
127-
roleSessionName: 'session-name',
128-
serviceName: 's3',
129-
region: 'us-east-1'
136+
// You can use Assume Role for the 'users' subgraph
137+
if (subgraph === 'users') {
138+
return {
139+
roleArn: 'arn:aws:iam::123456789012:role/role-name',
140+
roleSessionName: 'session-name',
141+
serviceName: 's3',
142+
region: 'us-east-1'
143+
}
130144
}
145+
return false
146+
}
147+
}
148+
})
149+
```
150+
151+
## Validating incoming requests
152+
153+
Hive Gateway can also mimic AWS services by validating the incoming requests with AWS SigV4.
154+
155+
But you have to provide some credentials using environment variables or directly in the
156+
configuration.
157+
158+
```ts filename="gateway.config.ts"
159+
import { defineConfig } from '@graphql-hive/gateway'
160+
161+
export const gatewayConfig = defineConfig({
162+
awsSigV4: {
163+
incoming: {
164+
// Hard-coded secret
165+
secretAccessKey: () => process.env.AWS_SECRET_ACCESS_KEY,
166+
167+
// Or Assume Role
168+
assumeRole: () => ({
169+
roleArn: process.env['AWS_ROLE_ARN'],
170+
roleSessionName: process.env['AWS_ROLE_SESSION_NAME'],
171+
region: process.env['AWS_REGION']
172+
})
173+
}
174+
}
175+
})
176+
```
177+
178+
### Combining with JWT
179+
180+
If you use JWT for authentication for some services, you can combine both depending on the prefix in
181+
the `Authorization` header.
182+
183+
In this case if the `Authorization` header starts with `Bearer`, the JWT will be used for
184+
authentication, otherwise, the request will be validated with AWS SigV4.
185+
186+
```ts filename="gateway.config.ts"
187+
import { defineConfig } from '@graphql-hive/gateway'
188+
189+
export const gatewayConfig = defineConfig({
190+
awsSigV4: {
191+
incoming: true
192+
},
193+
jwt: {
194+
reject: {
195+
missingToken: false,
196+
invalidToken: false
131197
}
132-
return false
133198
}
134199
})
135200
```

0 commit comments

Comments
 (0)