• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender / 2319410833

11 Feb 2026 01:17PM UTC coverage: 81.69% (+0.04%) from 81.646%
2319410833

push

gitlab-ci

michalkopczan
fix: Correctly handle HTTP 429 responses that contain a body

Ticket: MEN-9342
Changelog: Title

Signed-off-by: Michal Kopczan <michal.kopczan@northern.tech>

8 of 10 new or added lines in 2 files covered. (80.0%)

3 existing lines in 2 files now uncovered.

8999 of 11016 relevant lines covered (81.69%)

19929.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.45
/src/mender-update/deployments/deployments.cpp
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
#include <mender-update/deployments.hpp>
16

17
#include <algorithm>
18
#include <sstream>
19
#include <string>
20

21
#include <api/api.hpp>
22
#include <api/client.hpp>
23
#include <common/common.hpp>
24
#include <common/error.hpp>
25
#include <common/events.hpp>
26
#include <common/expected.hpp>
27
#include <common/http.hpp>
28
#include <common/io.hpp>
29
#include <common/json.hpp>
30
#include <common/log.hpp>
31
#include <common/optional.hpp>
32
#include <common/path.hpp>
33
#include <mender-update/context.hpp>
34

35
namespace mender {
36
namespace update {
37
namespace deployments {
38

39
using namespace std;
40

41
namespace api = mender::api;
42
namespace common = mender::common;
43
namespace context = mender::update::context;
44
namespace error = mender::common::error;
45
namespace events = mender::common::events;
46
namespace expected = mender::common::expected;
47
namespace http = mender::common::http;
48
namespace io = mender::common::io;
49
namespace json = mender::common::json;
50
namespace log = mender::common::log;
51
namespace path = mender::common::path;
52

53
const DeploymentsErrorCategoryClass DeploymentsErrorCategory;
54

55
const char *DeploymentsErrorCategoryClass::name() const noexcept {
×
56
        return "DeploymentsErrorCategory";
×
57
}
58

59
string DeploymentsErrorCategoryClass::message(int code) const {
33✔
60
        switch (code) {
33✔
61
        case NoError:
62
                return "Success";
×
63
        case InvalidDataError:
64
                return "Invalid data error";
×
65
        case BadResponseError:
66
                return "Bad response error";
4✔
67
        case DeploymentAbortedError:
68
                return "Deployment was aborted on the server";
3✔
69
        case TooManyRequestsError:
70
                return "Too many requests";
26✔
71
        }
72
        assert(false);
73
        return "Unknown";
×
74
}
75

76
error::Error MakeError(DeploymentsErrorCode code, const string &msg) {
52✔
77
        return error::Error(error_condition(code, DeploymentsErrorCategory), msg);
66✔
78
}
79

80
static const string check_updates_v1_uri = "/api/devices/v1/deployments/device/deployments/next";
81
static const string check_updates_v2_uri = "/api/devices/v2/deployments/device/deployments/next";
82

83
error::Error DeploymentClient::CheckNewDeployments(
10✔
84
        context::MenderContext &ctx, api::Client &client, CheckUpdatesAPIResponseHandler api_handler) {
85
        auto ex_compatible_type = ctx.GetCompatibleType();
20✔
86
        if (!ex_compatible_type) {
10✔
87
                return ex_compatible_type.error();
4✔
88
        }
89
        string compatible_type = ex_compatible_type.value();
6✔
90

91
        auto ex_provides = ctx.LoadProvides();
6✔
92
        if (!ex_provides) {
6✔
93
                return ex_provides.error();
×
94
        }
95
        auto provides = ex_provides.value();
6✔
96
        if (provides.find("artifact_name") == provides.end()) {
12✔
97
                return MakeError(InvalidDataError, "Missing artifact name data");
×
98
        }
99

100
        stringstream ss;
6✔
101
        ss << R"({"device_provides":{)";
6✔
102
        ss << R"("device_type":")";
6✔
103
        ss << json::EscapeString(compatible_type);
12✔
104

105
        for (const auto &kv : provides) {
14✔
106
                ss << "\",\"" + json::EscapeString(kv.first) + "\":\"";
8✔
107
                ss << json::EscapeString(kv.second);
16✔
108
        }
109

110
        ss << R"("}})";
6✔
111

112
        string v2_payload = ss.str();
