-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathImdsClient.h
386 lines (333 loc) · 18 KB
/
ImdsClient.h
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#pragma once
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/crt/DateTime.h>
#include <aws/crt/Exports.h>
#include <aws/crt/Types.h>
#include <functional>
struct aws_credentials;
struct aws_imds_client;
struct aws_imds_instance_info;
struct aws_imds_iam_profile;
namespace Aws
{
namespace Crt
{
namespace Io
{
class ClientBootstrap;
}
namespace Auth
{
class Credentials;
}
namespace Imds
{
struct AWS_CRT_CPP_API ImdsClientConfig
{
ImdsClientConfig() : Bootstrap(nullptr) {}
/**
* Connection bootstrap to use to create the http connection required to
* query resource from the Ec2 instance metadata service
*
* Note: If null, then the default ClientBootstrap is used
* (see Aws::Crt::ApiHandle::GetOrCreateStaticDefaultClientBootstrap)
*/
Io::ClientBootstrap *Bootstrap;
/* Should add retry strategy support once that is available */
};
/**
* https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-categories.html
*/
struct AWS_CRT_CPP_API IamProfileView
{
DateTime lastUpdated;
StringView instanceProfileArn;
StringView instanceProfileId;
};
/**
* A convenient class for you to persist data from IamProfileView, which has StringView members.
*/
struct AWS_CRT_CPP_API IamProfile
{
IamProfile() {}
IamProfile(const IamProfileView &other);
IamProfile &operator=(const IamProfileView &other);
DateTime lastUpdated;
String instanceProfileArn;
String instanceProfileId;
};
/**
* Block of per-instance EC2-specific data
*
* https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-identity-documents.html
*/
struct AWS_CRT_CPP_API InstanceInfoView
{
/* an array of StringView */
Vector<StringView> marketplaceProductCodes;
StringView availabilityZone;
StringView privateIp;
StringView version;
StringView instanceId;
/* an array of StringView */
Vector<StringView> billingProducts;
StringView instanceType;
StringView accountId;
StringView imageId;
DateTime pendingTime;
StringView architecture;
StringView kernelId;
StringView ramdiskId;
StringView region;
};
/**
* A convenient class for you to persist data from InstanceInfoView, which has StringView members.
*/
struct AWS_CRT_CPP_API InstanceInfo
{
InstanceInfo() {}
InstanceInfo(const InstanceInfoView &other);
InstanceInfo &operator=(const InstanceInfoView &other);
/* an array of StringView */
Vector<String> marketplaceProductCodes;
String availabilityZone;
String privateIp;
String version;
String instanceId;
/* an array of StringView */
Vector<String> billingProducts;
String instanceType;
String accountId;
String imageId;
DateTime pendingTime;
String architecture;
String kernelId;
String ramdiskId;
String region;
};
using OnResourceAcquired = std::function<void(const StringView &resource, int errorCode, void *userData)>;
using OnVectorResourceAcquired =
std::function<void(const Vector<StringView> &resource, int errorCode, void *userData)>;
using OnCredentialsAcquired =
std::function<void(const Auth::Credentials &credentials, int errorCode, void *userData)>;
using OnIamProfileAcquired =
std::function<void(const IamProfileView &iamProfile, int errorCode, void *userData)>;
using OnInstanceInfoAcquired =
std::function<void(const InstanceInfoView &instanceInfo, int errorCode, void *userData)>;
class AWS_CRT_CPP_API ImdsClient
{
public:
ImdsClient(const ImdsClientConfig &config, Allocator *allocator = ApiAllocator()) noexcept;
~ImdsClient();
ImdsClient(const ImdsClient &) = delete;
ImdsClient(ImdsClient &&) = delete;
ImdsClient &operator=(const ImdsClient &) = delete;
ImdsClient &operator=(ImdsClient &&) = delete;
aws_imds_client *GetUnderlyingHandle() { return m_client; }
/**
* Queries a generic resource (string) from the ec2 instance metadata document
*
* @param resourcePath path of the resource to query
* @param callback callback function to invoke on query success or failure
* @param userData opaque data to invoke the completion callback with
* @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
*/
int GetResource(const StringView &resourcePath, OnResourceAcquired callback, void *userData);
/**
* Gets the ami id of the ec2 instance from the instance metadata document
*
* @param callback callback function to invoke on query success or failure
* @param userData opaque data to invoke the completion callback with
* @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
*/
int GetAmiId(OnResourceAcquired callback, void *userData);
/**
* Gets the ami launch index of the ec2 instance from the instance metadata document
*
* @param callback callback function to invoke on query success or failure
* @param userData opaque data to invoke the completion callback with
* @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
*/
int GetAmiLaunchIndex(OnResourceAcquired callback, void *userData);
/**
* Gets the ami manifest path of the ec2 instance from the instance metadata document
*
* @param callback callback function to invoke on query success or failure
* @param userData opaque data to invoke the completion callback with
* @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
*/
int GetAmiManifestPath(OnResourceAcquired callback, void *userData);
/**
* Gets the list of ancestor ami ids of the ec2 instance from the instance metadata document
*
* @param callback callback function to invoke on query success or failure
* @param userData opaque data to invoke the completion callback with
* @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
*/
int GetAncestorAmiIds(OnVectorResourceAcquired callback, void *userData);
/**
* Gets the instance-action of the ec2 instance from the instance metadata document
*
* @param callback callback function to invoke on query success or failure
* @param userData opaque data to invoke the completion callback with
* @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
*/
int GetInstanceAction(OnResourceAcquired callback, void *userData);
/**
* Gets the instance id of the ec2 instance from the instance metadata document
*
* @param callback callback function to invoke on query success or failure
* @param userData opaque data to invoke the completion callback with
* @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
*/
int GetInstanceId(OnResourceAcquired callback, void *userData);
/**
* Gets the instance type of the ec2 instance from the instance metadata document
*
* @param callback callback function to invoke on query success or failure
* @param userData opaque data to invoke the completion callback with
* @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
*/
int GetInstanceType(OnResourceAcquired callback, void *userData);
/**
* Gets the mac address of the ec2 instance from the instance metadata document
*
* @param callback callback function to invoke on query success or failure
* @param userData opaque data to invoke the completion callback with
* @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
*/
int GetMacAddress(OnResourceAcquired callback, void *userData);
/**
* Gets the private ip address of the ec2 instance from the instance metadata document
*
* @param callback callback function to invoke on query success or failure
* @param userData opaque data to invoke the completion callback with
* @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
*/
int GetPrivateIpAddress(OnResourceAcquired callback, void *userData);
/**
* Gets the availability zone of the ec2 instance from the instance metadata document
*
* @param callback callback function to invoke on query success or failure
* @param userData opaque data to invoke the completion callback with
* @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
*/
int GetAvailabilityZone(OnResourceAcquired callback, void *userData);
/**
* Gets the product codes of the ec2 instance from the instance metadata document
*
* @param callback callback function to invoke on query success or failure
* @param userData opaque data to invoke the completion callback with
* @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
*/
int GetProductCodes(OnResourceAcquired callback, void *userData);
/**
* Gets the public key of the ec2 instance from the instance metadata document
*
* @param callback callback function to invoke on query success or failure
* @param userData opaque data to invoke the completion callback with
* @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
*/
int GetPublicKey(OnResourceAcquired callback, void *userData);
/**
* Gets the ramdisk id of the ec2 instance from the instance metadata document
*
* @param callback callback function to invoke on query success or failure
* @param userData opaque data to invoke the completion callback with
* @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
*/
int GetRamDiskId(OnResourceAcquired callback, void *userData);
/**
* Gets the reservation id of the ec2 instance from the instance metadata document
*
* @param callback callback function to invoke on query success or failure
* @param userData opaque data to invoke the completion callback with
* @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
*/
int GetReservationId(OnResourceAcquired callback, void *userData);
/**
* Gets the list of the security groups of the ec2 instance from the instance metadata document
*
* @param callback callback function to invoke on query success or failure
* @param userData opaque data to invoke the completion callback with
* @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
*/
int GetSecurityGroups(OnVectorResourceAcquired callback, void *userData);
/**
* Gets the list of block device mappings of the ec2 instance from the instance metadata document
*
* @param callback callback function to invoke on query success or failure
* @param userData opaque data to invoke the completion callback with
* @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
*/
int GetBlockDeviceMapping(OnVectorResourceAcquired callback, void *userData);
/**
* Gets the attached iam role of the ec2 instance from the instance metadata document
*
* @param callback callback function to invoke on query success or failure
* @param userData opaque data to invoke the completion callback with
* @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
*/
int GetAttachedIamRole(OnResourceAcquired callback, void *userData);
/**
* Gets temporary credentials based on the attached iam role of the ec2 instance
*
* @param iamRoleName iam role name to get temporary credentials through
* @param callback callback function to invoke on query success or failure
* @param userData opaque data to invoke the completion callback with
* @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
*/
int GetCredentials(const StringView &iamRoleName, OnCredentialsAcquired callback, void *userData);
/**
* Gets the iam profile information of the ec2 instance from the instance metadata document
*
* @param callback callback function to invoke on query success or failure
* @param userData opaque data to invoke the completion callback with
* @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
*/
int GetIamProfile(OnIamProfileAcquired callback, void *userData);
/**
* Gets the user data of the ec2 instance from the instance metadata document
*
* @param callback callback function to invoke on query success or failure
* @param userData opaque data to invoke the completion callback with
* @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
*/
int GetUserData(OnResourceAcquired callback, void *userData);
/**
* Gets the signature of the ec2 instance from the instance metadata document
*
* @param callback callback function to invoke on query success or failure
* @param userData opaque data to invoke the completion callback with
* @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
*/
int GetInstanceSignature(OnResourceAcquired callback, void *userData);
/**
* Gets the instance information data block of the ec2 instance from the instance metadata document
*
* @param callback callback function to invoke on query success or failure
* @param userData opaque data to invoke the completion callback with
* @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
*/
int GetInstanceInfo(OnInstanceInfoAcquired callback, void *userData);
private:
static void s_onResourceAcquired(const aws_byte_buf *resource, int erroCode, void *userData);
static void s_onVectorResourceAcquired(const aws_array_list *array, int errorCode, void *userData);
static void s_onCredentialsAcquired(const aws_credentials *credentials, int errorCode, void *userData);
static void s_onIamProfileAcquired(
const aws_imds_iam_profile *iamProfileInfo,
int errorCode,
void *userData);
static void s_onInstanceInfoAcquired(
const aws_imds_instance_info *instanceInfo,
int error_code,
void *userData);
aws_imds_client *m_client;
Allocator *m_allocator;
};
} // namespace Imds
} // namespace Crt
} // namespace Aws