-
Notifications
You must be signed in to change notification settings - Fork 29
/
config.ts
180 lines (159 loc) · 4.81 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { HTTPScheme } from './http'
import { Endpoint } from './endpoint'
/** Class holding an AWS connection information */
export class AWSConfig {
/**
* The AWS region to connect to, as listed: https://docs.aws.amazon.com/general/latest/gr/rande.html
*
* @type {string}
*/
region: string
/**
* Your user's AWS access key id credential.
*
* @type {string}
*/
accessKeyId: string
/**
* Your user's AWS secret access key credential.
*
* @type {string}
*/
secretAccessKey: string
/**
* Your user's AWS session token credential.
*
* @type {string}
*/
sessionToken?: string
/**
* The AWS hostname to connect to.
*
* @type {string} ['amazonaws.com']
*/
endpoint?: Endpoint
/**
* fromEnvironment creates an AWSConfig from the environment variables.
*
* It expects to find the following compulsory environment variables:
* * AWS_REGION
* * AWS_ACCESS_KEY_ID
* * AWS_SECRET_ACCESS_KEY
*
* If set, the following optional environment variables are also used:
* * AWS_SESSION_TOKEN
*
* Finally, the options parameter allows to explicitly set the scheme and endpoint
* to use when connecting to AWS.
*
* @param options {AWSConnectionOptions}
* @returns
*/
static fromEnvironment(options?: AWSConnectionOptions): AWSConfig {
const region = __ENV.AWS_REGION
const accessKeyId = __ENV.AWS_ACCESS_KEY_ID
const secretAccessKey = __ENV.AWS_SECRET_ACCESS_KEY
const sessionToken: string | undefined = __ENV.AWS_SESSION_TOKEN
const endpoint: Endpoint | string | undefined = options?.endpoint
return new AWSConfig({
region,
accessKeyId,
secretAccessKey,
sessionToken,
endpoint: endpoint,
})
}
/**
* Create an AWSConfig.
*
* @param {AWSConfigOptions} options - configuration attributes to use when interacting with AWS' APIs
* @throws {InvalidArgumentException}
*/
constructor(options: AWSConfigOptions) {
if (!options.region || options.region === '') {
throw new InvalidAWSConfigError(
`invalid AWS region; reason: expected a valid AWS region name (e.g. "us-east-1"), got \`${options.region}\``
)
}
if (!options.accessKeyId || options.accessKeyId === '') {
throw new InvalidAWSConfigError(
`invalid AWS access key ID; reason: expected a non empty string, got \`${options.accessKeyId}\``
)
}
if (options.accessKeyId.length < 16 || options.accessKeyId.length > 128) {
throw new InvalidAWSConfigError(
`invalid AWS access key ID; reason: size should be between 16 and 128 characters, got ${options.accessKeyId.length}`
)
}
if (!options.secretAccessKey || options.secretAccessKey === '') {
throw new InvalidAWSConfigError(
`invalid AWS secret access key; reason: expected a non empty string, got \`${options.secretAccessKey}\``
)
}
this.region = options.region
this.accessKeyId = options.accessKeyId
this.secretAccessKey = options.secretAccessKey
if (options.sessionToken !== undefined) {
this.sessionToken = options.sessionToken
}
if (options.endpoint !== undefined) {
if (typeof options.endpoint === 'string') {
this.endpoint = new Endpoint(options.endpoint)
} else {
this.endpoint = options.endpoint
}
}
}
}
/**
* Interface representing AWSConfig options
*/
export interface AWSConfigOptions extends AWSConnectionOptions {
/**
* The AWS region to connect to, as listed: https://docs.aws.amazon.com/general/latest/gr/rande.html
*
* @type {string}
*/
region: string
/**
* Your user's AWS access key id credential.
*
* @type {string}
*/
accessKeyId: string
/**
* Your user's AWS secret access key credential.
*
* @type {string}
*/
secretAccessKey: string
/**
* Your user's AWS session token credential.
*
* @type {string}
*/
sessionToken?: string
}
/**
* Interface representing AWS connection options
*/
export interface AWSConnectionOptions {
/**
* The HTTP scheme to use when connecting to AWS.
*
* @type {HTTPScheme}
*/
scheme?: HTTPScheme
/**
* The AWS hostname to connect to.
*
* @type {string}
*/
endpoint?: Endpoint | string
}
/** Class representing an invalid AWS configuration */
export class InvalidAWSConfigError extends Error {
constructor(message: string) {
super(message)
}
}