Skip to content

Commit

Permalink
Fix samples
Browse files Browse the repository at this point in the history
  • Loading branch information
alfred2g committed Dec 4, 2023
1 parent 4c65765 commit 6214f95
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 91 deletions.
35 changes: 18 additions & 17 deletions samples/mqtt/websocket_connect/README_custom_auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,30 @@ For this sample, using Websockets will attempt to fetch the AWS credentials to a
<details>
<summary> (code snipet to replace similar section)</summary>
<pre language="c++"> <code>
Utils::cmdData cmdData = Utils::parseSampleInputCustomAuthorizerConnect(argc, argv, &apiHandle);

// Create the MQTT builder and populate it with data from cmdData.
Aws::Iot::MqttClient client;
void connection_setup(int argc, char *argv[], ApiHandle &apiHandle, Utils::cmdData &cmdData,
Aws::Iot::MqttClientConnectionConfigBuilder &clientConfigBuilder)
{
cmdData = Utils::parseSampleInputCustomAuthorizerConnect(argc, argv, &apiHandle);

Aws::Crt::Auth::CredentialsProviderChainDefaultConfig defaultConfig;
Aws::Crt::Auth::CredentialsProviderChainDefaultConfig defaultConfig;

std::shared_ptr<Aws::Crt::Auth::ICredentialsProvider> provider =
Aws::Crt::Auth::CredentialsProvider::CreateCredentialsProviderChainDefault(defaultConfig);
std::shared_ptr<Aws::Crt::Auth::ICredentialsProvider> provider =
Aws::Crt::Auth::CredentialsProvider::CreateCredentialsProviderChainDefault(defaultConfig);

Aws::Iot::WebsocketConfig websocketConfig((cmdData.input_signingRegion), provider);
Aws::Iot::WebsocketConfig websocketConfig((cmdData.input_signingRegion), provider);

auto clientConfigBuilder = Aws::Iot::MqttClientConnectionConfigBuilder(websocketConfig);
clientConfigBuilder = Aws::Iot::MqttClientConnectionConfigBuilder(websocketConfig);

clientConfigBuilder.WithEndpoint((cmdData.input_endpoint));
clientConfigBuilder.WithEndpoint((cmdData.input_endpoint));

clientConfigBuilder.WithCustomAuthorizer(
(cmdData.input_customAuthUsername),
(cmdData.input_customAuthorizerName),
(cmdData.input_customAuthorizerSignature),
(cmdData.input_customAuthPassword),
(cmdData.input_customTokenKeyName),
(cmdData.input_customTokenValue));
clientConfigBuilder.WithCustomAuthorizer(
(cmdData.input_customAuthUsername),
(cmdData.input_customAuthorizerName),
(cmdData.input_customAuthorizerSignature),
(cmdData.input_customAuthPassword),
(cmdData.input_customTokenKeyName),
(cmdData.input_customTokenValue));
}
</code><pre>
</details>

Expand Down
67 changes: 34 additions & 33 deletions samples/mqtt/websocket_connect/README_proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,43 +36,44 @@ For this sample, using Websockets will attempt to fetch the AWS credentials to a
</details>

<details>
<summary> (code snipet to replace similar section)</summary>
<summary> (code snipet to replace the connection_setup function)</summary>
<pre language="c++"> <code>
Utils::cmdData cmdData = Utils::parseSampleInputWebsocketConnect(argc, argv, &apiHandle);

// Create the MQTT builder and populate it with data from cmdData.
Aws::Iot::MqttClient client;
Aws::Iot::MqttClientConnectionConfigBuilder clientConfigBuilder;
std::shared_ptr<Aws::Crt::Auth::ICredentialsProvider> provider = nullptr;
Aws::Crt::Auth::CredentialsProviderChainDefaultConfig defaultConfig;
provider = Aws::Crt::Auth::CredentialsProvider::CreateCredentialsProviderChainDefault(defaultConfig);
if (!provider)
void connection_setup(int argc, char *argv[], ApiHandle &apiHandle, Utils::cmdData &cmdData,
Aws::Iot::MqttClientConnectionConfigBuilder &clientConfigBuilder)
{
fprintf(stderr, "Failure to create credentials provider!\n");
exit(-1);
}
Aws::Iot::WebsocketConfig config(cmdData.input_signingRegion, provider);
clientConfigBuilder = Aws::Iot::MqttClientConnectionConfigBuilder(config);
if (cmdData.input_ca != "")
{
clientConfigBuilder.WithCertificateAuthority(cmdData.input_ca.c_str());
}
if (cmdData.input_proxyHost == "")
{
fprintf(stderr, "proxy address missing!\n");
exit(-1);
}
Aws::Crt::Http::HttpClientConnectionProxyOptions proxyOptions;
proxyOptions.HostName = cmdData.input_proxyHost;
proxyOptions.Port = static_cast<uint16_t>(cmdData.input_proxyPort);
proxyOptions.AuthType = Aws::Crt::Http::AwsHttpProxyAuthenticationType::None;
clientConfigBuilder.WithHttpProxyOptions(proxyOptions);
Utils::cmdData cmdData = Utils::parseSampleInputWebsocketConnect(argc, argv, &apiHandle);

