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

mendersoftware / mender / 1056184062

31 Oct 2023 12:41PM UTC coverage: 80.182% (+0.3%) from 79.877%
1056184062

push

gitlab-ci

kacf
chore: Do not try to resume requests that were cancelled by callers.

This requires us to move the destruction of the inner reader until
after the body handler has been called, otherwise it will trigger
another `operation_canceled` error which we don't want.

Ticket: MEN-6807

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

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

6890 of 8593 relevant lines covered (80.18%)

9363.89 hits per line

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

59.76
/src/mender-auth/cli/actions.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-auth/cli/actions.hpp>
16

17
#include <string>
18
#include <memory>
19

20
#include <api/auth.hpp>
21

22
#include <mender-auth/context.hpp>
23
#include <mender-auth/cli/keystore.hpp>
24

25
#include <common/conf.hpp>
26
#include <common/events.hpp>
27
#include <common/log.hpp>
28

29
#include <mender-auth/ipc/server.hpp>
30

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

35
using namespace std;
36

37
namespace auth_client = mender::api::auth;
38
namespace events = mender::common::events;
39
namespace ipc = mender::auth::ipc;
40
namespace log = mender::common::log;
41

42
shared_ptr<MenderKeyStore> KeystoreFromConfig(
4✔
43
        const conf::MenderConfig &config, const string &passphrase) {
44
        cli::StaticKey static_key = cli::StaticKey::No;
4✔
45
        string pem_file;
46
        string ssl_engine;
47

48
        // TODO: review and simplify logic as part of MEN-6668. See discussion at:
49
        // https://github.com/mendersoftware/mender/pull/1378#discussion_r1317185066
50
        if (config.https_client.key != "") {
4✔
51
                pem_file = config.https_client.key;
52
                ssl_engine = config.https_client.ssl_engine;
×
53
                static_key = cli::StaticKey::Yes;
×
54
        }
55
        if (config.security.auth_private_key != "") {
4✔
56
                pem_file = config.security.auth_private_key;
57
                ssl_engine = config.security.ssl_engine;
×
58
                static_key = cli::StaticKey::Yes;
×
59
        }
60
        if (config.https_client.key == "" && config.security.auth_private_key == "") {
4✔
61
                pem_file = config.paths.GetKeyFile();
4✔
62
                ssl_engine = config.https_client.ssl_engine;
4✔
63
                static_key = cli::StaticKey::No;
4✔
64
        }
65

66
        return make_shared<MenderKeyStore>(pem_file, ssl_engine, static_key, passphrase);
8✔
67
}
68

69
error::Error DoBootstrap(shared_ptr<MenderKeyStore> keystore, const bool force) {
4✔
70
        auto err = keystore->Load();
4✔
71
        if (err != error::NoError && err.code != MakeError(NoKeysError, "").code) {
10✔
72
                return err;
73
        }
74
        if (err.code == MakeError(NoKeysError, "").code || force) {
8✔
75
                log::Info("Generating new RSA key");
6✔
76
                err = keystore->Generate();
3✔
77
                if (err != error::NoError) {
3✔
78
                        return err;
79
                }
80
                err = keystore->Save();
6✔
81
                if (err != error::NoError) {
82
                        return err;
83
                }
84
        }
85
        return err;
86
}
87

