|
| 1 | +--- |
| 2 | +searchable: false |
| 3 | +--- |
| 4 | + |
| 5 | +# AWS Signature Version 4 (SigV4) |
| 6 | + |
| 7 | +Hive Gateway allows you to sign subgraph requests with |
| 8 | +[AWS Signature Version 4 (SigV4)](https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html) |
| 9 | +for secure communication between the Gateway and the subgraphs. |
| 10 | + |
| 11 | +```mermaid |
| 12 | +flowchart TD |
| 13 | + A[Consumer] -->|GraphQL Request| B(Hive Gateway) |
| 14 | + B --> C[Execution Engine] |
| 15 | + C -->|Subgraph request| D[AWS Sigv4 Plugin] |
| 16 | + D --> |Get the credentials| E[Assume Role I AM] |
| 17 | + E --> F[Sign the request] |
| 18 | + F --> |HTTP Request| G[Products Subgraph] |
| 19 | + C -->|Subgraph request| H[AWS Sigv4 Plugin] |
| 20 | + H --> |Get the credentials| I[Hard-coded configuration] |
| 21 | + I --> J[Sign the request] |
| 22 | + J --> |HTTP Request| K[Users Subgraph] |
| 23 | +``` |
| 24 | + |
| 25 | +## How to use? |
| 26 | + |
| 27 | +You can enable AWS SigV4 signing by setting the `awsSigV4` option to `true` in the Gateway |
| 28 | +configuration. |
| 29 | + |
| 30 | +```ts filename="gateway.config.ts" |
| 31 | +import { defineConfig } from '@graphql-hive/gateway' |
| 32 | + |
| 33 | +export const gatewayConfig = defineConfig({ |
| 34 | + awsSigV4: true |
| 35 | +}) |
| 36 | +``` |
| 37 | + |
| 38 | +## Credentials |
| 39 | + |
| 40 | +By default, Hive Gateway will use the standard environment variables to get the AWS credentials. But |
| 41 | +you can also provide the credentials directly in the configuration. |
| 42 | + |
| 43 | +```ts filename="gateway.config.ts" |
| 44 | +import { defineConfig } from '@graphql-hive/gateway' |
| 45 | + |
| 46 | +export const gatewayConfig = defineConfig({ |
| 47 | + awsSigV4: { |
| 48 | + accessKeyId: process.env.AWS_ACCESS_KEY_ID, |
| 49 | + secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, |
| 50 | + region: process.env.AWS_REGION |
| 51 | + } |
| 52 | +}) |
| 53 | +``` |
| 54 | + |
| 55 | +### Assume Role (IAM) |
| 56 | + |
| 57 | +You can provide the `roleArn` and `roleSessionName` to assume a role using the provided credentials. |
| 58 | + |
| 59 | +```ts filename="gateway.config.ts" |
| 60 | +import { defineConfig } from '@graphql-hive/gateway' |
| 61 | + |
| 62 | +export const gatewayConfig = defineConfig({ |
| 63 | + 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 |
| 68 | + } |
| 69 | +}) |
| 70 | +``` |
| 71 | + |
| 72 | +## Service and region configuration |
| 73 | + |
| 74 | +By default, the plugin extracts the service and region from the URL of the subgraph. But you can |
| 75 | +also provide the service and region directly in the configuration. |
| 76 | + |
| 77 | +```ts filename="gateway.config.ts" |
| 78 | +import { defineConfig } from '@graphql-hive/gateway' |
| 79 | + |
| 80 | +export const gatewayConfig = defineConfig({ |
| 81 | + 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' |
| 87 | + } |
| 88 | +}) |
| 89 | +``` |
| 90 | + |
| 91 | +## Subgraph-specific configuration |
| 92 | + |
| 93 | +You can also configure the SigV4 signing for specific subgraphs by setting the `awsSigV4` option in |
| 94 | +the subgraph configuration. |
| 95 | + |
| 96 | +```ts filename="gateway.config.ts" |
| 97 | +import { defineConfig } from '@graphql-hive/gateway' |
| 98 | + |
| 99 | +export const gatewayConfig = defineConfig({ |
| 100 | + // Allowing SigV4 signing for only the 'products' subgraph |
| 101 | + awsSigV4: subgraph => subgraph === 'products' |
| 102 | +}) |
| 103 | +``` |
| 104 | + |
| 105 | +or you can provide the credentials directly per subgraph. |
| 106 | + |
| 107 | +```ts filename="gateway.config.ts" |
| 108 | +import { defineConfig } from '@graphql-hive/gateway' |
| 109 | + |
| 110 | +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' |
| 121 | + } |
| 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' |
| 130 | + } |
| 131 | + } |
| 132 | + return false |
| 133 | + } |
| 134 | +}) |
| 135 | +``` |
0 commit comments