113
        log::Debug("deployments/next v2 payload " + v2_payload);
6✔
114
        http::BodyGenerator payload_gen = [v2_payload]() {
54✔
115
                return make_shared<io::StringReader>(v2_payload);
6✔
116
        };
6✔
117

118
        auto v2_req = make_shared<api::APIRequest>();
6✔
119
        v2_req->SetPath(check_updates_v2_uri);
120
        v2_req->SetMethod(http::Method::POST);
6✔
121
        v2_req->SetHeader("Content-Type", "application/json");
12✔
122
        v2_req->SetHeader("Content-Length", to_string(v2_payload.size()));
12✔
123
        v2_req->SetHeader("Accept", "application/json");
12✔
124
        v2_req->SetBodyGenerator(payload_gen);
6✔
125

126
        string v1_args = "artifact_name=" + http::URLEncode(provides["artifact_name"])
12✔
127
                                         + "&device_type=" + http::URLEncode(compatible_type);
18✔
128
        auto v1_req = make_shared<api::APIRequest>();
6✔
129
        v1_req->SetPath(check_updates_v1_uri + "?" + v1_args);
6✔
130
        v1_req->SetMethod(http::Method::GET);
6✔
131
        v1_req->SetHeader("Accept", "application/json");
12✔
132

133
        auto received_body = make_shared<vector<uint8_t>>();
6✔
134
        auto handle_data = [received_body, api_handler](unsigned status) {
4✔
135
                if (status == http::StatusOK) {
4✔
136
                        auto ex_j = json::Load(common::StringFromByteVector(*received_body));
4✔
137
                        if (ex_j) {
2✔
138
                                CheckUpdatesAPIResponse response {optional<json::Json> {ex_j.value()}};
2✔
139
                                api_handler(response);
4✔
140
                        } else {
141
                                api_handler(expected::unexpected(
×
142
                                        CheckUpdatesAPIResponseError {status, nullopt, ex_j.error()}));
×
143
                        }
144
                } else if (status == http::StatusNoContent) {
2✔
145
                        api_handler(CheckUpdatesAPIResponse {nullopt});
4✔
146
                } else {
147
                        log::Warning(
×
148
                                "DeploymentClient::CheckNewDeployments - received unhandled http response: "
149
                                + to_string(status));
×
150
                        api_handler(expected::unexpected(CheckUpdatesAPIResponseError {
×
151
                                status,
152
                                nullopt,
153
                                MakeError(
154
                                        DeploymentAbortedError,
155
                                        "received unhandled HTTP response: " + to_string(status))}));
×
156
                }
157
        };
10✔
158

159
        http::ResponseHandler header_handler =
160
                [this, received_body, api_handler](http::ExpectedIncomingResponsePtr exp_resp) {
12✔
161
                        this->HeaderHandler(received_body, api_handler, exp_resp);
27✔
162
                };
15✔
163

164
        http::ResponseHandler v1_body_handler =
165
                [received_body, api_handler, handle_data](http::ExpectedIncomingResponsePtr exp_resp) {
15✔
166
                        if (!exp_resp) {
3✔
167
                                log::Error("Request to check new deployments failed: " + exp_resp.error().message);
×
168
                                CheckUpdatesAPIResponse response = expected::unexpected(
×
169
                                        CheckUpdatesAPIResponseError {nullopt, nullopt, exp_resp.error()});
×
170
                                api_handler(response);
×
171
                                return;
172
                        }
173
                        auto resp = exp_resp.value();
3✔
174
                        auto status = resp->GetStatusCode();
3✔
175

176
                        // StatusTooManyRequests must have been handled in HeaderHandler already
177
                        assert(status != http::StatusTooManyRequests);
178

179
                        if ((status == http::StatusOK) || (status == http::StatusNoContent)) {
3✔
180
                                handle_data(status);
2✔
181
                        } else {
182
                                auto ex_err_msg = api::ErrorMsgFromErrorResponse(*received_body);
1✔
183
                                string err_str;
184
                                if (ex_err_msg) {
1✔
185
                                        err_str = ex_err_msg.value();
×
186
                                } else {
187
                                        err_str = resp->GetStatusMessage();
2✔
188
                                }
189
                                api_handler(expected::unexpected(CheckUpdatesAPIResponseError {
3✔
190
                                        status,
191
                                        nullopt,
192
                                        MakeError(
193
                                                BadResponseError,
194
                                                "Got unexpected response " + to_string(status) + ": " + err_str)}));
2✔
195
                        }
196
                };
