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

mendersoftware / mender / 1022753986

02 Oct 2023 10:37AM UTC coverage: 78.168% (-2.0%) from 80.127%
1022753986

push

gitlab-ci

oleorhagen
feat: Run the authentication loop once upon bootstrap

Ticket: MEN-6658
Changelog: None

Signed-off-by: Ole Petter <ole.orhagen@northern.tech>

32 of 32 new or added lines in 1 file covered. (100.0%)

6996 of 8950 relevant lines covered (78.17%)

10353.4 hits per line

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

78.18
/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 <common/common.hpp>
23
#include <common/error.hpp>
24
#include <common/events.hpp>
25
#include <common/expected.hpp>
26
#include <common/http.hpp>
27
#include <common/io.hpp>
28
#include <common/json.hpp>
29
#include <common/log.hpp>
30
#include <common/optional.hpp>
31
#include <common/path.hpp>
32
#include <mender-update/context.hpp>
33

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

38
using namespace std;
39

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

52
const DeploymentsErrorCategoryClass DeploymentsErrorCategory;
53

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

58
string DeploymentsErrorCategoryClass::message(int code) const {
2✔
59
        switch (code) {
2✔
60
        case NoError:
×
61
                return "Success";
×
62
        case InvalidDataError:
×
63
                return "Invalid data error";
×
64
        case BadResponseError:
×
65
                return "Bad response error";
×
66
        case DeploymentAbortedError:
2✔
67
                return "Deployment was aborted on the server";
2✔
68
        }
69
        assert(false);
×
70
        return "Unknown";
71
}
72

73
error::Error MakeError(DeploymentsErrorCode code, const string &msg) {
30✔
74
        return error::Error(error_condition(code, DeploymentsErrorCategory), msg);
60✔
75
}
76

77
static const string check_updates_v1_uri = "/api/devices/v1/deployments/device/deployments/next";
78
static const string check_updates_v2_uri = "/api/devices/v2/deployments/device/deployments/next";
79

80
error::Error DeploymentClient::CheckNewDeployments(
8✔
81
        context::MenderContext &ctx,
82
        const string &server_url,
83
        http::Client &client,
84
        CheckUpdatesAPIResponseHandler api_handler) {
85
        auto ex_dev_type = ctx.GetDeviceType();
16✔
86
        if (!ex_dev_type) {
8✔
87
                return ex_dev_type.error();
2✔
88
        }
89
        string device_type = ex_dev_type.value();
12✔
90

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

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

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

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

112
        string v2_payload = ss.str();
12✔
113
        http::BodyGenerator payload_gen = [v2_payload]() {
6✔
114
                return make_shared<io::StringReader>(v2_payload);
6✔
115
        };
12✔
116

117
        // TODO: APIRequest
118
        auto v2_req = make_shared<http::OutgoingRequest>();
12✔
119
        v2_req->SetAddress(http::JoinUrl(server_url, check_updates_v2_uri));
6✔
120
        v2_req->SetMethod(http::Method::POST);
6✔
121
        v2_req->SetHeader("Content-Type", "application/json");
6✔
122
        v2_req->SetHeader("Content-Length", to_string(v2_payload.size()));
6✔
123
        v2_req->SetHeader("Accept", "application/json");
6✔
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(device_type);
24✔
128
        auto v1_req = make_shared<http::OutgoingRequest>();
12✔
129
        v1_req->SetAddress(http::JoinUrl(server_url, check_updates_v1_uri) + "?" + v1_args);
6✔
130
        v1_req->SetMethod(http::Method::GET);
6✔
131
        v1_req->SetHeader("Accept", "application/json");
6✔
132

133
        auto received_body = make_shared<vector<uint8_t>>();
12✔
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::optional<json::Json> {ex_j.value()}};
2✔
139
                                api_handler(response);
2✔
140
                        } else {
141
                                api_handler(expected::unexpected(ex_j.error()));
×
142
                        }
143
                } else if (status == http::StatusNoContent) {
2✔
144
                        api_handler(CheckUpdatesAPIResponse {optional::nullopt});
2✔
145
                }
146
        };
16✔
147

148
        http::ResponseHandler header_handler =
