CernVM-FS  2.13.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
server_tool.cc
Go to the documentation of this file.
1 
5 #include "server_tool.h"
6 
7 #include "util/posix.h"
8 
10 
13  signature_manager_->Fini();
14  }
15 }
16 
17 bool ServerTool::InitDownloadManager(const bool follow_redirects,
18  const std::string &proxy,
19  const unsigned max_pool_handles) {
20  if (download_manager_.IsValid()) {
21  return true;
22  }
23 
25  max_pool_handles, perf::StatisticsTemplate("download", statistics()));
27 
29  download_manager_->SetRetryParameters(kDownloadRetries, 2000, 5000);
30  download_manager_->UseSystemCertificatePath();
31 
32  if (proxy != "") {
33  download_manager_->SetProxyChain(proxy, "",
35  }
36 
37  if (follow_redirects) {
38  download_manager_->EnableRedirects();
39  }
40 
41  return true;
42 }
43 
44 bool ServerTool::InitSignatureManager(const std::string &pubkey_path,
45  const std::string &certificate_path,
46  const std::string &private_key_path) {
48  return true;
49  }
50 
53  signature_manager_->Init();
54 
55  // We may not have a public key. In this case, the signature manager
56  // can only be used for signing, not for verification.
57  if (!pubkey_path.empty()) {
58  if (!signature_manager_->LoadPublicRsaKeys(pubkey_path)) {
59  LogCvmfs(kLogCvmfs, kLogStderr, "failed to load public repo key '%s'",
60  pubkey_path.c_str());
61  return false;
62  }
63  }
64 
65  // We may not have a certificate and key. In this case, the signature manager
66  // can only be used for verification, not for signing.
67  if (certificate_path.empty())
68  return true;
69 
70  if (!signature_manager_->LoadCertificatePath(certificate_path)) {
71  LogCvmfs(kLogCvmfs, kLogStderr, "failed to load certificate '%s'",
72  certificate_path.c_str());
73  return false;
74  }
75 
76  // Load private key
77  if (!signature_manager_->LoadPrivateKeyPath(private_key_path, "")) {
78  LogCvmfs(kLogCvmfs, kLogStderr, "failed to load private key '%s' (%s)",
79  private_key_path.c_str(),
80  signature_manager_->GetCryptoError().c_str());
81  return false;
82  }
83 
84  if (!signature_manager_->KeysMatch()) {
86  "the private key '%s' doesn't seem to match certificate '%s' (%s)",
87  private_key_path.c_str(), certificate_path.c_str(),
88  signature_manager_->GetCryptoError().c_str());
89  signature_manager_->UnloadPrivateKey();
90  return false;
91  }
92 
93  return true;
94 }
95 
98  return download_manager_.weak_ref();
99 }
100 
103  return signature_manager_.weak_ref();
104 }
105 
107  const std::string path) const {
108  return manifest::Manifest::LoadFile(path);
109 }
110 
112  const std::string &repository_url, const std::string &repository_name,
113  manifest::ManifestEnsemble *ensemble) const {
114  const uint64_t minimum_timestamp = 0;
115  const shash::Any *base_catalog = NULL;
116  return manifest::Fetch(repository_url, repository_name, minimum_timestamp,
117  base_catalog, signature_manager(), download_manager(),
118  ensemble);
119 }
120 
122  const std::string &repository_url, const std::string &repository_name,
123  const shash::Any &base_hash) const {
124  manifest::ManifestEnsemble manifest_ensemble;
126 
127  // fetch (and verify) the manifest
129  repository_url, repository_name, &manifest_ensemble);
130 
131  if (retval != manifest::kFailOk) {
133  "failed to fetch repository manifest "
134  "(%d - %s)",
135  retval, manifest::Code2Ascii(retval));
136  return NULL;
137  } else {
138  // copy-construct a fresh manifest object because ManifestEnsemble will
139  // free manifest_ensemble.manifest when it goes out of scope
140  manifest = new manifest::Manifest(*manifest_ensemble.manifest);
141  }
142 
143  // check if manifest fetching was successful
144  if (!manifest.IsValid()) {
145  LogCvmfs(kLogCvmfs, kLogStderr, "failed to load repository manifest");
146  return NULL;
147  }
148 
149  // check the provided base hash of the repository if provided
150  if (!base_hash.IsNull() && manifest->catalog_hash() != base_hash) {
152  "base hash does not match manifest "
153  "(found: %s expected: %s)",
154  manifest->catalog_hash().ToString().c_str(),
155  base_hash.ToString().c_str());
156  return NULL;
157  }
158 
159  // return the fetched manifest (releasing pointer ownership)
160  return manifest.Release();
161 }
162 
164  const std::string &temp_directory, const std::string &repo_name) {
165  // create a new Reflog if there was none found yet
166  const std::string tmp_path_prefix = temp_directory + "/new_reflog";
167  const std::string tmp_path = CreateTempPath(tmp_path_prefix, 0600);
168 
169  LogCvmfs(kLogCvmfs, kLogDebug, "creating new reflog '%s' for %s",
170  tmp_path.c_str(), repo_name.c_str());
171  return manifest::Reflog::Create(tmp_path, repo_name);
172 }
bool IsNull() const
Definition: hash.h:371
static const unsigned kDownloadTimeout
Definition: server_tool.h:56
const manifest::Manifest * manifest() const
Definition: repository.h:125
T * weak_ref() const
Definition: pointer.h:46
std::string ToString(const bool with_suffix=false) const
Definition: hash.h:241
UniquePtr< download::DownloadManager > download_manager_
Definition: server_tool.h:51
static const unsigned kDownloadRetries
Definition: server_tool.h:57
std::string CreateTempPath(const std::string &path_prefix, const int mode)
Definition: posix.cc:1041
manifest::Manifest * FetchRemoteManifest(const std::string &repository_url, const std::string &repository_name, const shash::Any &base_hash=shash::Any()) const
Definition: server_tool.cc:121
assert((mem||(size==0))&&"Out Of Memory")
manifest::Failures FetchRemoteManifestEnsemble(const std::string &repository_url, const std::string &repository_name, manifest::ManifestEnsemble *ensemble) const
Definition: server_tool.cc:111
static Reflog * Create(const std::string &database_path, const std::string &repo_name)
Definition: reflog.cc:32
signature::SignatureManager * signature_manager() const
Definition: server_tool.cc:101
download::DownloadManager * download_manager() const
Definition: server_tool.cc:96
Failures Fetch(const std::string &base_url, const std::string &repository_name, const uint64_t minimum_timestamp, const shash::Any *base_catalog, signature::SignatureManager *signature_manager, download::DownloadManager *download_manager, ManifestEnsemble *ensemble)
perf::Statistics * statistics()
Definition: server_tool.h:47
bool IsValid() const
Definition: pointer.h:47
UniquePtr< signature::SignatureManager > signature_manager_
Definition: server_tool.h:52
manifest::Manifest * OpenLocalManifest(const std::string path) const
Definition: server_tool.cc:106
bool InitSignatureManager(const std::string &pubkey_path, const std::string &certificate_path="", const std::string &private_key_path="")
Definition: server_tool.cc:44
T * Release()
Definition: pointer.h:48
virtual ~ServerTool()
Definition: server_tool.cc:11
manifest::Reflog * CreateEmptyReflog(const std::string &temp_directory, const std::string &repo_name)
Definition: server_tool.cc:163
bool InitDownloadManager(const bool follow_redirects, const std::string &proxy, const unsigned max_pool_handles=1)
Definition: server_tool.cc:17
static Manifest * LoadFile(const std::string &path)
Definition: manifest.cc:91
const char * Code2Ascii(const Failures error)
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)
Definition: logging.cc:545