6✔
197

198
        http::ResponseHandler v2_body_handler = [received_body,
18✔
199
                                                                                         v1_req,
200
                                                                                         header_handler,
201
                                                                                         v1_body_handler,
202
                                                                                         api_handler,
203
                                                                                         handle_data,
204
                                                                                         &client](http::ExpectedIncomingResponsePtr exp_resp) {
205
                if (!exp_resp) {
6✔
206
                        log::Error("Request to check new deployments failed: " + exp_resp.error().message);
×
207
                        CheckUpdatesAPIResponse response = expected::unexpected(
×
208
                                CheckUpdatesAPIResponseError {nullopt, nullopt, exp_resp.error()});
×
209
                        api_handler(response);
×
210
                        return;
211
                }
212
                auto resp = exp_resp.value();
6✔
213
                auto status = resp->GetStatusCode();
6✔
214

215
                // StatusTooManyRequests must have been handled in HeaderHandler already
216
                assert(status != http::StatusTooManyRequests);
217

218
                if ((status == http::StatusOK) || (status == http::StatusNoContent)) {
6✔
219
                        handle_data(status);
2✔
220
                } else if (status == http::StatusNotFound) {
4✔
221
                        log::Debug(
3✔
222
                                "POST request to v2 version of the deployments API failed, falling back to v1 version and GET");
223
                        auto err = client.AsyncCall(v1_req, header_handler, v1_body_handler);
9✔
224
                        if (err != error::NoError) {
3✔
225
                                api_handler(expected::unexpected(CheckUpdatesAPIResponseError {
×
226
                                        status, nullopt, err.WithContext("While calling v1 endpoint")}));
×
227
                        }
228
                } else {
229
                        auto ex_err_msg = api::ErrorMsgFromErrorResponse(*received_body);
1✔
230
                        string err_str;
231
                        if (ex_err_msg) {
1✔
232
                                err_str = ex_err_msg.value();
1✔
233
                        } else {
234
                                err_str = resp->GetStatusMessage();
×
235
                        }
236
                        api_handler(expected::unexpected(CheckUpdatesAPIResponseError {
3✔
237
                                status,
238
                                nullopt,
239
                                MakeError(
240
                                        BadResponseError,
241
                                        "Got unexpected response " + to_string(status) + ": " + err_str)}));
2✔
242
                }
243
        };
6✔
244

245
        return client.AsyncCall(v2_req, header_handler, v2_body_handler);
18✔
246
}
12✔
247

248
void DeploymentClient::HeaderHandler(
12✔
249
        shared_ptr<vector<uint8_t>> received_body,
250
        CheckUpdatesAPIResponseHandler api_handler,
251
        http::ExpectedIncomingResponsePtr exp_resp) {
252
        if (!exp_resp) {
12✔
253
                log::Error("Request to check new deployments failed: " + exp_resp.error().message);
×
254
                CheckUpdatesAPIResponse response =
255
                        expected::unexpected(CheckUpdatesAPIResponseError {nullopt, nullopt, exp_resp.error()});
×
256
                api_handler(response);
×
257
                return;
258
        }
259

260
        auto resp = exp_resp.value();
12✔
261
        auto status = resp->GetStatusCode();
12✔
262
        if (status == http::StatusTooManyRequests) {
12✔
263
                CheckUpdatesAPIResponse response = expected::unexpected(CheckUpdatesAPIResponseError {
6✔
264
                        status, resp->GetHeaders(), MakeError(TooManyRequestsError, "Too many requests")});
9✔
265
                api_handler(response);
6✔
266
        }
267
        received_body->clear();
12✔
268
        auto body_writer = make_shared<io::ByteWriter>(received_body);
12✔
269
        body_writer->SetUnlimited(true);
12✔
270
        resp->SetBodyWriter(body_writer);
24✔
271
}
272

273
static const string deployment_status_strings[static_cast<int>(DeploymentStatus::End_) + 1] = {
274
        "installing",
275
        "pause_before_installing",
276
        "downloading",
277
        "pause_before_rebooting",
278
        "rebooting",
279
        "pause_before_committing",
280
        "success",
281
        "failure",
282
        "already-installed"};
283