149
                [received_body, api_handler](http::ExpectedIncomingResponsePtr exp_resp) {
9✔
150
                        if (!exp_resp) {
9✔
151
                                log::Error("Request to check new deployments failed: " + exp_resp.error().message);
×
152
                                CheckUpdatesAPIResponse response = expected::unexpected(exp_resp.error());
×
153
                                api_handler(response);
×
154
                                return;
×
155
                        }
156

157
                        auto resp = exp_resp.value();
18✔
158
                        received_body->clear();
9✔
159
                        auto body_writer = make_shared<io::ByteWriter>(received_body);
9✔
160
                        body_writer->SetUnlimited(true);
9✔
161
                        resp->SetBodyWriter(body_writer);
9✔
162
                };
12✔
163

164
        http::ResponseHandler v1_body_handler =
165
                [received_body, api_handler, handle_data](http::ExpectedIncomingResponsePtr exp_resp) {
3✔
166
                        if (!exp_resp) {
3✔
167
                                log::Error("Request to check new deployments failed: " + exp_resp.error().message);
×
168
                                CheckUpdatesAPIResponse response = expected::unexpected(exp_resp.error());
×
169
                                api_handler(response);
×
170
                                return;
×
171
                        }
172
                        auto resp = exp_resp.value();
6✔
173
                        auto status = resp->GetStatusCode();
3✔
174
                        if ((status == http::StatusOK) || (status == http::StatusNoContent)) {
3✔
175
                                handle_data(status);
2✔
176
                        } else {
177
                                auto ex_err_msg = api::ErrorMsgFromErrorResponse(*received_body);
2✔
178
                                string err_str;
1✔
179
                                if (ex_err_msg) {
1✔
180
                                        err_str = ex_err_msg.value();
×
181
                                } else {
182
                                        err_str = resp->GetStatusMessage();
1✔
183
                                }
184
                                api_handler(expected::unexpected(MakeError(
2✔
185
                                        BadResponseError,
186
                                        "Got unexpected response " + to_string(status) + ": " + err_str)));
3✔
187
                        }
188
                };
12✔
189

190
        http::ResponseHandler v2_body_handler = [received_body,
6✔
191
                                                                                         v1_req,
192
                                                                                         header_handler,
193
                                                                                         v1_body_handler,
194
                                                                                         api_handler,
195
                                                                                         handle_data,
196
                                                                                         &client](http::ExpectedIncomingResponsePtr exp_resp) {
3✔
197
                if (!exp_resp) {
6✔
198
                        log::Error("Request to check new deployments failed: " + exp_resp.error().message);
×
199
                        CheckUpdatesAPIResponse response = expected::unexpected(exp_resp.error());
×
200
                        api_handler(response);
×
201
                        return;
×
202
                }
203
                auto resp = exp_resp.value();
12✔
204
                auto status = resp->GetStatusCode();
6✔
205
                if ((status == http::StatusOK) || (status == http::StatusNoContent)) {
6✔
206
                        handle_data(status);
2✔
207
                } else if (status == http::StatusNotFound) {
4✔
208
                        log::Info(
3✔
209
                                "POST request to v2 version of the deployments API failed, falling back to v1 version and GET");
6✔
210
                        auto err = client.AsyncCall(v1_req, header_handler, v1_body_handler);
9✔
211
                        if (err != error::NoError) {
3✔
212
                                api_handler(expected::unexpected(err.WithContext("While calling v1 endpoint")));
×
213
                        }
214
                } else {
215
                        auto ex_err_msg = api::ErrorMsgFromErrorResponse(*received_body);
2✔
216
                        string err_str;
1✔
217
                        if (ex_err_msg) {
1✔
218
                                err_str = ex_err_msg.value();
1✔
219
                        } else {
220
                                err_str = resp->GetStatusMessage();
×
221
                        }
222
                        api_handler(expected::unexpected(MakeError(
2✔
223
                                BadResponseError,
224
                                "Got unexpected response " + to_string(status) + ": " + err_str)));
3✔
225
                }
226
        };
6✔
227

228
        return client.AsyncCall(v2_req, header_handler, v2_body_handler);
6✔
229
}
230

