Search in sources :

Example 1 with MqttConnectionConfig

use of software.amazon.awssdk.crt.mqtt.MqttConnectionConfig in project aws-crt-java by awslabs.

the class ProxyTest method buildDirectMqttConnection.

private MqttClientConnection buildDirectMqttConnection(ProxyTestType testType, ProxyAuthType authType) {
    try (EventLoopGroup eventLoopGroup = new EventLoopGroup(1);
        HostResolver resolver = new HostResolver(eventLoopGroup);
        ClientBootstrap bootstrap = new ClientBootstrap(eventLoopGroup, resolver);
        TlsContext tlsContext = createX509TlsContext(null);
        MqttClient mqttClient = new MqttClient(bootstrap, tlsContext);
        MqttConnectionConfig connectionConfig = new MqttConnectionConfig();
        TlsContext proxyTlsContext = createProxyTlsContext(testType)) {
        HttpProxyOptions proxyOptions = buildProxyOptions(testType, authType, proxyTlsContext);
        String clientId = PROXY_TEST_CLIENTID + (UUID.randomUUID()).toString();
        connectionConfig.setMqttClient(mqttClient);
        connectionConfig.setEndpoint(MQTT_ENDPOINT);
        connectionConfig.setHttpProxyOptions(proxyOptions);
        connectionConfig.setCleanSession(true);
        connectionConfig.setClientId(clientId);
        connectionConfig.setPort(MQTT_DIRECT_PORT);
        connectionConfig.setProtocolOperationTimeoutMs(60000);
        return new MqttClientConnection(connectionConfig);
    }
}
Also used : MqttClient(software.amazon.awssdk.crt.mqtt.MqttClient) HttpProxyOptions(software.amazon.awssdk.crt.http.HttpProxyOptions) EventLoopGroup(software.amazon.awssdk.crt.io.EventLoopGroup) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) ClientBootstrap(software.amazon.awssdk.crt.io.ClientBootstrap) MqttConnectionConfig(software.amazon.awssdk.crt.mqtt.MqttConnectionConfig) ClientTlsContext(software.amazon.awssdk.crt.io.ClientTlsContext) TlsContext(software.amazon.awssdk.crt.io.TlsContext) HostResolver(software.amazon.awssdk.crt.io.HostResolver)

Example 2 with MqttConnectionConfig

use of software.amazon.awssdk.crt.mqtt.MqttConnectionConfig in project aws-iot-device-sdk-java-v2 by aws.

the class AwsIotMqttConnectionBuilder method build.

/**
 * Builds a new mqtt connection from the configuration stored in the builder.  Because some objects are created
 * lazily, certain properties should not be modified after this is first invoked (tls options, bootstrap).
 *
 * @return a new mqtt connection
 */
public MqttClientConnection build() {
    // Validate
    if (bootstrap == null) {
        bootstrap = ClientBootstrap.getOrCreateStaticDefault();
    }
    // has no affect on subsequently-created connections.
    synchronized (this) {
        // Check to see if a custom authorizer is being used but not through the builder.
        if (isUsingCustomAuthorizer == false) {
            if (config.getUsername() != null) {
                if (config.getUsername().contains("x-amz-customauthorizer-name=") || config.getUsername().contains("x-amz-customauthorizer-signature=")) {
                    isUsingCustomAuthorizer = true;
                }
            }
        }
        // Is the user trying to connect using a custom authorizer?
        if (isUsingCustomAuthorizer == true) {
            if (config.getPort() != 443) {
                Log.log(LogLevel.Warn, LogSubject.MqttClient, "Attempting to connect to authorizer with unsupported port. Port is not 443...");
            }
            if (tlsOptions.alpnList.size() == 1) {
                if (tlsOptions.alpnList.get(0) != "mqtt") {
                    tlsOptions.alpnList.clear();
                    tlsOptions.alpnList.add("mqtt");
                }
            } else {
                tlsOptions.alpnList.clear();
                tlsOptions.alpnList.add("mqtt");
            }
        }
        if (tlsOptions != null && (tlsContext == null || resetLazilyCreatedResources)) {
            try (ClientTlsContext clientTlsContext = new ClientTlsContext(tlsOptions)) {
                swapReferenceTo(tlsContext, clientTlsContext);
                tlsContext = clientTlsContext;
            }
        }
        if (client == null || resetLazilyCreatedResources) {
            try (MqttClient mqttClient = (tlsContext == null) ? new MqttClient(bootstrap) : new MqttClient(bootstrap, tlsContext)) {
                swapReferenceTo(client, mqttClient);
                client = mqttClient;
                config.setMqttClient(client);
            }
        }
    }
    resetLazilyCreatedResources = false;
    // Connection create
    try (MqttConnectionConfig connectionConfig = config.clone()) {
        // Whether or not a username has been added, append our metrics tokens
        String usernameOrEmpty = "";
        if (connectionConfig.getUsername() != null) {
            usernameOrEmpty = connectionConfig.getUsername();
        }
        String queryStringConcatenation = "?";
        if (usernameOrEmpty.contains("?")) {
            queryStringConcatenation = "&";
        }
        connectionConfig.setUsername(String.format("%s%sSDK=JavaV2&Version=%s", usernameOrEmpty, queryStringConcatenation, new PackageInfo().version.toString()));
        if (connectionConfig.getUseWebsockets() && connectionConfig.getWebsocketHandshakeTransform() == null) {
            if (websocketCredentialsProvider == null) {
                DefaultChainCredentialsProvider.DefaultChainCredentialsProviderBuilder providerBuilder = new DefaultChainCredentialsProvider.DefaultChainCredentialsProviderBuilder();
                providerBuilder.withClientBootstrap(bootstrap);
                try (CredentialsProvider defaultProvider = providerBuilder.build()) {
                    withWebsocketCredentialsProvider(defaultProvider);
                }
            }
            try (AwsSigningConfig signingConfig = new AwsSigningConfig()) {
                signingConfig.setAlgorithm(AwsSigningConfig.AwsSigningAlgorithm.SIGV4);
                signingConfig.setSignatureType(AwsSigningConfig.AwsSignatureType.HTTP_REQUEST_VIA_QUERY_PARAMS);
                signingConfig.setRegion(websocketSigningRegion);
                signingConfig.setService(IOT_SIGNING_SERVICE);
                signingConfig.setCredentialsProvider(websocketCredentialsProvider);
                signingConfig.setOmitSessionToken(true);
                try (AwsSigv4HandshakeTransformer transformer = new AwsSigv4HandshakeTransformer(signingConfig)) {
                    connectionConfig.setWebsocketHandshakeTransform(transformer);
                    /*
                         * transformer is actually a CrtResource since we track a SigningConfig (which tracks a Credentials Provider
                         * But the MqttConnectionConfig only knows of the transformer as a Consumer function, so it's not
                         * able to properly add a forward reference to the transformer.  So we do it manually here after setting.
                         */
                    connectionConfig.addReferenceTo(transformer);
                }
            }
        }
        return new MqttClientConnection(connectionConfig);
    }
}
Also used : MqttClient(software.amazon.awssdk.crt.mqtt.MqttClient) ClientTlsContext(software.amazon.awssdk.crt.io.ClientTlsContext) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) PackageInfo(software.amazon.awssdk.crt.utils.PackageInfo) AwsSigningConfig(software.amazon.awssdk.crt.auth.signing.AwsSigningConfig) MqttConnectionConfig(software.amazon.awssdk.crt.mqtt.MqttConnectionConfig) DefaultChainCredentialsProvider(software.amazon.awssdk.crt.auth.credentials.DefaultChainCredentialsProvider) DefaultChainCredentialsProvider(software.amazon.awssdk.crt.auth.credentials.DefaultChainCredentialsProvider) CredentialsProvider(software.amazon.awssdk.crt.auth.credentials.CredentialsProvider)