284
static const string deployments_uri_prefix = "/api/devices/v1/deployments/device/deployments";
285
static const string status_uri_suffix = "/status";
286

287
string DeploymentStatusString(DeploymentStatus status) {
501✔
288
        return deployment_status_strings[static_cast<int>(status)];
505✔
289
}
290

291
error::Error DeploymentClient::PushStatus(
4✔
292
        const string &deployment_id,
293
        DeploymentStatus status,
294
        const string &substate,
295
        api::Client &client,
296
        StatusAPIResponseHandler api_handler) {
297
        // Cannot push a status update without a deployment ID
298
        AssertOrReturnError(deployment_id != "");
4✔
299
        string payload = R"({"status":")" + DeploymentStatusString(status) + "\"";
4✔
300
        if (substate != "") {
4✔
301
                payload += R"(,"substate":")" + json::EscapeString(substate) + "\"}";
6✔
302
        } else {
303
                payload += "}";
1✔
304
        }
305
        http::BodyGenerator payload_gen = [payload]() {
36✔
306
                return make_shared<io::StringReader>(payload);
4✔
307
        };
4✔
308

309
        auto req = make_shared<api::APIRequest>();
4✔
310
        req->SetPath(http::JoinUrl(deployments_uri_prefix, deployment_id, status_uri_suffix));
4✔
311
        req->SetMethod(http::Method::PUT);
4✔
312
        req->SetHeader("Content-Type", "application/json");
8✔
313
        req->SetHeader("Content-Length", to_string(payload.size()));
8✔
314
        req->SetHeader("Accept", "application/json");
8✔
315
        req->SetBodyGenerator(payload_gen);
4✔
316

317
        auto received_body = make_shared<vector<uint8_t>>();
4✔
318
        return client.AsyncCall(
16✔
319
                req,
320
                [this, received_body, api_handler](http::ExpectedIncomingResponsePtr exp_resp) {
8✔
321
                        this->PushStatusHeaderHandler(received_body, api_handler, exp_resp);
12✔
322
                },
4✔
323
                [received_body, api_handler](http::ExpectedIncomingResponsePtr exp_resp) {
12✔
324
                        if (!exp_resp) {
4✔
325
                                log::Error("Request to push status data failed: " + exp_resp.error().message);
×
326
                                api_handler(StatusAPIResponse {nullopt, nullopt, exp_resp.error()});
×
327
                                return;
×
328
                        }
329

330
                        auto resp = exp_resp.value();
4✔
331
                        auto status = resp->GetStatusCode();
4✔
332

333
                        // StatusTooManyRequests must have been handled in PushStatusHeaderHandler already
334
                        assert(status != http::StatusTooManyRequests);
335

336
                        if (status == http::StatusNoContent) {
4✔
337
                                api_handler(StatusAPIResponse {status, nullopt, error::NoError});
2✔
338
                        } else if (status == http::StatusConflict) {
2✔
339
                                api_handler(StatusAPIResponse {
2✔
340
                                        status,
341
                                        nullopt,
342
                                        MakeError(DeploymentAbortedError, "Could not send status update to server")});
2✔
343
                        } else {
344
                                auto ex_err_msg = api::ErrorMsgFromErrorResponse(*received_body);
1✔
345
                                string err_str;
346
                                if (ex_err_msg) {
1✔
347
                                        err_str = ex_err_msg.value();
1✔
348
                                } else {
349
                                        err_str = resp->GetStatusMessage();
×
350
                                }
351
                                api_handler(StatusAPIResponse {
2✔
352
                                        status,
353
                                        nullopt,
354
                                        MakeError(
355
                                                BadResponseError,
356
                                                "Got unexpected response " + to_string(status)
1✔
357
                                                        + " from status API: " + err_str)});
2✔
358
                        }
359
                });
4✔
360
}
361