88
error::Error DoAuthenticate(context::MenderContext &main_context) {
4✔
89
        events::EventLoop loop;
90
        auto &config = main_context.GetConfig();
91
        if (config.servers.size() == 0) {
4✔
92
                log::Info("No server set in the configuration, skipping authentication");
6✔
93
                return error::NoError;
3✔
94
        }
95
        mender::common::events::Timer timer {loop};
2✔
96
        http::Client client {main_context.GetConfig().GetHttpClientConfig(), loop};
3✔
97
        auto err = auth_client::FetchJWTToken(
98
                client,
99
                config.servers,
1✔
100
                config.paths.GetKeyFile(),
1✔
101
                config.paths.GetIdentityScript(),
1✔
102
                [&loop, &timer](auth_client::APIResponse resp) {
3✔
103
                        log::Info("Got Auth response");
2✔
104
                        if (resp) {
1✔
105
                                log::Info(
1✔
106
                                        "Successfully authorized with the server '" + resp.value().server_url + "'");
2✔
107
                        } else {
108
                                log::Error(resp.error().String());
×
109
                        }
110
                        timer.Cancel();
1✔
111
                        loop.Stop();
1✔
112
                },
1✔
113
                config.tenant_token);
2✔
114
        if (err != error::NoError) {
1✔
115
                return err;
×
116
        }
117
        timer.AsyncWait(chrono::seconds {30}, [&loop](error::Error err) { loop.Stop(); });
1✔
118
        loop.Run();
1✔
119
        return error::NoError;
1✔
120
}
121

122
DaemonAction::DaemonAction(shared_ptr<MenderKeyStore> keystore, const bool force_bootstrap) :
×
123
        keystore_(keystore),
124
        force_bootstrap_(force_bootstrap) {
×
125
}
×
126

127
ExpectedActionPtr DaemonAction::Create(
×
128
        const conf::MenderConfig &config, const string &passphrase, const bool force_bootstrap) {
129
        auto key_store = KeystoreFromConfig(config, passphrase);
×
130

131
        return make_shared<DaemonAction>(key_store, force_bootstrap);
×
132
}
133

134
error::Error DaemonAction::Execute(context::MenderContext &main_context) {
×
135
        auto &config = main_context.GetConfig();
136
        if (none_of(config.servers.cbegin(), config.servers.cend(), [](const string &it) {
×
137
                        return it != "";
×
138
                })) {
139
                log::Error("Cannot run in daemon mode with no server URL specified");
×
140
                return error::MakeError(error::ExitWithFailureError, "");
×
141
        }
142

143
        auto err = DoBootstrap(keystore_, force_bootstrap_);
×
144
        if (err != error::NoError) {
×
145
                log::Error("Failed to bootstrap: " + err.String());
×
146
                return error::MakeError(error::ExitWithFailureError, "");
×
147
        }
148

149
        events::EventLoop loop {};
×
150

151
        ipc::Server ipc_server {loop, config};
×
152

153
        err = ipc_server.Listen(
×
154
                config.security.auth_private_key == "" ? config.paths.GetKeyFile()
×
155
                                                                                           : config.security.auth_private_key,
156
                config.paths.GetIdentityScript());
×
157
        if (err != error::NoError) {
×
158
                log::Error("Failed to start the listen loop");
×
159
                log::Error(err.String());
×
160
                return error::MakeError(error::ExitWithFailureError, "");
×
161
        }
162

163
        loop.Run();
×
164

165
        return error::NoError;
×
166
}
167

168
BootstrapAction::BootstrapAction(shared_ptr<MenderKeyStore> keystore, bool force_bootstrap) :
4✔
169
        keystore_(keystore),
170
        force_bootstrap_(force_bootstrap) {
8✔
171
}
4✔
172

173
ExpectedActionPtr BootstrapAction::Create(
4✔
174
        const conf::MenderConfig &config, const string &passphrase, bool force_bootstrap) {
175
        auto key_store = KeystoreFromConfig(config, passphrase);
4✔
176

177
        return make_shared<BootstrapAction>(key_store, force_bootstrap);
8✔
178
}
179

180
error::Error BootstrapAction::Execute(context::MenderContext &main_context) {
4✔
181
        auto err = DoBootstrap(keystore_, force_bootstrap_);
4✔
182
        if (err != error::NoError) {
4✔
183
                return err;
×
184
        }
185
        return DoAuthenticate(main_context);
4✔
186
}
187

188
} // namespace cli
189
} // namespace auth
190
} // 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