231
static const string deployment_status_strings[static_cast<int>(DeploymentStatus::End_) + 1] = {
232
        "installing",
233
        "pause_before_installing",
234
        "downloading",
235
        "pause_before_rebooting",
236
        "rebooting",
237
        "pause_before_committing",
238
        "success",
239
        "failure",
240
        "already-installed"};
241

242
static const string deployments_uri_prefix = "/api/devices/v1/deployments/device/deployments";
243
static const string status_uri_suffix = "/status";
244

245
string DeploymentStatusString(DeploymentStatus status) {
492✔
246
        return deployment_status_strings[static_cast<int>(status)];
492✔
247
}
248

249
error::Error DeploymentClient::PushStatus(
4✔
250
        const string &deployment_id,
251
        DeploymentStatus status,
252
        const string &substate,
253
        const string &server_url,
254
        http::Client &client,
255
        StatusAPIResponseHandler api_handler) {
256
        string payload = R"({"status":")" + DeploymentStatusString(status) + "\"";
12✔
257
        if (substate != "") {
4✔
258
                payload += R"(,"substate":")" + json::EscapeString(substate) + "\"}";
3✔
259
        } else {
260
                payload += "}";
1✔
261
        }
262
        http::BodyGenerator payload_gen = [payload]() {
4✔
263
                return make_shared<io::StringReader>(payload);
4✔
264
        };
8✔
265

266
        auto req = make_shared<http::OutgoingRequest>();
8✔
267
        req->SetAddress(
4✔
268
                http::JoinUrl(server_url, deployments_uri_prefix, deployment_id, status_uri_suffix));
8✔
269
        req->SetMethod(http::Method::PUT);
4✔
270
        req->SetHeader("Content-Type", "application/json");
4✔
271
        req->SetHeader("Content-Length", to_string(payload.size()));
4✔
272
        req->SetHeader("Accept", "application/json");
4✔
273
        req->SetBodyGenerator(payload_gen);
4✔
274

275
        auto received_body = make_shared<vector<uint8_t>>();
4✔
276
        return client.AsyncCall(
277
                req,
278
                [received_body, api_handler](http::ExpectedIncomingResponsePtr exp_resp) {
4✔
279
                        if (!exp_resp) {
4✔
280
                                log::Error("Request to push status data failed: " + exp_resp.error().message);
×
281
                                api_handler(exp_resp.error());
×
282
                                return;
×
283
                        }
284

285
                        auto body_writer = make_shared<io::ByteWriter>(received_body);
8✔
286
                        auto resp = exp_resp.value();
8✔
287
                        auto content_length = resp->GetHeader("Content-Length");
8✔
288
                        if (!content_length) {
4✔
289
                                log::Error(
×
290
                                        "Failed to get content length from the status API response headers: "
291
                                        + content_length.error().String());
×
292
                                body_writer->SetUnlimited(true);
×
293
                        } else {
294
                                auto ex_len = common::StringToLongLong(content_length.value());
8✔
295
                                if (!ex_len) {
4✔
296
                                        log::Error(
×
297
                                                "Failed to convert the content length from the status API response headers to an integer: "
298
                                                + ex_len.error().String());
×
299
                                        body_writer->SetUnlimited(true);
×
300
                                } else {
301
                                        received_body->resize(ex_len.value());
4✔
302
                                }
303
                        }
304
                        resp->SetBodyWriter(body_writer);
4✔
305
                },
306
                [received_body, api_handler](http::ExpectedIncomingResponsePtr exp_resp) {
4✔
307
                        if (!exp_resp) {
4✔
308
                                log::Error("Request to push status data failed: " + exp_resp.error().message);
×
309
                                api_handler(exp_resp.error());
×
310
                                return;
×
311
                        }
312

313
                        auto resp = exp_resp.value();
8✔
314
                        auto status = resp->GetStatusCode();
4✔
315
                        if (status == http::StatusNoContent) {
4✔
316
                                api_handler(error::NoError);
2✔
317
                        } else if (status == http::StatusConflict) {
2✔
318
                                api_handler(
1✔
319
                                        MakeError(DeploymentAbortedError, "Could not send status update to server"));
2✔
320
                        } else {
321
                                auto ex_err_msg = api::ErrorMsgFromErrorResponse(*received_body);
2✔
322
                                string err_str;
1✔
323
                                if (ex_err_msg) {
1✔
324
                                        err_str = ex_err_msg.value();
1✔
325
                                } else {
326
                                        err_str = resp->GetStatusMessage();
×
327
                                }
328
                                api_handler(MakeError(
1✔
329
                                        BadResponseError,
330
                                        "Got unexpected response " + to_string(status)
2✔
331
                                                + " from status API: " + err_str));
2✔
332
                        }
333
                });
