Skip to content

Commit 36cb93f

Browse files
committed
[all] Create UUIDs via crypto provider interface
This patch removes CreateUUID free function and replaces it with calls to the crypto provider interface. Signed-off-by: Mykhailo Lohvynenko <[email protected]>
1 parent 3c7b098 commit 36cb93f

File tree

10 files changed

+103
-75
lines changed

10 files changed

+103
-75
lines changed

include/aos/common/tools/uuid.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ constexpr auto cUUIDLen = AOS_CONFIG_TOOLS_UUID_LEN;
2828
*/
2929
using UUID = StaticArray<uint8_t, cUUIDSize>;
3030

31-
/**
32-
* Creates unique UUID.
33-
*
34-
* @return UUID.
35-
*/
36-
UUID CreateUUID();
37-
3831
/**
3932
* Converts UUID to string.
4033
*

include/aos/iam/permhandler.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ class PermHandlerItf {
9494
*/
9595
class PermHandler : public PermHandlerItf {
9696
public:
97+
/**
98+
* Initializes permission handler.
99+
*
100+
* @param uuidProvider UUID provider.
101+
* @returns Error.
102+
*/
103+
Error Init(crypto::UUIDItf& uuidProvider);
104+
97105
/**
98106
* Adds new service instance and its permissions into cache.
99107
*
@@ -129,11 +137,12 @@ class PermHandler : public PermHandlerItf {
129137
const Array<FunctionServicePermissions>& instancePermissions);
130138
InstancePermissions* FindBySecret(const String& secret);
131139
InstancePermissions* FindByInstanceIdent(const InstanceIdent& instanceIdent);
132-
StaticString<cSecretLen> GenerateSecret();
140+
RetWithError<StaticString<cSecretLen>> GenerateSecret();
133141
RetWithError<StaticString<cSecretLen>> GetSecretForInstance(const InstanceIdent& instanceIdent);
134142

135143
Mutex mMutex;
136144
StaticArray<InstancePermissions, cMaxNumInstances> mInstancesPerms;
145+
crypto::UUIDItf* mUUIDProvider = {};
137146
};
138147

139148
/** @}*/

include/aos/sm/launcher.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,14 +260,16 @@ class Launcher : public LauncherItf,
260260
* @param statusReceiver status receiver instance.
261261
* @param connectionPublisher connection publisher instance.
262262
* @param storage storage instance.
263+
* @param uuidProvider UUID provider instance.
263264
* @return Error.
264265
*/
265266
Error Init(const Config& config, iam::nodeinfoprovider::NodeInfoProviderItf& nodeInfoProvider,
266267
servicemanager::ServiceManagerItf& serviceManager, layermanager::LayerManagerItf& layerManager,
267268
resourcemanager::ResourceManagerItf& resourceManager, networkmanager::NetworkManagerItf& networkManager,
268269
iam::permhandler::PermHandlerItf& permHandler, runner::RunnerItf& runner, RuntimeItf& runtime,
269270
monitoring::ResourceMonitorItf& resourceMonitor, oci::OCISpecItf& ociManager,
270-
InstanceStatusReceiverItf& statusReceiver, ConnectionPublisherItf& connectionPublisher, StorageItf& storage);
271+
InstanceStatusReceiverItf& statusReceiver, ConnectionPublisherItf& connectionPublisher, StorageItf& storage,
272+
crypto::UUIDItf& uuidProvider);
271273

