aws-crt-cpp
C++ wrapper around the aws-c-* libraries. Provides Cross-Platform Transport Protocols and SSL/TLS implementations for C++.
Credentials.h
Go to the documentation of this file.
1 #pragma once
2 
7 #include <aws/crt/Exports.h>
8 #include <aws/crt/Types.h>
10 #include <aws/crt/io/TlsOptions.h>
11 
12 #include <chrono>
13 #include <functional>
14 
15 struct aws_credentials;
16 struct aws_credentials_provider;
17 
18 namespace Aws
19 {
20  namespace Crt
21  {
22  namespace Io
23  {
24  class ClientBootstrap;
25  }
26 
27  namespace Http
28  {
30  }
31 
32  namespace Auth
33  {
38  {
39  public:
40  Credentials(const aws_credentials *credentials) noexcept;
42  ByteCursor access_key_id,
43  ByteCursor secret_access_key,
44  ByteCursor session_token,
45  uint64_t expiration_timepoint_in_seconds,
46  Allocator *allocator = ApiAllocator()) noexcept;
47 
53  Credentials(Allocator *allocator = ApiAllocator()) noexcept;
54 
55  ~Credentials();
56 
57  Credentials(const Credentials &) = delete;
58  Credentials(Credentials &&) = delete;
59  Credentials &operator=(const Credentials &) = delete;
60  Credentials &operator=(Credentials &&) = delete;
61 
65  ByteCursor GetAccessKeyId() const noexcept;
66 
70  ByteCursor GetSecretAccessKey() const noexcept;
71 
75  ByteCursor GetSessionToken() const noexcept;
76 
80  uint64_t GetExpirationTimepointInSeconds() const noexcept;
81 
85  ByteCursor GetAccountId() const noexcept;
86 
90  explicit operator bool() const noexcept;
91 
95  const aws_credentials *GetUnderlyingHandle() const noexcept { return m_credentials; }
96 
97  private:
98  const aws_credentials *m_credentials;
99  };
100 
105  using OnCredentialsResolved = std::function<void(std::shared_ptr<Credentials>, int errorCode)>;
106 
110  using GetCredentialsHandler = std::function<std::shared_ptr<Credentials>()>;
111 
116  class AWS_CRT_CPP_API ICredentialsProvider : public std::enable_shared_from_this<ICredentialsProvider>
117  {
118  public:
119  virtual ~ICredentialsProvider() = default;
120 
124  virtual bool GetCredentials(const OnCredentialsResolved &onCredentialsResolved) const = 0;
125 
132  virtual aws_credentials_provider *GetUnderlyingHandle() const noexcept = 0;
133 
137  virtual bool IsValid() const noexcept = 0;
138  };
139 
144  {
146  {
147  AWS_ZERO_STRUCT(AccessKeyId);
148  AWS_ZERO_STRUCT(SecretAccessKey);
149  AWS_ZERO_STRUCT(SessionToken);
150  }
151 
156 
161 
166  };
167 
172  {
173  CredentialsProviderProfileConfig() : Bootstrap(nullptr), TlsContext(nullptr)
174  {
175  AWS_ZERO_STRUCT(ProfileNameOverride);
176  AWS_ZERO_STRUCT(ConfigFileNameOverride);
177  AWS_ZERO_STRUCT(CredentialsFileNameOverride);
178  }
179 
184 
190 
196 
202 
212 
217  };
218 
223  {
224  CredentialsProviderImdsConfig() : Bootstrap(nullptr) {}
225 
234  };
235 
242  {
244 
249  };
250 
255  {
256  CredentialsProviderCachedConfig() : Provider(), CachedCredentialTTL() {}
257 
261  std::shared_ptr<ICredentialsProvider> Provider;
262 
266  std::chrono::milliseconds CachedCredentialTTL;
267  };
268 
276  {
277  CredentialsProviderChainDefaultConfig() : Bootstrap(nullptr), TlsContext(nullptr) {}
278 
286 
295  };
296 
301  {
303  : Bootstrap(nullptr), TlsOptions(), ThingName(), RoleAlias(), Endpoint(), ProxyOptions()
304  {
305  }
306 
315 
316  /* TLS connection options that have been initialized with your x509 certificate and private key */
318 
319  /* IoT thing name you registered with AWS IOT for your device, it will be used in http request header */
321 
322  /* Iot role alias you created with AWS IoT for your IAM role, it will be used in http request path */
324 
332 
337  };
338 
343  {
344  /* handler to provider credentials */
346  };
347 
352  {
353 
358 
363  };
364 
369  {
371 
376 
381 
386 
392 
401 
406 
411  };
412 
417  {
419 
423  std::shared_ptr<ICredentialsProvider> Provider;
424 
429 
434 
438  uint16_t DurationSeconds;
439 
448 
453 
458  };
459 
464  {
466 
471 
476 
481 
486 
495 
500 
505  };
506 
511  {
513 
518 
524 
529 
538 
543  };
544 
552  {
553  public:
554  CredentialsProvider(aws_credentials_provider *provider, Allocator *allocator = ApiAllocator()) noexcept;
555 
556  virtual ~CredentialsProvider();
557 
558  CredentialsProvider(const CredentialsProvider &) = delete;
560  CredentialsProvider &operator=(const CredentialsProvider &) = delete;
561  CredentialsProvider &operator=(CredentialsProvider &&) = delete;
562 
566  virtual bool GetCredentials(const OnCredentialsResolved &onCredentialsResolved) const override;
567 
571  virtual aws_credentials_provider *GetUnderlyingHandle() const noexcept override { return m_provider; }
572 
576  virtual bool IsValid() const noexcept override { return m_provider != nullptr; }
577 
578  /*
579  * Factory methods for all of the basic credentials provider types
580  */
581 
585  static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderStatic(
586  const CredentialsProviderStaticConfig &config,
587  Allocator *allocator = ApiAllocator());
588 
593  static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderAnonymous(
594  Allocator *allocator = ApiAllocator());
595 
599  static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderEnvironment(
600  Allocator *allocator = ApiAllocator());
601 
605  static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderProfile(
606  const CredentialsProviderProfileConfig &config,
607  Allocator *allocator = ApiAllocator());
608 
612  static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderImds(
613  const CredentialsProviderImdsConfig &config,
614  Allocator *allocator = ApiAllocator());
615 
620  static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderChain(
621  const CredentialsProviderChainConfig &config,
622  Allocator *allocator = ApiAllocator());
623 
624  /*
625  * Creates a provider that puts a simple time-based cache in front of its queries
626  * to a subordinate provider.
627  */
628  static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderCached(
629  const CredentialsProviderCachedConfig &config,
630  Allocator *allocator = ApiAllocator());
631 
638  static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderChainDefault(
640  Allocator *allocator = ApiAllocator());
641 
646  static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderX509(
647  const CredentialsProviderX509Config &config,
648  Allocator *allocator = ApiAllocator());
649 
654  static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderDelegate(
655  const CredentialsProviderDelegateConfig &config,
656  Allocator *allocator = ApiAllocator());
657 
661  static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderCognito(
662  const CredentialsProviderCognitoConfig &config,
663  Allocator *allocator = ApiAllocator());
664 
668  static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderSTS(
669  const CredentialsProviderSTSConfig &config,
670  Allocator *allocator = ApiAllocator());
671 
672  static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderSTSWebIdentity(
674  Allocator *allocator = ApiAllocator());
675 
679  static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderLogin(
680  const CredentialsProviderLoginConfig &config,
681  Allocator *allocator = ApiAllocator());
682 
683  private:
684  static void s_onCredentialsResolved(aws_credentials *credentials, int error_code, void *user_data);
685 
686  Allocator *m_allocator;
687  aws_credentials_provider *m_provider;
688  };
689  } // namespace Auth
690  } // namespace Crt
691 } // namespace Aws
Io::TlsContext TlsCtx
Definition: Credentials.h:405
Io::TlsConnectionOptions TlsOptions
Definition: Credentials.h:317
ByteCursor ConfigFileNameOverride
Definition: Credentials.h:189
Definition: TlsOptions.h:349
ByteCursor AccessKeyId
Definition: Credentials.h:155
CredentialsProviderProfileConfig()
Definition: Credentials.h:173
virtual aws_credentials_provider * GetUnderlyingHandle() const noexcept override
Definition: Credentials.h:571
String SessionName
Definition: Credentials.h:433
String Region
Definition: Credentials.h:480
String TokenFilePath
Definition: Credentials.h:485
#define AWS_CRT_CPP_API
Definition: Exports.h:36
String ThingName
Definition: Credentials.h:320
String Endpoint
Definition: Credentials.h:375
Io::ClientBootstrap * Bootstrap
Definition: Credentials.h:400
ByteCursor CredentialsFileNameOverride
Definition: Credentials.h:195
Optional< Http::HttpClientConnectionProxyOptions > ProxyOptions
Definition: Credentials.h:457
CredentialsProviderChainConfig()
Definition: Credentials.h:243
ByteCursor SessionToken
Definition: Credentials.h:165
CredentialsProviderX509Config()
Definition: Credentials.h:302
const aws_credentials * GetUnderlyingHandle() const noexcept
Definition: Credentials.h:95
std::basic_string< char, std::char_traits< char >, StlAllocator< char >> String
Definition: Types.h:45
Io::TlsContext * TlsContext
Definition: Credentials.h:211
String IdentityProviderName
Definition: Credentials.h:357
aws_allocator Allocator
Definition: Allocator.h:14
aws_byte_cursor ByteCursor
Definition: Types.h:31
Optional< Http::ProxyEnvVarOptions > ProxyEnvVarOptions
Definition: Credentials.h:504
std::chrono::milliseconds CachedCredentialTTL
Definition: Credentials.h:266
Definition: HttpConnection.h:298
String Identity
Definition: Credentials.h:380
Io::ClientBootstrap * Bootstrap
Definition: Credentials.h:201
CredentialsProviderStaticConfig()
Definition: Credentials.h:145
ByteCursor ProfileNameOverride
Definition: Credentials.h:183
std::shared_ptr< ICredentialsProvider > Provider
Definition: Credentials.h:261
Io::ClientBootstrap * Bootstrap
Definition: Credentials.h:494
AWS_CRT_CPP_API Allocator * ApiAllocator() noexcept
Definition: Allocator.cpp:24
Io::ClientBootstrap * Bootstrap
Definition: Credentials.h:447
Optional< Http::ProxyEnvVarOptions > ProxyEnvVarOptions
Definition: Credentials.h:216
uint16_t DurationSeconds
Definition: Credentials.h:438
Optional< Http::HttpClientConnectionProxyOptions > ProxyOptions
Definition: Credentials.h:410
String IdentityProviderToken
Definition: Credentials.h:362
CredentialsProviderImdsConfig()
Definition: Credentials.h:224
String Endpoint
Definition: Credentials.h:331
String RoleArn
Definition: Credentials.h:428
String RoleAlias
Definition: Credentials.h:323
std::shared_ptr< ICredentialsProvider > Provider
Definition: Credentials.h:423
CredentialsProviderCachedConfig()
Definition: Credentials.h:256
Definition: Credentials.h:116
Definition: Credentials.h:551
Definition: Bootstrap.h:34
CredentialsProviderChainDefaultConfig()
Definition: Credentials.h:277
String RoleArn
Definition: Credentials.h:470
std::vector< T, StlAllocator< T >> Vector
Definition: Types.h:53
GetCredentialsHandler Handler
Definition: Credentials.h:345
Io::ClientBootstrap * Bootstrap
Definition: Credentials.h:233
Io::ClientBootstrap * Bootstrap
Definition: Credentials.h:285
Optional< Http::HttpClientConnectionProxyOptions > ProxyOptions
Definition: Credentials.h:336
Optional< Vector< CognitoLoginPair > > Logins
Definition: Credentials.h:385
Io::ClientBootstrap * Bootstrap
Definition: Credentials.h:314
ByteCursor SecretAccessKey
Definition: Credentials.h:160
String LoginSession
Definition: Credentials.h:517
Definition: Credentials.h:351
String LoginRegion
Definition: Credentials.h:528
Definition: TlsOptions.h:292
Io::ClientBootstrap * Bootstrap
Definition: Credentials.h:537
virtual bool IsValid() const noexcept override
Definition: Credentials.h:576
std::function< void(std::shared_ptr< Credentials >, int errorCode)> OnCredentialsResolved
Definition: Credentials.h:105
Io::TlsContext TlsCtx
Definition: Credentials.h:452
Io::TlsConnectionOptions TlsConnectionOptions
Definition: Credentials.h:499
String SessionName
Definition: Credentials.h:475
String LoginCacheOverride
Definition: Credentials.h:523
Io::TlsContext * TlsContext
Definition: Credentials.h:294
Definition: Credentials.h:37
Io::TlsConnectionOptions TlsConnectionOptions
Definition: Credentials.h:542
Vector< std::shared_ptr< ICredentialsProvider > > Providers
Definition: Credentials.h:248
Optional< String > CustomRoleArn
Definition: Credentials.h:391
Definition: Allocator.h:10
std::function< std::shared_ptr< Credentials >()> GetCredentialsHandler
Definition: Credentials.h:110