Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mqtt): mqtt over proxy no longer requires websockets #994

Merged
merged 2 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ public class DeviceProvisioningHelper {
+ " \"logs:CreateLogStream\",\n"
+ " \"logs:PutLogEvents\",\n"
+ " \"logs:DescribeLogStreams\",\n"
+ " \"iot:Connect\",\n"
+ " \"iot:Publish\",\n"
+ " \"iot:Subscribe\",\n"
+ " \"iot:Receive\",\n"
+ " \"s3:GetBucketLocation\"\n"
+ " ],\n"
+ " \"Resource\": \"*\"\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.aws.greengrass.logging.api.Logger;
import com.aws.greengrass.logging.impl.LogManager;
import com.aws.greengrass.util.Coerce;
import com.aws.greengrass.util.ProxyUtils;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -248,12 +247,6 @@ private CompletableFuture<Boolean> establishConnection(boolean overrideCleanSess
if (error != null) {
connectionCleanup();
logger.atError().log("Unable to connect to AWS IoT Core", error);
if (ProxyUtils.getProxyConfiguration() != null) {
logger.atInfo().log("You are using a proxy which uses a websocket connection and "
+ "TokenExchangeService credentials. Verify that the IAM role which the IoT Role "
+ "Alias is aliasing has a policy which allows for iot:Connect, iot:Subscribe, "
+ "iot:Publish, and iot:Receive.");
}
}
});
}
Expand Down
47 changes: 7 additions & 40 deletions src/main/java/com/aws/greengrass/mqttclient/MqttClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@
import software.amazon.awssdk.crt.auth.credentials.X509CredentialsProvider;
import software.amazon.awssdk.crt.http.HttpProxyOptions;
import software.amazon.awssdk.crt.io.ClientBootstrap;
import software.amazon.awssdk.crt.io.ClientTlsContext;
import software.amazon.awssdk.crt.io.EventLoopGroup;
import software.amazon.awssdk.crt.io.HostResolver;
import software.amazon.awssdk.crt.io.SocketOptions;
import software.amazon.awssdk.crt.io.TlsContextOptions;
import software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents;
import software.amazon.awssdk.crt.mqtt.MqttMessage;
import software.amazon.awssdk.iot.AwsIotMqttConnectionBuilder;
Expand Down Expand Up @@ -164,10 +162,8 @@ public MqttClient(DeviceConfiguration deviceConfiguration, ScheduledExecutorServ
ExecutorService executorService) {
this(deviceConfiguration, null, ses, executorService);

HttpProxyOptions httpProxyOptions = ProxyUtils.getHttpProxyOptions(deviceConfiguration);

if (httpProxyOptions == null) {
this.builderProvider = (clientBootstrap) -> AwsIotMqttConnectionBuilder
this.builderProvider = (clientBootstrap) -> {
AwsIotMqttConnectionBuilder builder = AwsIotMqttConnectionBuilder
.newMtlsBuilderFromPath(Coerce.toString(deviceConfiguration.getCertificateFilePath()),
Coerce.toString(deviceConfiguration.getPrivateKeyFilePath()))
.withCertificateAuthorityFromPath(null, Coerce.toString(deviceConfiguration.getRootCAFilePath()))
Expand All @@ -180,41 +176,12 @@ public MqttClient(DeviceConfiguration deviceConfiguration, ScheduledExecutorServ
Coerce.toInt(mqttTopics.findOrDefault(DEFAULT_MQTT_PING_TIMEOUT, MQTT_PING_TIMEOUT_KEY)))
.withSocketOptions(new SocketOptions()).withTimeoutMs(Coerce.toInt(
mqttTopics.findOrDefault(DEFAULT_MQTT_SOCKET_TIMEOUT, MQTT_SOCKET_TIMEOUT_KEY)));
} else {
String tesRoleAlias = Coerce.toString(deviceConfiguration.getIotRoleAlias());

try (TlsContextOptions x509TlsOptions = TlsContextOptions
.createWithMtlsFromPath(Coerce.toString(deviceConfiguration.getCertificateFilePath()),
Coerce.toString(deviceConfiguration.getPrivateKeyFilePath()))) {

x509TlsOptions.withCertificateAuthorityFromPath(null,
Coerce.toString(deviceConfiguration.getRootCAFilePath()));

try (ClientTlsContext x509TlsContext = new ClientTlsContext(x509TlsOptions)) {
this.credentialsProvider = new X509CredentialsProvider.X509CredentialsProviderBuilder()
.withClientBootstrap(clientBootstrap).withTlsContext(x509TlsContext)
.withEndpoint(Coerce.toString(deviceConfiguration.getIotCredentialEndpoint()))
.withRoleAlias(tesRoleAlias)
.withThingName(Coerce.toString(deviceConfiguration.getThingName()))
.withProxyOptions(httpProxyOptions).build();

this.builderProvider =
(clientBootstrap) -> AwsIotMqttConnectionBuilder.newMtlsBuilderFromPath(null, null)
.withEndpoint(Coerce.toString(deviceConfiguration.getIotDataEndpoint()))
.withCleanSession(false).withBootstrap(clientBootstrap).withKeepAliveMs(
Coerce.toInt(mqttTopics.findOrDefault(DEFAULT_MQTT_KEEP_ALIVE_TIMEOUT,
MQTT_KEEP_ALIVE_TIMEOUT_KEY)))
.withProtocolOperationTimeoutMs(getMqttOperationTimeoutMillis())
.withPingTimeoutMs(Coerce.toInt(
mqttTopics.findOrDefault(DEFAULT_MQTT_PING_TIMEOUT, MQTT_PING_TIMEOUT_KEY)))
.withSocketOptions(new SocketOptions()).withTimeoutMs(Coerce.toInt(mqttTopics
.findOrDefault(DEFAULT_MQTT_SOCKET_TIMEOUT, MQTT_SOCKET_TIMEOUT_KEY)))
.withWebsockets(true).withWebsocketCredentialsProvider(credentialsProvider)
.withWebsocketSigningRegion(Coerce.toString(deviceConfiguration.getAWSRegion()))
.withWebsocketProxyOptions(httpProxyOptions);
}
HttpProxyOptions httpProxyOptions = ProxyUtils.getHttpProxyOptions(deviceConfiguration);
if (httpProxyOptions != null) {
builder.withHttpProxyOptions(httpProxyOptions);
}
}
return builder;
};
}

protected MqttClient(DeviceConfiguration deviceConfiguration,
Expand Down