astarte-platform / astarte-device-sdk-qt5

Astarte Qt5 Device SDK
http://astarte-platform.org/
Apache License 2.0
11 stars 12 forks source link

Allow support for Pairing using specific port #42

Open rickvargas opened 2 years ago

rickvargas commented 2 years ago

Description:

The Astarte SDK requests to Pairing API only works with 80/443 ports (differently from the connection to the broker that is able to work with different ports). Depending on the security policy related to open ports from different systems, it could be the case which the Astarte SDK needs to work with single port.

Environment:

Will happen on all versions.

Steps:

Deploy Astarte with a port that not 443:

  1. Configure transport.conf to have endpoint=https://api.astarte.myinstance.com:8883/pairing/v1/myrealm;
  2. Start system that has astarte-device-sdk;
  3. See Pairing Request will fail as port 80/443 has been used and service is running in 8883 port;

Current Results:

See Pairing Request will fail as port 80/443 has been used and service is running in 8883 port.

Expected Results (Suggestions):

Pairing Request to use port 8883.

Additional Information:

None.

rickvargas commented 2 years ago

In order to solve this I have made the following changes:

diff --git a/transports/astartehttpendpoint.cpp b/transports/astartehttpendpoint.cpp
index ad208e1..11fdb13 100644
--- a/transports/astartehttpendpoint.cpp
+++ b/transports/astartehttpendpoint.cpp
@@ -58,6 +58,7 @@ void HTTPEndpointPrivate::connectToEndpoint()
 {
     QUrl infoEndpoint = endpoint;
     infoEndpoint.setPath(QStringLiteral("%1/devices/%2").arg(endpoint.path()).arg(QString::fromLatin1(hardwareId)));
+    infoEndpoint.setPort(endpoint.port());
     QNetworkRequest req(infoEndpoint);
     #if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
     req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
@@ -270,6 +271,7 @@ QNetworkReply *HTTPEndpoint::sendRequest(const QString& relativeEndpoint, const
         // Build the endpoint
         QUrl target = d->endpoint;
         target.setPath(QStringLiteral("%1/devices/%2%3").arg(d->endpoint.path()).arg(QString::fromLatin1(d->hardwareId)).arg(relativeEndpoint));
+        target.setPort(d->endpoint.port());
         req.setUrl(target);

         req.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json"));
diff --git a/transports/defaultcredentialssecretprovider.cpp b/transports/defaultcredentialssecretprovider.cpp
index e71e6e5..50ca0f7 100644
--- a/transports/defaultcredentialssecretprovider.cpp
+++ b/transports/defaultcredentialssecretprovider.cpp
@@ -117,6 +117,7 @@ void DefaultCredentialsSecretProvider::sendRegistrationRequest()
     req.setRawHeader("Authorization", "Bearer " + m_pairingJwt);
     QUrl reqUrl = m_endpointUrl;
     reqUrl.setPath(m_endpointUrl.path() + QStringLiteral("/agent/devices"));
+    reqUrl.setPort(m_endpointUrl.port());
     req.setUrl(reqUrl);

     qCDebug(defaultCredSecretProviderDC) << "Sending registration request.";