8✔
334
}
335

336
using mender::common::expected::ExpectedSize;
337

338
static ExpectedSize GetLogFileDataSize(const string &path) {
3✔
339
        auto ex_istr = io::OpenIfstream(path);
6✔
340
        if (!ex_istr) {
3✔
341
                return expected::unexpected(ex_istr.error());
×
342
        }
343
        auto istr = std::move(ex_istr.value());
6✔
344

345
        // We want the size of the actual data without a potential trailing
346
        // newline. So let's seek one byte before the end of file, check if the last
347
        // byte is a newline and return the appropriate number.
348
        istr.seekg(-1, ios_base::end);
3✔
349
        char c = istr.get();
3✔
350
        if (c == '\n') {
3✔
351
                return istr.tellg() - static_cast<ifstream::off_type>(1);
6✔
352
        } else {
353
                return istr.tellg();
×
354
        }
355
}
356

357
const vector<uint8_t> JsonLogMessagesReader::header_ = {
358
        '{', '"', 'm', 'e', 's', 's', 'a', 'g', 'e', 's', '"', ':', '['};
359
const vector<uint8_t> JsonLogMessagesReader::closing_ = {']', '}'};
360

361
ExpectedSize JsonLogMessagesReader::Read(
89✔
362
        vector<uint8_t>::iterator start, vector<uint8_t>::iterator end) {
363
        if (header_rem_ > 0) {
89✔
364
                io::Vsize target_size = end - start;
9✔
365
                auto copy_end = copy_n(
366
                        header_.begin() + (header_.size() - header_rem_), min(header_rem_, target_size), start);
9✔
367
                auto n_copied = copy_end - start;
9✔
368
                header_rem_ -= n_copied;
9✔
369
                return static_cast<size_t>(n_copied);
18✔
370
        } else if (rem_raw_data_size_ > 0) {
80✔
371
                if (static_cast<size_t>(end - start) > rem_raw_data_size_) {
64✔
372
                        end = start + rem_raw_data_size_;
8✔
373
                }
374
                auto ex_sz = reader_->Read(start, end);
128✔
375
                if (!ex_sz) {
64✔
376
                        return ex_sz;
×
377
                }
378
                auto n_read = ex_sz.value();
64✔
379
                rem_raw_data_size_ -= n_read;
64✔
380

381
                // We control how much we read from the file so we should never read
382
                // 0 bytes (meaning EOF reached). If we do, it means the file is
383
                // smaller than what we were told.
384
                assert(n_read > 0);
64✔
385
                if (n_read == 0) {
64✔
386
                        return expected::unexpected(
×
387
                                MakeError(InvalidDataError, "Unexpected EOF when reading logs file"));
×
388
                }
389

390
                // Replace all newlines with commas
391
                const auto read_end = start + n_read;
64✔
392
                for (auto it = start; it < read_end; it++) {
1,916✔
393
                        if (it[0] == '\n') {
1,852✔
394
                                it[0] = ',';
12✔
395
                        }
396
                }
397
                return n_read;
64✔
398
        } else if (closing_rem_ > 0) {
16✔
399
                io::Vsize target_size = end - start;
8✔
400
                auto copy_end = copy_n(
401
                        closing_.begin() + (closing_.size() - closing_rem_),
×
402
                        min(closing_rem_, target_size),
8✔
403
                        start);
8✔
404
                auto n_copied = copy_end - start;
8✔
405
                closing_rem_ -= n_copied;
8✔
406
                return static_cast<size_t>(copy_end - start);
16✔
407
        } else {
408
                return 0;
8✔
409
        }
410
};
411

412
static const string logs_uri_suffix = "/log";
413

