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

mendersoftware / mender / 1039476627

17 Oct 2023 10:49AM UTC coverage: 79.701% (-0.6%) from 80.278%
1039476627

push

gitlab-ci

oleorhagen
fix(mender-auth): Remember to pass in the needed params

Just add the missing identity script, and private key params.

Ticket: MEN-6671
Changelog: None

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

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

6557 of 8227 relevant lines covered (79.7%)

9764.5 hits per line

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

69.05
/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::http;
28
namespace log = mender::common::log;
29

30
http::ExpectedOutgoingRequestPtr APIRequest::WithAuthData(const auth::AuthData &auth_data) {
26✔
31
        AssertOrReturnUnexpected(auth_data.server_url != "");
26✔
32

33
        auto out_req = make_shared<http::OutgoingRequest>(*this);
26✔
34
        if (auth_data.token != "") {
26✔
35
                out_req->SetHeader("Authorization", "Bearer " + auth_data.token);
16✔
36
        }
37
        auto err = out_req->SetAddress(http::JoinUrl(auth_data.server_url, address_.path));
26✔
38
        if (err != error::NoError) {
26✔
39
                return expected::unexpected(err);
×
40
        }
41
        return out_req;
26✔
42
}
43

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

73
                        auto err = http_client_.AsyncCall(ex_req.value(), header_handler, body_handler);
3✔
74
                        if (err != error::NoError) {
1✔
75
                                log::Error("Failed to schedule an HTTP request with the new token");
×
76
                                event_loop_.Post([header_handler, err]() {
×
77
                                        error::Error err_copy {err};
×
78
                                        header_handler(expected::unexpected(err_copy));
×
79
                                });
×
80
                                return;
81
                        }
82
                };
8✔
83

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

148
} // namespace api
149
} // 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