|
| 1 | +#include <aws/core/platform/Environment.h> |
| 2 | +#include <aws/core/utils/logging/LogMacros.h> |
| 3 | + |
| 4 | +#include <kinesis_video_streamer/credentials.h> |
| 5 | + |
| 6 | +using namespace Aws::Auth; |
| 7 | + |
| 8 | +/// Logging tag used for all messages emitting from this module |
| 9 | +static const char AWS_LOG_TAG[] = "CustomAWSCredentialsProviderChain"; |
| 10 | +static const char AWS_ECS_CONTAINER_CREDENTIALS_RELATIVE_URI[] = "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"; |
| 11 | +static const char AWS_ECS_CONTAINER_CREDENTIALS_FULL_URI[] = "AWS_CONTAINER_CREDENTIALS_FULL_URI"; |
| 12 | +static const char AWS_ECS_CONTAINER_AUTHORIZATION_TOKEN[] = "AWS_CONTAINER_AUTHORIZATION_TOKEN"; |
| 13 | +static const char AWS_EC2_METADATA_DISABLED[] = "AWS_EC2_METADATA_DISABLED"; |
| 14 | + |
| 15 | +namespace Aws |
| 16 | +{ |
| 17 | + namespace Auth |
| 18 | + { |
| 19 | + |
| 20 | + /** |
| 21 | + * \brief Validates an instance of an IotRoleConfig struct |
| 22 | + * @param config The struct to validate |
| 23 | + * @return True if the struct is valid, meaning all the config needed to connect is there |
| 24 | + */ |
| 25 | + static bool IsIotConfigValid(const IotRoleConfig & config) |
| 26 | + { |
| 27 | + return config.cafile.length() > 0 && config.certfile.length() > 0 && |
| 28 | + config.keyfile.length() > 0 && config.host.length() > 0 && config.role.length() > 0 && |
| 29 | + config.name.length() > 0 && config.connect_timeout_ms > 0 && config.total_timeout_ms > 0; |
| 30 | + } |
| 31 | + |
| 32 | + CustomAWSCredentialsProviderChain::CustomAWSCredentialsProviderChain(const ServiceAuthConfig &config): |
| 33 | + AWSCredentialsProviderChain() |
| 34 | + { |
| 35 | + // Add IoT credentials provider if valid |
| 36 | + if (IsIotConfigValid(config.iot)) { |
| 37 | + AWS_LOG_INFO(AWS_LOG_TAG, "Found valid IoT auth config, adding IotRoleCredentialsProvider"); |
| 38 | + auto provider = Aws::MakeShared<IotRoleCredentialsProvider>(__func__, config.iot); |
| 39 | + AddProvider(provider); |
| 40 | + } else { |
| 41 | + AWS_LOG_INFO(AWS_LOG_TAG, "No valid IoT auth config, skipping IotRoleCredentialsProvider"); |
| 42 | + } |
| 43 | + |
| 44 | + // Add environment credentials provider |
| 45 | + AddProvider(Aws::MakeShared<EnvironmentAWSCredentialsProvider>(AWS_LOG_TAG)); |
| 46 | + AddProvider(Aws::MakeShared<ProfileConfigFileAWSCredentialsProvider>(AWS_LOG_TAG)); |
| 47 | + |
| 48 | + //ECS TaskRole Credentials only available when ENVIRONMENT VARIABLE is set |
| 49 | + const auto relativeUri = Aws::Environment::GetEnv(AWS_ECS_CONTAINER_CREDENTIALS_RELATIVE_URI); |
| 50 | + AWS_LOGSTREAM_DEBUG(AWS_LOG_TAG, "The environment variable value " << AWS_ECS_CONTAINER_CREDENTIALS_RELATIVE_URI |
| 51 | + << " is " << relativeUri); |
| 52 | + |
| 53 | + const auto absoluteUri = Aws::Environment::GetEnv(AWS_ECS_CONTAINER_CREDENTIALS_FULL_URI); |
| 54 | + AWS_LOGSTREAM_DEBUG(AWS_LOG_TAG, "The environment variable value " << AWS_ECS_CONTAINER_CREDENTIALS_FULL_URI |
| 55 | + << " is " << absoluteUri); |
| 56 | + |
| 57 | + const auto ec2MetadataDisabled = Aws::Environment::GetEnv(AWS_EC2_METADATA_DISABLED); |
| 58 | + AWS_LOGSTREAM_DEBUG(AWS_LOG_TAG, "The environment variable value " << AWS_EC2_METADATA_DISABLED |
| 59 | + << " is " << ec2MetadataDisabled); |
| 60 | + |
| 61 | + if (!relativeUri.empty()) |
| 62 | + { |
| 63 | + AddProvider(Aws::MakeShared<TaskRoleCredentialsProvider>(AWS_LOG_TAG, relativeUri.c_str())); |
| 64 | + AWS_LOGSTREAM_INFO(AWS_LOG_TAG, "Added ECS metadata service credentials provider with relative path: [" |
| 65 | + << relativeUri << "] to the provider chain."); |
| 66 | + } |
| 67 | + else if (!absoluteUri.empty()) |
| 68 | + { |
| 69 | + const auto token = Aws::Environment::GetEnv(AWS_ECS_CONTAINER_AUTHORIZATION_TOKEN); |
| 70 | + AddProvider(Aws::MakeShared<TaskRoleCredentialsProvider>(AWS_LOG_TAG, |
| 71 | + absoluteUri.c_str(), token.c_str())); |
| 72 | + |
| 73 | + //DO NOT log the value of the authorization token for security purposes. |
| 74 | + AWS_LOGSTREAM_INFO(AWS_LOG_TAG, "Added ECS credentials provider with URI: [" |
| 75 | + << absoluteUri << "] to the provider chain with a" << (token.empty() ? "n empty " : " non-empty ") |
| 76 | + << "authorization token."); |
| 77 | + } |
| 78 | + else if (Aws::Utils::StringUtils::ToLower(ec2MetadataDisabled.c_str()) != "true") |
| 79 | + { |
| 80 | + AddProvider(Aws::MakeShared<InstanceProfileCredentialsProvider>(AWS_LOG_TAG)); |
| 81 | + AWS_LOGSTREAM_INFO(AWS_LOG_TAG, "Added EC2 metadata service credentials provider to the provider chain."); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + } // namespace Auth |
| 86 | +} // namespace Aws |
| 87 | + |
| 88 | +namespace Aws { |
| 89 | + namespace Kinesis { |
| 90 | + /** |
| 91 | + * Credentials provider which uses the AWS SDK's default credential provider chain. |
| 92 | + * @note You need to have called Aws::InitAPI before using this provider. |
| 93 | + */ |
| 94 | + CustomProducerSdkAWSCredentialsProvider::CustomProducerSdkAWSCredentialsProvider( |
| 95 | + std::shared_ptr<Aws::Auth::AWSCredentialsProvider> aws_credentials_provider) |
| 96 | + { |
| 97 | + if (aws_credentials_provider) { |
| 98 | + aws_credentials_provider_ = aws_credentials_provider; |
| 99 | + } else { |
| 100 | + aws_credentials_provider_ = |
| 101 | + Aws::MakeShared<Aws::Auth::DefaultAWSCredentialsProviderChain>(__func__); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + void CustomProducerSdkAWSCredentialsProvider::updateCredentials( |
| 106 | + com::amazonaws::kinesis::video::Credentials & producer_sdk_credentials) |
| 107 | + { |
| 108 | + Aws::Auth::AWSCredentials aws_sdk_credentials = |
| 109 | + aws_credentials_provider_->GetAWSCredentials(); |
| 110 | + producer_sdk_credentials.setAccessKey(aws_sdk_credentials.GetAWSAccessKeyId().c_str()); |
| 111 | + producer_sdk_credentials.setSecretKey(aws_sdk_credentials.GetAWSSecretKey().c_str()); |
| 112 | + producer_sdk_credentials.setSessionToken(aws_sdk_credentials.GetSessionToken().c_str()); |
| 113 | + auto now = std::chrono::duration_cast<std::chrono::seconds>( |
| 114 | + std::chrono::system_clock::now().time_since_epoch()); |
| 115 | + auto refresh_interval = std::chrono::duration_cast<std::chrono::seconds>( |
| 116 | + std::chrono::milliseconds(Aws::Auth::REFRESH_THRESHOLD)); |
| 117 | + producer_sdk_credentials.setExpiration(now + refresh_interval); |
| 118 | + } |
| 119 | + |
| 120 | + } // namespace Kinesis |
| 121 | +} // namespace Aws |
0 commit comments