@prairielearn/aws 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +0 -0
- package/README.md +79 -0
- package/dist/config.d.ts +24 -0
- package/dist/config.js +43 -0
- package/dist/config.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/package.json +23 -0
- package/src/config.ts +62 -0
- package/src/index.ts +1 -0
- package/tsconfig.json +7 -0
|
File without changes
|
package/README.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# `@prairielearn/aws`
|
|
2
|
+
|
|
3
|
+
This package contains utilities that help us correctly configure AWS SDK clients.
|
|
4
|
+
|
|
5
|
+
Specifically, it's meant to address the fact that clients from the v3 AWS SDK don't share resolved credentials with each other. This means that, by default, every time a new client is created, it has to independently resolve credentials for itself. In production, this is problematic, as that requires talking to the IMDS, which will throttle requests if we make a ton of them in rapid succession. This results in clients being unable to obtain credentials, and thus the AWS API operations fail.
|
|
6
|
+
|
|
7
|
+
We resolve this with this package, which helps ensure that all AWS SDk clients are constructed with a shared and memoized credential provider.
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
Create a config provider with `makeAwsConfigProvider`:
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { makeAwsConfigProvider } from '@prairielearn/aws';
|
|
15
|
+
import { fromNodeProviderChain } from '@aws-sdk/credential-providers';
|
|
16
|
+
|
|
17
|
+
import { config } from './config';
|
|
18
|
+
|
|
19
|
+
const awsConfigProvider = makeAwsConfigProvider({
|
|
20
|
+
credentials: fromNodeProviderChain(),
|
|
21
|
+
getClientConfig: () => ({
|
|
22
|
+
region: config.awsRegion,
|
|
23
|
+
}),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
export const makeAwsClientConfig = awsConfigProvider.makeAwsClientConfig;
|
|
27
|
+
export const makeS3ClientConfig = awsConfigProvider.makeS3ClientConfig;
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Then, use the `makeAwsClientConfig` and `makeS3ClientConfig` functions to configure AWS clients:
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { EC2Client } from '@aws-sdk/client-ec2';
|
|
34
|
+
import { S3Client } from '@aws-sdk/client-s3';
|
|
35
|
+
|
|
36
|
+
const ec2 = new EC2Client(makeAwsClientConfig());
|
|
37
|
+
const s3 = new S3Client(makeS3ClientConfig());
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Providing extra config
|
|
41
|
+
|
|
42
|
+
The `get...` functions support passing in extra config. If this extra config conflicts with other config, such as that returned from `getClientConfig()`, this extra config will take precedence.
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
const s3 = new S3Client(
|
|
46
|
+
makeS3ClientConfig({
|
|
47
|
+
maxAttempts: 3,
|
|
48
|
+
}),
|
|
49
|
+
);
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Customizing S3 config
|
|
53
|
+
|
|
54
|
+
The config for S3 clients can be customized independently to support pointing it to other S3-compatible stores.
|
|
55
|
+
|
|
56
|
+
For instance, to use [`s3rver`](https://github.com/jamhall/s3rver) when running in dev mode, you could use something like the following config:
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
const awsConfigProvider = makeAwsConfigProvider({
|
|
60
|
+
credentials: fromNodeProviderChain(),
|
|
61
|
+
getClientConfig: () => ({
|
|
62
|
+
region: config.awsRegion,
|
|
63
|
+
}),
|
|
64
|
+
makeS3ClientConfig: () => {
|
|
65
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
66
|
+
return {
|
|
67
|
+
forcePathStyle: true,
|
|
68
|
+
credentials: {
|
|
69
|
+
accessKeyId: 'S3RVER',
|
|
70
|
+
secretAccessKey: 'S3RVER',
|
|
71
|
+
},
|
|
72
|
+
endpoint: 'http://localhost:5000',
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return {};
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
```
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { AwsCredentialIdentityProvider } from '@smithy/types';
|
|
2
|
+
interface AwsClientConfig {
|
|
3
|
+
region: string;
|
|
4
|
+
[key: string]: any;
|
|
5
|
+
}
|
|
6
|
+
export declare function makeAwsConfigProvider({ credentials, getClientConfig, getS3ClientConfig, }: {
|
|
7
|
+
credentials: AwsCredentialIdentityProvider;
|
|
8
|
+
getClientConfig: () => AwsClientConfig;
|
|
9
|
+
getS3ClientConfig?: () => Record<string, any>;
|
|
10
|
+
}): {
|
|
11
|
+
makeAwsClientConfig: <T extends Record<string, any>>(extraConfig?: T) => {
|
|
12
|
+
region: string;
|
|
13
|
+
endpoint: string | undefined;
|
|
14
|
+
credentials: import("@smithy/types").MemoizedProvider<import("@smithy/types").AwsCredentialIdentity>;
|
|
15
|
+
} & T;
|
|
16
|
+
makeS3ClientConfig: <T_1 extends Record<string, any>>(extraConfig?: T_1) => {
|
|
17
|
+
region: string;
|
|
18
|
+
endpoint: string | undefined;
|
|
19
|
+
credentials: import("@smithy/types").MemoizedProvider<import("@smithy/types").AwsCredentialIdentity>;
|
|
20
|
+
} & {
|
|
21
|
+
[x: string]: any;
|
|
22
|
+
} & T_1;
|
|
23
|
+
};
|
|
24
|
+
export {};
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeAwsConfigProvider = void 0;
|
|
4
|
+
const property_provider_1 = require("@smithy/property-provider");
|
|
5
|
+
// Attempt to refresh credentials 5 minutes before they actually expire.
|
|
6
|
+
// This value is the same value that the AWS SDK uses internally:
|
|
7
|
+
// https://github.com/aws/aws-sdk-js-v3/blob/3f8b581af7c0c8146c5b111f92ba6a024310c525/packages/middleware-signing/src/awsAuthConfiguration.ts#L18
|
|
8
|
+
const CREDENTIAL_EXPIRE_WINDOW = 300000;
|
|
9
|
+
function makeAwsConfigProvider({ credentials, getClientConfig, getS3ClientConfig, }) {
|
|
10
|
+
// Clients don't share credentials by default, which means that we'll flood
|
|
11
|
+
// the IMDS with requests for credentials if we construct and use a lot of
|
|
12
|
+
// clients in quick succession. IMDS has rate-limiting, so we'll end up failing
|
|
13
|
+
// to get credentials.
|
|
14
|
+
//
|
|
15
|
+
// To work around this, we'll share a single credential provider chain across
|
|
16
|
+
// all clients we create. We'll also memoize the credential provider chain so
|
|
17
|
+
// that we don't end up making unnecessarily many requests to the IMDS.
|
|
18
|
+
//
|
|
19
|
+
// Memoization is based on the following:
|
|
20
|
+
// https://github.com/aws/aws-sdk-js-v3/blob/3f8b581af7c0c8146c5b111f92ba6a024310c525/packages/middleware-signing/src/awsAuthConfiguration.ts#L257
|
|
21
|
+
const memoizedCredentials = (0, property_provider_1.memoize)(credentials, (credentials) => credentials.expiration !== undefined &&
|
|
22
|
+
credentials.expiration.getTime() - Date.now() < CREDENTIAL_EXPIRE_WINDOW, (credentials) => credentials.expiration !== undefined);
|
|
23
|
+
function makeAwsClientConfig(extraConfig = {}) {
|
|
24
|
+
return {
|
|
25
|
+
endpoint: process.env.AWS_ENDPOINT,
|
|
26
|
+
credentials: memoizedCredentials,
|
|
27
|
+
...getClientConfig(),
|
|
28
|
+
...extraConfig,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function makeS3ClientConfig(extraConfig = {}) {
|
|
32
|
+
return makeAwsClientConfig({
|
|
33
|
+
...(getS3ClientConfig?.() ?? {}),
|
|
34
|
+
...extraConfig,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
makeAwsClientConfig,
|
|
39
|
+
makeS3ClientConfig,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
exports.makeAwsConfigProvider = makeAwsConfigProvider;
|
|
43
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":";;;AAAA,iEAAoD;AAQpD,wEAAwE;AACxE,iEAAiE;AACjE,iJAAiJ;AACjJ,MAAM,wBAAwB,GAAG,MAAM,CAAC;AAExC,SAAgB,qBAAqB,CAAC,EACpC,WAAW,EACX,eAAe,EACf,iBAAiB,GAKlB;IACC,2EAA2E;IAC3E,0EAA0E;IAC1E,+EAA+E;IAC/E,sBAAsB;IACtB,EAAE;IACF,6EAA6E;IAC7E,6EAA6E;IAC7E,uEAAuE;IACvE,EAAE;IACF,yCAAyC;IACzC,kJAAkJ;IAClJ,MAAM,mBAAmB,GAAG,IAAA,2BAAO,EACjC,WAAW,EACX,CAAC,WAAW,EAAE,EAAE,CACd,WAAW,CAAC,UAAU,KAAK,SAAS;QACpC,WAAW,CAAC,UAAU,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,wBAAwB,EAC1E,CAAC,WAAW,EAAE,EAAE,CAAC,WAAW,CAAC,UAAU,KAAK,SAAS,CACtD,CAAC;IAEF,SAAS,mBAAmB,CAAgC,cAAiB,EAAO;QAClF,OAAO;YACL,QAAQ,EAAE,OAAO,CAAC,GAAG,CAAC,YAAY;YAClC,WAAW,EAAE,mBAAmB;YAChC,GAAG,eAAe,EAAE;YACpB,GAAG,WAAW;SACf,CAAC;IACJ,CAAC;IAED,SAAS,kBAAkB,CAAgC,cAAiB,EAAO;QACjF,OAAO,mBAAmB,CAAC;YACzB,GAAG,CAAC,iBAAiB,EAAE,EAAE,IAAI,EAAE,CAAC;YAChC,GAAG,WAAW;SACf,CAAC,CAAC;IACL,CAAC;IAED,OAAO;QACL,mBAAmB;QACnB,kBAAkB;KACnB,CAAC;AACJ,CAAC;AAhDD,sDAgDC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { makeAwsConfigProvider } from './config';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeAwsConfigProvider = void 0;
|
|
4
|
+
var config_1 = require("./config");
|
|
5
|
+
Object.defineProperty(exports, "makeAwsConfigProvider", { enumerable: true, get: function () { return config_1.makeAwsConfigProvider; } });
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,mCAAiD;AAAxC,+GAAA,qBAAqB,OAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@prairielearn/aws",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/PrairieLearn/PrairieLearn.git",
|
|
8
|
+
"directory": "packages/aws"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"dev": "tsc --watch --preserveWatchOutput"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@prairielearn/tsconfig": "*",
|
|
16
|
+
"@types/node": "^18.17.14",
|
|
17
|
+
"typescript": "^5.2.2"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@smithy/property-provider": "^2.0.7",
|
|
21
|
+
"@smithy/types": "^2.3.0"
|
|
22
|
+
}
|
|
23
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { memoize } from '@smithy/property-provider';
|
|
2
|
+
import { AwsCredentialIdentityProvider } from '@smithy/types';
|
|
3
|
+
|
|
4
|
+
interface AwsClientConfig {
|
|
5
|
+
region: string;
|
|
6
|
+
[key: string]: any;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// Attempt to refresh credentials 5 minutes before they actually expire.
|
|
10
|
+
// This value is the same value that the AWS SDK uses internally:
|
|
11
|
+
// https://github.com/aws/aws-sdk-js-v3/blob/3f8b581af7c0c8146c5b111f92ba6a024310c525/packages/middleware-signing/src/awsAuthConfiguration.ts#L18
|
|
12
|
+
const CREDENTIAL_EXPIRE_WINDOW = 300000;
|
|
13
|
+
|
|
14
|
+
export function makeAwsConfigProvider({
|
|
15
|
+
credentials,
|
|
16
|
+
getClientConfig,
|
|
17
|
+
getS3ClientConfig,
|
|
18
|
+
}: {
|
|
19
|
+
credentials: AwsCredentialIdentityProvider;
|
|
20
|
+
getClientConfig: () => AwsClientConfig;
|
|
21
|
+
getS3ClientConfig?: () => Record<string, any>;
|
|
22
|
+
}) {
|
|
23
|
+
// Clients don't share credentials by default, which means that we'll flood
|
|
24
|
+
// the IMDS with requests for credentials if we construct and use a lot of
|
|
25
|
+
// clients in quick succession. IMDS has rate-limiting, so we'll end up failing
|
|
26
|
+
// to get credentials.
|
|
27
|
+
//
|
|
28
|
+
// To work around this, we'll share a single credential provider chain across
|
|
29
|
+
// all clients we create. We'll also memoize the credential provider chain so
|
|
30
|
+
// that we don't end up making unnecessarily many requests to the IMDS.
|
|
31
|
+
//
|
|
32
|
+
// Memoization is based on the following:
|
|
33
|
+
// https://github.com/aws/aws-sdk-js-v3/blob/3f8b581af7c0c8146c5b111f92ba6a024310c525/packages/middleware-signing/src/awsAuthConfiguration.ts#L257
|
|
34
|
+
const memoizedCredentials = memoize(
|
|
35
|
+
credentials,
|
|
36
|
+
(credentials) =>
|
|
37
|
+
credentials.expiration !== undefined &&
|
|
38
|
+
credentials.expiration.getTime() - Date.now() < CREDENTIAL_EXPIRE_WINDOW,
|
|
39
|
+
(credentials) => credentials.expiration !== undefined,
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
function makeAwsClientConfig<T extends Record<string, any>>(extraConfig: T = {} as T) {
|
|
43
|
+
return {
|
|
44
|
+
endpoint: process.env.AWS_ENDPOINT,
|
|
45
|
+
credentials: memoizedCredentials,
|
|
46
|
+
...getClientConfig(),
|
|
47
|
+
...extraConfig,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function makeS3ClientConfig<T extends Record<string, any>>(extraConfig: T = {} as T) {
|
|
52
|
+
return makeAwsClientConfig({
|
|
53
|
+
...(getS3ClientConfig?.() ?? {}),
|
|
54
|
+
...extraConfig,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
makeAwsClientConfig,
|
|
60
|
+
makeS3ClientConfig,
|
|
61
|
+
};
|
|
62
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { makeAwsConfigProvider } from './config';
|