Skip to content

Creating an event should use the local c8y proxy #2938

@rina23q

Description

@rina23q

Describe the bug
When tedge-mapper-c8y tries to upload a file for a log operation, it needs to create two requests

  1. Create an event so that tedge-mapper-c8y can get an event ID
  2. Upload a file by attaching to the event created by the step 1.

However, I found that the step 1 is done in the old way, using bearer auth and actual http host and port (like https://example.cumulocity.com:443/event/events). The step 2 is already using the local c8y proxy endpoint.

Here is the proof from our code:

At the end of one log_upload command, c8y-mapper calls this function.

let binary_upload_event_url = self
.upload_file(
&topic_id,
&file_path,
None,
Some(mime::TEXT_PLAIN),
&cmd_id,
event_type,
None,
)
.await?;

Then, first it tries to get the event_response_id.

let event_response_id = self.http_proxy.send_event(create_event).await?;

This one is called as part of http_proxy, which is a part of C8yHttpProxyActor. The function send_event() calls the function send_event_internal() eventually. You see now the create_event_url holds the actual URL.

async fn send_event_internal(
&mut self,
device_id: String,
create_event: impl Fn(String) -> C8yCreateEvent,
) -> Result<EventId, C8YRestError> {
// Get and set child device internal id
if device_id.ne(&self.end_point.device_id) {
self.get_and_set_internal_id(device_id.clone()).await?;
}
let build_request = |end_point: &C8yEndPoint| {
let create_event_url = end_point.get_url_for_create_event();
let internal_id = end_point
.get_internal_id(device_id.clone())
.map_err(|e| C8YRestError::CustomError(e.to_string()));
async {
let updated_c8y_event = create_event(internal_id?);
Ok::<_, C8YRestError>(
HttpRequestBuilder::post(create_event_url)
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.json(&updated_c8y_event),
)
}
};
let http_result = self.execute(device_id.clone(), build_request).await?;

And if we go further, the execute function uses bearer authentication. It's a proof that the http_proxy.send_event() doesn't use the local c8y proxy.

.bearer_auth(self.end_point.token.clone().unwrap_or_default())

To Reproduce
Actually I found this issue while reading the code. If you sniff the HTTP requests, you can confirm it.

Expected behavior
To get an event, we should also use local c8y proxy endpoint. Probably, we can stop using the C8yHttpProxyActor completely.

Screenshots

Environment (please complete the following information):

  • OS [incl. version]
  • Hardware [incl. revision]
  • System-Architecture [e.g. result of "uname -a"]
  • thin-edge.io version [1.1.1]

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions