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

mendersoftware / mender / 969241802

16 Aug 2023 08:29AM UTC coverage: 79.019% (+0.2%) from 78.825%
969241802

push

gitlab-ci

oleorhagen
chore(crypto): Make the PrivateKey constructor public

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

5314 of 6725 relevant lines covered (79.02%)

200.29 hits per line

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

68.18
/common/conf/conf.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 <common/conf.hpp>
16

17
#include <string>
18
#include <cstdlib>
19
#include <cerrno>
20

21
#include <common/error.hpp>
22
#include <common/expected.hpp>
23
#include <common/conf/paths.hpp>
24
#include <common/log.hpp>
25
#include <common/json.hpp>
26

27
namespace mender {
28
namespace common {
29
namespace conf {
30

31
using namespace std;
32
namespace error = mender::common::error;
33
namespace expected = mender::common::expected;
34
namespace log = mender::common::log;
35
namespace json = mender::common::json;
36

37
const ConfigErrorCategoryClass ConfigErrorCategory;
38

39
const char *ConfigErrorCategoryClass::name() const noexcept {
×
40
        return "ConfigErrorCategory";
×
41
}
42

43
string ConfigErrorCategoryClass::message(int code) const {
8✔
44
        switch (code) {
8✔
45
        case NoError:
×
46
                return "Success";
×
47
        case InvalidOptionsError:
8✔
48
                return "Invalid options given";
8✔
49
        default:
×
50
                return "Unknown";
×
51
        }
52
}
53

54
error::Error MakeError(ConfigErrorCode code, const string &msg) {
13✔
55
        return error::Error(error_condition(code, ConfigErrorCategory), msg);
26✔
56
}
57

58

59
string GetEnv(const string &var_name, const string &default_value) {
616✔
60
        const char *value = getenv(var_name.c_str());
616✔
61
        if (value == nullptr) {
616✔
62
                return string(default_value);
615✔
63
        } else {
64
                return string(value);
1✔
65
        }
66
}
67

68
ExpectedOptionValue CmdlineOptionsIterator::Next() {
293✔
69
        string option = "";
586✔
70
        string value = "";
586✔
71

72
        if (start_ + pos_ >= end_) {
293✔
73
                return ExpectedOptionValue({"", ""});
77✔
74
        }
75

76
        if (past_double_dash_) {
216✔
77
                OptionValue opt_val {"", start_[pos_]};
9✔
78
                pos_++;
3✔
79
                return ExpectedOptionValue(opt_val);
3✔
80
        }
81

82
        if (start_[pos_] == "--") {
213✔
83
                past_double_dash_ = true;
1✔
84
                pos_++;
1✔
85
                return ExpectedOptionValue({"--", ""});
1✔
86
        }
87

88
        if (start_[pos_][0] == '-') {
212✔
89
                auto eq_idx = start_[pos_].find('=');
93✔
90
                if (eq_idx != string::npos) {
93✔
91
                        option = start_[pos_].substr(0, eq_idx);
3✔
92
                        value = start_[pos_].substr(eq_idx + 1, start_[pos_].size() - eq_idx - 1);
3✔
93
                        pos_++;
3✔
94
                } else {
95
                        option = start_[pos_];
90✔
96
                        pos_++;
90✔
97
                }
98

99
                if (opts_with_value_.count(option) != 0) {
93✔
100
                        // option with value
101
                        if ((value == "") && ((start_ + pos_ >= end_) || (start_[pos_][0] == '-'))) {
84✔
102
                                // the next item is not a value
103
                                error::Error err = MakeError(
104
                                        ConfigErrorCode::InvalidOptionsError, "Option " + option + " missing value");
4✔
105
                                return ExpectedOptionValue(expected::unexpected(err));
4✔
106
                        } else if (value == "") {
82✔
107
                                // only assign the next item as value if there was no value
108
                                // specified as '--opt=value' (parsed above)
109
                                value = start_[pos_];
80✔
110
                                pos_++;
80✔
111
                        }
112
                } else if (opts_wo_value_.count(option) == 0) {
9✔
113
                        // unknown option
114
                        error::Error err = MakeError(
115
                                ConfigErrorCode::InvalidOptionsError, "Unrecognized option '" + option + "'");
8✔
116
                        return ExpectedOptionValue(expected::unexpected(err));
8✔
117
                } else if (value != "") {
5✔
118
                        // option without a value, yet, there was a value specified as '--opt=value' (parsed
119
                        // above)
120
                        error::Error err = MakeError(
121
                                ConfigErrorCode::InvalidOptionsError,
122
                                "Option " + option + " doesn't expect a value");
2✔
123
                        return ExpectedOptionValue(expected::unexpected(err));
2✔
124
                }
125
        } else {
126
                switch (mode_) {
119✔
127
                case ArgumentsMode::AcceptBareArguments:
37✔
128
                        value = start_[pos_];
37✔
129
                        pos_++;
37✔
130
                        break;
37✔
131
                case ArgumentsMode::RejectBareArguments:
3✔
132
                        return expected::unexpected(MakeError(
3✔
133
                                ConfigErrorCode::InvalidOptionsError,
134
                                "Unexpected argument '" + start_[pos_] + "'"));
6✔
135
                case ArgumentsMode::StopAtBareArguments:
79✔
136
                        return ExpectedOptionValue({"", ""});
79✔
137
                }
138
        }
139

140
        return ExpectedOptionValue({std::move(option), std::move(value)});
123✔
141
}
142

143
expected::ExpectedSize MenderConfig::ProcessCmdlineArgs(
78✔
144
        vector<string>::const_iterator start, vector<string>::const_iterator end) {
145
        string config_path = paths::DefaultConfFile;
156✔
146
        bool explicit_config_path = false;
78✔
147
        string fallback_config_path = paths::DefaultFallbackConfFile;
156✔
148
        bool explicit_fallback_config_path = false;
78✔
149
        string log_file = "";
156✔
150
        string log_level = log::ToStringLogLevel(log::kDefaultLogLevel);
156✔
151

152
        CmdlineOptionsIterator opts_iter(
153
                start,
154
                end,
155
                {"--config",
156
                 "-c",
157
                 "--fallback-config",
158
                 "-b",
159
                 "--data",
160
                 "-d",
161
                 "--log-file",
162
                 "-L",
163
                 "--log-level",
164
                 "-l"},
165
                {});
1,092✔
166
        opts_iter.SetArgumentsMode(ArgumentsMode::StopAtBareArguments);
78✔
167
        auto ex_opt_val = opts_iter.Next();
156✔
168
        while (ex_opt_val && ((ex_opt_val.value().option != "") || (ex_opt_val.value().value != ""))) {
153✔
169
                auto opt_val = ex_opt_val.value();
75✔
170
                if ((opt_val.option == "--config") || (opt_val.option == "-c")) {
75✔
171
                        config_path = opt_val.value;
×
172
                        explicit_config_path = true;
×
173
                } else if ((opt_val.option == "--fallback-config") || (opt_val.option == "-b")) {
75✔
174
                        fallback_config_path = opt_val.value;
×
175
                        explicit_fallback_config_path = true;
×
176
                } else if ((opt_val.option == "--data") || (opt_val.option == "-d")) {
75✔
177
                        data_store_dir = opt_val.value;
75✔
178
                } else if ((opt_val.option == "--log-file") || (opt_val.option == "-L")) {
×
179
                        log_file = opt_val.value;
×
180
                } else if ((opt_val.option == "--log-level") || (opt_val.option == "-l")) {
×
181
                        log_level = opt_val.value;
×
182
                }
183
                ex_opt_val = opts_iter.Next();
75✔
184
        }
185
        if (!ex_opt_val) {
78✔
186
                return expected::unexpected(ex_opt_val.error());
×
187
        }
188

189
        if (log_file != "") {
78✔
190
                auto err = log::SetupFileLogging(log_file, true);
×
191
                if (error::NoError != err) {
×
192
                        return expected::unexpected(err);
×
193
                }
194
        }
195

196
        auto ex_log_level = log::StringToLogLevel(log_level);
156✔
197
        if (!ex_log_level) {
78✔
198
                return expected::unexpected(ex_log_level.error());
×
199
        }
200
        SetLevel(ex_log_level.value());
78✔
201

202
        auto err = LoadConfigFile_(fallback_config_path, explicit_fallback_config_path);
156✔
203
        if (error::NoError != err) {
78✔
204
                this->Reset();
×
205
                return expected::unexpected(err);
×
206
        }
207

208
        err = LoadConfigFile_(config_path, explicit_config_path);
78✔
209
        if (error::NoError != err) {
78✔
210
                this->Reset();
×
211
                return expected::unexpected(err);
×
212
        }
213

214
        return opts_iter.GetPos();
156✔
215
}
216

217
error::Error MenderConfig::LoadConfigFile_(const string &path, bool required) {
156✔
218
        auto ret = this->LoadFile(path);
312✔
219
        if (!ret) {
156✔
220
                if (required) {
156✔
221
                        // any failure when a file is required (e.g. path was given explicitly) means an error
222
                        log::Error("Failed to load config from '" + path + "': " + ret.error().message);
×
223
                        return ret.error();
×
224
                } else if (ret.error().IsErrno(ENOENT)) {
156✔
225
                        // File doesn't exist, OK for non-required
226
                        log::Debug("Failed to load config from '" + path + "': " + ret.error().message);
156✔
227
                        return error::NoError;
156✔
228
                } else {
229
                        // other errors (parsing errors,...) for default paths should produce warnings
230
                        log::Warning("Failed to load config from '" + path + "': " + ret.error().message);
×
231
                        return error::NoError;
×
232
                }
233
        }
234
        // else
235
        auto valid = this->ValidateConfig();
×
236
        if (!valid) {
×
237
                // validation error is always an error
238
                log::Error("Failed to validate config from '" + path + "': " + valid.error().message);
×
239
                return valid.error();
×
240
        }
241

242
        return error::NoError;
×
243
}
244

245
error::Error MenderConfig::LoadDefaults() {
×
246
        auto err = LoadConfigFile_(paths::DefaultFallbackConfFile, false);
×
247
        if (error::NoError != err) {
×
248
                this->Reset();
×
249
                return err;
×
250
        }
251

252
        err = LoadConfigFile_(paths::DefaultConfFile, false);
×
253
        if (error::NoError != err) {
×
254
                this->Reset();
×
255
                return err;
×
256
        }
257

258
        return error::NoError;
×
259
}
260

261
} // namespace conf
262
} // namespace common
263
} // 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