Skip to content

Allow modifying websocket requests.#642

Merged
yatinmaan merged 1 commit intobytebeamio:mainfrom
reitermarkus:aws-iot
Jul 10, 2023
Merged

Allow modifying websocket requests.#642
yatinmaan merged 1 commit intobytebeamio:mainfrom
reitermarkus:aws-iot

Conversation

@reitermarkus
Copy link
Contributor

@reitermarkus reitermarkus commented Jun 11, 2023

I am trying to connect to an AWS IoT Shadow via MQTT and SigV4 but couldn't find any way to add additional headers to the request. This adds a way to modify the request before connecting.

Example usage:

    mqtt_options.set_request_modifier(move |request| {
      let credential_provider = credential_provider.clone();
      let region = region.clone();

      async move {
          let request_config = RequestConfig {
              request_ts: SystemTime::now(),
              region: &SigningRegion::from(region.clone()),
              service: &SigningService::from_static("iotdata"),
              payload_override: None,
          };

        let (parts, body) = request.into_parts();
        let mut request: http::Request<SdkBody> = http::Request::from_parts(parts, SdkBody::empty());

        let signer = SigV4Signer::new();
        signer.sign(
          &OperationSigningConfig::default_config(),
          &request_config,
          &credential_provider.provide_credentials().await.unwrap(),
          &mut request,
        ).unwrap();

        let (parts, _) = request.into_parts();
        http::Request::from_parts(parts, body)
      }
    });

Type of change

  • New feature (non-breaking change which adds functionality)

Checklist:

  • Formatted with cargo fmt
  • Make an entry to CHANGELOG.md if its relevant of user of the library. If its not relevant mention why.

@h3nill
Copy link

h3nill commented Jun 12, 2023

@reitermarkus can you check CI failures?

Also I am not sure if the updated Cargo.lock file is required here, remove it if its not relevant with this changes.

@reitermarkus
Copy link
Contributor Author

Added #[cfg(feature = "websocket")] for the added parts and removed the Cargo.lock changes.

@reitermarkus
Copy link
Contributor Author

Added a RequestModifierFn type alias to fix the complex type warning.

@yatinmaan
Copy link
Contributor

@reitermarkus

Do you have any specific reason you chose to go with mqtt_options.set_request_modifier(Fn(http::Request<()>)) instead of just passing in a fully formed request with something like mqtt_options.set_request(http::Request)?

You could modify the request later (for re-connections etc.) via eventloop.mqttoptions.set_request(updated_req). This seems much simpler to me. Unless there's a use case I missed which can't be serviced like this?

@reitermarkus
Copy link
Contributor Author

Well, I only need to set the auth/signature headers, so I don't want to also have to set all MQTT protocol-specific headers.

Also, the AWS ProvideCredentials trait may be implemented in a such way that credentials are automatically refreshed, so no additional changes would be necessary when reconnecting.

@yatinmaan
Copy link
Contributor

@reitermarkus

Well, I only need to set the auth/signature headers, so I don't want to also have to set all MQTT protocol-specific headers.

Yeah, I meant doing it such that rumqttc would still add the required missing headers to the, otherwise "fully formed", user supplied request. Now that I think of it, it'll be even better to just let the user pass in any additional headers that rumqttc then adds to the request itself.

Also, the AWS ProvideCredentials trait may be implemented in a such way that credentials are automatically refreshed, so no additional changes would be necessary when reconnecting.

This one is a slight advantage. But it'll still be possible to do this with .set_headers,just that you would have to manually update it if/when needed for re-connections.

I see the niceties of this closure style API but for rumqttc at the moment passing in additional headers would be simpler and more in line with how rest of the API works (eg see the manual acks examples).

@reitermarkus
Copy link
Contributor Author

That seems very unergonomic and unintuitive to use. I'd have to create an empty request with the URL I already passed to MqttOptions::new.

@yatinmaan
Copy link
Contributor

That seems very unergonomic and unintuitive to use. I'd have to create an empty request with the URL I already passed to MqttOptions::new.

@reitermarkus Agreed, Iike I said

Now that I think of it, it'll be even better to just let the user pass in any additional headers that rumqttc then adds to the request itself.

The better way to do it would be to just let the user set any additional headers they want to be sent along via an API like mqttoptions.set_ws_headers(headers: http::header::map::HeaderMap)

@reitermarkus
Copy link
Contributor Author

To get the headers I would still have to create a request, since signing is done on a fully formed request. There is no way to only get signature headers without a request in the first place.

@stale stale bot added the stale Not moving forward; blocked label Jul 9, 2023
@yatinmaan
Copy link
Contributor

yatinmaan commented Jul 10, 2023

To get the headers I would still have to create a request, since signing is done on a fully formed request. There is no way to only get signature headers without a request in the first place.

@reitermarkus Makes sense. That's the sort of stuff I was looking for when I asked:

Unless there's a use case I missed which can't be serviced like this?

Sorry for the delay and thanks for your contribution 🚀

@stale stale bot removed the stale Not moving forward; blocked label Jul 10, 2023
@yatinmaan yatinmaan merged commit b5331a4 into bytebeamio:main Jul 10, 2023
carlocorradini pushed a commit to carlocorradini/rumqtt that referenced this pull request Aug 3, 2023
@reitermarkus reitermarkus deleted the aws-iot branch October 2, 2023 13:31
@reitermarkus
Copy link
Contributor Author

@yatinmaan, could you release a new version with this included? Thanks.

@yatinmaan
Copy link
Contributor

@yatinmaan, could you release a new version with this included? Thanks.

@swanandx ^^

@swanandx
Copy link
Contributor

swanandx commented Oct 4, 2023

thnx for the ping @yatinmaan ! We will release rumqttc this week @reitermarkus 💯

@swanandx
Copy link
Contributor

update: rumqttc v0.23.0 is released, which includes this change!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants