-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathapi-key.ts
261 lines (229 loc) · 7.25 KB
/
api-key.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import * as iam from '@aws-cdk/aws-iam';
import { ArnFormat, IResource as IResourceBase, Resource, Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnApiKey } from './apigateway.generated';
import { ResourceOptions } from './resource';
import { IRestApi } from './restapi';
import { QuotaSettings, ThrottleSettings, UsagePlan, UsagePlanPerApiStage } from './usage-plan';
/**
* API keys are alphanumeric string values that you distribute to
* app developer customers to grant access to your API
*/
export interface IApiKey extends IResourceBase {
/**
* The API key ID.
* @attribute
*/
readonly keyId: string;
/**
* The API key ARN.
*/
readonly keyArn: string;
}
/**
* The options for creating an API Key.
*/
export interface ApiKeyOptions extends ResourceOptions {
/**
* A name for the API key. If you don't specify a name, AWS CloudFormation generates a unique physical ID and uses that ID for the API key name.
* @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-apikey.html#cfn-apigateway-apikey-name
* @default automically generated name
*/
readonly apiKeyName?: string;
/**
* The value of the API key. Must be at least 20 characters long.
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-apikey.html#cfn-apigateway-apikey-value
* @default none
*/
readonly value?: string;
/**
* A description of the purpose of the API key.
* @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-apikey.html#cfn-apigateway-apikey-description
* @default none
*/
readonly description?: string;
}
/**
* ApiKey Properties.
*/
export interface ApiKeyProps extends ApiKeyOptions {
/**
* A list of resources this api key is associated with.
* @default none
*/
readonly resources?: IRestApi[];
/**
* An AWS Marketplace customer identifier to use when integrating with the AWS SaaS Marketplace.
* @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-apikey.html#cfn-apigateway-apikey-customerid
* @default none
*/
readonly customerId?: string;
/**
* Indicates whether the API key can be used by clients.
* @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-apikey.html#cfn-apigateway-apikey-enabled
* @default true
*/
readonly enabled?: boolean;
/**
* Specifies whether the key identifier is distinct from the created API key value.
* @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-apikey.html#cfn-apigateway-apikey-generatedistinctid
* @default false
*/
readonly generateDistinctId?: boolean;
}
/**
* Base implementation that is common to the various implementations of IApiKey
*/
abstract class ApiKeyBase extends Resource implements IApiKey {
public abstract readonly keyId: string;
public abstract readonly keyArn: string;
/**
* Permits the IAM principal all read operations through this key
*
* @param grantee The principal to grant access to
*/
public grantRead(grantee: iam.IGrantable): iam.Grant {
return iam.Grant.addToPrincipal({
grantee,
actions: readPermissions,
resourceArns: [this.keyArn],
});
}
/**
* Permits the IAM principal all write operations through this key
*
* @param grantee The principal to grant access to
*/
public grantWrite(grantee: iam.IGrantable): iam.Grant {
return iam.Grant.addToPrincipal({
grantee,
actions: writePermissions,
resourceArns: [this.keyArn],
});
}
/**
* Permits the IAM principal all read and write operations through this key
*
* @param grantee The principal to grant access to
*/
public grantReadWrite(grantee: iam.IGrantable): iam.Grant {
return iam.Grant.addToPrincipal({
grantee,
actions: [...readPermissions, ...writePermissions],
resourceArns: [this.keyArn],
});
}
}
/**
* An API Gateway ApiKey.
*
* An ApiKey can be distributed to API clients that are executing requests
* for Method resources that require an Api Key.
*/
export class ApiKey extends ApiKeyBase {
/**
* Import an ApiKey by its Id
*/
public static fromApiKeyId(scope: Construct, id: string, apiKeyId: string): IApiKey {
class Import extends ApiKeyBase {
public keyId = apiKeyId;
public keyArn = Stack.of(this).formatArn({
service: 'apigateway',
account: '',
resource: '/apikeys',
arnFormat: ArnFormat.SLASH_RESOURCE_NAME,
resourceName: apiKeyId,
});
}
return new Import(scope, id);
}
public readonly keyId: string;
public readonly keyArn: string;
constructor(scope: Construct, id: string, props: ApiKeyProps = { }) {
super(scope, id, {
physicalName: props.apiKeyName,
});
const resource = new CfnApiKey(this, 'Resource', {
customerId: props.customerId,
description: props.description,
enabled: props.enabled ?? true,
generateDistinctId: props.generateDistinctId,
name: this.physicalName,
stageKeys: this.renderStageKeys(props.resources),
value: props.value,
});
this.keyId = resource.ref;
this.keyArn = Stack.of(this).formatArn({
service: 'apigateway',
account: '',
resource: '/apikeys',
arnFormat: ArnFormat.SLASH_RESOURCE_NAME,
resourceName: this.keyId,
});
}
private renderStageKeys(resources: IRestApi[] | undefined): CfnApiKey.StageKeyProperty[] | undefined {
if (!resources) {
return undefined;
}
return resources.map((resource: IRestApi) => {
const restApi = resource;
const restApiId = restApi.restApiId;
const stageName = restApi.deploymentStage!.stageName.toString();
return { restApiId, stageName };
});
}
}
/**
* RateLimitedApiKey properties.
*/
export interface RateLimitedApiKeyProps extends ApiKeyProps {
/**
* API Stages to be associated with the RateLimitedApiKey.
* @default none
*/
readonly apiStages?: UsagePlanPerApiStage[];
/**
* Number of requests clients can make in a given time period.
* @default none
*/
readonly quota?: QuotaSettings;
/**
* Overall throttle settings for the API.
* @default none
*/
readonly throttle?: ThrottleSettings;
}
/**
* An API Gateway ApiKey, for which a rate limiting configuration can be specified.
*
* @resource AWS::ApiGateway::ApiKey
*/
export class RateLimitedApiKey extends ApiKeyBase {
public readonly keyId: string;
public readonly keyArn: string;
constructor(scope: Construct, id: string, props: RateLimitedApiKeyProps = { }) {
super(scope, id, {
physicalName: props.apiKeyName,
});
const resource = new ApiKey(this, 'Resource', props);
if (props.apiStages || props.quota || props.throttle) {
const usageplan = new UsagePlan(this, 'UsagePlanResource', {
apiStages: props.apiStages,
quota: props.quota,
throttle: props.throttle,
});
usageplan.addApiKey(resource);
}
this.keyId = resource.keyId;
this.keyArn = resource.keyArn;
}
}
const readPermissions = [
'apigateway:GET',
];
const writePermissions = [
'apigateway:POST',
'apigateway:PUT',
'apigateway:PATCH',
'apigateway:DELETE',
];