Skip to content

Commit

Permalink
Refactor initialization process
Browse files Browse the repository at this point in the history
The purpose of this change is to allow SotaUptaneClient to operate offline
(before provisioning has completed on-line). At the moment SotaUptaneClient
still requires provisioning to complete (see SotaUptaneClient::initialize()),
but this refactor is a step along the path outlined in
uptane/aktualizr#8

Signed-off-by: Phil Wise <[email protected]>
  • Loading branch information
cajun-rat committed Oct 17, 2021
1 parent 399bbd5 commit 296fa5f
Show file tree
Hide file tree
Showing 14 changed files with 497 additions and 369 deletions.
16 changes: 10 additions & 6 deletions src/libaktualizr/primary/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
set(SOURCES aktualizr.cc
aktualizr_helpers.cc
initializer.cc
provisioner.cc
reportqueue.cc
secondary_provider.cc
sotauptaneclient.cc)

set(HEADERS aktualizr_helpers.h
initializer.h
provisioner.h
reportqueue.h
secondary_config.h
secondary_provider_builder.h
sotauptaneclient.h)

add_library(primary OBJECT ${SOURCES})

add_library(provisioner_test_utils STATIC provisioner_test_utils.cc)
aktualizr_source_file_checks(provisioner_test_utils.cc provisioner_test_utils.h)


