• 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

62.07
/mender-auth/cli/cli.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/cli.hpp>
16

17
#include <string>
18

19
#include <common/conf.hpp>
20
#include <common/expected.hpp>
21
#include <common/io.hpp>
22
#include <common/setup.hpp>
23

24
#include <mender-auth/cli/actions.hpp>
25
#include <mender-auth/context.hpp>
26
#include <mender-auth/ipc/server.hpp>
27

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

32
using namespace std;
33

34
namespace conf = mender::common::conf;
35
namespace expected = mender::common::expected;
36
namespace io = mender::common::io;
37
namespace setup = mender::common::setup;
38

39
namespace context = mender::auth::context;
40
namespace ipc = mender::auth::ipc;
41

42
static expected::ExpectedString GetPassphraseFromFile(const string &filepath) {
×
43
        string passphrase = "";
×
44
        if (filepath == "") {
×
45
                return passphrase;
×
46
        }
47

48
        auto ex_ifs = io::OpenIfstream(filepath == "-" ? io::paths::Stdin : filepath);
×
49
        if (!ex_ifs) {
×
50
                return expected::unexpected(ex_ifs.error());
×
51
        }
52
        auto &ifs = ex_ifs.value();
×
53

54
        errno = 0;
×
55
        getline(ifs, passphrase);
×
56
        if (ifs.bad()) {
×
57
                int io_errno = errno;
×
58
                error::Error err {
59
                        generic_category().default_error_condition(io_errno),
×
60
                        "Failed to read passphrase from '" + filepath + "'"};
×
61
                return expected::unexpected(err);
×
62
        }
63

64
        return passphrase;
×
65
}
66

67
static ExpectedActionPtr ParseUpdateArguments(
6✔
68
        const conf::MenderConfig &config,
69
        vector<string>::const_iterator start,
70
        vector<string>::const_iterator end) {
71
        if (start == end) {
6✔
72
                return expected::unexpected(conf::MakeError(conf::InvalidOptionsError, "Need an action"));
3✔
73
        }
74

75
        string passphrase = "";
5✔
76
        bool forcebootstrap = false;
77
        if (start[0] == "bootstrap" || start[0] == "daemon") {
5✔
78
                conf::CmdlineOptionsIterator opts_iter(
79
                        start + 1, end, {"--passphrase-file"}, {"--forcebootstrap", "-F"});
40✔
80
                auto ex_opt_val = opts_iter.Next();
4✔
81

82
                while (ex_opt_val
83
                           && ((ex_opt_val.value().option != "") || (ex_opt_val.value().value != ""))) {
5✔
84
                        auto opt_val = ex_opt_val.value();
1✔
85
                        if ((opt_val.option == "--passphrase-file")) {
1✔
86
                                auto ex_passphrase = GetPassphraseFromFile(opt_val.value);
×
87
                                if (!ex_passphrase) {
×
88
                                        return expected::unexpected(ex_passphrase.error());
×
89
                                }
90
                                passphrase = ex_passphrase.value();
×
91
                        } else if ((opt_val.option == "--forcebootstrap" || opt_val.option == "-F")) {
1✔
92
                                forcebootstrap = true;
93
                        }
94
                        ex_opt_val = opts_iter.Next();
2✔
95
                }
96
                if (!ex_opt_val) {
4✔
97
                        return expected::unexpected(ex_opt_val.error());
×
98
                }
99
        }
100

101
        if (start[0] == "bootstrap") {
5✔
102
                return BootstrapAction::Create(config, passphrase, forcebootstrap);
4✔
103
        } else if (start[0] == "daemon") {
1✔
104
                return DaemonAction::Create(config, passphrase, forcebootstrap);
×
105
        } else {
106
                return expected::unexpected(
1✔
107
                        conf::MakeError(conf::InvalidOptionsError, "No such action: " + start[0]));
3✔
108
        }
109
}
110

111
error::Error DoMain(
8✔
112
        const vector<string> &args, function<void(context::MenderContext &ctx)> test_hook) {
113
        setup::GlobalSetup();
8✔
114

115
        conf::MenderConfig config;
16✔
116
        auto arg_pos = config.ProcessCmdlineArgs(args.begin(), args.end());
8✔
117
        if (!arg_pos) {
8✔
118
                return arg_pos.error();
2✔
119
        }
120

121
        auto action = ParseUpdateArguments(config, args.begin() + arg_pos.value(), args.end());
6✔
122
        if (!action) {
6✔
123
                return action.error();
2✔
124
        }
125

126
        context::MenderContext context(config);
127

128
        test_hook(context);
4✔
129

130
        return action.value()->Execute(context);
4✔
131
}
132

133
int Main(const vector<string> &args, function<void(context::MenderContext &ctx)> test_hook) {
8✔
134
        auto err = mender::auth::cli::DoMain(args, test_hook);
8✔
135

136
        if (err != error::NoError) {
8✔
137
                if (err.code == error::MakeError(error::ExitWithSuccessError, "").code) {
4✔
138
                        return 0;
139
                } else if (err.code != error::MakeError(error::ExitWithFailureError, "").code) {
3✔
140
                        cerr << "Failed to process command line options: " + err.String() << endl;
6✔
141
                }
142
                return 1;
3✔
143
        }
144

145
        return 0;
146
}
147

148
} // namespace cli
149
} // namespace auth
150
} // 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