ImdsClient.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/auth/aws_imds_client.h>
  6. #include <aws/auth/credentials.h>
  7. #include <aws/crt/Api.h>
  8. #include <aws/crt/ImdsClient.h>
  9. #include <aws/crt/auth/Credentials.h>
  10. #include <aws/crt/http/HttpConnection.h>
  11. #include <aws/crt/io/Bootstrap.h>
  12. namespace Aws
  13. {
  14. namespace Crt
  15. {
  16. namespace Imds
  17. {
  18. IamProfile::IamProfile(const IamProfileView &other)
  19. : lastUpdated(other.lastUpdated),
  20. instanceProfileArn(other.instanceProfileArn.data(), other.instanceProfileArn.size()),
  21. instanceProfileId(other.instanceProfileId.data(), other.instanceProfileId.size())
  22. {
  23. }
  24. IamProfile &IamProfile::operator=(const IamProfileView &other)
  25. {
  26. lastUpdated = other.lastUpdated;
  27. instanceProfileArn = String(other.instanceProfileArn.data(), other.instanceProfileArn.size());
  28. instanceProfileId = String(other.instanceProfileId.data(), other.instanceProfileId.size());
  29. return *this;
  30. }
  31. InstanceInfo::InstanceInfo(const InstanceInfoView &other)
  32. : availabilityZone(other.availabilityZone.data(), other.availabilityZone.size()),
  33. privateIp(other.privateIp.data(), other.privateIp.size()),
  34. version(other.version.data(), other.version.size()),
  35. instanceId(other.instanceId.data(), other.instanceId.size()),
  36. instanceType(other.instanceType.data(), other.instanceType.size()),
  37. accountId(other.accountId.data(), other.accountId.size()),
  38. imageId(other.imageId.data(), other.imageId.size()), pendingTime(other.pendingTime),
  39. architecture(other.architecture.data(), other.architecture.size()),
  40. kernelId(other.kernelId.data(), other.kernelId.size()),
  41. ramdiskId(other.ramdiskId.data(), other.ramdiskId.size()),
  42. region(other.region.data(), other.region.size())
  43. {
  44. for (const auto &m : other.marketplaceProductCodes)
  45. {
  46. marketplaceProductCodes.emplace_back(m.data(), m.size());
  47. }
  48. for (const auto &m : other.billingProducts)
  49. {
  50. billingProducts.emplace_back(m.data(), m.size());
  51. }
  52. }
  53. InstanceInfo &InstanceInfo::operator=(const InstanceInfoView &other)
  54. {
  55. availabilityZone = {other.availabilityZone.data(), other.availabilityZone.size()};
  56. privateIp = {other.privateIp.data(), other.privateIp.size()};
  57. version = {other.version.data(), other.version.size()};
  58. instanceId = {other.instanceId.data(), other.instanceId.size()};
  59. instanceType = {other.instanceType.data(), other.instanceType.size()};
  60. accountId = {other.accountId.data(), other.accountId.size()};
  61. imageId = {other.imageId.data(), other.imageId.size()};
  62. pendingTime = other.pendingTime;
  63. architecture = {other.architecture.data(), other.architecture.size()};
  64. kernelId = {other.kernelId.data(), other.kernelId.size()};
  65. ramdiskId = {other.ramdiskId.data(), other.ramdiskId.size()};
  66. region = {other.region.data(), other.region.size()};
  67. for (const auto &m : other.marketplaceProductCodes)
  68. {
  69. marketplaceProductCodes.emplace_back(m.data(), m.size());
  70. }
  71. for (const auto &m : other.billingProducts)
  72. {
  73. billingProducts.emplace_back(m.data(), m.size());
  74. }
  75. return *this;
  76. }
  77. ImdsClient::ImdsClient(const ImdsClientConfig &config, Allocator *allocator) noexcept
  78. {
  79. struct aws_imds_client_options raw_config;
  80. AWS_ZERO_STRUCT(raw_config);
  81. if (config.Bootstrap != nullptr)
  82. {
  83. raw_config.bootstrap = config.Bootstrap->GetUnderlyingHandle();
  84. }
  85. else
  86. {
  87. raw_config.bootstrap = ApiHandle::GetOrCreateStaticDefaultClientBootstrap()->GetUnderlyingHandle();
  88. }
  89. m_client = aws_imds_client_new(allocator, &raw_config);
  90. m_allocator = allocator;
  91. }
  92. ImdsClient::~ImdsClient()
  93. {
  94. if (m_client)
  95. {
  96. aws_imds_client_release(m_client);
  97. m_client = nullptr;
  98. }
  99. }
  100. template <typename T> struct WrappedCallbackArgs
  101. {
  102. WrappedCallbackArgs(Allocator *allocator, T callback, void *userData)
  103. : allocator(allocator), callback(callback), userData(userData)
  104. {
  105. }
  106. Allocator *allocator;
  107. T callback;
  108. void *userData;
  109. };
  110. void ImdsClient::s_onResourceAcquired(const aws_byte_buf *resource, int errorCode, void *userData)
  111. {
  112. WrappedCallbackArgs<OnResourceAcquired> *callbackArgs =
  113. static_cast<WrappedCallbackArgs<OnResourceAcquired> *>(userData);
  114. callbackArgs->callback(
  115. ByteCursorToStringView(aws_byte_cursor_from_buf(resource)), errorCode, callbackArgs->userData);
  116. Aws::Crt::Delete(callbackArgs, callbackArgs->allocator);
  117. }
  118. void ImdsClient::s_onVectorResourceAcquired(const aws_array_list *array, int errorCode, void *userData)
  119. {
  120. WrappedCallbackArgs<OnVectorResourceAcquired> *callbackArgs =
  121. static_cast<WrappedCallbackArgs<OnVectorResourceAcquired> *>(userData);
  122. callbackArgs->callback(
  123. ArrayListToVector<ByteCursor, StringView>(array, ByteCursorToStringView),
  124. errorCode,
  125. callbackArgs->userData);
  126. Aws::Crt::Delete(callbackArgs, callbackArgs->allocator);
  127. }
  128. void ImdsClient::s_onCredentialsAcquired(const aws_credentials *credentials, int errorCode, void *userData)
  129. {
  130. WrappedCallbackArgs<OnCredentialsAcquired> *callbackArgs =
  131. static_cast<WrappedCallbackArgs<OnCredentialsAcquired> *>(userData);
  132. auto credentialsPtr = Aws::Crt::MakeShared<Auth::Credentials>(callbackArgs->allocator, credentials);
  133. callbackArgs->callback(credentials, errorCode, callbackArgs->userData);
  134. Aws::Crt::Delete(callbackArgs, callbackArgs->allocator);
  135. }
  136. void ImdsClient::s_onIamProfileAcquired(
  137. const aws_imds_iam_profile *iamProfileInfo,
  138. int errorCode,
  139. void *userData)
  140. {
  141. WrappedCallbackArgs<OnIamProfileAcquired> *callbackArgs =
  142. static_cast<WrappedCallbackArgs<OnIamProfileAcquired> *>(userData);
  143. IamProfileView iamProfile;
  144. iamProfile.lastUpdated = aws_date_time_as_epoch_secs(&(iamProfileInfo->last_updated));
  145. iamProfile.instanceProfileArn = ByteCursorToStringView(iamProfileInfo->instance_profile_arn);
  146. iamProfile.instanceProfileId = ByteCursorToStringView(iamProfileInfo->instance_profile_id);
  147. callbackArgs->callback(iamProfile, errorCode, callbackArgs->userData);
  148. Aws::Crt::Delete(callbackArgs, callbackArgs->allocator);
  149. }
  150. void ImdsClient::s_onInstanceInfoAcquired(
  151. const aws_imds_instance_info *instanceInfo,
  152. int errorCode,
  153. void *userData)
  154. {
  155. WrappedCallbackArgs<OnInstanceInfoAcquired> *callbackArgs =
  156. static_cast<WrappedCallbackArgs<OnInstanceInfoAcquired> *>(userData);
  157. InstanceInfoView info;
  158. info.marketplaceProductCodes = ArrayListToVector<ByteCursor, StringView>(
  159. &(instanceInfo->marketplace_product_codes), ByteCursorToStringView);
  160. info.availabilityZone = ByteCursorToStringView(instanceInfo->availability_zone);
  161. info.privateIp = ByteCursorToStringView(instanceInfo->private_ip);
  162. info.version = ByteCursorToStringView(instanceInfo->version);
  163. info.instanceId = ByteCursorToStringView(instanceInfo->instance_id);
  164. info.billingProducts = ArrayListToVector<ByteCursor, StringView>(
  165. &(instanceInfo->billing_products), ByteCursorToStringView);
  166. info.instanceType = ByteCursorToStringView(instanceInfo->instance_type);
  167. info.accountId = ByteCursorToStringView(instanceInfo->account_id);
  168. info.imageId = ByteCursorToStringView(instanceInfo->image_id);
  169. info.pendingTime = aws_date_time_as_epoch_secs(&(instanceInfo->pending_time));
  170. info.architecture = ByteCursorToStringView(instanceInfo->architecture);
  171. info.kernelId = ByteCursorToStringView(instanceInfo->kernel_id);
  172. info.ramdiskId = ByteCursorToStringView(instanceInfo->ramdisk_id);
  173. info.region = ByteCursorToStringView(instanceInfo->region);
  174. callbackArgs->callback(info, errorCode, callbackArgs->userData);
  175. Aws::Crt::Delete(callbackArgs, callbackArgs->allocator);
  176. }
  177. int ImdsClient::GetResource(const StringView &resourcePath, OnResourceAcquired callback, void *userData)
  178. {
  179. auto wrappedCallbackArgs = Aws::Crt::New<WrappedCallbackArgs<OnResourceAcquired>>(
  180. m_allocator, m_allocator, callback, userData);
  181. if (wrappedCallbackArgs == nullptr)
  182. {
  183. return AWS_OP_ERR;
  184. }
  185. return aws_imds_client_get_resource_async(
  186. m_client, StringViewToByteCursor(resourcePath), s_onResourceAcquired, wrappedCallbackArgs);
  187. }
  188. int ImdsClient::GetAmiId(OnResourceAcquired callback, void *userData)
  189. {
  190. auto wrappedCallbackArgs = Aws::Crt::New<WrappedCallbackArgs<OnResourceAcquired>>(
  191. m_allocator, m_allocator, callback, userData);
  192. if (wrappedCallbackArgs == nullptr)
  193. {
  194. return AWS_OP_ERR;
  195. }
  196. return aws_imds_client_get_ami_id(m_client, s_onResourceAcquired, wrappedCallbackArgs);
  197. }
  198. int ImdsClient::GetAmiLaunchIndex(OnResourceAcquired callback, void *userData)
  199. {
  200. auto wrappedCallbackArgs = Aws::Crt::New<WrappedCallbackArgs<OnResourceAcquired>>(
  201. m_allocator, m_allocator, callback, userData);
  202. if (wrappedCallbackArgs == nullptr)
  203. {
  204. return AWS_OP_ERR;
  205. }
  206. return aws_imds_client_get_ami_launch_index(m_client, s_onResourceAcquired, wrappedCallbackArgs);
  207. }
  208. int ImdsClient::GetAmiManifestPath(OnResourceAcquired callback, void *userData)
  209. {
  210. auto wrappedCallbackArgs = Aws::Crt::New<WrappedCallbackArgs<OnResourceAcquired>>(
  211. m_allocator, m_allocator, callback, userData);
  212. if (wrappedCallbackArgs == nullptr)
  213. {
  214. return AWS_OP_ERR;
  215. }
  216. return aws_imds_client_get_ami_manifest_path(m_client, s_onResourceAcquired, wrappedCallbackArgs);
  217. }
  218. int ImdsClient::GetAncestorAmiIds(OnVectorResourceAcquired callback, void *userData)
  219. {
  220. auto wrappedCallbackArgs = Aws::Crt::New<WrappedCallbackArgs<OnVectorResourceAcquired>>(
  221. m_allocator, m_allocator, callback, userData);
  222. if (wrappedCallbackArgs == nullptr)
  223. {
  224. return AWS_OP_ERR;
  225. }
  226. return aws_imds_client_get_ancestor_ami_ids(m_client, s_onVectorResourceAcquired, wrappedCallbackArgs);
  227. }
  228. int ImdsClient::GetInstanceAction(OnResourceAcquired callback, void *userData)
  229. {
  230. auto wrappedCallbackArgs = Aws::Crt::New<WrappedCallbackArgs<OnResourceAcquired>>(
  231. m_allocator, m_allocator, callback, userData);
  232. if (wrappedCallbackArgs == nullptr)
  233. {
  234. return AWS_OP_ERR;
  235. }
  236. return aws_imds_client_get_instance_action(m_client, s_onResourceAcquired, wrappedCallbackArgs);
  237. }
  238. int ImdsClient::GetInstanceId(OnResourceAcquired callback, void *userData)
  239. {
  240. auto wrappedCallbackArgs = Aws::Crt::New<WrappedCallbackArgs<OnResourceAcquired>>(
  241. m_allocator, m_allocator, callback, userData);
  242. if (wrappedCallbackArgs == nullptr)
  243. {
  244. return AWS_OP_ERR;
  245. }
  246. return aws_imds_client_get_instance_id(m_client, s_onResourceAcquired, wrappedCallbackArgs);
  247. }
  248. int ImdsClient::GetInstanceType(OnResourceAcquired callback, void *userData)
  249. {
  250. auto wrappedCallbackArgs = Aws::Crt::New<WrappedCallbackArgs<OnResourceAcquired>>(
  251. m_allocator, m_allocator, callback, userData);
  252. if (wrappedCallbackArgs == nullptr)
  253. {
  254. return AWS_OP_ERR;
  255. }
  256. return aws_imds_client_get_instance_type(m_client, s_onResourceAcquired, wrappedCallbackArgs);
  257. }
  258. int ImdsClient::GetMacAddress(OnResourceAcquired callback, void *userData)
  259. {
  260. auto wrappedCallbackArgs = Aws::Crt::New<WrappedCallbackArgs<OnResourceAcquired>>(
  261. m_allocator, m_allocator, callback, userData);
  262. if (wrappedCallbackArgs == nullptr)
  263. {
  264. return AWS_OP_ERR;
  265. }
  266. return aws_imds_client_get_mac_address(m_client, s_onResourceAcquired, wrappedCallbackArgs);
  267. }
  268. int ImdsClient::GetPrivateIpAddress(OnResourceAcquired callback, void *userData)
  269. {
  270. auto wrappedCallbackArgs = Aws::Crt::New<WrappedCallbackArgs<OnResourceAcquired>>(
  271. m_allocator, m_allocator, callback, userData);
  272. if (wrappedCallbackArgs == nullptr)
  273. {
  274. return AWS_OP_ERR;
  275. }
  276. return aws_imds_client_get_private_ip_address(m_client, s_onResourceAcquired, wrappedCallbackArgs);
  277. }
  278. int ImdsClient::GetAvailabilityZone(OnResourceAcquired callback, void *userData)
  279. {
  280. auto wrappedCallbackArgs = Aws::Crt::New<WrappedCallbackArgs<OnResourceAcquired>>(
  281. m_allocator, m_allocator, callback, userData);
  282. if (wrappedCallbackArgs == nullptr)
  283. {
  284. return AWS_OP_ERR;
  285. }
  286. return aws_imds_client_get_availability_zone(m_client, s_onResourceAcquired, wrappedCallbackArgs);
  287. }
  288. int ImdsClient::GetProductCodes(OnResourceAcquired callback, void *userData)
  289. {
  290. auto wrappedCallbackArgs = Aws::Crt::New<WrappedCallbackArgs<OnResourceAcquired>>(
  291. m_allocator, m_allocator, callback, userData);
  292. if (wrappedCallbackArgs == nullptr)
  293. {
  294. return AWS_OP_ERR;
  295. }
  296. return aws_imds_client_get_product_codes(m_client, s_onResourceAcquired, wrappedCallbackArgs);
  297. }
  298. int ImdsClient::GetPublicKey(OnResourceAcquired callback, void *userData)
  299. {
  300. auto wrappedCallbackArgs = Aws::Crt::New<WrappedCallbackArgs<OnResourceAcquired>>(
  301. m_allocator, m_allocator, callback, userData);
  302. if (wrappedCallbackArgs == nullptr)
  303. {
  304. return AWS_OP_ERR;
  305. }
  306. return aws_imds_client_get_public_key(m_client, s_onResourceAcquired, wrappedCallbackArgs);
  307. }
  308. int ImdsClient::GetRamDiskId(OnResourceAcquired callback, void *userData)
  309. {
  310. auto wrappedCallbackArgs = Aws::Crt::New<WrappedCallbackArgs<OnResourceAcquired>>(
  311. m_allocator, m_allocator, callback, userData);
  312. if (wrappedCallbackArgs == nullptr)
  313. {
  314. return AWS_OP_ERR;
  315. }
  316. return aws_imds_client_get_ramdisk_id(m_client, s_onResourceAcquired, wrappedCallbackArgs);
  317. }
  318. int ImdsClient::GetReservationId(OnResourceAcquired callback, void *userData)
  319. {
  320. auto wrappedCallbackArgs = Aws::Crt::New<WrappedCallbackArgs<OnResourceAcquired>>(
  321. m_allocator, m_allocator, callback, userData);
  322. if (wrappedCallbackArgs == nullptr)
  323. {
  324. return AWS_OP_ERR;
  325. }
  326. return aws_imds_client_get_reservation_id(m_client, s_onResourceAcquired, wrappedCallbackArgs);
  327. }
  328. int ImdsClient::GetSecurityGroups(OnVectorResourceAcquired callback, void *userData)
  329. {
  330. auto wrappedCallbackArgs = Aws::Crt::New<WrappedCallbackArgs<OnVectorResourceAcquired>>(
  331. m_allocator, m_allocator, callback, userData);
  332. if (wrappedCallbackArgs == nullptr)
  333. {
  334. return AWS_OP_ERR;
  335. }
  336. return aws_imds_client_get_security_groups(m_client, s_onVectorResourceAcquired, wrappedCallbackArgs);
  337. }
  338. int ImdsClient::GetBlockDeviceMapping(OnVectorResourceAcquired callback, void *userData)
  339. {
  340. auto wrappedCallbackArgs = Aws::Crt::New<WrappedCallbackArgs<OnVectorResourceAcquired>>(
  341. m_allocator, m_allocator, callback, userData);
  342. if (wrappedCallbackArgs == nullptr)
  343. {
  344. return AWS_OP_ERR;
  345. }
  346. return aws_imds_client_get_block_device_mapping(
  347. m_client, s_onVectorResourceAcquired, wrappedCallbackArgs);
  348. }
  349. int ImdsClient::GetAttachedIamRole(OnResourceAcquired callback, void *userData)
  350. {
  351. auto wrappedCallbackArgs = Aws::Crt::New<WrappedCallbackArgs<OnResourceAcquired>>(
  352. m_allocator, m_allocator, callback, userData);
  353. if (wrappedCallbackArgs == nullptr)
  354. {
  355. return AWS_OP_ERR;
  356. }
  357. return aws_imds_client_get_attached_iam_role(m_client, s_onResourceAcquired, wrappedCallbackArgs);
  358. }
  359. int ImdsClient::GetCredentials(
  360. const StringView &iamRoleName,
  361. OnCredentialsAcquired callback,
  362. void *userData)
  363. {
  364. auto wrappedCallbackArgs = Aws::Crt::New<WrappedCallbackArgs<OnCredentialsAcquired>>(
  365. m_allocator, m_allocator, callback, userData);
  366. if (wrappedCallbackArgs == nullptr)
  367. {
  368. return AWS_OP_ERR;
  369. }
  370. return aws_imds_client_get_credentials(
  371. m_client, StringViewToByteCursor(iamRoleName), s_onCredentialsAcquired, wrappedCallbackArgs);
  372. }
  373. int ImdsClient::GetIamProfile(OnIamProfileAcquired callback, void *userData)
  374. {
  375. auto wrappedCallbackArgs = Aws::Crt::New<WrappedCallbackArgs<OnIamProfileAcquired>>(
  376. m_allocator, m_allocator, callback, userData);
  377. if (wrappedCallbackArgs == nullptr)
  378. {
  379. return AWS_OP_ERR;
  380. }
  381. return aws_imds_client_get_iam_profile(m_client, s_onIamProfileAcquired, wrappedCallbackArgs);
  382. }
  383. int ImdsClient::GetUserData(OnResourceAcquired callback, void *userData)
  384. {
  385. auto wrappedCallbackArgs = Aws::Crt::New<WrappedCallbackArgs<OnResourceAcquired>>(
  386. m_allocator, m_allocator, callback, userData);
  387. if (wrappedCallbackArgs == nullptr)
  388. {
  389. return AWS_OP_ERR;
  390. }
  391. return aws_imds_client_get_user_data(m_client, s_onResourceAcquired, wrappedCallbackArgs);
  392. }
  393. int ImdsClient::GetInstanceSignature(OnResourceAcquired callback, void *userData)
  394. {
  395. auto wrappedCallbackArgs = Aws::Crt::New<WrappedCallbackArgs<OnResourceAcquired>>(
  396. m_allocator, m_allocator, callback, userData);
  397. if (wrappedCallbackArgs == nullptr)
  398. {
  399. return AWS_OP_ERR;
  400. }
  401. return aws_imds_client_get_instance_signature(m_client, s_onResourceAcquired, wrappedCallbackArgs);
  402. }
  403. int ImdsClient::GetInstanceInfo(OnInstanceInfoAcquired callback, void *userData)
  404. {
  405. auto wrappedCallbackArgs = Aws::Crt::New<WrappedCallbackArgs<OnInstanceInfoAcquired>>(
  406. m_allocator, m_allocator, callback, userData);
  407. if (wrappedCallbackArgs == nullptr)
  408. {
  409. return AWS_OP_ERR;
  410. }
  411. return aws_imds_client_get_instance_info(m_client, s_onInstanceInfoAcquired, wrappedCallbackArgs);
  412. }
  413. } // namespace Imds
  414. } // namespace Crt
  415. } // namespace Aws