1 #include "libaktualizr/secondary_provider.h"
2 #include "logging/logging.h"
3 #include "storage/invstorage.h"
4 #include "uptane/tuf.h"
6 bool SecondaryProvider::getMetadata(Uptane::MetaBundle* meta_bundle,
const Uptane::Target& target)
const {
12 if (!getDirectorMetadata(&root, &targets)) {
15 meta_bundle->emplace(std::make_pair(Uptane::RepositoryType::Director(), Uptane::Role::Root()), root);
16 meta_bundle->emplace(std::make_pair(Uptane::RepositoryType::Director(), Uptane::Role::Targets()), targets);
18 if (!getImageRepoMetadata(&root, ×tamp, &snapshot, &targets)) {
21 meta_bundle->emplace(std::make_pair(Uptane::RepositoryType::Image(), Uptane::Role::Root()), root);
22 meta_bundle->emplace(std::make_pair(Uptane::RepositoryType::Image(), Uptane::Role::Timestamp()), timestamp);
23 meta_bundle->emplace(std::make_pair(Uptane::RepositoryType::Image(), Uptane::Role::Snapshot()), snapshot);
24 meta_bundle->emplace(std::make_pair(Uptane::RepositoryType::Image(), Uptane::Role::Targets()), targets);
33 bool SecondaryProvider::getDirectorMetadata(std::string* root, std::string* targets)
const {
34 if (!storage_->loadLatestRoot(root, Uptane::RepositoryType::Director())) {
35 LOG_ERROR <<
"No Director Root metadata to send";
38 if (!storage_->loadNonRoot(targets, Uptane::RepositoryType::Director(), Uptane::Role::Targets())) {
39 LOG_ERROR <<
"No Director Targets metadata to send";
45 bool SecondaryProvider::getImageRepoMetadata(std::string* root, std::string* timestamp, std::string* snapshot,
46 std::string* targets)
const {
47 if (!storage_->loadLatestRoot(root, Uptane::RepositoryType::Image())) {
48 LOG_ERROR <<
"No Image repo Root metadata to send";
51 if (!storage_->loadNonRoot(timestamp, Uptane::RepositoryType::Image(), Uptane::Role::Timestamp())) {
52 LOG_ERROR <<
"No Image repo Timestamp metadata to send";
55 if (!storage_->loadNonRoot(snapshot, Uptane::RepositoryType::Image(), Uptane::Role::Snapshot())) {
56 LOG_ERROR <<
"No Image repo Snapshot metadata to send";
59 if (!storage_->loadNonRoot(targets, Uptane::RepositoryType::Image(), Uptane::Role::Targets())) {
60 LOG_ERROR <<
"No Image repo Targets metadata to send";
66 std::string SecondaryProvider::getTreehubCredentials()
const {
67 if (config_.tls.pkey_source != CryptoSource::kFile || config_.tls.cert_source != CryptoSource::kFile ||
68 config_.tls.ca_source != CryptoSource::kFile) {
69 LOG_ERROR <<
"Cannot send OSTree update to a Secondary when not using file as credential sources";
75 if (!storage_->loadTlsCreds(&ca, &cert, &pkey)) {
76 LOG_ERROR <<
"Could not load TLS credentials from storage";
80 const std::string treehub_url = config_.pacman.ostree_server;
81 std::map<std::string, std::string> archive_map = {
82 {
"ca.pem", ca}, {
"client.pem", cert}, {
"pkey.pem", pkey}, {
"server.url", treehub_url}};
86 Utils::writeArchive(archive_map, as);
89 }
catch (std::runtime_error& exc) {
90 LOG_ERROR <<
"Could not create credentials archive: " << exc.what();
95 std::ifstream SecondaryProvider::getTargetFileHandle(
const Uptane::Target& target)
const {
96 return package_manager_->openTargetFile(target);