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

mendersoftware / mender / 1022753986

02 Oct 2023 10:37AM UTC coverage: 78.168% (-2.0%) from 80.127%
1022753986

push

gitlab-ci

oleorhagen
feat: Run the authentication loop once upon bootstrap

Ticket: MEN-6658
Changelog: None

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

32 of 32 new or added lines in 1 file covered. (100.0%)

6996 of 8950 relevant lines covered (78.17%)

10353.4 hits per line

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

62.5
/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) {
14✔
56
        return error::Error(error_condition(code, KeyStoreErrorCategory), msg);
28✔
57
}
58

59
error::Error MenderKeyStore::Load() {
6✔
60
        auto exp_key = crypto::PrivateKey::LoadFromPEM(key_name_, passphrase_);
12✔
61
        if (!exp_key) {
6✔
62
                return MakeError(
63
                        NoKeysError,
64
                        "Error loading private key from " + key_name_ + ": " + exp_key.error().message);
6✔
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() {
6✔
73
        if (!key_) {
6✔
74
                return MakeError(NoKeysError, "Need to load or generate a key before save");
1✔
75
        }
76

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

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

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

91
        return error::NoError;
6✔
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