-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathSharedCrtResourceManager.cpp
More file actions
593 lines (526 loc) · 20 KB
/
SharedCrtResourceManager.cpp
File metadata and controls
593 lines (526 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
#include "SharedCrtResourceManager.h"
#include "Version.h"
#include "aws/crt/http/HttpProxyStrategy.h"
#include "logging/LoggerFactory.h"
#include "util/FileUtils.h"
#include "util/ProxyUtils.h"
#include "util/Retry.h"
#include "util/StringUtils.h"
#include <aws/crt/Api.h>
#include <aws/crt/io/Pkcs11.h>
#include <sys/stat.h>
using namespace std;
using namespace Aws::Crt;
using namespace Aws::Crt::Io;
using namespace Aws::Crt::Mqtt;
using namespace Aws::Iot;
using namespace Aws::Iot::DeviceClient;
using namespace Aws::Iot::DeviceClient::Util;
using namespace Aws::Iot::DeviceClient::Logging;
constexpr int SharedCrtResourceManager::DEFAULT_WAIT_TIME_SECONDS;
constexpr char SharedCrtResourceManager::DEFAULT_SDK_LOG_FILE[];
SharedCrtResourceManager::~SharedCrtResourceManager()
{
if (memTraceLevel != AWS_MEMTRACE_NONE)
{
allocator = aws_mem_tracer_destroy(allocator);
}
}
bool SharedCrtResourceManager::initialize(
const PlainConfig &config,
std::shared_ptr<Util::FeatureRegistry> featureRegistry)
{
features = featureRegistry;
initialized = buildClient(config) == SharedCrtResourceManager::SUCCESS;
return initialized;
}
void SharedCrtResourceManager::loadMemTraceLevelFromEnvironment()
{
const char *memTraceLevelStr = std::getenv("AWS_CRT_MEMORY_TRACING");
if (memTraceLevelStr)
{
switch (atoi(memTraceLevelStr))
{
case AWS_MEMTRACE_BYTES:
LOG_DEBUG(Config::TAG, "Set AWS_CRT_MEMORY_TRACING=AWS_MEMTRACE_BYTES");
memTraceLevel = AWS_MEMTRACE_BYTES;
break;
case AWS_MEMTRACE_STACKS:
LOG_DEBUG(Config::TAG, "Set AWS_CRT_MEMORY_TRACING=AWS_MEMTRACE_STACKS");
memTraceLevel = AWS_MEMTRACE_STACKS;
break;
default:
break;
}
}
}
bool SharedCrtResourceManager::locateCredentials(const PlainConfig &config) const
{
struct stat fileInfo;
bool locatedAll = true;
if (config.secureElement.enabled)
{
if (stat(config.secureElement.pkcs11Lib->c_str(), &fileInfo) != 0)
{
LOGM_ERROR(
TAG,
"Failed to find PKCS#11 library file: %s, cannot establish MQTT connection",
Sanitize(config.secureElement.pkcs11Lib->c_str()).c_str());
locatedAll = false;
}
else
{
string parentDir = FileUtils::ExtractParentDirectory(config.secureElement.pkcs11Lib->c_str());
if (!FileUtils::ValidateFilePermissions(parentDir, Permissions::PKCS11_LIB_DIR) ||
!FileUtils::ValidateFilePermissions(
config.secureElement.pkcs11Lib->c_str(), Permissions::PKCS11_LIB_FILE))
{
LOG_ERROR(TAG, "Incorrect permissions on PKCS#11 library file and/or it's parent directory");
locatedAll = false;
}
}
}
else
{
if (stat(config.key->c_str(), &fileInfo) != 0)
{
LOGM_ERROR(
TAG, "Failed to find %s, cannot establish MQTT connection", Sanitize(config.key->c_str()).c_str());
locatedAll = false;
}
else
{
string parentDir = FileUtils::ExtractParentDirectory(config.key->c_str());
if (!FileUtils::ValidateFilePermissions(parentDir, Permissions::KEY_DIR) ||
!FileUtils::ValidateFilePermissions(config.key->c_str(), Permissions::PRIVATE_KEY))
{
LOG_ERROR(TAG, "Incorrect permissions on private key file and/or parent directory");
locatedAll = false;
}
}
}
if (stat(config.cert->c_str(), &fileInfo) != 0)
{
LOGM_ERROR(TAG, "Failed to find %s, cannot establish MQTT connection", Sanitize(config.cert->c_str()).c_str());
locatedAll = false;
}
else
{
string parentDir = FileUtils::ExtractParentDirectory(config.cert->c_str());
if (!FileUtils::ValidateFilePermissions(parentDir, Permissions::CERT_DIR) ||
!FileUtils::ValidateFilePermissions(config.cert->c_str(), Permissions::PUBLIC_CERT))
{
LOG_ERROR(TAG, "Incorrect permissions on public cert file and/or parent directory");
locatedAll = false;
}
}
return locatedAll;
}
bool SharedCrtResourceManager::setupLogging(const PlainConfig &config) const
{
// Absolute path to the sdk log file.
std::string logFilePath{DEFAULT_SDK_LOG_FILE};
if (!config.logConfig.sdkLogFile.empty())
{
logFilePath = config.logConfig.sdkLogFile;
}
std::string logFileDir = FileUtils::ExtractParentDirectory(logFilePath);
if (!FileUtils::DirectoryExists(logFileDir))
{
// Create an empty directory with the expected permissions.
if (!FileUtils::CreateDirectoryWithPermissions(logFileDir.c_str(), S_IRWXU | S_IRGRP | S_IROTH | S_IXOTH))
{
return false;
}
}
else
{
// Verify the directory permissions.
auto rcvDirPermissions = FileUtils::GetFilePermissions(logFileDir);
if (Permissions::LOG_DIR != rcvDirPermissions)
{
LOGM_ERROR(
TAG,
"Incorrect directory permissions for SDK log file: %s expected: %d received: %d",
Sanitize(logFileDir).c_str(),
Permissions::LOG_DIR,
rcvDirPermissions);
return false;
}
}
if (!FileUtils::FileExists(logFilePath))
{
// Create an empty file with the expected permissions.
if (!FileUtils::CreateEmptyFileWithPermissions(logFilePath, S_IRUSR | S_IWUSR))
{
return false;
}
}
else
{
// Verify the file permissions.
auto rcvFilePermissions = FileUtils::GetFilePermissions(logFilePath);
if (Permissions::LOG_FILE != rcvFilePermissions)
{
LOGM_ERROR(
TAG,
"Incorrect file permissions for SDK log file: %s expected: %d received: %d",
Sanitize(logFilePath).c_str(),
Permissions::LOG_FILE,
rcvFilePermissions);
return false;
}
}
// Configure the SDK with the log file path.
apiHandle->InitializeLogging(config.logConfig.sdkLogLevel, logFilePath.c_str());
LOGM_INFO(TAG, "SDK logging is enabled. Check %s for SDK logs.", Sanitize(logFilePath).c_str());
return true;
}
void SharedCrtResourceManager::initializeAllocator()
{
loadMemTraceLevelFromEnvironment();
allocator = aws_default_allocator();
if (memTraceLevel != AWS_MEMTRACE_NONE)
{
// If memTraceLevel == AWS_MEMTRACE_STACKS(2), then by default 8 frames per stack are used.
allocator = aws_mem_tracer_new(allocator, nullptr, memTraceLevel, 0);
}
// We MUST declare an instance of the ApiHandle to perform global initialization
// of the SDK libraries
apiHandle = unique_ptr<ApiHandle>(new ApiHandle());
}
int SharedCrtResourceManager::buildClient(const PlainConfig &config)
{
if (config.logConfig.sdkLoggingEnabled)
{
if (!setupLogging(config))
{
return SharedCrtResourceManager::ABORT;
}
}
else
{
LOG_INFO(
TAG,
"SDK logging is disabled. Enable it with --enable-sdk-logging on the command line or "
"logging::enable-sdk-logging in your configuration file");
}
eventLoopGroup = unique_ptr<EventLoopGroup>(
new EventLoopGroup(1 // The number of threads used depends on your use-case. IF you have a maximum of less than
// a few hundred connections 1 thread is the ideal threadCount.
));
if (!eventLoopGroup)
{
// cppcheck-suppress nullPointerRedundantCheck
LOGM_ERROR(
TAG, "MQTT Event Loop Group Creation failed with error: %s", ErrorDebugString(eventLoopGroup->LastError()));
// cppcheck-suppress nullPointerRedundantCheck
return eventLoopGroup->LastError();
}
defaultHostResolver = unique_ptr<DefaultHostResolver>(new DefaultHostResolver(*eventLoopGroup, 2, 30));
clientBootstrap = unique_ptr<ClientBootstrap>(new ClientBootstrap(*eventLoopGroup, *defaultHostResolver));
if (!clientBootstrap)
{
// cppcheck-suppress nullPointerRedundantCheck
LOGM_ERROR(TAG, "MQTT ClientBootstrap failed with error: %s", ErrorDebugString(clientBootstrap->LastError()));
// cppcheck-suppress nullPointerRedundantCheck
return clientBootstrap->LastError();
}
/*
* Now Create a client. This can not throw.
* An instance of a client must outlive its connections.
* It is the users responsibility to make sure of this.
*/
mqttClient = unique_ptr<MqttClient>(new MqttClient(*clientBootstrap));
return SharedCrtResourceManager::SUCCESS;
}
void SharedCrtResourceManager::initializeAWSHttpLib()
{
if (!initialized)
{
LOG_WARN(TAG, "Tried to aws_http_library_init but the SharedCrtResourceManager has not yet been initialized!");
return;
}
if (initializedAWSHttpLib)
{
LOG_WARN(TAG, "Tried to aws_http_library_init but it was already initialized!");
return;
}
aws_http_library_init(getAllocator());
initializedAWSHttpLib = true;
}
int SharedCrtResourceManager::establishConnection(const PlainConfig &config)
{
if (!locateCredentials(config))
{
LOGM_ERROR(
TAG,
"*** %s: Failed to find file(s) with correct permissions required for establishing the MQTT connection ***",
DeviceClient::DC_FATAL_ERROR);
return SharedCrtResourceManager::ABORT;
}
Aws::Iot::MqttClientConnectionConfigBuilder clientConfigBuilder;
if (config.secureElement.enabled)
{
std::shared_ptr<Io::Pkcs11Lib> pkcs11Lib =
Aws::Crt::Io::Pkcs11Lib::Create(config.secureElement.pkcs11Lib.value().c_str(), allocator);
if (!pkcs11Lib)
{
LOGM_INFO(TAG, "Pkcs11Lib failed: %s", ErrorDebugString(Aws::Crt::LastError()));
return ABORT;
}
Io::TlsContextPkcs11Options pkcs11Options(pkcs11Lib);
pkcs11Options.SetCertificateFilePath(config.cert->c_str());
pkcs11Options.SetUserPin(config.secureElement.secureElementPin->c_str());
if (config.secureElement.secureElementTokenLabel.has_value() &&
!config.secureElement.secureElementTokenLabel->empty())
{
pkcs11Options.SetTokenLabel(config.secureElement.secureElementTokenLabel->c_str());
}
if (config.secureElement.secureElementSlotId.has_value())
{
pkcs11Options.SetSlotId(config.secureElement.secureElementSlotId.value());
}
if (config.secureElement.secureElementKeyLabel.has_value() &&
!config.secureElement.secureElementKeyLabel->empty())
{
pkcs11Options.SetPrivateKeyObjectLabel(config.secureElement.secureElementKeyLabel->c_str());
}
clientConfigBuilder = MqttClientConnectionConfigBuilder(pkcs11Options);
}
else
{
clientConfigBuilder = MqttClientConnectionConfigBuilder(config.cert->c_str(), config.key->c_str());
}
clientConfigBuilder.WithEndpoint(config.endpoint->c_str());
if (config.rootCa.has_value() && !config.rootCa->empty())
{
clientConfigBuilder.WithCertificateAuthority(config.rootCa->c_str());
}
clientConfigBuilder.WithSdkName(SharedCrtResourceManager::BINARY_NAME);
clientConfigBuilder.WithSdkVersion(DEVICE_CLIENT_VERSION);
PlainConfig::HttpProxyConfig proxyConfig = config.httpProxyConfig;
Aws::Crt::Http::HttpClientConnectionProxyOptions proxyOptions;
if (proxyConfig.httpProxyEnabled)
{
proxyOptions.HostName = proxyConfig.proxyHost->c_str();
proxyOptions.Port = proxyConfig.proxyPort.value();
proxyOptions.ProxyConnectionType = Aws::Crt::Http::AwsHttpProxyConnectionType::Tunneling;
LOGM_INFO(
TAG,
"Attempting to establish MQTT connection with proxy: %s:%u",
proxyOptions.HostName.c_str(),
proxyOptions.Port);
if (proxyConfig.httpProxyAuthEnabled)
{
LOG_INFO(TAG, "Proxy Authentication is enabled");
Aws::Crt::Http::HttpProxyStrategyBasicAuthConfig basicAuthConfig;
basicAuthConfig.ConnectionType = Aws::Crt::Http::AwsHttpProxyConnectionType::Tunneling;
proxyOptions.AuthType = Aws::Crt::Http::AwsHttpProxyAuthenticationType::Basic;
basicAuthConfig.Username = proxyConfig.proxyUsername->c_str();
basicAuthConfig.Password = proxyConfig.proxyPassword->c_str();
proxyOptions.ProxyStrategy =
Aws::Crt::Http::HttpProxyStrategy::CreateBasicHttpProxyStrategy(basicAuthConfig, Aws::Crt::g_allocator);
}
else
{
LOG_INFO(TAG, "Proxy Authentication is disabled");
proxyOptions.AuthType = Aws::Crt::Http::AwsHttpProxyAuthenticationType::None;
}
clientConfigBuilder.WithHttpProxyOptions(proxyOptions);
}
auto clientConfig = clientConfigBuilder.Build();
if (!clientConfig)
{
LOGM_ERROR(
TAG,
"MQTT Client Configuration initialization failed with error: %s",
ErrorDebugString(clientConfig.LastError()));
return ABORT;
}
connection = mqttClient->NewConnection(clientConfig);
if (config.fleetProvisioningRuntimeConfig.completedFleetProvisioning && config.lastWillTopic.has_value() && config.lastWillMessage.has_value()) {
Aws::Crt::ByteBuf payload = Aws::Crt::ByteBufFromCString(config.lastWillMessage->c_str());
if (connection->SetWill(config.lastWillTopic->c_str(), Aws::Crt::Mqtt::QOS::AWS_MQTT_QOS_AT_LEAST_ONCE, false, payload)) {
LOG_INFO(TAG, "MQTT connection set will succeeded");
} else {
LOG_INFO(TAG, "MQTT connection set will failed");
}
}
if (!*connection)
{
LOGM_ERROR(TAG, "MQTT Connection Creation failed with error: %s", ErrorDebugString(connection->LastError()));
return ABORT;
}
promise<int> connectionCompletedPromise;
connectionClosedPromise = std::promise<void>();
/*
* This will execute when an mqtt connect has completed or failed.
*/
auto onConnectionCompleted = [this, &connectionCompletedPromise](
const Mqtt::MqttConnection &, int errorCode, Mqtt::ReturnCode returnCode, bool) {
if (errorCode)
{
LOGM_ERROR(TAG, "MQTT Connection failed with error: %s", ErrorDebugString(errorCode));
if (AWS_ERROR_MQTT_UNEXPECTED_HANGUP == errorCode)
{
LOG_ERROR(
TAG,
"*** Did you make sure you are using valid certificate with recommended policy attached to it? "
"Please refer README->Fleet Provisioning Feature section for more details on recommended policies "
"for AWS IoT Device Client. ***");
}
connectionCompletedPromise.set_value(errorCode);
}
else
{
LOGM_INFO(TAG, "MQTT connection established with return code: %d", returnCode);
connectionCompletedPromise.set_value(0);
}
};
/*
* Invoked when a disconnect message has completed.
*/
auto onDisconnect = [this](const Mqtt::MqttConnection & /*conn*/) {
{
LOG_INFO(TAG, "MQTT Connection is now disconnected");
connectionClosedPromise.set_value();
}
};
/*
* Invoked when connection is interrupted.
*/
auto OnConnectionInterrupted = [this](const Mqtt::MqttConnection &, int errorCode) {
{
if (errorCode)
{
LOGM_ERROR(
TAG,
"MQTT Connection interrupted with error: `%s`. Device Client will retry connection until it is "
"successfully connected to the core. ",
ErrorDebugString(errorCode));
}
}
};
/*
* Invoked when connection is resumed.
*/
auto OnConnectionResumed = [this](const Mqtt::MqttConnection &, int returnCode, bool) {
{
LOGM_INFO(TAG, "MQTT connection resumed with return code: %d", returnCode);
}
};
connection->OnConnectionCompleted = move(onConnectionCompleted);
connection->OnDisconnect = move(onDisconnect);
connection->OnConnectionInterrupted = move(OnConnectionInterrupted);
connection->OnConnectionResumed = move(OnConnectionResumed);
LOGM_INFO(TAG, "Establishing MQTT connection with client id %s...", config.thingName->c_str());
if (!connection->SetReconnectTimeout(15, 240))
{
LOG_ERROR(TAG, "Device Client is not able to set reconnection settings. Device Client will retry again.");
return RETRY;
}
int keepAliveTimeSecs = 0;
if (config.connectKeepAlive.has_value()) {
keepAliveTimeSecs = *config.connectKeepAlive;
}
int pingTimeoutMs = 0;
if (config.connectTimeout.has_value()) {
pingTimeoutMs = *config.connectTimeout;
}
if (!connection->Connect(config.thingName->c_str(), false, keepAliveTimeSecs, pingTimeoutMs))
{
LOGM_ERROR(TAG, "MQTT Connection failed with error: %s", ErrorDebugString(connection->LastError()));
return RETRY;
}
int connectionStatus = connectionCompletedPromise.get_future().get();
if (SharedCrtResourceManager::SUCCESS == connectionStatus)
{
LOG_INFO(TAG, "Shared MQTT connection is ready!");
return SharedCrtResourceManager::SUCCESS;
}
else
{
LOG_ERROR(TAG, "Failed to establish shared MQTT connection, but will attempt retry...");
return SharedCrtResourceManager::RETRY;
}
}
shared_ptr<MqttConnection> SharedCrtResourceManager::getConnection()
{
if (!initialized)
{
LOG_WARN(TAG, "Tried to get connection but the SharedCrtResourceManager has not yet been initialized!");
return nullptr;
}
return connection;
}
EventLoopGroup *SharedCrtResourceManager::getEventLoopGroup()
{
if (!initialized)
{
LOG_WARN(TAG, "Tried to get eventLoopGroup but the SharedCrtResourceManager has not yet been initialized!");
return nullptr;
}
return eventLoopGroup.get();
}
aws_event_loop *SharedCrtResourceManager::getNextEventLoop()
{
if (!initialized)
{
LOG_WARN(TAG, "Tried to get eventLoop but the SharedCrtResourceManager has not yet been initialized!");
return nullptr;
}
return aws_event_loop_group_get_next_loop(eventLoopGroup->GetUnderlyingHandle());
}
aws_allocator *SharedCrtResourceManager::getAllocator()
{
if (!initialized)
{
LOG_WARN(TAG, "Tried to get allocator but the SharedCrtResourceManager has not yet been initialized!");
return nullptr;
}
return allocator;
}
Aws::Crt::Io::ClientBootstrap *SharedCrtResourceManager::getClientBootstrap()
{
if (!initialized)
{
LOG_WARN(TAG, "Tried to get clientBootstrap but the SharedCrtResourceManager has not yet been initialized!");
return nullptr;
}
return clientBootstrap.get();
}
void SharedCrtResourceManager::disconnect()
{
LOG_DEBUG(TAG, "Attempting to disconnect MQTT connection");
if (connection == NULL)
{
return;
}
if (connection->Disconnect())
{
if (connectionClosedPromise.get_future().wait_for(std::chrono::seconds(DEFAULT_WAIT_TIME_SECONDS)) ==
future_status::timeout)
{
LOG_ERROR(TAG, "MQTT Connection timed out to disconnect.");
}
}
else
{
LOG_ERROR(TAG, "MQTT Connection failed to disconnect");
}
}
void SharedCrtResourceManager::startDeviceClientFeatures() const
{
LOG_INFO(TAG, "Starting Device Client features.");
features->startAll();
}
void SharedCrtResourceManager::dumpMemTrace()
{
if (memTraceLevel != AWS_MEMTRACE_NONE)
{
aws_mem_tracer_dump(allocator);
}
}