362
void DeploymentClient::PushStatusHeaderHandler(
7✔
363
        shared_ptr<vector<uint8_t>> received_body,
364
        StatusAPIResponseHandler api_handler,
365
        http::ExpectedIncomingResponsePtr exp_resp) {
366
        if (!exp_resp) {
7✔
367
                log::Error("Request to push status data failed: " + exp_resp.error().message);
×
368
                api_handler(StatusAPIResponse {nullopt, nullopt, exp_resp.error()});
×
UNCOV
369
                return;
×
370
        }
371

372
        auto body_writer = make_shared<io::ByteWriter>(received_body);
7✔
373
        auto resp = exp_resp.value();
7✔
374
        auto status = resp->GetStatusCode();
7✔
375
        if (status == http::StatusTooManyRequests) {
7✔
376
                StatusAPIResponse response = {
377
                        status, resp->GetHeaders(), MakeError(TooManyRequestsError, "Too many requests")};
3✔
378
                api_handler(response);
3✔
379
        }
3✔
380
        auto content_length = resp->GetHeader("Content-Length");
14✔
381
        if (!content_length) {
7✔
382
                log::Debug(
3✔
383
                        "Failed to get content length from the deployment status API response headers: "
384
                        + content_length.error().String());
6✔
385
                body_writer->SetUnlimited(true);
3✔
386
        } else {
387
                auto ex_len = common::StringTo<size_t>(content_length.value());
4✔
388
                if (!ex_len) {
4✔
389
                        log::Error(
×
390
                                "Failed to convert the content length from the deployment status API response headers to an integer: "
391
                                + ex_len.error().String());
×
392
                        body_writer->SetUnlimited(true);
×
393
                } else {
394
                        received_body->resize(ex_len.value());
4✔
395
                }
396
        }
397
        resp->SetBodyWriter(body_writer);
14✔
398
}
399

400
using mender::common::expected::ExpectedSize;
401

402
static ExpectedSize GetLogFileDataSize(const string &path) {
18✔
403
        auto ex_istr = io::OpenIfstream(path);
18✔
404
        if (!ex_istr) {
18✔
405
                return expected::unexpected(ex_istr.error());
×
406
        }
407
        auto istr = std::move(ex_istr.value());
18✔
408

409
        // We want the size of the actual data without a potential trailing
410
        // comma. So let's seek one byte before the end of file, check if the last
411
        // byte is a comma and return the appropriate number.
412
        istr.seekg(-1, ios_base::end);
18✔
413
        int c = istr.get();
18✔
414
        if (c == ',') {
18✔
415
                return istr.tellg() - static_cast<ifstream::off_type>(1);
18✔
416
        } else {
417
                return istr.tellg();
×
418
        }
419
}
18✔
420

421
const vector<uint8_t> JsonLogMessagesReader::header_ = {
422
        '{', '"', 'm', 'e', 's', 's', 'a', 'g', 'e', 's', '"', ':', '['};
423
const vector<uint8_t> JsonLogMessagesReader::closing_ = {']', '}'};
424
const string JsonLogMessagesReader::default_tstamp_ = "1970-01-01T00:00:00.000000000Z";
425
const string JsonLogMessagesReader::bad_data_msg_tmpl_ =
426
        R"d({"timestamp": "1970-01-01T00:00:00.000000000Z", "level": "ERROR", "message": "(THE ORIGINAL LOGS CONTAINED INVALID ENTRIES)"},)d";
427

428
JsonLogMessagesReader::~JsonLogMessagesReader() {
42✔
429
        reader_.reset();
430
        if (!sanitized_fpath_.empty() && path::FileExists(sanitized_fpath_)) {
14✔
431
                auto del_err = path::FileDelete(sanitized_fpath_);
14✔
432
                if (del_err != error::NoError) {
14✔
433
                        log::Error("Failed to delete auxiliary logs file: " + del_err.String());
×
434
                }
435
        }
436
        sanitized_fpath_.erase();
14✔
437
}
28✔
438

439
static error::Error DoSanitizeLogs(
18✔
440
        const string &orig_path, const string &new_path, bool &all_valid, string &first_tstamp) {
441
        auto ex_ifs = io::OpenIfstream(orig_path);
18✔
442
        if (!ex_ifs) {
18✔
443
                return ex_ifs.error();
×
444
        }
445
        auto ex_ofs = io::OpenOfstream(new_path);
18✔
446
        if (!ex_ofs) {
18✔
447
                return ex_ofs.error();
×
448
        }
449
        auto &ifs = ex_ifs.value();
18✔
450
        auto &ofs = ex_ofs.value();
18✔
451

452
        string last_known_tstamp = first_tstamp;
18✔
453
        const string tstamp_prefix_data = R"d({"timestamp": ")d";
18✔
454
        const string corrupt_msg_suffix_data =
455
                R"d(", "level": "ERROR", "message": "(CORRUPTED LOG DATA)"},)d";
