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

mendersoftware / mender / 1057637229

01 Nov 2023 01:17PM UTC coverage: 79.933% (-0.3%) from 80.207%
1057637229

push

gitlab-ci

oleorhagen
chore: Alias mender-authd.service -> mender-client.service

This aliases the systemd.service for `mender-authd` to `mender-client`, so that
other existing third-party services relying on it will still work transparently.

Ticket: MEN-6812

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

6899 of 8631 relevant lines covered (79.93%)

9322.97 hits per line

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

60.0
/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
        if (config.security.auth_private_key != "") {
4✔
49
                pem_file = config.security.auth_private_key;
50
                ssl_engine = config.security.ssl_engine;
×
51
                static_key = cli::StaticKey::Yes;
×
52
        } else {
53
                pem_file = config.paths.GetKeyFile();
4✔
54
                static_key = cli::StaticKey::No;
4✔
55
        }
56

57
        return make_shared<MenderKeyStore>(pem_file, ssl_engine, static_key, passphrase);
8✔
58
}
59

60
error::Error DoBootstrap(shared_ptr<MenderKeyStore> keystore, const bool force) {
4✔
61
        auto err = keystore->Load();
4✔
62
        if (err != error::NoError && err.code != MakeError(NoKeysError, "").code) {
10✔
63
                return err;
64
        }
65
        if (err.code == MakeError(NoKeysError, "").code || force) {
8✔
66
                log::Info("Generating new RSA key");
6✔
67
                err = keystore->Generate();
3✔
68
                if (err != error::NoError) {
3✔
69
                        return err;
70
                }
71
                err = keystore->Save();
6✔
72
                if (err != error::NoError) {
73
                        return err;
74
                }
75
        }
76
        return err;
77
}
78

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

114
DaemonAction::DaemonAction(shared_ptr<MenderKeyStore> keystore, const bool force_bootstrap) :
×
115
        keystore_(keystore),
116
        force_bootstrap_(force_bootstrap) {
×
117
}
×
118

119
ExpectedActionPtr DaemonAction::Create(
×
120
        const conf::MenderConfig &config, const string &passphrase, const bool force_bootstrap) {
121
        auto key_store = KeystoreFromConfig(config, passphrase);
×
122

123
        return make_shared<DaemonAction>(key_store, force_bootstrap);
×
124
}
125

126
error::Error DaemonAction::Execute(context::MenderContext &main_context) {
×
127
        auto &config = main_context.GetConfig();
128
        if (none_of(config.servers.cbegin(), config.servers.cend(), [](const string &it) {
×
129
                        return it != "";
×
130
                })) {
131
                log::Error("Cannot run in daemon mode with no server URL specified");
×
132
                return error::MakeError(error::ExitWithFailureError, "");
×
133
        }
134

135
        auto err = DoBootstrap(keystore_, force_bootstrap_);
×
136
        if (err != error::NoError) {
×
137
                log::Error("Failed to bootstrap: " + err.String());
×
138
                return error::MakeError(error::ExitWithFailureError, "");
×
139
        }
140

141
        events::EventLoop loop {};
×
142

143
        ipc::Server ipc_server {loop, config};
×
144

145
        err = ipc_server.Listen(
×
146
                {keystore_->KeyName(), keystore_->PassPhrase(), keystore_->SSLEngine()},
147
                config.paths.GetIdentityScript());
×
148
        if (err != error::NoError) {
×
149
                log::Error("Failed to start the listen loop");
×
150
                log::Error(err.String());
×
151
                return error::MakeError(error::ExitWithFailureError, "");
×
152
        }
153

154
        loop.Run();
×
155

156
        return error::NoError;
×
157
}
158

159
BootstrapAction::BootstrapAction(shared_ptr<MenderKeyStore> keystore, bool force_bootstrap) :
4✔
160
        keystore_(keystore),
161
        force_bootstrap_(force_bootstrap) {
8✔
162
}
4✔
163

164
ExpectedActionPtr BootstrapAction::Create(
4✔
165
        const conf::MenderConfig &config, const string &passphrase, bool force_bootstrap) {
166
        auto key_store = KeystoreFromConfig(config, passphrase);
4✔
167

168
        return make_shared<BootstrapAction>(key_store, force_bootstrap);
8✔
169
}
170

171
error::Error BootstrapAction::Execute(context::MenderContext &main_context) {
4✔
172
        auto err = DoBootstrap(keystore_, force_bootstrap_);
4✔
173
        if (err != error::NoError) {
4✔
174
                return err;
×
175
        }
176
        return DoAuthenticate(main_context, keystore_);
8✔
177
}
178

179
} // namespace cli
180
} // namespace auth
181
} // 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