Example 3 with MqttConnectionConfig

use of software.amazon.awssdk.crt.mqtt.MqttConnectionConfig in project aws-greengrass-nucleus by aws-greengrass.

the class WrapperMqttClientConnection method getMqttConnectionConfig.

/*
     * This is to initialize a valid MqttConnectionConfig which could be used
     * in the WrapperMqttClientConnection
     *
     * @return MqttConnectionConfig
     */
private static MqttConnectionConfig getMqttConnectionConfig() {
    try (EventLoopGroup eventLoopGroup = new EventLoopGroup(0);
        HostResolver resolver = new HostResolver(eventLoopGroup);
        ClientBootstrap clientBootstrap = new ClientBootstrap(eventLoopGroup, resolver);
        software.amazon.awssdk.crt.mqtt.MqttClient oldMqttClient = new software.amazon.awssdk.crt.mqtt.MqttClient(clientBootstrap)) {
        String fakeClientId = "fakeClientId";
        String fakeEndpoint = "fakeEndpoint";
        int fakePortNumber = 1;
        MqttConnectionConfig fakeConfig = new MqttConnectionConfig();
        fakeConfig.setMqttClient(oldMqttClient);
        fakeConfig.setPort(fakePortNumber);
        fakeConfig.setClientId(fakeClientId);
        fakeConfig.setEndpoint(fakeEndpoint);
        return fakeConfig;
    }
}
Also used : HostResolver(software.amazon.awssdk.crt.io.HostResolver) EventLoopGroup(software.amazon.awssdk.crt.io.EventLoopGroup) ClientBootstrap(software.amazon.awssdk.crt.io.ClientBootstrap) MqttConnectionConfig(software.amazon.awssdk.crt.mqtt.MqttConnectionConfig)

Aggregations

MqttConnectionConfig (software.amazon.awssdk.crt.mqtt.MqttConnectionConfig)3 ClientBootstrap (software.amazon.awssdk.crt.io.ClientBootstrap)2 ClientTlsContext (software.amazon.awssdk.crt.io.ClientTlsContext)2 EventLoopGroup (software.amazon.awssdk.crt.io.EventLoopGroup)2 HostResolver (software.amazon.awssdk.crt.io.HostResolver)2 MqttClient (software.amazon.awssdk.crt.mqtt.MqttClient)2 MqttClientConnection (software.amazon.awssdk.crt.mqtt.MqttClientConnection)2 CredentialsProvider (software.amazon.awssdk.crt.auth.credentials.CredentialsProvider)1 DefaultChainCredentialsProvider (software.amazon.awssdk.crt.auth.credentials.DefaultChainCredentialsProvider)1 AwsSigningConfig (software.amazon.awssdk.crt.auth.signing.AwsSigningConfig)1 HttpProxyOptions (software.amazon.awssdk.crt.http.HttpProxyOptions)1 TlsContext (software.amazon.awssdk.crt.io.TlsContext)1 PackageInfo (software.amazon.awssdk.crt.utils.PackageInfo)1