18✔
456

457
        string line;
458
        first_tstamp.erase();
18✔
459
        all_valid = true;
18✔
460
        error::Error err;
18✔
461
        while (!ifs.eof()) {
86✔
462
                getline(ifs, line);
68✔
463
                if (!ifs.eof() && !ifs) {
68✔
464
                        int io_errno = errno;
×
465
                        return error::Error(
466
                                generic_category().default_error_condition(io_errno),
×
467
                                "Failed to get line from deployment logs file '" + orig_path
×
468
                                        + "': " + strerror(io_errno));
×
469
                }
470
                if (line.empty()) {
68✔
471
                        // skip empty lines
472
                        continue;
18✔
473
                }
474
                auto ex_json = json::Load(line);
100✔
475
                if (ex_json) {
50✔
476
                        // valid JSON log line, just replace the newline after it with a comma and save the
477
                        // timestamp for later
478
                        auto ex_tstamp = ex_json.value().Get("timestamp").and_then(json::ToString);
82✔
479
                        if (ex_tstamp) {
41✔
480
                                if (first_tstamp.empty()) {
41✔
481
                                        first_tstamp = ex_tstamp.value();
17✔
482
                                }
483
                                last_known_tstamp = std::move(ex_tstamp.value());
41✔
484
                        }
485
                        line.append(1, ',');
41✔
486
                        err = io::WriteStringIntoOfstream(ofs, line);
41✔
487
                        if (err != error::NoError) {
41✔
488
                                return err.WithContext("Failed to write pre-processed deployment logs data");
×
489
                        }
490
                } else {
491
                        all_valid = false;
9✔
492
                        if (first_tstamp.empty()) {
9✔
493
                                // If we still don't have the first valid tstamp, we need to
494
                                // save the last known one (potentially pre-set) as the first
495
                                // one.
496
                                first_tstamp = last_known_tstamp;
497
                        }
498
                        err = io::WriteStringIntoOfstream(
9✔
499
                                ofs, tstamp_prefix_data + last_known_tstamp + corrupt_msg_suffix_data);
18✔
500
                        if (err != error::NoError) {
9✔
501
                                return err.WithContext("Failed to write pre-processed deployment logs data");
×
502
                        }
503
                }
504
        }
505
        return error::NoError;
18✔
506
}
507

508
error::Error JsonLogMessagesReader::SanitizeLogs() {
18✔
509
        if (!sanitized_fpath_.empty()) {
18✔
510
                return error::NoError;
×
511
        }
512

513
        string prep_fpath = log_fpath_ + ".sanitized";
18✔
514
        string first_tstamp = default_tstamp_;
18✔
515
        auto err = DoSanitizeLogs(log_fpath_, prep_fpath, clean_logs_, first_tstamp);
18✔
516
        if (err != error::NoError) {
18✔
517
                if (path::FileExists(prep_fpath)) {
×
518
                        auto del_err = path::FileDelete(prep_fpath);
×
519
                        if (del_err != error::NoError) {
×
520
                                log::Error("Failed to delete auxiliary logs file: " + del_err.String());
×
521
                        }
522
                }
523
        } else {
524
                sanitized_fpath_ = std::move(prep_fpath);
18✔
525
                reader_ = make_unique<io::FileReader>(sanitized_fpath_);
36✔
526
                auto ex_sz = GetLogFileDataSize(sanitized_fpath_);
18✔
527
                if (!ex_sz) {
18✔
528
                        return ex_sz.error().WithContext("Failed to determine deployment logs size");
×
529
                }
530
                raw_data_size_ = ex_sz.value();
18✔
531
                rem_raw_data_size_ = raw_data_size_;
18✔
532
                if (!clean_logs_) {
18✔
533
                        auto bad_data_msg_tstamp_start =
534
                                bad_data_msg_.begin() + 15; // len(R"({"timestamp": ")")
535
                        copy_n(first_tstamp.cbegin(), first_tstamp.size(), bad_data_msg_tstamp_start);
7✔
536
                }
537
        }
538
        return err;
18✔
539
}
540

