service_credentials_provider_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #include <gtest/gtest.h>
17 // #include <gmock/gmock.h>
20 #include <aws/core/Aws.h>
21 #include <aws/core/utils/json/JsonSerializer.h>
22 
23 using namespace Aws::Client;
24 using namespace Aws::Utils;
25 using namespace Aws::Auth;
26 using namespace Aws::Utils::Json;
27 using ::testing::_;
28 using ::testing::Matcher;
29 using ::testing::DoAll;
30 using ::testing::SetArgReferee;
31 using ::testing::Return;
32 using Aws::AwsError;
33 
34 namespace Aws {
35 namespace Auth {
36 
37 bool operator==(const AWSCredentials & left, const AWSCredentials & right)
38 {
39  return (
40  (left.GetAWSAccessKeyId() == right.GetAWSAccessKeyId())
41  && (left.GetAWSSecretKey() == right.GetAWSSecretKey())
42  && (left.GetSessionToken() == right.GetSessionToken())
43  );
44 }
45 
46 } // namespace Auth
47 } // namespace Aws
48 
49 class ServiceCredentialsProviderFixture : public ::testing::Test
50 {
51 public:
52  static const std::map<std::string, std::string> kFullIotConfigMap;
53  static const std::list<std::string> kFullIotConfigMandatoryKeys;
55  static const std::map<std::string, std::string> kFullCredentialsInfo;
56  static const std::list<std::string> kFullCredentialsInfoKeys;
57 
58 protected:
59  std::shared_ptr<ParameterReaderMock> param_reader_ =
60  std::make_shared<ParameterReaderMock>();
61 };
62 
63 const std::map<std::string, std::string> ServiceCredentialsProviderFixture::kFullIotConfigMap = {
64  {"cafile", "M2M1NTA0NTQxMDg4YTUxMzcyMzY4MTNh"}, {"certfile", "MmQ2NWEyZmFmNThlOWM1"},
65  {"keyfile", "MDAxZmZiY2VjYmIwMGM"}, {"endpoint", "xNDhkYzU5MjNm"},
66  {"role", "YmIwNTMx"}, {"thing_name", "MzExMTdlMGI2YzY5ZjJmYTli"},
67  {"connect_timeout_ms", "42"}, {"total_timeout_ms", "27"}
68 };
69 
71  "cafile", "certfile", "keyfile", "endpoint", "role", "thing_name"
72 };
73 
75  kFullIotConfigMap.at("cafile").c_str(),
76  kFullIotConfigMap.at("certfile").c_str(),
77  kFullIotConfigMap.at("keyfile").c_str(),
78  kFullIotConfigMap.at("endpoint").c_str(),
79  kFullIotConfigMap.at("role").c_str(),
80  kFullIotConfigMap.at("thing_name").c_str(),
81  StringUtils::ConvertToInt32(kFullIotConfigMap.at("connect_timeout_ms").c_str()),
82  StringUtils::ConvertToInt32(kFullIotConfigMap.at("total_timeout_ms").c_str())
83 );
84 
85 const std::map<std::string, std::string> ServiceCredentialsProviderFixture::kFullCredentialsInfo = {
86  {"expiration", "2019-01-10T21:57:06Z"},
87  {"accessKeyId", "ZWM2ODYzNDEwZWJhNGM0NjZiYzk4ZDI4"},
88  {"secretAccessKey", "YWYyNWM0NmEzZWE1NWQy"},
89  {"sessionToken", "YTFhM2NhNjM5OGZlMDlmYmRmMTY3Mzk5WQyNDVkMTJjYThi"}
90 };
91 
93  "expiration", "accessKeyId", "secretAccessKey", "sessionToken"
94 };
95 
96 TEST_F(ServiceCredentialsProviderFixture, TestGetServiceAuthConfigNoIotConfig)
97 {
98  EXPECT_CALL(*param_reader_, ReadParam(_, Matcher<std::map<std::string, std::string> &>(_)))
99  .WillRepeatedly(Return(AwsError::AWS_ERR_NOT_FOUND));
100 
101  ServiceAuthConfig config;
102  bool success = GetServiceAuthConfig(config, param_reader_);
103 
104  EXPECT_FALSE(success);
105 }
106 
109  public ::testing::WithParamInterface<std::string> {};
110 
111 TEST_P(TestGetServiceAuthConfigFixture, TestGetServiceAuthConfigPartialIotConfig)
112 {
113  auto missing_config_key = GetParam();
114  auto partial_iot_config = std::map<std::string, std::string>(kFullIotConfigMap);
115  partial_iot_config.erase(missing_config_key);
116  EXPECT_CALL(*param_reader_, ReadParam(_, Matcher<std::map<std::string, std::string> &>(_)))
117  .WillRepeatedly(DoAll(SetArgReferee<1>(partial_iot_config), Return(AwsError::AWS_ERR_OK)));
118 
119  ServiceAuthConfig config;
120  bool success = GetServiceAuthConfig(config, param_reader_);
121 
122  EXPECT_FALSE(success);
123 }
124 
126  TestGetServiceAuthConfigPartialIotConfig,
129 );
130 
131 TEST_F(ServiceCredentialsProviderFixture, TestGetServiceAuthConfigCompleteIotConfig)
132 {
133  EXPECT_CALL(*param_reader_, ReadParam(_, Matcher<std::map<std::string, std::string> &>(_)))
134  .WillRepeatedly(DoAll(SetArgReferee<1>(kFullIotConfigMap), Return(AwsError::AWS_ERR_OK)));
135 
136  ServiceAuthConfig config;
137  bool success = GetServiceAuthConfig(config, param_reader_);
138 
139  EXPECT_TRUE(success);
140  EXPECT_STREQ(kFullIotConfigMap.at("cafile").c_str(), config.iot.cafile.c_str());
141  EXPECT_STREQ(kFullIotConfigMap.at("certfile").c_str(), config.iot.certfile.c_str());
142  EXPECT_STREQ(kFullIotConfigMap.at("keyfile").c_str(), config.iot.keyfile.c_str());
143  EXPECT_STREQ(kFullIotConfigMap.at("endpoint").c_str(), config.iot.host.c_str());
144  EXPECT_STREQ(kFullIotConfigMap.at("role").c_str(), config.iot.role.c_str());
145  EXPECT_STREQ(kFullIotConfigMap.at("thing_name").c_str(), config.iot.name.c_str());
146  EXPECT_EQ(StringUtils::ConvertToInt32(kFullIotConfigMap.at("connect_timeout_ms").c_str()),
147  config.iot.connect_timeout_ms);
148  EXPECT_EQ(StringUtils::ConvertToInt32(kFullIotConfigMap.at("total_timeout_ms").c_str()),
149  config.iot.total_timeout_ms);
150 }
151 
152 TEST_F(ServiceCredentialsProviderFixture, TestServiceCredentialsProviderChainValidIotConf)
153 {
154  ServiceCredentialsProviderChain default_conf_chain;
155  ServiceCredentialsProviderChain configured_chain(ServiceAuthConfig{kFullIotConfig});
156 
157  // a new credential provider is added to the chain
158  EXPECT_EQ(default_conf_chain.GetProviders().size() + 1, configured_chain.GetProviders().size());
159 }
160 
161 TEST_F(ServiceCredentialsProviderFixture, TestServiceCredentialsProviderChainInvalidIotConf)
162 {
163  ServiceAuthConfig config = ServiceAuthConfig{kFullIotConfig};
164  config.iot.cafile = "";
165 
166  ServiceCredentialsProviderChain default_conf_chain;
167  ServiceCredentialsProviderChain configured_chain(config);
168 
169  // no new credential provider is added to the chain
170  EXPECT_EQ(default_conf_chain.GetProviders().size(), configured_chain.GetProviders().size());
171 }
172 
174 {
175 public:
177 
178  void PublicRefresh() { IotRoleCredentialsProvider::Refresh(); }
179  void PublicSetCredentials(AWSCredentials & creds) { IotRoleCredentialsProvider::SetCredentials(creds); }
180  bool PublicValidateResponse(Aws::Utils::Json::JsonValue & value) {
181  return IotRoleCredentialsProvider::ValidateResponse(value);
182  }
183 
184  Aws::Auth::AWSCredentials GetCachedCredentials() { return this->cached_; }
185 };
186 
187 TEST_F(ServiceCredentialsProviderFixture, TestIotRoleCredentialsProviderRefreshWrongHost)
188 {
189  auto provider = std::make_shared<OpenIotRoleCredentialsProvider>(kFullIotConfig);
190  AWSCredentials initial_credentials(provider->GetAWSCredentials());
191 
192  provider->PublicRefresh();
193 
194  // credentials are not changed if the request to get new credentials fails
195  EXPECT_EQ(initial_credentials, provider->GetAWSCredentials());
196 }
197 
198 TEST_F(ServiceCredentialsProviderFixture, TestIotRoleCredentialsProviderSetCredentials)
199 {
200  auto provider = std::make_shared<OpenIotRoleCredentialsProvider>(kFullIotConfig);
201  AWSCredentials aws_credentials{"ZWM2ODYzNDEwZWJhNGM0NjZiYzk4ZDI4",
202  "YWYyNWM0NmEzZWE1NWQy", "YTFhM2NhNjM5OGZlMDlmYmRmMTY3Mzk5WQyNDVkMTJjYThi"};
203 
204  provider->PublicSetCredentials(aws_credentials);
205 
206  EXPECT_EQ(aws_credentials, provider->GetAWSCredentials());
207 }
208 
209 TEST_F(ServiceCredentialsProviderFixture, TestIotRoleCredentialsValidateResponse)
210 {
211  auto provider = std::make_shared<OpenIotRoleCredentialsProvider>(kFullIotConfig);
212 
213  Json::JsonValue malformed_json(Aws::String("malformed"));
214  EXPECT_FALSE(provider->PublicValidateResponse(malformed_json));
215 
216  auto response = Json::JsonValue();
217  EXPECT_FALSE(provider->PublicValidateResponse(response));
218 
219  EXPECT_FALSE(provider->PublicValidateResponse(response.WithString("credentials", Aws::String("foo"))));
220 
221  auto credentials = Json::JsonValue();
222  EXPECT_FALSE(provider->PublicValidateResponse(response.WithObject("credentials", credentials)));
223 
224  credentials = credentials.WithString("expiration", Aws::String("2019-01-10T21:57:06Z"));
225  EXPECT_FALSE(provider->PublicValidateResponse(response.WithObject("credentials", credentials)));
226 
227  credentials = credentials.WithString("accessKeyId", Aws::String("ZWM2ODYzNDEwZWJhNGM0NjZiYzk4ZDI4"));
228  EXPECT_FALSE(provider->PublicValidateResponse(response.WithObject("credentials", credentials)));
229 
230  credentials = credentials.WithString("secretAccessKey", Aws::String("YWYyNWM0NmEzZWE1NWQy"));
231  EXPECT_FALSE(provider->PublicValidateResponse(response.WithObject("credentials", credentials)));
232 
233  credentials = credentials.WithString("sessionToken", Aws::String("YTFhM2NhNjM5OGZlMDlmYmRmMTY3Mzk5WQyNDVkMTJjYThi"));
234  EXPECT_TRUE(provider->PublicValidateResponse(response.WithObject("credentials", credentials)));
235 }
236 
237 int main(int argc, char ** argv)
238 {
239  Aws::SDKOptions options;
240  Aws::InitAPI(options);
241 
242  testing::InitGoogleTest(&argc, argv);
243  auto test_result = RUN_ALL_TESTS();
244 
245  Aws::ShutdownAPI(options);
246 
247  return test_result;
248 }
static const std::list< std::string > kFullCredentialsInfoKeys
OpenIotRoleCredentialsProvider(const IotRoleConfig &config)
Credentials provider chain for ROS AWS service integrations.
Aws::String host
Host name of the iot:CredentialProvider endpoint.
bool operator==(const ClientConfiguration &left, const ClientConfiguration &right)
IotRoleConfig iot
IoT-specific configuration.
static const std::list< std::string > kFullIotConfigMandatoryKeys
static const std::map< std::string, std::string > kFullIotConfigMap
long connect_timeout_ms
Number of ms to wait before timing out when connecting to the endpoint.
TEST_P(TestGetServiceAuthConfigFixture, TestGetServiceAuthConfigPartialIotConfig)
AwsError
Defines error return codes for functions This enum defines standard error codes that will be returned...
Definition: aws_error.h:29
Aws::String cafile
Path to the Root CA for the endpoint.
bool PublicValidateResponse(Aws::Utils::Json::JsonValue &value)
Auth configuration needed to retrieve AWS credentials via the IoT service.
Aws::String name
Thing name for the device.
Aws::String certfile
Path to the certificate which identifies the device.
Aws::String role
Name of the AWS IoT Role Alias for the device.
int main(int argc, char **argv)
TEST_F(ServiceCredentialsProviderFixture, TestGetServiceAuthConfigNoIotConfig)
static const std::map< std::string, std::string > kFullCredentialsInfo
INSTANTIATE_TEST_CASE_P(TestGetServiceAuthConfigPartialIotConfig, TestGetServiceAuthConfigFixture,::testing::ValuesIn(ServiceCredentialsProviderFixture::kFullIotConfigMandatoryKeys))
long total_timeout_ms
Total number of ms to wait for the entire connect/request/response transaction.
bool GetServiceAuthConfig(ServiceAuthConfig &config, const std::shared_ptr< Aws::Client::ParameterReaderInterface > &parameters)
Retrieves service authorization data from a ParameterReaderInterface and populates the ServiceAuthCon...
Aws::String keyfile
Path to the related private key for the certificate.
AWSCredentialsProvider that obtains credentials using the AWS IoT Core service.
Auth configuration for ROS AWS service integration.


aws_common
Author(s): AWS RoboMaker
autogenerated on Sat Mar 6 2021 03:11:38