272274
/**
273275
* Starts launcher.
@@ -412,6 +414,7 @@ class Launcher : public LauncherItf,
412414
servicemanager::ServiceManagerItf* mServiceManager {};
413415
StorageItf* mStorage {};
414416
RuntimeItf* mRuntime {};
417+
crypto::UUIDItf* mUUIDProvider {};
415418

416419
mutable StaticAllocator<cAllocatorSize> mAllocator;
417420

src/common/tools/uuid.cpp

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,13 @@
1212

1313
namespace aos::uuid {
1414

15-
// UUID template assumed to have even number of digits between separators.
16-
static const String cTemplate = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
17-
static const String cEmptyUUID = "00000000-0000-0000-0000-000000000000";
18-
19-
UUID CreateUUID()
20-
{
21-
UUID result;
22-
23-
while (result.Size() < result.MaxSize()) {
24-
unsigned value = rand();
25-
auto chunk = Array<uint8_t>(reinterpret_cast<uint8_t*>(&value), sizeof(value));
15+
namespace {
2616

27-
auto chunkSize = Min(result.MaxSize() - result.Size(), chunk.Size());
28-
29-
result.Insert(result.end(), chunk.begin(), chunk.begin() + chunkSize);
30-
}
17+
// UUID template assumed to have even number of digits between separators.
18+
const String cTemplate = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
19+
const String cEmptyUUID = "00000000-0000-0000-0000-000000000000";
3120

32-
return result;
33-
}
21+
} // namespace
3422

3523
StaticString<cUUIDLen> UUIDToString(const UUID& src)
3624
{

src/iam/certmodules/pkcs11/pkcs11.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,10 @@ RetWithError<SharedPtr<crypto::PrivateKeyItf>> PKCS11Module::CreateKey(const Str
198198
PKCS11Module::PendingKey pendingKey;
199199
Error err = ErrorEnum::eNone;
200200

201-
pendingKey.mUUID = uuid::CreateUUID();
201+
Tie(pendingKey.mUUID, err) = mCryptoProvider->CreateUUIDv4();
202+
if (!err.IsNone()) {
203+
return {nullptr, AOS_ERROR_WRAP(err)};
204+
}
202205

203206
SharedPtr<pkcs11::SessionContext> session;
204207

@@ -787,7 +790,12 @@ Error PKCS11Module::CreateCertificateChain(const SharedPtr<pkcs11::SessionContex
787790
continue;
788791
}
789792

790-
auto uuid = uuid::CreateUUID();
793+
uuid::UUID uuid;
794+
795+
Tie(uuid, err) = mCryptoProvider->CreateUUIDv4();
796+
if (!err.IsNone()) {
797+
return AOS_ERROR_WRAP(err);
798+
}
791799

792800
LOG_DBG() << "Import root certificate with id: " << aos::uuid::UUIDToString(uuid);
793801

src/iam/permhandler/permhandler.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ namespace aos::iam::permhandler {
1515
* Public
1616
**********************************************************************************************************************/
1717