541
error::Error JsonLogMessagesReader::Rewind() {
4✔
542
        AssertOrReturnError(!sanitized_fpath_.empty());
4✔
543
        header_rem_ = header_.size();
4✔
544
        closing_rem_ = closing_.size();
4✔
545
        bad_data_msg_rem_ = bad_data_msg_.size();
4✔
546

547
        // release/close the file first so that the FileDelete() below can actually
548
        // delete it and free space up
549
        reader_.reset();
550
        auto del_err = path::FileDelete(sanitized_fpath_);
4✔
551
        if (del_err != error::NoError) {
4✔
552
                log::Error("Failed to delete auxiliary logs file: " + del_err.String());
×
553
        }
554
        sanitized_fpath_.erase();
4✔
555
        return SanitizeLogs();
4✔
556
}
557

558
int64_t JsonLogMessagesReader::TotalDataSize() {
14✔
559
        assert(!sanitized_fpath_.empty());
560

561
        auto ret = raw_data_size_ + header_.size() + closing_.size();
14✔
562
        if (!clean_logs_) {
14✔
563
                ret += bad_data_msg_.size();
7✔
564
        }
565
        return ret;
14✔
566
}
567

568
ExpectedSize JsonLogMessagesReader::Read(
147✔
569
        vector<uint8_t>::iterator start, vector<uint8_t>::iterator end) {
570
        AssertOrReturnUnexpected(!sanitized_fpath_.empty());
147✔
571

572
        if (header_rem_ > 0) {
147✔
573
                io::Vsize target_size = end - start;
16✔
574
                auto copy_end = copy_n(
16✔
575
                        header_.begin() + (header_.size() - header_rem_), min(header_rem_, target_size), start);
16✔
576
                auto n_copied = copy_end - start;
577
                header_rem_ -= n_copied;
16✔
578
                return static_cast<size_t>(n_copied);
579
        } else if (!clean_logs_ && (bad_data_msg_rem_ > 0)) {
131✔
580
                io::Vsize target_size = end - start;
14✔
581
                auto copy_end = copy_n(
14✔
582
                        bad_data_msg_.begin() + (bad_data_msg_.size() - bad_data_msg_rem_),
14✔
583
                        min(bad_data_msg_rem_, target_size),
584
                        start);
585
                auto n_copied = copy_end - start;
586
                bad_data_msg_rem_ -= n_copied;
14✔
587
                return static_cast<size_t>(n_copied);
588
        } else if (rem_raw_data_size_ > 0) {
117✔
589
                if (end - start > rem_raw_data_size_) {
87✔
590
                        end = start + static_cast<size_t>(rem_raw_data_size_);
591
                }
592
                auto ex_sz = reader_->Read(start, end);
87✔
593
                if (!ex_sz) {
87✔
594
                        return ex_sz;
595
                }
596
                auto n_read = ex_sz.value();
87✔
597
                rem_raw_data_size_ -= n_read;
87✔
598

599
                // We control how much we read from the file so we should never read
600
                // 0 bytes (meaning EOF reached). If we do, it means the file is
601
                // smaller than what we were told.
602
                assert(n_read > 0);
603
                if (n_read == 0) {
87✔
604
                        return expected::unexpected(
×
605
                                MakeError(InvalidDataError, "Unexpected EOF when reading logs file"));
×
606
                }
607
                return n_read;
608
        } else if (closing_rem_ > 0) {
30✔
609
                io::Vsize target_size = end - start;
15✔
610
                auto copy_end = copy_n(
15✔
611
                        closing_.begin() + (closing_.size() - closing_rem_),
15✔
612
                        min(closing_rem_, target_size),
613
                        start);
614
                auto n_copied = copy_end - start;
615
                closing_rem_ -= n_copied;
15✔
616
                return static_cast<size_t>(copy_end - start);
617
        } else {
618
                return 0;
619
        }
620
};
621

622
static const string logs_uri_suffix = "/log";
623