if (cmdData.input_port != 0)
{
clientConfigBuilder.WithPortOverride(static_cast<uint16_t>(cmdData.input_port));
std::shared_ptr<Aws::Crt::Auth::ICredentialsProvider> provider = nullptr;
Aws::Crt::Auth::CredentialsProviderChainDefaultConfig defaultConfig;
provider = Aws::Crt::Auth::CredentialsProvider::CreateCredentialsProviderChainDefault(defaultConfig);
if (!provider)
{
fprintf(stderr, "Failure to create credentials provider!\n");
exit(-1);
}
Aws::Iot::WebsocketConfig config(cmdData.input_signingRegion, provider);
clientConfigBuilder = Aws::Iot::MqttClientConnectionConfigBuilder(config);
if (cmdData.input_ca != "")
{
clientConfigBuilder.WithCertificateAuthority(cmdData.input_ca.c_str());
}
if (cmdData.input_proxyHost == "")
{
fprintf(stderr, "proxy address missing!\n");
exit(-1);
}
Aws::Crt::Http::HttpClientConnectionProxyOptions proxyOptions;
proxyOptions.HostName = cmdData.input_proxyHost;
proxyOptions.Port = static_cast<uint16_t>(cmdData.input_proxyPort);
proxyOptions.AuthType = Aws::Crt::Http::AwsHttpProxyAuthenticationType::None;
clientConfigBuilder.WithHttpProxyOptions(proxyOptions);

if (cmdData.input_port != 0)
{
clientConfigBuilder.WithPortOverride(static_cast<uint16_t>(cmdData.input_port));
}
clientConfigBuilder.WithEndpoint(cmdData.input_endpoint);
}
clientConfigBuilder.WithEndpoint(cmdData.input_endpoint);
</code>
</pre>
</details>
Expand Down
27 changes: 15 additions & 12 deletions samples/mqtt/websocket_connect/README_static_credentials.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,26 @@ For this sample, using Websockets will attempt to fetch the AWS credentials to a
<summary> (code snipet to replace similar section)</summary>
<pre>
<pre language="c++"> <code>
Utils::cmdData cmdData = Utils::parseSampleInputWebsocketConnect(argc, argv, &apiHandle);
void connection_setup(int argc, char *argv[], ApiHandle &apiHandle, Utils::cmdData &cmdData,
Aws::Iot::MqttClientConnectionConfigBuilder &clientConfigBuilder)
{
cmdData = Utils::parseSampleInputWebsocketConnect(argc, argv, &apiHandle);

Aws::Crt::Auth::CredentialsProviderChainDefaultConfig defaultConfig;
Aws::Crt::Auth::CredentialsProviderChainDefaultConfig defaultConfig;

std::shared_ptr<Aws::Crt::Auth::ICredentialsProvider> provider = nullptr;
Aws::Crt::Auth::CredentialsProviderStaticConfig providerConfig;
std::shared_ptr<Aws::Crt::Auth::ICredentialsProvider> provider = nullptr;
Aws::Crt::Auth::CredentialsProviderStaticConfig providerConfig;

providerConfig.AccessKeyId = aws_byte_cursor_from_c_str((cmdData.input_accessKeyId.c_str()));
providerConfig.SecretAccessKey = aws_byte_cursor_from_c_str((cmdData.input_secretAccessKey.c_str()));
providerConfig.SessionToken = aws_byte_cursor_from_c_str((cmdData.input_sessionToken.c_str()));
providerConfig.AccessKeyId = aws_byte_cursor_from_c_str((cmdData.input_accessKeyId.c_str()));
providerConfig.SecretAccessKey = aws_byte_cursor_from_c_str((cmdData.input_secretAccessKey.c_str()));
providerConfig.SessionToken = aws_byte_cursor_from_c_str((cmdData.input_sessionToken.c_str()));

provider = Aws::Crt::Auth::CredentialsProvider::CreateCredentialsProviderStatic(providerConfig);
Aws::Iot::WebsocketConfig config(cmdData.input_signingRegion, provider);
provider = Aws::Crt::Auth::CredentialsProvider::CreateCredentialsProviderStatic(providerConfig);
Aws::Iot::WebsocketConfig config(cmdData.input_signingRegion, provider);

Aws::Iot::MqttClient client;
auto clientConfigBuilder = Aws::Iot::MqttClientConnectionConfigBuilder(config);
clientConfigBuilder.WithEndpoint((cmdData.input_endpoint));
clientConfigBuilder = Aws::Iot::MqttClientConnectionConfigBuilder(config);
clientConfigBuilder.WithEndpoint((cmdData.input_endpoint));
}
</code>
</pre>
</details>
Expand Down
31 changes: 17 additions & 14 deletions samples/mqtt/websocket_connect/README_username_password.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,25 @@ For this sample, using Websockets will attempt to fetch the AWS credentials to a
<details>
<summary> (code snipet to replace similar section)</summary>
<pre language="c++"> <code>
Utils::cmdData cmdData = Utils::parseSampleInputCustomAuthorizerConnect(argc, argv, &apiHandle);
void connection_setup(int argc, char *argv[], ApiHandle &apiHandle, Utils::cmdData &cmdData,
Aws::Iot::MqttClientConnectionConfigBuilder &clientConfigBuilder)
{
cmdData = Utils::parseSampleInputCustomAuthorizerConnect(argc, argv, &apiHandle);

// Create the MQTT builder and populate it with data from cmdData.
Aws::Iot::MqttClient client;
Aws::Crt::Auth::CredentialsProviderChainDefaultConfig defaultConfig;
std::shared_ptr<Aws::Crt::Auth::ICredentialsProvider> provider =
Aws::Crt::Auth::CredentialsProvider::CreateCredentialsProviderChainDefault(defaultConfig);
Aws::Iot::WebsocketConfig websocketConfig((cmdData.input_signingRegion), provider);
// Create the MQTT builder and populate it with data from cmdData.
Aws::Crt::Auth::CredentialsProviderChainDefaultConfig defaultConfig;
std::shared_ptr<Aws::Crt::Auth::ICredentialsProvider> provider =
Aws::Crt::Auth::CredentialsProvider::CreateCredentialsProviderChainDefault(defaultConfig);
Aws::Iot::WebsocketConfig websocketConfig((cmdData.input_signingRegion), provider);

auto clientConfigBuilder = Aws::Iot::MqttClientConnectionConfigBuilder(websocketConfig);
clientConfigBuilder.WithEndpoint((cmdData.input_endpoint));
clientConfigBuilder.WithCustomAuthorizer(
(cmdData.input_customAuthUsername),
(cmdData.input_customAuthorizerName),
(cmdData.input_customAuthorizerSignature),
(cmdData.input_customAuthPassword));
clientConfigBuilder = Aws::Iot::MqttClientConnectionConfigBuilder(websocketConfig);
clientConfigBuilder.WithEndpoint((cmdData.input_endpoint));
clientConfigBuilder.WithCustomAuthorizer(
(cmdData.input_customAuthUsername),
(cmdData.input_customAuthorizerName),
(cmdData.input_customAuthorizerSignature),
(cmdData.input_customAuthPassword));
}
</code>
</pre>
</details>
Expand Down
37 changes: 22 additions & 15 deletions samples/mqtt/websocket_connect/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,11 @@