18+
Error PermHandler::Init(crypto::UUIDItf& uuidProvider)
19+
{
20+
LOG_DBG() << "Init permission handler";
21+
22+
mUUIDProvider = &uuidProvider;
23+
24+
return ErrorEnum::eNone;
25+
}
26+
1827
RetWithError<StaticString<cSecretLen>> PermHandler::RegisterInstance(
1928
const InstanceIdent& instanceIdent, const Array<FunctionServicePermissions>& instancePermissions)
2029
{
@@ -30,7 +39,10 @@ RetWithError<StaticString<cSecretLen>> PermHandler::RegisterInstance(
3039
return {secret};
3140
}
3241

33-
secret = GenerateSecret();
42+
Tie(secret, err) = GenerateSecret();
43+
if (!err.IsNone()) {
44+
return {{}, AOS_ERROR_WRAP(err)};
45+
}
3446

3547
err = AddSecret(secret, instanceIdent, instancePermissions);
3648
if (!err.IsNone()) {
@@ -108,16 +120,23 @@ InstancePermissions* PermHandler::FindByInstanceIdent(const InstanceIdent& insta
108120
return mInstancesPerms.FindIf([&instanceIdent](const auto& elem) { return instanceIdent == elem.mInstanceIdent; });
109121
}
110122

111-
StaticString<cSecretLen> PermHandler::GenerateSecret()
123+
RetWithError<StaticString<cSecretLen>> PermHandler::GenerateSecret()
112124
{
113-
StaticString<cSecretLen> newSecret;
125+
StaticString<cSecretLen> secret;
126+
uuid::UUID uuid;
127+
Error err;
114128

115129
do {
116-
newSecret = uuid::UUIDToString(uuid::CreateUUID());
130+
Tie(uuid, err) = mUUIDProvider->CreateUUIDv4();
131+
if (!err.IsNone()) {
132+
return {secret, err};
133+
}
134+
135+
secret.Assign(uuid::UUIDToString(uuid));
117136

118-
} while (FindBySecret(newSecret) != mInstancesPerms.end());
137+
} while (FindBySecret(secret) != mInstancesPerms.end());
119138

120-
return newSecret;
139+
return {secret};
121140
}
122141

123142
RetWithError<StaticString<cSecretLen>> PermHandler::GetSecretForInstance(const InstanceIdent& instanceIdent)

src/sm/launcher/launcher.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ Error Launcher::Init(const Config& config, iam::nodeinfoprovider::NodeInfoProvid
3232
resourcemanager::ResourceManagerItf& resourceManager, networkmanager::NetworkManagerItf& networkManager,
3333
iam::permhandler::PermHandlerItf& permHandler, runner::RunnerItf& runner, RuntimeItf& runtime,
3434
monitoring::ResourceMonitorItf& resourceMonitor, oci::OCISpecItf& ociManager,
35-
InstanceStatusReceiverItf& statusReceiver, ConnectionPublisherItf& connectionPublisher, StorageItf& storage)
35+
InstanceStatusReceiverItf& statusReceiver, ConnectionPublisherItf& connectionPublisher, StorageItf& storage,
36+
crypto::UUIDItf& uuidProvider)
3637
{
3738
LOG_DBG() << "Init launcher";
3839

@@ -49,6 +50,7 @@ Error Launcher::Init(const Config& config, iam::nodeinfoprovider::NodeInfoProvid
4950
mServiceManager = &serviceManager;
5051
mStatusReceiver = &statusReceiver;
5152
mStorage = &storage;
53+
mUUIDProvider = &uuidProvider;
5254

5355
Error err;
5456

@@ -589,14 +591,21 @@ Error Launcher::GetDesiredInstancesData(
589591
return instance.mInstanceInfo.mInstanceIdent == instanceInfo.mInstanceIdent;
590592
});
591593
if (currentInstance == currentInstances->end()) {
592-
const auto instanceID = uuid::UUIDToString(uuid::CreateUUID());
594+
auto [uuid, err] = mUUIDProvider->CreateUUIDv4();
595+
if (!err.IsNone()) {
596+
return AOS_ERROR_WRAP(err);
597+
}
598+
599+
const auto instanceID = uuid::UUIDToString(uuid);
593600

594-
if (auto err = desiredInstancesData.EmplaceBack(instanceInfo, instanceID); !err.IsNone()) {
601+
err = desiredInstancesData.EmplaceBack(instanceInfo, instanceID);
602+
if (!err.IsNone()) {
595603
return AOS_ERROR_WRAP(err);
596604
}
597605

598-
if (auto err = mStorage->AddInstance(desiredInstancesData.Back()); !err.IsNone()) {
599-
LOG_ERR() << "Can't add instance: instanceID=" << instanceID << ", err=" << err;
606+
err = mStorage->AddInstance(desiredInstancesData.Back());
607+
if (!err.IsNone()) {
608+
LOG_ERR() << "Can't add instance" << Log::Field("instanceID", instanceID) << Log::Field(err);
600609
}
601610

602611
continue;

tests/common/src/tools/uuid_test.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,6 @@
1414

1515
namespace aos::uuid {
1616

17-
TEST(UUIDTest, CreateUUID)
18-
{
19-
static constexpr auto cTestUUIDsCount = 1000;
20-
21-
std::vector<UUID> uuids;
22-
23-
for (int i = 0; i < cTestUUIDsCount; i++) {
24-
auto tmp = CreateUUID();
25-
26-
ASSERT_EQ(tmp.Size(), tmp.MaxSize());
27-
28-
ASSERT_EQ(std::find(uuids.begin(), uuids.end(), tmp), uuids.end());
29-
30-
uuids.push_back(tmp);
31-
}
32-
}
33-
3417
TEST(UUIDTest, UUIDToString)
3518
{
3619
uint8_t uuidBlob[uuid::cUUIDSize]

tests/iam/permhandler/permhandler_test.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,33 @@
99

1010
#include <gtest/gtest.h>
1111

12+
#include "aos/common/crypto/cryptoprovider.hpp"
1213
#include "aos/common/tools/buffer.hpp"
1314
#include "aos/iam/permhandler.hpp"
1415
#include "aos/test/log.hpp"
16+
1517
#include "mocks/identhandlermock.hpp"
1618

17-
using namespace aos;
18-
using namespace aos::iam::permhandler;
1919
using namespace testing;
2020

21+
namespace aos::iam::permhandler {
22+
2123
/***********************************************************************************************************************
2224
* Suite
2325
**********************************************************************************************************************/
2426

2527
class PermHandlerTest : public Test {
2628
protected:
27-
void SetUp() override { test::InitLog(); }
29+
void SetUp() override
30+
{
31+
test::InitLog();
2832

29-
PermHandler mPermHandler;
33+
ASSERT_TRUE(mCryptoProvider.Init().IsNone()) << "Failed to initialize crypto provider";
34+
ASSERT_TRUE(mPermHandler.Init(mCryptoProvider).IsNone()) << "Failed to initialize PermHandler";
35+
}
36+
37+
crypto::DefaultCryptoProvider mCryptoProvider;
38+
PermHandler mPermHandler;
3039
};
3140

3241
/***********************************************************************************************************************
@@ -204,3 +213,5 @@ TEST_F(PermHandlerTest, TestInstancePermissions)
204213
err = mPermHandler.UnregisterInstance(instanceIdent1);
205214
ASSERT_TRUE(err.Is(ErrorEnum::eNotFound)) << err.Message();
206215
}
216+
217+
} // namespace aos::iam::permhandler

tests/sm/launcher/launcher_test.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <gtest/gtest.h>
99

10+
#include "aos/common/crypto/cryptoprovider.hpp"
1011
#include "aos/sm/launcher.hpp"
1112

1213
#include "aos/test/log.hpp"
@@ -97,9 +98,12 @@ class LauncherTest : public Test {
9798
mOCIManager = std::make_unique<OCISpecStub>();
9899
mStatusReceiver = std::make_unique<StatusReceiverStub>();
99100
mStorage = std::make_unique<StorageStub>();
101+
mCryptoProvider = std::make_unique<crypto::DefaultCryptoProvider>();
100102

101103
mLauncher = std::make_unique<Launcher>();
102104

105+
ASSERT_TRUE(mCryptoProvider->Init().IsNone()) << "crypto provider initialization failed";
106+
103107
EXPECT_CALL(mNetworkManager, GetNetnsPath).WillRepeatedly(Invoke([](const String& instanceID) {
104108
return RetWithError<StaticString<cFilePathLen>>(fs::JoinPath("/var/run/netns", instanceID));
105109
}));
@@ -110,7 +114,7 @@ class LauncherTest : public Test {
110114
ASSERT_TRUE(mLauncher
111115
->Init(Config {}, mNodeInfoProvider, *mServiceManager, *mLayerManager, mResourceManager,
112116
mNetworkManager, mPermHandler, mRunner, mRuntime, mResourceMonitor, *mOCIManager,
113-
*mStatusReceiver, mConnectionPublisher, *mStorage)
117+
*mStatusReceiver, mConnectionPublisher, *mStorage, *mCryptoProvider)
114118
.IsNone());
115119

116120
ASSERT_TRUE(mLauncher->Start().IsNone());
@@ -155,20 +159,21 @@ class LauncherTest : public Test {
155159
return ErrorEnum::eNone;
156160
}
157161

158-
std::unique_ptr<Launcher> mLauncher;
159-
NiceMock<ConnectionPublisherMock> mConnectionPublisher;
160-
std::unique_ptr<LayerManagerStub> mLayerManager;
161-
NiceMock<NetworkManagerMock> mNetworkManager;
162-
NiceMock<NodeInfoProviderMock> mNodeInfoProvider;
163-
std::unique_ptr<OCISpecStub> mOCIManager;
164-
NiceMock<PermHandlerMock> mPermHandler;
165-
NiceMock<ResourceManagerMock> mResourceManager;
166-
NiceMock<ResourceMonitorMock> mResourceMonitor;
167-
NiceMock<RunnerMock> mRunner;
168-
NiceMock<RuntimeMock> mRuntime;
169-
std::unique_ptr<ServiceManagerStub> mServiceManager;
170-
std::unique_ptr<StatusReceiverStub> mStatusReceiver;
171-
std::unique_ptr<StorageStub> mStorage;
162+
std::unique_ptr<Launcher> mLauncher;
163+
NiceMock<ConnectionPublisherMock> mConnectionPublisher;
164+
std::unique_ptr<LayerManagerStub> mLayerManager;
165+
NiceMock<NetworkManagerMock> mNetworkManager;
166+
NiceMock<NodeInfoProviderMock> mNodeInfoProvider;
167+
std::unique_ptr<OCISpecStub> mOCIManager;
168+
NiceMock<PermHandlerMock> mPermHandler;
169+
NiceMock<ResourceManagerMock> mResourceManager;
170+
NiceMock<ResourceMonitorMock> mResourceMonitor;
171+
NiceMock<RunnerMock> mRunner;
172+
NiceMock<RuntimeMock> mRuntime;
173+
std::unique_ptr<ServiceManagerStub> mServiceManager;
174+
std::unique_ptr<StatusReceiverStub> mStatusReceiver;
175+
std::unique_ptr<StorageStub> mStorage;
176+
std::unique_ptr<crypto::DefaultCryptoProvider> mCryptoProvider;
172177
};
173178

174179
} // namespace

0 commit comments

Comments
 (0)