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

mendersoftware / mender / 1644050704

28 Jan 2025 07:35AM UTC coverage: 79.704% (-0.002%) from 79.706%
1644050704

push

gitlab-ci

web-flow
Merge pull request #1727 from jo-lund/4.0.x

4.0.x: fix: Clear the inventory data hash on re-authentication

3 of 10 new or added lines in 3 files covered. (30.0%)

1 existing line in 1 file now uncovered.

7214 of 9051 relevant lines covered (79.7%)

12088.5 hits per line

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

74.77
/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) {
3✔
82
        return error::Error(error_condition(code, AuthClientErrorCategory), msg);
6✔
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 = crypto::Sign(crypto_args, common::ByteVectorFromString(request_body));
12✔
145
        if (!expected_signature) {
6✔
146
                return expected_signature.error();
×
147
        }
148
        auto signature = expected_signature.value();
6✔
149

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

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

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

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

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

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

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

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

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

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

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

270
void Authenticator::ExpireToken() {
28✔
271
        if (!token_fetch_in_progress_) {
28✔
272
                RequestNewToken(nullopt);
56✔
273
        }
274
}
28✔
275

276
error::Error Authenticator::StartWatchingTokenSignal() {
12✔
277
        auto err = dbus_client_.RegisterSignalHandler<dbus::ExpectedStringPair>(
278
                "io.mender.Authentication1",
279
                "JwtTokenStateChange",
280
                [this](dbus::ExpectedStringPair ex_auth_dbus_data) {
16✔
281
                        auth_timeout_timer_.Cancel();
4✔
282
                        token_fetch_in_progress_ = false;
4✔
283
                        ExpectedAuthData ex_auth_data;
284
                        if (!ex_auth_dbus_data) {
4✔
285
                                mlog::Error(
×
286
                                        "Error from the JwtTokenStateChange DBus signal: "
287
                                        + ex_auth_dbus_data.error().String());
×
288
                                ex_auth_data = ExpectedAuthData(expected::unexpected(ex_auth_dbus_data.error()));
×
289
                        } else {
290
                                auto &token = ex_auth_dbus_data.value().first;
4✔
291
                                auto &server_url = ex_auth_dbus_data.value().second;
4✔
292

293
                                mlog::Debug("Got new authentication token for server " + server_url);
4✔
294

295
                                AuthData auth_data {server_url, token};
8✔
296
                                ex_auth_data = ExpectedAuthData(std::move(auth_data));
4✔
297
                                if (action_ != nullptr) {
4✔
NEW
298
                                        action_();
×
299
                                }
300
                        }
301
                        PostPendingActions(ex_auth_data);
4✔
302
                });
28✔
303

304
        watching_token_signal_ = (err == error::NoError);
12✔
305
        return err;
12✔
306
}
307

308
void Authenticator::PostPendingActions(ExpectedAuthData &ex_auth_data) {
43✔
309
        for (auto action : pending_actions_) {
51✔
310
                loop_.Post([action, ex_auth_data]() { action(ex_auth_data); });
40✔
311
        }
312
        pending_actions_.clear();
43✔
313
}
43✔
314

315
error::Error Authenticator::RequestNewToken(optional<AuthenticatedAction> opt_action) {
30✔
316
        if (token_fetch_in_progress_) {
30✔
317
                // Just make sure the action (if any) is called once the token is
318
                // obtained.
319
                if (opt_action) {
×
320
                        pending_actions_.push_back(*opt_action);
×
321
                }
322
                return error::NoError;
×
323
        }
324

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

359
        token_fetch_in_progress_ = true;
7✔
360

361
        // Make sure the action (if any) is called once the token is
362
        // obtained.
363
        if (opt_action) {
7✔
364
                pending_actions_.push_back(*opt_action);
2✔
365
        }
366

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

382
                        // In case it did happen, run the stacked up actions and unset the
383
                        // in_progress_ flag or things may got stuck.
384
                        token_fetch_in_progress_ = false;
×
385
                        ExpectedAuthData ex_auth_data = expected::unexpected(err);
×
386
                        PostPendingActions(ex_auth_data);
×
387
                }
388
        });
×
389
        return error::NoError;
7✔
390
}
391

392
error::Error Authenticator::WithToken(AuthenticatedAction action) {
22✔
393
        if (!watching_token_signal_) {
22✔
394
                auto err = StartWatchingTokenSignal();
12✔
395
                if (err != error::NoError) {
12✔
396
                        // Should never fail. We rely on the signal heavily so let's fail
397
                        // hard if it does.
398
                        return err;
1✔
399
                }
400
        }
401

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

426
                                mlog::Debug("Got authentication token for server " + server_url);
26✔
427

428
                                // Post/schedule pending actions before running the given action
429
                                // because the action can actually add more actions or even
430
                                // expire the token, etc. So post actions stacked up before we
431
                                // got here with the current token and only then give action a
432
                                // chance to mess with things.
433
                                ExpectedAuthData ex_auth_data {std::move(auth_data)};
434
                                PostPendingActions(ex_auth_data);
13✔
435
                                action(ex_auth_data);
26✔
436
                        } else {
437
                                // No valid token, let's request fetching of a new one
438
                                RequestNewToken(action);
4✔
439
                        }
440
                });
45✔
441
        if (err != error::NoError) {
15✔
442
                // No token and failed to try to get one (should never happen).
443
                ExpectedAuthData ex_auth_data = expected::unexpected(err);
×
444
                PostPendingActions(ex_auth_data);
×
445
                return err;
×
446
        }
447
        // else record that token is already being fetched (by GetJwtToken()).
448
        token_fetch_in_progress_ = true;
15✔
449
        return error::NoError;
15✔
450
}
451
} // namespace auth
452
} // namespace api
453
} // 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