add_aktualizr_test(NAME aktualizr
SOURCES aktualizr_test.cc
PROJECT_WORKING_DIRECTORY
Expand Down Expand Up @@ -55,10 +59,10 @@ else (BUILD_OSTREE)
aktualizr_source_file_checks(aktualizr_fullostree_test.cc download_nonostree_test.cc aktualizr_lite_test.cc)
endif (BUILD_OSTREE)

add_aktualizr_test(NAME initializer
SOURCES initializer_test.cc
add_aktualizr_test(NAME provisioner
SOURCES provisioner_test.cc
PROJECT_WORKING_DIRECTORY
LIBRARIES PUBLIC uptane_generator_lib)
LIBRARIES PUBLIC uptane_generator_lib provisioner_test_utils)

add_aktualizr_test(NAME reportqueue
SOURCES reportqueue_test.cc
Expand Down Expand Up @@ -97,7 +101,7 @@ add_aktualizr_test(NAME metadata_expiration
add_aktualizr_test(NAME device_cred_prov
SOURCES device_cred_prov_test.cc
PROJECT_WORKING_DIRECTORY
LIBRARIES uptane_generator_lib)
LIBRARIES uptane_generator_lib provisioner_test_utils)
set_tests_properties(test_device_cred_prov PROPERTIES LABELS "crypto")


Expand Down
51 changes: 26 additions & 25 deletions src/libaktualizr/primary/device_cred_prov_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

#include "httpfake.h"
#include "logging/logging.h"
#include "primary/initializer.h"
#include "primary/provisioner.h"
#include "primary/provisioner_test_utils.h"
#include "primary/sotauptaneclient.h"
#include "storage/invstorage.h"
#include "uptane/uptanerepository.h"
Expand All @@ -28,10 +29,10 @@ TEST(DeviceCredProv, DeviceIdFailure) {

auto storage = INvStorage::newStorage(config.storage);
auto http = std::make_shared<HttpFake>(temp_dir.Path());
KeyManager keys(storage, config.keymanagerConfig());
auto keys = std::make_shared<KeyManager>(storage, config.keymanagerConfig());

// Expect failure when trying to read the certificate to get the device ID.
EXPECT_THROW(Initializer(config.provision, storage, http, keys, {}), std::exception);
ExpectProvisionError(Provisioner(config.provision, storage, http, keys, {}));
}

/**
Expand All @@ -49,10 +50,10 @@ TEST(DeviceCredProv, TlsFailure) {

auto storage = INvStorage::newStorage(config.storage);
auto http = std::make_shared<HttpFake>(temp_dir.Path());
KeyManager keys(storage, config.keymanagerConfig());
auto keys = std::make_shared<KeyManager>(storage, config.keymanagerConfig());

// Expect failure when trying to read the TLS credentials.
EXPECT_THROW(Initializer(config.provision, storage, http, keys, {}), Initializer::Error);
ExpectProvisionError(Provisioner(config.provision, storage, http, keys, {}));
}

/**
Expand All @@ -79,9 +80,9 @@ TEST(DeviceCredProv, Incomplete) {
boost::filesystem::copy_file("tests/test_data/device_cred_prov/ca.pem", temp_dir / "import/ca.pem");
auto storage = INvStorage::newStorage(config.storage);
storage->importData(config.import);
KeyManager keys(storage, config.keymanagerConfig());
auto keys = std::make_shared<KeyManager>(storage, config.keymanagerConfig());

EXPECT_THROW(Initializer(config.provision, storage, http, keys, {}), Initializer::Error);
ExpectProvisionError(Provisioner(config.provision, storage, http, keys, {}));
}

{
Expand All @@ -93,9 +94,9 @@ TEST(DeviceCredProv, Incomplete) {
boost::filesystem::copy_file("tests/test_data/device_cred_prov/client.pem", temp_dir / "import/client.pem");
auto storage = INvStorage::newStorage(config.storage);
storage->importData(config.import);
KeyManager keys(storage, config.keymanagerConfig());
auto keys = std::make_shared<KeyManager>(storage, config.keymanagerConfig());

EXPECT_THROW(Initializer(config.provision, storage, http, keys, {}), Initializer::Error);
ExpectProvisionError(Provisioner(config.provision, storage, http, keys, {}));
}

{
Expand All @@ -107,9 +108,9 @@ TEST(DeviceCredProv, Incomplete) {
boost::filesystem::copy_file("tests/test_data/device_cred_prov/pkey.pem", temp_dir / "import/pkey.pem");
auto storage = INvStorage::newStorage(config.storage);
storage->importData(config.import);
KeyManager keys(storage, config.keymanagerConfig());
auto keys = std::make_shared<KeyManager>(storage, config.keymanagerConfig());

EXPECT_THROW(Initializer(config.provision, storage, http, keys, {}), Initializer::Error);
ExpectProvisionError(Provisioner(config.provision, storage, http, keys, {}));
}

{
Expand All @@ -122,9 +123,9 @@ TEST(DeviceCredProv, Incomplete) {
boost::filesystem::copy_file("tests/test_data/device_cred_prov/client.pem", temp_dir / "import/client.pem");
auto storage = INvStorage::newStorage(config.storage);
storage->importData(config.import);
KeyManager keys(storage, config.keymanagerConfig());
auto keys = std::make_shared<KeyManager>(storage, config.keymanagerConfig());

EXPECT_THROW(Initializer(config.provision, storage, http, keys, {}), Initializer::Error);
ExpectProvisionError(Provisioner(config.provision, storage, http, keys, {}));
}

{
Expand All @@ -137,9 +138,9 @@ TEST(DeviceCredProv, Incomplete) {
boost::filesystem::copy_file("tests/test_data/device_cred_prov/pkey.pem", temp_dir / "import/pkey.pem");
auto storage = INvStorage::newStorage(config.storage);
storage->importData(config.import);
KeyManager keys(storage, config.keymanagerConfig());
auto keys = std::make_shared<KeyManager>(storage, config.keymanagerConfig());

EXPECT_THROW(Initializer(config.provision, storage, http, keys, {}), Initializer::Error);
ExpectProvisionError(Provisioner(config.provision, storage, http, keys, {}));
}

{
Expand All @@ -152,9 +153,9 @@ TEST(DeviceCredProv, Incomplete) {
boost::filesystem::copy_file("tests/test_data/device_cred_prov/pkey.pem", temp_dir / "import/pkey.pem");
auto storage = INvStorage::newStorage(config.storage);
storage->importData(config.import);
KeyManager keys(storage, config.keymanagerConfig());
auto keys = std::make_shared<KeyManager>(storage, config.keymanagerConfig());

EXPECT_THROW(Initializer(config.provision, storage, http, keys, {}), Initializer::Error);
ExpectProvisionError(Provisioner(config.provision, storage, http, keys, {}));
}

// Do one last round with all three files to make sure it actually works as
Expand All @@ -169,9 +170,9 @@ TEST(DeviceCredProv, Incomplete) {
boost::filesystem::copy_file("tests/test_data/device_cred_prov/pkey.pem", temp_dir / "import/pkey.pem");
auto storage = INvStorage::newStorage(config.storage);
storage->importData(config.import);
KeyManager keys(storage, config.keymanagerConfig());
auto keys = std::make_shared<KeyManager>(storage, config.keymanagerConfig());

EXPECT_NO_THROW(Initializer(config.provision, storage, http, keys, {}));
ExpectProvisionOK(Provisioner(config.provision, storage, http, keys, {}));
}

/**
Expand All @@ -195,9 +196,9 @@ TEST(DeviceCredProv, Success) {
auto storage = INvStorage::newStorage(config.storage);
storage->importData(config.import);
auto http = std::make_shared<HttpFake>(temp_dir.Path());
KeyManager keys(storage, config.keymanagerConfig());
auto keys = std::make_shared<KeyManager>(storage, config.keymanagerConfig());

EXPECT_NO_THROW(Initializer(config.provision, storage, http, keys, {}));
ExpectProvisionOK(Provisioner(config.provision, storage, http, keys, {}));
}

/**
Expand Down Expand Up @@ -226,8 +227,8 @@ TEST(DeviceCredProv, ReImportCert) {
/* prepare storage initialized with device_id from config where cert CN and device id are differen*/
auto storage = INvStorage::newStorage(config.storage);
storage->importData(config.import);
KeyManager keys(storage, config.keymanagerConfig());
EXPECT_NO_THROW(Initializer(config.provision, storage, http, keys, {}));
auto keys = std::make_shared<KeyManager>(storage, config.keymanagerConfig());
ExpectProvisionOK(Provisioner(config.provision, storage, http, keys, {}));
std::string device_id;
EXPECT_TRUE(storage->loadDeviceId(&device_id));
EXPECT_EQ(device_id, "AnYsTrInG");
Expand All @@ -237,8 +238,8 @@ TEST(DeviceCredProv, ReImportCert) {
config.import.tls_clientcert_path = utils::BasedPath("newcert.pem");
auto storage = INvStorage::newStorage(config.storage);
EXPECT_NO_THROW(storage->importData(config.import));
KeyManager keys(storage, config.keymanagerConfig());
EXPECT_NO_THROW(Initializer(config.provision, storage, http, keys, {}));
auto keys = std::make_shared<KeyManager>(storage, config.keymanagerConfig());
ExpectProvisionOK(Provisioner(config.provision, storage, http, keys, {}));
std::string device_id;
EXPECT_TRUE(storage->loadDeviceId(&device_id));
EXPECT_EQ(device_id, "AnYsTrInG");
Expand Down
77 changes: 0 additions & 77 deletions src/libaktualizr/primary/initializer.h

This file was deleted.

Loading

0 comments on commit 296fa5f

Please sign in to comment.