414
error::Error DeploymentClient::PushLogs(
3✔
415
        const string &deployment_id,
416
        const string &log_file_path,
417
        const string &server_url,
418
        http::Client &client,
419
        LogsAPIResponseHandler api_handler) {
420
        auto ex_size = GetLogFileDataSize(log_file_path);
6✔
421
        if (!ex_size) {
3✔
422
                // api_handler(ex_size.error()) ???
423
                return ex_size.error();
×
424
        }
425
        auto data_size = ex_size.value();
3✔
426

427
        auto file_reader = make_shared<io::FileReader>(log_file_path);
6✔
428
        auto logs_reader = make_shared<JsonLogMessagesReader>(file_reader, data_size);
6✔
429

430
        auto req = make_shared<http::OutgoingRequest>();
6✔
431
        req->SetAddress(
3✔
432
                http::JoinUrl(server_url, deployments_uri_prefix, deployment_id, logs_uri_suffix));
6✔
433
        req->SetMethod(http::Method::PUT);
3✔
434
        req->SetHeader("Content-Type", "application/json");
3✔
435
        req->SetHeader("Content-Length", to_string(JsonLogMessagesReader::TotalDataSize(data_size)));
3✔
436
        req->SetHeader("Accept", "application/json");
3✔
437
        req->SetBodyGenerator([logs_reader]() {
6✔
438
                logs_reader->Rewind();
3✔
439
                return logs_reader;
3✔
440
        });
6✔
441

442
        auto received_body = make_shared<vector<uint8_t>>();
3✔
443
        return client.AsyncCall(
444
                req,
445
                [received_body, api_handler](http::ExpectedIncomingResponsePtr exp_resp) {
3✔
446
                        if (!exp_resp) {
3✔
447
                                log::Error("Request to push logs data failed: " + exp_resp.error().message);
×
448
                                api_handler(exp_resp.error());
×
449
                                return;
×
450
                        }
451

452
                        auto body_writer = make_shared<io::ByteWriter>(received_body);
6✔
453
                        auto resp = exp_resp.value();
6✔
454
                        auto content_length = resp->GetHeader("Content-Length");
6✔
455
                        if (!content_length) {
3✔
456
                                log::Error(
×
457
                                        "Failed to get content length from the status API response headers: "
458
                                        + content_length.error().String());
×
459
                                body_writer->SetUnlimited(true);
×
460
                        } else {
461
                                auto ex_len = common::StringToLongLong(content_length.value());
6✔
462
                                if (!ex_len) {
3✔
463
                                        log::Error(
×
464
                                                "Failed to convert the content length from the status API response headers to an integer: "
465
                                                + ex_len.error().String());
×
466
                                        body_writer->SetUnlimited(true);
×
467
                                } else {
468
                                        received_body->resize(ex_len.value());
3✔
469
                                }
470
                        }
471
                        resp->SetBodyWriter(body_writer);
3✔
472
                },
473
                [received_body, api_handler](http::ExpectedIncomingResponsePtr exp_resp) {
3✔
474
                        if (!exp_resp) {
3✔
475
                                log::Error("Request to push logs data failed: " + exp_resp.error().message);
×
476
                                api_handler(exp_resp.error());
×
477
                                return;
×
478
                        }
479

480
                        auto resp = exp_resp.value();
6✔
481
                        auto status = resp->GetStatusCode();
3✔
482
                        if (status == http::StatusNoContent) {
3✔
483
                                api_handler(error::NoError);
2✔
484
                        } else {
485
                                auto ex_err_msg = api::ErrorMsgFromErrorResponse(*received_body);
2✔
486
                                string err_str;
1✔
487
                                if (ex_err_msg) {
1✔
488
                                        err_str = ex_err_msg.value();
1✔
489
                                } else {
490
                                        err_str = resp->GetStatusMessage();
×
491
                                }
492
                                api_handler(MakeError(
1✔
493
                                        BadResponseError,
494
                                        "Got unexpected response " + to_string(status) + " from logs API: " + err_str));
2✔
495
                        }
496
                });
3✔
497
}
498

499
} // namespace deployments
500
} // namespace update
501
} // 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

© 2025 Coveralls, Inc