• 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

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

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

20
#include <common/log.hpp>
21
#include <common/crypto.hpp>
22

23

24
namespace mender {
25
namespace auth {
26
namespace cli {
27

28
using namespace std;
29

30
namespace log = mender::common::log;
31

32
namespace crypto = mender::common::crypto;
33

34
const KeyStoreErrorCategoryClass KeyStoreErrorCategory;
35

36
const char *KeyStoreErrorCategoryClass::name() const noexcept {
×
37
        return "KeyStoreErrorCategory";
×
38
}
39

40
string KeyStoreErrorCategoryClass::message(int code) const {
×
41
        switch (code) {
×
42
        case NoError:
43
                return "Success";
×
44
        case NoKeysError:
45
                return "No key in memory";
×
46
        case StaticKeyError:
47
                return "Cannot replace static key";
×
48
        }
49
        // Don't use "default" case. This should generate a warning if we ever add any enums. But
50
        // still assert here for safety.
51
        assert(false);
52
        return "Unknown";
×
53
}
54

55
error::Error MakeError(KeyStoreErrorCode code, const string &msg) {
7✔
56
        return error::Error(error_condition(code, KeyStoreErrorCategory), msg);
11✔
57
}
58

59
error::Error MenderKeyStore::Load() {
5✔
60
        auto exp_key = crypto::PrivateKey::LoadFromPEM(key_name_, passphrase_);
5✔
61
        if (!exp_key) {
5✔
62
                return MakeError(
63
                        NoKeysError,
64
                        "Error loading private key from " + key_name_ + ": " + exp_key.error().message);
4✔
65
        }
66
        log::Debug("Successfully loaded private key from " + key_name_);
3✔
67
        key_ = move(exp_key.value());
3✔
68

69
        return error::NoError;
3✔
70
}
71

72
error::Error MenderKeyStore::Save() {
5✔
73
        if (!key_) {
5✔
74
                return MakeError(NoKeysError, "Need to load or generate a key before save");
2✔
75
        }
76

77
        return key_.get()->SaveToPEM(key_name_);
4✔
78
}
79

80
error::Error MenderKeyStore::Generate() {
6✔
81
        if (static_key_ == StaticKey::Yes) {
6✔
82
                return MakeError(StaticKeyError, "An static key cannot be re-generated");
2✔
83
        }
84

85
        auto exp_key = crypto::PrivateKey::Generate(MENDER_DEFAULT_KEY_LENGTH);
5✔
86
        if (!exp_key) {
5✔
87
                return exp_key.error();
×
88
        }
89
        key_ = std::move(exp_key.value());
5✔
90

91
        return error::NoError;
5✔
92
}
93

94
} // namespace cli
95
} // namespace auth
96
} // 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