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

mendersoftware / mender / 1641906300

27 Jan 2025 05:31AM UTC coverage: 75.952% (-0.002%) from 75.954%
1641906300

push

gitlab-ci

web-flow
Merge pull request #1734 from mendersoftware/cherry-5.0.x-MEN-7900-client-stuck-after-sync-error

[Cherry 5.0.x]: MEN-7900: Fix Mender client getting stuck after failure in sync state

24 of 40 new or added lines in 3 files covered. (60.0%)

100 existing lines in 4 files now uncovered.

7381 of 9718 relevant lines covered (75.95%)

11129.08 hits per line

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

75.28
/src/api/client.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 <api/client.hpp>
16

17
#include <common/error.hpp>
18
#include <common/expected.hpp>
19
#include <common/http.hpp>
20
#include <common/log.hpp>
21

22
namespace mender {
23
namespace api {
24

25
namespace error = mender::common::error;
26
namespace expected = mender::common::expected;
27
namespace http = mender::common::http;
28
namespace log = mender::common::log;
29

30
http::ExpectedOutgoingRequestPtr APIRequest::WithAuthData(const auth::AuthData &auth_data) {
30✔
31
        if (auth_data.server_url == "") {
30✔
32
                if (auth_data.token != "") {
1✔
33
                        return expected::unexpected(
×
34
                                auth::MakeError(auth::APIError, "Token did not come with a server URL"));
×
35
                } else {
36
                        return expected::unexpected(
1✔
37
                                auth::MakeError(auth::UnauthorizedError, "Cannot submit API request"));
3✔
38
                }
39
        }
40

41
        auto out_req = make_shared<http::OutgoingRequest>(*this);
29✔
42
        if (auth_data.token != "") {
29✔
43
                out_req->SetHeader("Authorization", "Bearer " + auth_data.token);
20✔
44
        }
45
        auto err = out_req->SetAddress(http::JoinUrl(auth_data.server_url, address_.path));
29✔
46
        if (err != error::NoError) {
29✔
47
                return expected::unexpected(err);
×
48
        }
49
        return out_req;
29✔
50
}
51

52
error::Error HTTPClient::AsyncCall(
10✔
53
        APIRequestPtr req, http::ResponseHandler header_handler, http::ResponseHandler body_handler) {
54
        // If the first request fails with 401, we need to get a new token and then
55
        // try again with the new token. We should avoid using the same
56
        // OutgoingRequest object for the two different requests, hence a copy and a
57
        // different handler using the copy instead of the original OutgoingRequest
58
        // given.
59
        auto reauth_req = make_shared<APIRequest>(*req);
10✔
60
        auto reauthenticated_handler =
61
                [this, reauth_req, header_handler, body_handler](auth::ExpectedAuthData ex_auth_data) {
3✔
62
                        if (!ex_auth_data) {
3✔
63
                                log::Error("Failed to obtain authentication credentials");
2✔
64
                                event_loop_.Post([header_handler, ex_auth_data]() {
2✔
65
                                        error::Error err = ex_auth_data.error();
1✔
66
                                        header_handler(expected::unexpected(err));
3✔
67
                                });
4✔
68
                                return;
2✔
69
                        }
70
                        auto ex_req = reauth_req->WithAuthData(ex_auth_data.value());
2✔
71
                        if (!ex_req) {
2✔
72
                                log::Error("Failed to set new authentication data on HTTP request");
2✔
73
                                auto err = ex_req.error();
1✔
74
                                event_loop_.Post([header_handler, err]() {
1✔
75
                                        error::Error err_copy {err};
1✔
76
                                        header_handler(expected::unexpected(err_copy));
3✔
77
                                });
3✔
78
                                return;
79
                        }
80

81
                        auto err = http_client_.AsyncCall(ex_req.value(), header_handler, body_handler);
3✔
82
                        if (err != error::NoError) {
1✔
83
                                log::Error("Failed to schedule an HTTP request with the new token");
×
84
                                event_loop_.Post([header_handler, err]() {
×
85
                                        error::Error err_copy {err};
×
86
                                        header_handler(expected::unexpected(err_copy));
×
87
                                });
×
88
                                return;
89
                        }
90
                };
10✔
91

92
        return authenticator_.WithToken(
10✔
93
                [this, req, header_handler, body_handler, reauthenticated_handler](
10✔
94
                        auth::ExpectedAuthData ex_auth_data) {
19✔
95
                        if (!ex_auth_data) {
10✔
96
                                log::Error("Failed to obtain authentication credentials");
2✔
97
                                event_loop_.Post([header_handler, ex_auth_data]() {
2✔
98
                                        error::Error err = ex_auth_data.error();
1✔
99
                                        header_handler(expected::unexpected(err));
3✔
100
                                });
4✔
101
                                return;
1✔
102
                        }
103
                        auto ex_req = req->WithAuthData(ex_auth_data.value());
9✔
104
                        if (!ex_req) {
9✔
UNCOV
105
                                log::Error("Failed to set new authentication data on HTTP request");
×
106
                                auto err = ex_req.error();
×
107
                                event_loop_.Post([header_handler, err]() {
×
108
                                        error::Error err_copy {err};
×
109
                                        header_handler(expected::unexpected(err_copy));
×
110
                                });
×
111
                                return;
112
                        }
113
                        auto err = http_client_.AsyncCall(
114
                                ex_req.value(),
9✔
115
                                [this, header_handler, reauthenticated_handler](
9✔
116
                                        http::ExpectedIncomingResponsePtr ex_resp) {
6✔
117
                                        if (!ex_resp) {
9✔
UNCOV
118
                                                header_handler(ex_resp);
×
119
                                                return;
6✔
120
                                        }
121
                                        auto resp = ex_resp.value();
9✔
122
                                        auto status = resp->GetStatusCode();
9✔
123
                                        if (status != http::StatusUnauthorized) {
9✔
124
                                                header_handler(ex_resp);
12✔
125
                                                return;
126
                                        }
127
                                        log::Debug(
3✔
128
                                                "Got " + to_string(http::StatusUnauthorized)
6✔
129
                                                + " from the server, expiring token");
6✔
130
                                        authenticator_.ExpireToken();
3✔
131
                                        authenticator_.WithToken(reauthenticated_handler);
6✔
132
                                },
133
                                [body_handler](http::ExpectedIncomingResponsePtr ex_resp) {
63✔
134
                                        if (!ex_resp) {
9✔
UNCOV
135
                                                body_handler(ex_resp);
×
136
                                                return;
×
137
                                        }
138
                                        auto resp = ex_resp.value();
9✔
139
                                        auto status = resp->GetStatusCode();
9✔
140
                                        if (status != http::StatusUnauthorized) {
9✔
141
                                                body_handler(ex_resp);
12✔
142
                                        }
143
                                        // 401 handled by the header handler
144
                                });
27✔
145
                        if (err != error::NoError) {
9✔
UNCOV
146
                                log::Error("Failed to schedule an HTTP request with an existing new token");
×
147
                                event_loop_.Post([header_handler, err]() {
×
148
                                        error::Error err_copy {err};
×
149
                                        header_handler(expected::unexpected(err_copy));
×
150
                                });
×
151
                                return;
152
                        }
153
                });
30✔
154
}
155

156
} // namespace api
157
} // 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