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

mendersoftware / mender / 1058883419

02 Nov 2023 12:38PM UTC coverage: 80.182% (+0.2%) from 79.944%
1058883419

push

gitlab-ci

kacf
fix: Make sure RebootAction::Automatic is handled in RollbackReboot.

Changelog: None
Ticket: None

Signed-off-by: Kristian Amlie <kristian.amlie@northern.tech>

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

6947 of 8664 relevant lines covered (80.18%)

9289.26 hits per line

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

73.11
/src/api/auth/auth.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/auth.hpp>
16

17
#include <mutex>
18
#include <string>
19
#include <vector>
20

21
#include <common/common.hpp>
22
#include <common/crypto.hpp>
23
#include <common/json.hpp>
24
#include <common/error.hpp>
25
#include <common/log.hpp>
26
#include <common/path.hpp>
27
#include <common/expected.hpp>
28
#include <common/identity_parser.hpp>
29
#include <common/optional.hpp>
30

31
namespace mender {
32
namespace api {
33
namespace auth {
34

35

36
using namespace std;
37
namespace error = mender::common::error;
38
namespace common = mender::common;
39
namespace conf = mender::common::conf;
40

41

42
namespace identity_parser = mender::common::identity_parser;
43
namespace key_value_parser = mender::common::key_value_parser;
44
namespace path = mender::common::path;
45
namespace mlog = mender::common::log;
46
namespace expected = mender::common::expected;
47
namespace io = mender::common::io;
48
namespace json = mender::common::json;
49
namespace crypto = mender::common::crypto;
50

51

52
const string request_uri = "/api/devices/v1/authentication/auth_requests";
53

54
const AuthClientErrorCategoryClass AuthClientErrorCategory;
55

56
const char *AuthClientErrorCategoryClass::name() const noexcept {
×
57
        return "AuthClientErrorCategory";
×
58
}
59

60
string AuthClientErrorCategoryClass::message(int code) const {
4✔
61
        switch (code) {
4✔
62
        case NoError:
63
                return "Success";
×
64
        case SetupError:
65
                return "Error during setup";
×
66
        case RequestError:
67
                return "HTTP client request error";
×
68
        case ResponseError:
69
                return "HTTP client response error";
×
70
        case APIError:
71
                return "API error";
2✔
72
        case UnauthorizedError:
73
                return "Unauthorized error";
×
74
        case AuthenticationError:
75
                return "Authentication error";
2✔
76
        default:
77
                return "Unknown";
×
78
        }
79
}
80

81
error::Error MakeError(AuthClientErrorCode code, const string &msg) {
×
82
        return error::Error(error_condition(code, AuthClientErrorCategory), msg);
3✔
83
}
84

85
namespace http {
86

87
error::Error MakeHTTPResponseError(
2✔
88
        const AuthClientErrorCode code,
89
        const mender::http::ResponsePtr resp,
90
        const string &response_body,
91
        const string &msg) {
92
        return error::Error(
93
                error_condition(code, AuthClientErrorCategory),
2✔
94
                "Authentication error(" + resp->GetStatusMessage() + "): " + msg + "(" + response_body
4✔
95
                        + ")");
6✔
96
}
97

98
static void TryAuthenticate(
99
        vector<string>::const_iterator server_it,
100
        vector<string>::const_iterator end,
101
        mender::http::Client &client,
102
        const string request_body,
103
        const string signature,
104
        APIResponseHandler api_handler);
105

106
error::Error FetchJWTToken(
6✔
107
        mender::http::Client &client,
108
        const vector<string> &servers,
109
        const crypto::Args &crypto_args,
110
        const string &device_identity_script_path,
111
        APIResponseHandler api_handler,
112
        const string &tenant_token) {
113
        key_value_parser::ExpectedKeyValuesMap expected_identity_data =
114
                identity_parser::GetIdentityData(device_identity_script_path);
6✔
115
        if (!expected_identity_data) {
6✔
116
                return expected_identity_data.error();
×
117
        }
118

119
        auto identity_data_json = identity_parser::DumpIdentityData(expected_identity_data.value());
6✔
120
        mlog::Debug("Got identity data: " + identity_data_json);
12✔
121

122
        // Create the request body
123
        unordered_map<string, string> request_body_map {
124
                {"id_data", identity_data_json},
125
        };
18✔
126

127
        if (tenant_token.size() > 0) {
6✔
128
                request_body_map.insert({"tenant_token", tenant_token});
4✔
129
        }
130

131
        auto expected_public_key = crypto::ExtractPublicKey(crypto_args);
6✔
132
        if (!expected_public_key) {
6✔
133
                return expected_public_key.error();
×
134
        }
135
        request_body_map.insert({"pubkey", expected_public_key.value()});
12✔
136

137
        auto expected_request_body = json::Dump(request_body_map);
12✔
138
        if (!expected_request_body) {
6✔
139
                return expected_request_body.error();
×
140
        }
141
        auto request_body = expected_request_body.value();
6✔
142

143
        // Sign the body
144
        auto expected_signature =
145
                crypto::SignRawData(crypto_args, common::ByteVectorFromString(request_body));
12✔
146
        if (!expected_signature) {
6✔
147
                return expected_signature.error();
×
148
        }
149
        auto signature = expected_signature.value();
6✔
150

151
        // TryAuthenticate() calls the handler on any potential further errors, we
152
        // are done here with no errors.
153
        TryAuthenticate(servers.cbegin(), servers.cend(), client, request_body, signature, api_handler);
12✔
154
        return error::NoError;
6✔
155
}
156

157
static void TryAuthenticate(
10✔
158
        vector<string>::const_iterator server_it,
159
        vector<string>::const_iterator end,
160
        mender::http::Client &client,
161
        const string request_body,
162
        const string signature,
163
        APIResponseHandler api_handler) {
164
        if (server_it == end) {
10✔
165
                auto err = MakeError(AuthenticationError, "No more servers to try for authentication");
2✔
166
                api_handler(expected::unexpected(err));
2✔
167
                return;
168
        }
169

170
        auto whole_url = mender::http::JoinUrl(*server_it, request_uri);
9✔
171
        auto req = make_shared<mender::http::OutgoingRequest>();
9✔
172
        req->SetMethod(mender::http::Method::POST);
9✔
173
        req->SetAddress(whole_url);
18✔
174
        req->SetHeader("Content-Type", "application/json");
18✔
175
        req->SetHeader("Content-Length", to_string(request_body.size()));
18✔
176
        req->SetHeader("Accept", "application/json");
18✔
177
        req->SetHeader("X-MEN-Signature", signature);
18✔
178
        req->SetHeader("Authorization", "API_KEY");
18✔
179

180
        req->SetBodyGenerator([request_body]() -> io::ExpectedReaderPtr {
45✔
181
                return make_shared<io::StringReader>(request_body);
7✔
182
        });
18✔
183

184
        auto received_body = make_shared<vector<uint8_t>>();
9✔
185

186
        auto err = client.AsyncCall(
187
                req,
188
                [received_body, server_it, end, &client, request_body, signature, api_handler](
9✔
189
                        mender::http::ExpectedIncomingResponsePtr exp_resp) {
2✔
190
                        if (!exp_resp) {
9✔
191
                                mlog::Info(
2✔
192
                                        "Authentication error trying server '" + *server_it
4✔
193
                                        + "': " + exp_resp.error().String());
6✔
194
                                TryAuthenticate(
2✔
195
                                        std::next(server_it), end, client, request_body, signature, api_handler);
4✔
196
                                return;
2✔
197
                        }
198
                        auto resp = exp_resp.value();
7✔
199

200
                        auto body_writer = make_shared<io::ByteWriter>(received_body);
7✔
201
                        body_writer->SetUnlimited(true);
7✔
202
                        resp->SetBodyWriter(body_writer);
7✔
203

204
                        mlog::Debug("Received response header value:");
14✔
205
                        mlog::Debug("Status code:" + to_string(resp->GetStatusCode()));
14✔
206
                        mlog::Debug("Status message: " + resp->GetStatusMessage());
14✔
207
                },
208
                [received_body, server_it, end, &client, request_body, signature, api_handler](
7✔
209
                        mender::http::ExpectedIncomingResponsePtr exp_resp) {
2✔
210
                        if (!exp_resp) {
7✔
211
                                mlog::Info(
×
212
                                        "Authentication error trying server '" + *server_it
×
213
                                        + "': " + exp_resp.error().String());
×
214
                                TryAuthenticate(
×
215
                                        std::next(server_it), end, client, request_body, signature, api_handler);
×
216
                                return;
×
217
                        }
218
                        auto resp = exp_resp.value();
7✔
219

220
                        string response_body = common::StringFromByteVector(*received_body);
7✔
221

222
                        error::Error err;
7✔
223
                        switch (resp->GetStatusCode()) {
7✔
224
                        case mender::http::StatusOK:
5✔
225
                                api_handler(AuthData {*server_it, response_body});
15✔
226
                                return;
5✔
227
                        case mender::http::StatusUnauthorized:
228
                                err = MakeHTTPResponseError(
×
229
                                        UnauthorizedError, resp, response_body, "Failed to authorize with the server.");
×
230
                                mlog::Info(
×
231
                                        "Authentication error trying server '" + *server_it + "': " + err.String());
×
232
                                TryAuthenticate(
×
233
                                        std::next(server_it), end, client, request_body, signature, api_handler);
×
234
                                return;
×
235
                        case mender::http::StatusBadRequest:
236
                        case mender::http::StatusInternalServerError:
237
                                err = MakeHTTPResponseError(
2✔
238
                                        APIError, resp, response_body, "Failed to authorize with the server.");
6✔
239
                                mlog::Info(
2✔
240
                                        "Authentication error trying server '" + *server_it + "': " + err.String());
4✔
241
                                TryAuthenticate(
2✔
242
                                        std::next(server_it), end, client, request_body, signature, api_handler);
4✔
243
                                return;
2✔
244
                        default:
245
                                err =
246
                                        MakeError(ResponseError, "Unexpected error code: " + resp->GetStatusMessage());
×
247
                                mlog::Info(
×
248
                                        "Authentication error trying server '" + *server_it + "': " + err.String());
×
249
                                TryAuthenticate(
×
250
                                        std::next(server_it), end, client, request_body, signature, api_handler);
×
251
                                return;
×
252
                        }
253
                });
36✔
254
        if (err != error::NoError) {
9✔
255
                api_handler(expected::unexpected(err));
×
256
        }
257
}
258
} // namespace http
259

260
error::Error FetchJWTToken(
6✔
261
        mender::http::Client &client,
262
        const vector<string> &servers,
263
        const crypto::Args &crypto_args,
264
        const string &device_identity_script_path,
265
        APIResponseHandler api_handler,
266
        const string &tenant_token) {
267
        return http::FetchJWTToken(
268
                client, servers, crypto_args, device_identity_script_path, api_handler, tenant_token);
12✔
269
}
270

271
void Authenticator::ExpireToken() {
27✔
272
        if (!token_fetch_in_progress_) {
27✔
273
                RequestNewToken(nullopt);
54✔
274
        }
275
}
27✔
276

277
error::Error Authenticator::StartWatchingTokenSignal() {
10✔
278
        auto err = dbus_client_.RegisterSignalHandler<dbus::ExpectedStringPair>(
279
                "io.mender.Authentication1",
280
                "JwtTokenStateChange",
281
                [this](dbus::ExpectedStringPair ex_auth_dbus_data) {
9✔
282
                        auth_timeout_timer_.Cancel();
3✔
283
                        token_fetch_in_progress_ = false;
3✔
284
                        ExpectedAuthData ex_auth_data;
285
                        if (!ex_auth_dbus_data) {
3✔
286
                                mlog::Error(
×
287
                                        "Error from the JwtTokenStateChange DBus signal: "
288
                                        + ex_auth_dbus_data.error().String());
×
289
                                ex_auth_data = ExpectedAuthData(expected::unexpected(ex_auth_dbus_data.error()));
×
290
                        } else {
291
                                auto &token = ex_auth_dbus_data.value().first;
3✔
292
                                auto &server_url = ex_auth_dbus_data.value().second;
3✔
293
                                AuthData auth_data {server_url, token};
3✔
294
                                ex_auth_data = ExpectedAuthData(std::move(auth_data));
3✔
295
                        }
296
                        PostPendingActions(ex_auth_data);
3✔
297
                });
23✔
298

299
        watching_token_signal_ = (err == error::NoError);
10✔
300
        return err;
10✔
301
}
302

303
void Authenticator::PostPendingActions(ExpectedAuthData &ex_auth_data) {
39✔
304
        for (auto action : pending_actions_) {
47✔
305
                loop_.Post([action, ex_auth_data]() { action(ex_auth_data); });
40✔
306
        }
307
        pending_actions_.clear();
39✔
308
}
39✔
309

310
error::Error Authenticator::RequestNewToken(optional<AuthenticatedAction> opt_action) {
29✔
311
        if (token_fetch_in_progress_) {
29✔
312
                // Just make sure the action (if any) is called once the token is
313
                // obtained.
314
                if (opt_action) {
×
315
                        pending_actions_.push_back(*opt_action);
×
316
                }
317
                return error::NoError;
×
318
        }
319

320
        auto err = dbus_client_.CallMethod<expected::ExpectedBool>(
321
                "io.mender.AuthenticationManager",
322
                "/io/mender/AuthenticationManager",
323
                "io.mender.Authentication1",
324
                "FetchJwtToken",
325
                [this](expected::ExpectedBool ex_value) {
6✔
326
                        if (!ex_value) {
6✔
327
                                token_fetch_in_progress_ = false;
1✔
328
                                mlog::Error("Failed to request new token fetching: " + ex_value.error().String());
2✔
329
                                ExpectedAuthData ex_auth_data = expected::unexpected(ex_value.error());
1✔
330
                                PostPendingActions(ex_auth_data);
1✔
331
                        } else if (!ex_value.value()) {
5✔
332
                                // mender-auth encountered an error not sent over DBus (should never happen)
333
                                token_fetch_in_progress_ = false;
×
334
                                mlog::Error(
×
335
                                        "Failed to request new token fetching (see mender-auth logs for details)");
×
336
                                ExpectedAuthData ex_auth_data = expected::unexpected(MakeError(
×
337
                                        AuthenticationError, "Failed to request new token fetching from mender-auth"));
×
338
                                PostPendingActions(ex_auth_data);
×
339
                        }
340
                });
64✔
341
        if (err != error::NoError) {
29✔
342
                // A sync DBus error.
343
                mlog::Error("Failed to request new token fetching: " + err.String());
46✔
344
                token_fetch_in_progress_ = false;
23✔
345
                ExpectedAuthData ex_auth_data = expected::unexpected(err);
23✔
346
                PostPendingActions(ex_auth_data);
23✔
347
                if (opt_action) {
23✔
348
                        (*opt_action)(ex_auth_data);
×
349
                }
350
                return err;
23✔
351
        }
352
        // else everything went OK
353

354
        token_fetch_in_progress_ = true;
6✔
355

356
        // Make sure the action (if any) is called once the token is
357
        // obtained.
358
        if (opt_action) {
6✔
359
                pending_actions_.push_back(*opt_action);
2✔
360
        }
361

362
        // Make sure we don't wait for the token forever.
363
        auth_timeout_timer_.AsyncWait(auth_timeout_, [this](error::Error err) {
6✔
364
                if (err.code == make_error_condition(errc::operation_canceled)) {
5✔
365
                        return;
366
                } else if (err == error::NoError) {
2✔
367
                        mlog::Warning("Timed-out waiting for a new token");
4✔
368
                        token_fetch_in_progress_ = false;
2✔
369
                        ExpectedAuthData ex_auth_data = expected::unexpected(
2✔
370
                                MakeError(AuthenticationError, "Timed-out waiting for a new token"));
6✔
371
                        PostPendingActions(ex_auth_data);
2✔
372
                } else {
373
                        // should never happen
374
                        assert(false);
375
                        mlog::Error("Authentication timer error: " + err.String());
×
376

377
                        // In case it did happen, run the stacked up actions and unset the
378
                        // in_progress_ flag or things may got stuck.
379
                        token_fetch_in_progress_ = false;
×
380
                        ExpectedAuthData ex_auth_data = expected::unexpected(err);
×
381
                        PostPendingActions(ex_auth_data);
×
382
                }
383
        });
×
384
        return error::NoError;
6✔
385
}
386

387
error::Error Authenticator::WithToken(AuthenticatedAction action) {
18✔
388
        if (!watching_token_signal_) {
18✔
389
                auto err = StartWatchingTokenSignal();
10✔
390
                if (err != error::NoError) {
10✔
391
                        // Should never fail. We rely on the signal heavily so let's fail
392
                        // hard if it does.
393
                        action(expected::unexpected(err));
×
394
                        return err;
×
395
                }
396
        }
397

398
        if (token_fetch_in_progress_) {
18✔
399
                // Already waiting for a new token, just make sure the action is called
400
                // once it arrives (or once the wait times out).
401
                pending_actions_.push_back(action);
6✔
402
                return error::NoError;
6✔
403
        }
404
        // else => should fetch the token, cache it and call all pending actions
405
        // Try to get token from mender-auth
406
        auto err = dbus_client_.CallMethod<dbus::ExpectedStringPair>(
407
                "io.mender.AuthenticationManager",
408
                "/io/mender/AuthenticationManager",
409
                "io.mender.Authentication1",
410
                "GetJwtToken",
411
                [this, action](dbus::ExpectedStringPair ex_auth_dbus_data) {
24✔
412
                        token_fetch_in_progress_ = false;
12✔
413
                        auth_timeout_timer_.Cancel();
12✔
414
                        if (ex_auth_dbus_data && (ex_auth_dbus_data.value().first != "")
11✔
415
                                && (ex_auth_dbus_data.value().second != "")) {
22✔
416
                                // Got a valid token, let's save it and then call action and any
417
                                // previously-pending actions (if any) with it.
418
                                auto &token = ex_auth_dbus_data.value().first;
10✔
419
                                auto &server_url = ex_auth_dbus_data.value().second;
10✔
420
                                AuthData auth_data {server_url, token};
20✔
421

422
                                // Post/schedule pending actions before running the given action
423
                                // because the action can actually add more actions or even
424
                                // expire the token, etc. So post actions stacked up before we
425
                                // got here with the current token and only then give action a
426
                                // chance to mess with things.
427
                                ExpectedAuthData ex_auth_data {std::move(auth_data)};
428
                                PostPendingActions(ex_auth_data);
10✔
429
                                action(ex_auth_data);
20✔
430
                        } else {
431
                                // No valid token, let's request fetching of a new one
432
                                RequestNewToken(action);
4✔
433
                        }
434
                });
36✔
435
        if (err != error::NoError) {
12✔
436
                // No token and failed to try to get one (should never happen).
437
                ExpectedAuthData ex_auth_data = expected::unexpected(err);
×
438
                PostPendingActions(ex_auth_data);
×
439
                action(ex_auth_data);
×
440
                return err;
×
441
        }
442
        // else record that token is already being fetched (by GetJwtToken()).
443
        token_fetch_in_progress_ = true;
12✔
444

445
        return error::NoError;
12✔
446
}
447

448
} // namespace auth
449
} // namespace api
450
} // 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