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

mendersoftware / mender / 1022567176

02 Oct 2023 07:50AM UTC coverage: 80.127% (+2.5%) from 77.645%
1022567176

push

gitlab-ci

kacf
chore: Centralize selection of `std::filesystem` library.

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

6447 of 8046 relevant lines covered (80.13%)

9912.21 hits per line

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

50.0
/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 <mender-auth/context.hpp>
21
#include <mender-auth/cli/keystore.hpp>
22

23
#include <common/conf.hpp>
24
#include <common/events.hpp>
25
#include <common/log.hpp>
26

27
#include <mender-auth/ipc/server.hpp>
28

29
namespace mender {
30
namespace auth {
31
namespace cli {
32

33
using namespace std;
34

35
namespace events = mender::common::events;
36
namespace log = mender::common::log;
37

38
namespace ipc = mender::auth::ipc;
39

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

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

64
        return make_shared<MenderKeyStore>(pem_file, ssl_engine, static_key, passphrase);
6✔
65
}
66

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

86
DaemonAction::DaemonAction(shared_ptr<MenderKeyStore> keystore, const bool force_bootstrap) :
×
87
        keystore_(keystore),
88
        force_bootstrap_(force_bootstrap) {
×
89
}
×
90

91
ExpectedActionPtr DaemonAction::Create(
×
92
        const conf::MenderConfig &config, const string &passphrase, const bool force_bootstrap) {
93
        auto key_store = KeystoreFromConfig(config, passphrase);
×
94

95
        return make_shared<DaemonAction>(key_store, force_bootstrap);
×
96
}
97

98
error::Error DaemonAction::Execute(context::MenderContext &main_context) {
×
99
        auto err = DoBootstrap(keystore_, force_bootstrap_);
×
100
        if (err != error::NoError) {
×
101
                log::Error("Failed to bootstrap: " + err.String());
×
102
                return error::MakeError(error::ExitWithFailureError, "");
×
103
        }
104

105
        events::EventLoop loop {};
×
106

107
        ipc::Server ipc_server {loop, main_context.GetConfig()};
×
108

109
        err = ipc_server.Listen();
×
110
        if (err != error::NoError) {
×
111
                log::Error("Failed to start the listen loop");
×
112
                log::Error(err.String());
×
113
                return error::MakeError(error::ExitWithFailureError, "");
×
114
        }
115

116
        loop.Run();
×
117

118
        return error::NoError;
×
119
}
120

121
BootstrapAction::BootstrapAction(shared_ptr<MenderKeyStore> keystore, bool force_bootstrap) :
3✔
122
        keystore_(keystore),
123
        force_bootstrap_(force_bootstrap) {
6✔
124
}
3✔
125

126
ExpectedActionPtr BootstrapAction::Create(
3✔
127
        const conf::MenderConfig &config, const string &passphrase, bool force_bootstrap) {
128
        auto key_store = KeystoreFromConfig(config, passphrase);
3✔
129

130
        return make_shared<BootstrapAction>(key_store, force_bootstrap);
6✔
131
}
132

133
error::Error BootstrapAction::Execute(context::MenderContext &main_context) {
3✔
134
        return DoBootstrap(keystore_, force_bootstrap_);
6✔
135
}
136

137
} // namespace cli
138
} // namespace auth
139
} // 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