624
error::Error DeploymentClient::PushLogs(
3✔
625
        const string &deployment_id,
626
        const string &log_file_path,
627
        api::Client &client,
628
        LogsAPIResponseHandler api_handler) {
629
        auto logs_reader = make_shared<JsonLogMessagesReader>(log_file_path);
3✔
630
        auto err = logs_reader->SanitizeLogs();
3✔
631
        if (err != error::NoError) {
3✔
632
                return err;
×
633
        }
634

635
        auto req = make_shared<api::APIRequest>();
3✔
636
        req->SetPath(http::JoinUrl(deployments_uri_prefix, deployment_id, logs_uri_suffix));
3✔
637
        req->SetMethod(http::Method::PUT);
3✔
638
        req->SetHeader("Content-Type", "application/json");
6✔
639
        req->SetHeader("Content-Length", to_string(logs_reader->TotalDataSize()));
6✔
640
        req->SetHeader("Accept", "application/json");
6✔
641
        req->SetBodyGenerator([logs_reader]() {
15✔
642
                logs_reader->Rewind();
6✔
643
                return logs_reader;
3✔
644
        });
645

646
        auto received_body = make_shared<vector<uint8_t>>();
3✔
647
        return client.AsyncCall(
12✔
648
                req,
649
                [this, received_body, api_handler](http::ExpectedIncomingResponsePtr exp_resp) {
6✔
650
                        this->PushLogsHeaderHandler(received_body, api_handler, exp_resp);
9✔
651
                },
3✔
652
                [received_body, api_handler](http::ExpectedIncomingResponsePtr exp_resp) {
9✔
653
                        if (!exp_resp) {
3✔
654
                                log::Error("Request to push logs data failed: " + exp_resp.error().message);
×
655
                                api_handler(LogsAPIResponse {nullopt, nullopt, exp_resp.error()});
×
656
                                return;
×
657
                        }
658

659
                        auto resp = exp_resp.value();
3✔
660
                        auto status = resp->GetStatusCode();
3✔
661

662
                        // StatusTooManyRequests must have been handled in PushLogsHeaderHandler already
663
                        assert(status != http::StatusTooManyRequests);
664

665
                        if (status == http::StatusNoContent) {
3✔
666
                                api_handler(LogsAPIResponse {status, nullopt, error::NoError});
2✔
667
                        } else {
668
                                auto ex_err_msg = api::ErrorMsgFromErrorResponse(*received_body);
1✔
669
                                string err_str;
670
                                if (ex_err_msg) {
1✔
671
                                        err_str = ex_err_msg.value();
1✔
672
                                } else {
673
                                        err_str = resp->GetStatusMessage();
×
674
                                }
675
                                api_handler(LogsAPIResponse {
2✔
676
                                        status,
677
                                        nullopt,
678
                                        MakeError(
679
                                                BadResponseError,
680
                                                "Got unexpected response " + to_string(status)
1✔
681
                                                        + " from logs API: " + err_str)});
2✔
682
                        }
683
                });
3✔
684
}
685

686
void DeploymentClient::PushLogsHeaderHandler(
6✔
687
        shared_ptr<vector<uint8_t>> received_body,
688
        LogsAPIResponseHandler api_handler,
689
        http::ExpectedIncomingResponsePtr exp_resp) {
690
        if (!exp_resp) {
6✔
691
                log::Error("Request to push logs data failed: " + exp_resp.error().message);
×
692
                api_handler(LogsAPIResponse {nullopt, nullopt, exp_resp.error()});
×
UNCOV
693
                return;
×
694
        }
695

696
        auto body_writer = make_shared<io::ByteWriter>(received_body);
6✔
697
        auto resp = exp_resp.value();
6✔
698
        auto status = resp->GetStatusCode();
6✔
699
        if (status == http::StatusTooManyRequests) {
6✔
700
                LogsAPIResponse response = {
701
                        status, resp->GetHeaders(), MakeError(TooManyRequestsError, "Too many requests")};
3✔
702
                api_handler(response);
3✔
703
        }
3✔
704
        auto content_length = resp->GetHeader("Content-Length");
12✔
705
        if (!content_length) {
6✔
706
                log::Debug(
3✔
707
                        "Failed to get content length from the deployment log API response headers: "
708
                        + content_length.error().String());
6✔
709
                body_writer->SetUnlimited(true);
3✔
710
        } else {
711
                auto ex_len = common::StringTo<size_t>(content_length.value());
3✔
712
                if (!ex_len) {
3✔
713
                        log::Error(
×
714
                                "Failed to convert the content length from the deployment log API response headers to an integer: "
715
                                + ex_len.error().String());
×
716
                        body_writer->SetUnlimited(true);
×
717
                } else {
718
                        received_body->resize(ex_len.value());
3✔
719
                }
720
        }
721
        resp->SetBodyWriter(body_writer);
18✔
722
}
723

724
} // namespace deployments
725
} // namespace update
726
} // namespace mender
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc