Debian Codegraph @

crt/aws-c-auth/include/aws/auth/aws_imds_client.h

raw symbols SCIP
1 #ifndef AWS_AUTH_IMDS_CLIENT_H
2 #define AWS_AUTH_IMDS_CLIENT_H
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8 #include <aws/auth/auth.h>
9 #include <aws/auth/credentials.h>
10 #include <aws/common/array_list.h>
11 #include <aws/common/date_time.h>
12 #include <aws/http/connection_manager.h>
13 #include <aws/io/retry_strategy.h>
14
15 AWS_PUSH_SANE_WARNING_LEVEL
16
17 typedef void(aws_imds_client_shutdown_completed_fn)(void *user_data);
18
19 /**
20 * Optional callback and user data to be invoked when an imds client has fully shut down
21 */
22 struct aws_imds_client_shutdown_options {
23 aws_imds_client_shutdown_completed_fn *shutdown_callback;
24 void *shutdown_user_data;
25 };
26
27 /**
28 * Configuration options when creating an imds client
29 */
30 struct aws_imds_client_options {
31 /*
32 * Completion callback to be invoked when the client has fully shut down
33 */
34 struct aws_imds_client_shutdown_options shutdown_options;
35
36 /*
37 * Client bootstrap to use when this client makes network connections
38 */
39 struct aws_client_bootstrap *bootstrap;
40
41 /*
42 * Retry strategy instance that governs how failed requests are retried
43 */
44 struct aws_retry_strategy *retry_strategy;
45
46 /*
47 * (Optional) Settings propagated down to http connection manager to choose proxy options from environment. Read
48 * aws_http_credentials_provider.h for more information.
49 */
50 const struct proxy_env_var_settings *proxy_ev_settings;
51
52 /*
53 * What version of the imds protocol to use
54 *
55 * Defaults to IMDS_PROTOCOL_V2
56 */
57 enum aws_imds_protocol_version imds_version;
58
59 /*
60 * If true, fallback from v2 to v1 will be disabled for all cases
61 */
62 bool ec2_metadata_v1_disabled;
63
64 /*
65 * (Optional) Override the default IMDS endpoint.
66 * If this cursor is empty (ptr == NULL or len == 0), the endpoint will be determined by:
67 * 1. AWS_EC2_METADATA_SERVICE_ENDPOINT environment variable (if set)
68 * 2. Default endpoint based on imds_endpoint_mode (IPv4: "http://169.254.169.254", IPv6: "http://[fd00:ec2::254]")
69 *
70 * Should be a valid URI string. Supports both HTTP and HTTPS schemes:
71 * - HTTP: Uses default port 80 if not specified
72 * - HTTPS: Uses default port 443 if not specified, with automatic TLS configuration
73 * HTTPS connections use default client TLS settings with SNI hostname set automatically.
74 * Example: aws_byte_cursor_from_c_str("http://127.0.0.1:8080")
75 */
76 struct aws_byte_cursor imds_endpoint;
77
78 /*
79 * (Optional) IP address mode for IMDS connections.
80 * If set to AWS_IMDS_ENDPOINT_MODE_DEFAULT (0), the mode will be determined by:
81 * 1. AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE environment variable (if set to "IPv4" or "IPv6")
82 * 2. Default mode (IPv4)
83 *
84 * AWS_IMDS_ENDPOINT_MODE_DEFAULT (0) - Use environment variable or default to IPv4
85 * AWS_IMDS_ENDPOINT_MODE_IPV4 - Explicitly use IPv4
86 * AWS_IMDS_ENDPOINT_MODE_IPV6 - Explicitly use IPv6
87 */
88 enum aws_imds_endpoint_mode imds_endpoint_mode;
89
90 /*
91 * Table holding all cross-system functional dependencies for an imds client.
92 *
93 * For mocking the http layer in tests, leave NULL otherwise
94 */
95 struct aws_auth_http_system_vtable *function_table;
96 };
97
98 /*
99 * Standard callback for instance metadata queries
100 */
101 typedef void(
102 aws_imds_client_on_get_resource_callback_fn)(const struct aws_byte_buf *resource, int error_code, void *user_data);
103
104 /**
105 * https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-categories.html
106 */
107 struct aws_imds_iam_profile {
108 struct aws_date_time last_updated;
109 struct aws_byte_cursor instance_profile_arn;
110 struct aws_byte_cursor instance_profile_id;
111 };
112
113 /**
114 * Block of per-instance EC2-specific data
115 *
116 * https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-identity-documents.html
117 */
118 struct aws_imds_instance_info {
119 /* an array of aws_byte_cursor */
120 struct aws_array_list marketplace_product_codes;
121 struct aws_byte_cursor availability_zone;
122 struct aws_byte_cursor private_ip;
123 struct aws_byte_cursor version;
124 struct aws_byte_cursor instance_id;
125 /* an array of aws_byte_cursor */
126 struct aws_array_list billing_products;
127 struct aws_byte_cursor instance_type;
128 struct aws_byte_cursor account_id;
129 struct aws_byte_cursor image_id;
130 struct aws_date_time pending_time;
131 struct aws_byte_cursor architecture;
132 struct aws_byte_cursor kernel_id;
133 struct aws_byte_cursor ramdisk_id;
134 struct aws_byte_cursor region;
135 };
136
137 /* the item typed stored in array is pointer to aws_byte_cursor */
138 typedef void(
139 aws_imds_client_on_get_array_callback_fn)(const struct aws_array_list *array, int error_code, void *user_data);
140
141 typedef void(aws_imds_client_on_get_credentials_callback_fn)(
142 const struct aws_credentials *credentials,
143 int error_code,
144 void *user_data);
145
146 typedef void(aws_imds_client_on_get_iam_profile_callback_fn)(
147 const struct aws_imds_iam_profile *iam_profile_info,
148 int error_code,
149 void *user_data);
150
151 typedef void(aws_imds_client_on_get_instance_info_callback_fn)(
152 const struct aws_imds_instance_info *instance_info,
153 int error_code,
154 void *user_data);
155
156 /**
157 * AWS EC2 Metadata Client is used to retrieve AWS EC2 Instance Metadata info.
158 */
159 struct aws_imds_client;
160
161 AWS_EXTERN_C_BEGIN
162
163 /**
164 * Creates a new imds client
165 *
166 * @param allocator memory allocator to use for creation and queries
167 * @param options configuration options for the imds client
168 *
169 * @return a newly-constructed imds client, or NULL on failure
170 */
171 AWS_AUTH_API
172 struct aws_imds_client *aws_imds_client_new(
173 struct aws_allocator *allocator,
174 const struct aws_imds_client_options *options);
175
176 /**
177 * Increments the ref count on the client
178 *
179 * @param client imds client to acquire a reference to
180 */
181 AWS_AUTH_API
182 void aws_imds_client_acquire(struct aws_imds_client *client);
183
184 /**
185 * Decrements the ref count on the client
186 *
187 * @param client imds client to release a reference to
188 */
189 AWS_AUTH_API
190 void aws_imds_client_release(struct aws_imds_client *client);
191
192 /**
193 * Queries a generic resource (string) from the ec2 instance metadata document
194 *
195 * @param client imds client to use for the query
196 * @param resource_path path of the resource to query
197 * @param callback callback function to invoke on query success or failure
198 * @param user_data opaque data to invoke the completion callback with
199 * @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
200 */
201 AWS_AUTH_API
202 int aws_imds_client_get_resource_async(
203 struct aws_imds_client *client,
204 struct aws_byte_cursor resource_path,
205 aws_imds_client_on_get_resource_callback_fn callback,
206 void *user_data);
207
208 /**
209 * Gets the ami id of the ec2 instance from the instance metadata document
210 *
211 * @param client imds client to use for the query
212 * @param callback callback function to invoke on query success or failure
213 * @param user_data opaque data to invoke the completion callback with
214 * @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
215 */
216 AWS_AUTH_API
217 int aws_imds_client_get_ami_id(
218 struct aws_imds_client *client,
219 aws_imds_client_on_get_resource_callback_fn callback,
220 void *user_data);
221
222 /**
223 * Gets the ami launch index of the ec2 instance from the instance metadata document
224 *
225 * @param client imds client to use for the query
226 * @param callback callback function to invoke on query success or failure
227 * @param user_data opaque data to invoke the completion callback with
228 * @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
229 */
230 AWS_AUTH_API
231 int aws_imds_client_get_ami_launch_index(
232 struct aws_imds_client *client,
233 aws_imds_client_on_get_resource_callback_fn callback,
234 void *user_data);
235
236 /**
237 * Gets the ami manifest path of the ec2 instance from the instance metadata document
238 *
239 * @param client imds client to use for the query
240 * @param callback callback function to invoke on query success or failure
241 * @param user_data opaque data to invoke the completion callback with
242 * @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
243 */
244 AWS_AUTH_API
245 int aws_imds_client_get_ami_manifest_path(
246 struct aws_imds_client *client,
247 aws_imds_client_on_get_resource_callback_fn callback,
248 void *user_data);
249
250 /**
251 * Gets the list of ancestor ami ids of the ec2 instance from the instance metadata document
252 *
253 * @param client imds client to use for the query
254 * @param callback callback function to invoke on query success or failure
255 * @param user_data opaque data to invoke the completion callback with
256 * @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
257 */
258 AWS_AUTH_API
259 int aws_imds_client_get_ancestor_ami_ids(
260 struct aws_imds_client *client,
261 aws_imds_client_on_get_array_callback_fn callback,
262 void *user_data);
263
264 /**
265 * Gets the instance-action of the ec2 instance from the instance metadata document
266 *
267 * @param client imds client to use for the query
268 * @param callback callback function to invoke on query success or failure
269 * @param user_data opaque data to invoke the completion callback with
270 * @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
271 */
272 AWS_AUTH_API
273 int aws_imds_client_get_instance_action(
274 struct aws_imds_client *client,
275 aws_imds_client_on_get_resource_callback_fn callback,
276 void *user_data);
277
278 /**
279 * Gets the instance id of the ec2 instance from the instance metadata document
280 *
281 * @param client imds client to use for the query
282 * @param callback callback function to invoke on query success or failure
283 * @param user_data opaque data to invoke the completion callback with
284 * @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
285 */
286 AWS_AUTH_API
287 int aws_imds_client_get_instance_id(
288 struct aws_imds_client *client,
289 aws_imds_client_on_get_resource_callback_fn callback,
290 void *user_data);
291
292 /**
293 * Gets the instance type of the ec2 instance from the instance metadata document
294 *
295 * @param client imds client to use for the query
296 * @param callback callback function to invoke on query success or failure
297 * @param user_data opaque data to invoke the completion callback with
298 * @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
299 */
300 AWS_AUTH_API
301 int aws_imds_client_get_instance_type(
302 struct aws_imds_client *client,
303 aws_imds_client_on_get_resource_callback_fn callback,
304 void *user_data);
305
306 /**
307 * Gets the mac address of the ec2 instance from the instance metadata document
308 *
309 * @param client imds client to use for the query
310 * @param callback callback function to invoke on query success or failure
311 * @param user_data opaque data to invoke the completion callback with
312 * @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
313 */
314 AWS_AUTH_API
315 int aws_imds_client_get_mac_address(
316 struct aws_imds_client *client,
317 aws_imds_client_on_get_resource_callback_fn callback,
318 void *user_data);
319
320 /**
321 * Gets the private ip address of the ec2 instance from the instance metadata document
322 *
323 * @param client imds client to use for the query
324 * @param callback callback function to invoke on query success or failure
325 * @param user_data opaque data to invoke the completion callback with
326 * @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
327 */
328 AWS_AUTH_API
329 int aws_imds_client_get_private_ip_address(
330 struct aws_imds_client *client,
331 aws_imds_client_on_get_resource_callback_fn callback,
332 void *user_data);
333
334 /**
335 * Gets the availability zone of the ec2 instance from the instance metadata document
336 *
337 * @param client imds client to use for the query
338 * @param callback callback function to invoke on query success or failure
339 * @param user_data opaque data to invoke the completion callback with
340 * @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
341 */
342 AWS_AUTH_API
343 int aws_imds_client_get_availability_zone(
344 struct aws_imds_client *client,
345 aws_imds_client_on_get_resource_callback_fn callback,
346 void *user_data);
347
348 /**
349 * Gets the product codes of the ec2 instance from the instance metadata document
350 *
351 * @param client imds client to use for the query
352 * @param callback callback function to invoke on query success or failure
353 * @param user_data opaque data to invoke the completion callback with
354 * @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
355 */
356 AWS_AUTH_API
357 int aws_imds_client_get_product_codes(
358 struct aws_imds_client *client,
359 aws_imds_client_on_get_resource_callback_fn callback,
360 void *user_data);
361
362 /**
363 * Gets the public key of the ec2 instance from the instance metadata document
364 *
365 * @param client imds client to use for the query
366 * @param callback callback function to invoke on query success or failure
367 * @param user_data opaque data to invoke the completion callback with
368 * @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
369 */
370 AWS_AUTH_API
371 int aws_imds_client_get_public_key(
372 struct aws_imds_client *client,
373 aws_imds_client_on_get_resource_callback_fn callback,
374 void *user_data);
375
376 /**
377 * Gets the ramdisk id of the ec2 instance from the instance metadata document
378 *
379 * @param client imds client to use for the query
380 * @param callback callback function to invoke on query success or failure
381 * @param user_data opaque data to invoke the completion callback with
382 * @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
383 */
384 AWS_AUTH_API
385 int aws_imds_client_get_ramdisk_id(
386 struct aws_imds_client *client,
387 aws_imds_client_on_get_resource_callback_fn callback,
388 void *user_data);
389
390 /**
391 * Gets the reservation id of the ec2 instance from the instance metadata document
392 *
393 * @param client imds client to use for the query
394 * @param callback callback function to invoke on query success or failure
395 * @param user_data opaque data to invoke the completion callback with
396 * @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
397 */
398 AWS_AUTH_API
399 int aws_imds_client_get_reservation_id(
400 struct aws_imds_client *client,
401 aws_imds_client_on_get_resource_callback_fn callback,
402 void *user_data);
403
404 /**
405 * Gets the list of the security groups of the ec2 instance from the instance metadata document
406 *
407 * @param client imds client to use for the query
408 * @param callback callback function to invoke on query success or failure
409 * @param user_data opaque data to invoke the completion callback with
410 * @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
411 */
412 AWS_AUTH_API
413 int aws_imds_client_get_security_groups(
414 struct aws_imds_client *client,
415 aws_imds_client_on_get_array_callback_fn callback,
416 void *user_data);
417
418 /**
419 * Gets the list of block device mappings of the ec2 instance from the instance metadata document
420 *
421 * @param client imds client to use for the query
422 * @param callback callback function to invoke on query success or failure
423 * @param user_data opaque data to invoke the completion callback with
424 * @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
425 */
426 AWS_AUTH_API
427 int aws_imds_client_get_block_device_mapping(
428 struct aws_imds_client *client,
429 aws_imds_client_on_get_array_callback_fn callback,
430 void *user_data);
431
432 /**
433 * Gets the attached iam role of the ec2 instance from the instance metadata document
434 *
435 * @param client imds client to use for the query
436 * @param callback callback function to invoke on query success or failure
437 * @param user_data opaque data to invoke the completion callback with
438 * @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
439 */
440 AWS_AUTH_API
441 int aws_imds_client_get_attached_iam_role(
442 struct aws_imds_client *client,
443 aws_imds_client_on_get_resource_callback_fn callback,
444 void *user_data);
445
446 /**
447 * Gets temporary credentials based on the attached iam role of the ec2 instance
448 *
449 * @param client imds client to use for the query
450 * @param iam_role_name iam role name to get temporary credentials through
451 * @param callback callback function to invoke on query success or failure
452 * @param user_data opaque data to invoke the completion callback with
453 * @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
454 */
455 AWS_AUTH_API
456 int aws_imds_client_get_credentials(
457 struct aws_imds_client *client,
458 struct aws_byte_cursor iam_role_name,
459 aws_imds_client_on_get_credentials_callback_fn callback,
460 void *user_data);
461
462 /**
463 * Gets the iam profile information of the ec2 instance from the instance metadata document
464 *
465 * @param client imds client to use for the query
466 * @param callback callback function to invoke on query success or failure
467 * @param user_data opaque data to invoke the completion callback with
468 * @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
469 */
470 AWS_AUTH_API
471 int aws_imds_client_get_iam_profile(
472 struct aws_imds_client *client,
473 aws_imds_client_on_get_iam_profile_callback_fn callback,
474 void *user_data);
475
476 /**
477 * Gets the user data of the ec2 instance from the instance metadata document
478 *
479 * @param client imds client to use for the query
480 * @param callback callback function to invoke on query success or failure
481 * @param user_data opaque data to invoke the completion callback with
482 * @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
483 */
484 AWS_AUTH_API
485 int aws_imds_client_get_user_data(
486 struct aws_imds_client *client,
487 aws_imds_client_on_get_resource_callback_fn callback,
488 void *user_data);
489
490 /**
491 * Gets the signature of the ec2 instance from the instance metadata document
492 *
493 * @param client imds client to use for the query
494 * @param callback callback function to invoke on query success or failure
495 * @param user_data opaque data to invoke the completion callback with
496 * @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
497 */
498 AWS_AUTH_API
499 int aws_imds_client_get_instance_signature(
500 struct aws_imds_client *client,
501 aws_imds_client_on_get_resource_callback_fn callback,
502 void *user_data);
503
504 /**
505 * Gets the instance information data block of the ec2 instance from the instance metadata document
506 *
507 * @param client imds client to use for the query
508 * @param callback callback function to invoke on query success or failure
509 * @param user_data opaque data to invoke the completion callback with
510 * @return AWS_OP_SUCCESS if the query was successfully started, AWS_OP_ERR otherwise
511 */
512 AWS_AUTH_API
513 int aws_imds_client_get_instance_info(
514 struct aws_imds_client *client,
515 aws_imds_client_on_get_instance_info_callback_fn callback,
516 void *user_data);
517
518 AWS_EXTERN_C_END
519 AWS_POP_SANE_WARNING_LEVEL
520
521 #endif /* AWS_AUTH_IMDS_CLIENT_H */