Skip to content

Commit e1ce83c

Browse files
authored
X509 Credentials Provider Binding (aws#151)
* X509 Binding * Submodules
1 parent c8695c9 commit e1ce83c

File tree

5 files changed

+102
-6
lines changed

5 files changed

+102
-6
lines changed

aws-common-runtime/aws-c-io

include/aws/crt/auth/Credentials.h

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include <aws/crt/Exports.h>
88
#include <aws/crt/Types.h>
9+
#include <aws/crt/http/HttpConnection.h>
10+
#include <aws/crt/io/TlsOptions.h>
911

1012
#include <chrono>
1113
#include <functional>
@@ -22,6 +24,11 @@ namespace Aws
2224
class ClientBootstrap;
2325
}
2426

27+
namespace Http
28+
{
29+
class HttpClientConnectionProxyOptions;
30+
}
31+
2532
namespace Auth
2633
{
2734
/**
@@ -228,6 +235,45 @@ namespace Aws
228235
Io::ClientBootstrap *Bootstrap;
229236
};
230237

238+
/**
239+
* Configuration options for the X509 credentials provider
240+
*/
241+
struct AWS_CRT_CPP_API CredentialsProviderX509Config
242+
{
243+
CredentialsProviderX509Config()
244+
: Bootstrap(nullptr), TlsOptions(), ThingName(), RoleAlias(), Endpoint(), ProxyOptions()
245+
{
246+
}
247+
248+
/**
249+
* Connection bootstrap to use to create the http connection required to
250+
* query credentials from the x509 provider
251+
*/
252+
Io::ClientBootstrap *Bootstrap;
253+
254+
/* TLS connection options that have been initialized with your x509 certificate and private key */
255+
Io::TlsConnectionOptions TlsOptions;
256+
257+
/* IoT thing name you registered with AWS IOT for your device, it will be used in http request header */
258+
String ThingName;
259+
260+
/* Iot role alias you created with AWS IoT for your IAM role, it will be used in http request path */
261+
String RoleAlias;
262+
263+
/**
264+
* AWS account specific endpoint that can be acquired using AWS CLI following instructions from the demo
265+
* example: c2sakl5huz0afv.credentials.iot.us-east-1.amazonaws.com
266+
*
267+
* This a different endpoint than the IoT data mqtt broker endpoint.
268+
*/
269+
String Endpoint;
270+
271+
/**
272+
* (Optional) Http proxy configuration for the http request that fetches credentials
273+
*/
274+
Optional<Http::HttpClientConnectionProxyOptions> ProxyOptions;
275+
};
276+
231277
/**
232278
* Simple credentials provider implementation that wraps one of the internal C-based implementations.
233279
*
@@ -313,13 +359,21 @@ namespace Aws
313359
/**
314360
* Creates the SDK-standard default credentials provider which is a cache-fronted chain of:
315361
*
316-
* Environment -> Profile -> IMDS
362+
* Environment -> Profile -> IMDS/ECS
317363
*
318364
*/
319365
static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderChainDefault(
320366
const CredentialsProviderChainDefaultConfig &config,
321367
Allocator *allocator = g_allocator);
322368

369+
/**
370+
* Creates a provider that sources credentials from the IoT X509 provider service
371+
*
372+
*/
373+
static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderX509(
374+
const CredentialsProviderX509Config &config,
375+
Allocator *allocator = g_allocator);
376+
323377
private:
324378
static void s_onCredentialsResolved(aws_credentials *credentials, int error_code, void *user_data);
325379

@@ -328,4 +382,4 @@ namespace Aws
328382
};
329383
} // namespace Auth
330384
} // namespace Crt
331-
} // namespace Aws
385+
} // namespace Aws

source/auth/Credentials.cpp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55

66
#include <aws/crt/auth/Credentials.h>
77

8+
#include <aws/crt/http/HttpConnection.h>
89
#include <aws/crt/io/Bootstrap.h>
910

1011
#include <aws/auth/credentials.h>
1112
#include <aws/common/string.h>
1213

1314
#include <algorithm>
15+
#include <aws/http/connection.h>
1416

1517
namespace Aws
1618
{
@@ -267,6 +269,45 @@ namespace Aws
267269
return s_CreateWrappedProvider(
268270
aws_credentials_provider_new_chain_default(allocator, &raw_config), allocator);
269271
}
272+
273+
std::shared_ptr<ICredentialsProvider> CredentialsProvider::CreateCredentialsProviderX509(
274+
const CredentialsProviderX509Config &config,
275+
Allocator *allocator)
276+
{
277+
struct aws_credentials_provider_x509_options raw_config;
278+
AWS_ZERO_STRUCT(raw_config);
279+
280+
raw_config.bootstrap = config.Bootstrap->GetUnderlyingHandle();
281+
raw_config.tls_connection_options = config.TlsOptions.GetUnderlyingHandle();
282+
raw_config.thing_name = aws_byte_cursor_from_c_str(config.ThingName.c_str());
283+
raw_config.role_alias = aws_byte_cursor_from_c_str(config.RoleAlias.c_str());
284+
raw_config.endpoint = aws_byte_cursor_from_c_str(config.Endpoint.c_str());
285+
286+
struct aws_http_proxy_options proxy_options;
287+
AWS_ZERO_STRUCT(proxy_options);
288+
if (config.ProxyOptions.has_value())
289+
{
290+
const Http::HttpClientConnectionProxyOptions &proxy_config = config.ProxyOptions.value();
291+
292+
proxy_options.host = aws_byte_cursor_from_c_str(proxy_config.HostName.c_str());
293+
proxy_options.port = proxy_config.Port;
294+
proxy_options.tls_options = proxy_config.TlsOptions->GetUnderlyingHandle();
295+
proxy_options.auth_type = (enum aws_http_proxy_authentication_type)proxy_config.AuthType;
296+
proxy_options.auth_username = aws_byte_cursor_from_c_str(proxy_config.BasicAuthUsername.c_str());
297+
proxy_options.auth_password = aws_byte_cursor_from_c_str(proxy_config.BasicAuthPassword.c_str());
298+
299+
raw_config.proxy_options = &proxy_options;
300+
}
301+
302+
/**
303+
* Sets the TLS options for the proxy connection.
304+
* Optional.
305+
*/
306+
Optional<Io::TlsConnectionOptions> TlsOptions;
307+
308+
return s_CreateWrappedProvider(aws_credentials_provider_new_x509(allocator, &raw_config), allocator);
309+
}
310+
270311
} // namespace Auth
271312
} // namespace Crt
272-
} // namespace Aws
313+
} // namespace Aws

source/iot/MqttClient.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ namespace Aws
236236
return *this;
237237
}
238238

239-
MqttClientConnectionConfigBuilder &MqttClientConnectionConfigBuilder::WithMinimumTlsVersion(aws_tls_versions minimumTlsVersion) noexcept
239+
MqttClientConnectionConfigBuilder &MqttClientConnectionConfigBuilder::WithMinimumTlsVersion(
240+
aws_tls_versions minimumTlsVersion) noexcept
240241
{
241242
m_contextOptions.SetMinimumTlsVersion(minimumTlsVersion);
242243
return *this;

0 commit comments

Comments
 (0)