using namespace Aws::Crt;

int main(int argc, char *argv[])
void connection_setup(int argc, char *argv[], ApiHandle &apiHandle, Utils::cmdData &cmdData,
Aws::Iot::MqttClientConnectionConfigBuilder &clientConfigBuilder)
{
/************************ Setup ****************************/

// Do the global initialization for the API.
ApiHandle apiHandle;

/**
* cmdData is the arguments/input from the command line placed into a single struct for
* use in this sample. This handles all of the command line parsing, validating, etc.
* See the Utils/CommandLineUtils for more information.
*/
Utils::cmdData cmdData = Utils::parseSampleInputWebsocketConnect(argc, argv, &apiHandle);
cmdData = Utils::parseSampleInputWebsocketConnect(argc, argv, &apiHandle);

// Create the MQTT builder and populate it with data from cmdData.
Aws::Iot::MqttClient client;
Aws::Iot::MqttClientConnectionConfigBuilder clientConfigBuilder;
std::shared_ptr<Aws::Crt::Auth::ICredentialsProvider> provider = nullptr;
Aws::Crt::Auth::CredentialsProviderChainDefaultConfig defaultConfig;
provider = Aws::Crt::Auth::CredentialsProvider::CreateCredentialsProviderChainDefault(defaultConfig);
Expand Down Expand Up @@ -61,6 +49,25 @@ int main(int argc, char *argv[])
clientConfigBuilder.WithPortOverride(static_cast<uint16_t>(cmdData.input_port));
}
clientConfigBuilder.WithEndpoint(cmdData.input_endpoint);
}

int main(int argc, char *argv[])
{
/************************ Setup ****************************/

// Do the global initialization for the API.
ApiHandle apiHandle;
/**
* cmdData is the arguments/input from the command line placed into a single struct for
* use in this sample. This handles all of the command line parsing, validating, etc.
* See the Utils/CommandLineUtils for more information.
*/
Utils::cmdData cmdData;
Aws::Iot::MqttClient client;
// Create the MQTT builder and populate it with data from cmdData.
Aws::Iot::MqttClientConnectionConfigBuilder clientConfigBuilder;

connection_setup(argc, argv, apiHandle, cmdData, clientConfigBuilder);

// Create the MQTT connection from the MQTT builder
auto clientConfig = clientConfigBuilder.Build();
Expand Down

0 comments on commit 6214f95

Please sign in to comment.