CernVM-FS  2.12.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 
24  download_manager_ = new download::DownloadManager(max_pool_handles,
25  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 
45  const std::string &pubkey_path,
46  const std::string &certificate_path,
47  const std::string &private_key_path)
48 {
50  return true;
51  }
52 
55  signature_manager_->Init();
56 
57  // We may not have a public key. In this case, the signature manager
58  // can only be used for signing, not for verification.
59  if (!pubkey_path.empty()) {
60  if (!signature_manager_->LoadPublicRsaKeys(pubkey_path)) {
61  LogCvmfs(kLogCvmfs, kLogStderr, "failed to load public repo key '%s'",
62  pubkey_path.c_str());
63  return false;
64  }
65  }
66 
67  // We may not have a certificate and key. In this case, the signature manager
68  // can only be used for verification, not for signing.
69  if (certificate_path.empty())
70  return true;
71 
72  if (!signature_manager_->LoadCertificatePath(certificate_path)) {
73  LogCvmfs(kLogCvmfs, kLogStderr, "failed to load certificate '%s'",
74  certificate_path.c_str());
75  return false;
76  }
77 
78  // Load private key
79  if (!signature_manager_->LoadPrivateKeyPath(private_key_path, "")) {
80  LogCvmfs(kLogCvmfs, kLogStderr, "failed to load private key '%s' (%s)",
81  private_key_path.c_str(),
82  signature_manager_->GetCryptoError().c_str());
83  return false;
84  }
85 
86  if (!signature_manager_->KeysMatch()) {
88  "the private key '%s' doesn't seem to match certificate '%s' (%s)",
89  private_key_path.c_str(), certificate_path.c_str(),
90  signature_manager_->GetCryptoError().c_str());
91  signature_manager_->UnloadPrivateKey();
92  return false;
93  }
94 
95  return true;
96 }
97 
100  return download_manager_.weak_ref();
101 }
102 
105  return signature_manager_.weak_ref();
106 }
107 
109  const std::string path) const {
110  return manifest::Manifest::LoadFile(path);
111 }
112 
114  const std::string &repository_url, const std::string &repository_name,
115  manifest::ManifestEnsemble *ensemble) const {
116  const uint64_t minimum_timestamp = 0;
117  const shash::Any *base_catalog = NULL;
118  return manifest::Fetch(repository_url, repository_name, minimum_timestamp,
119  base_catalog, signature_manager(), download_manager(),
120  ensemble);
121 }
122 
124  const std::string &repository_url, const std::string &repository_name,
125  const shash::Any &base_hash) const {
126  manifest::ManifestEnsemble manifest_ensemble;
128 
129  // fetch (and verify) the manifest
131  repository_url, repository_name, &manifest_ensemble);
132 
133  if (retval != manifest::kFailOk) {
135  "failed to fetch repository manifest "
136  "(%d - %s)",
137  retval, manifest::Code2Ascii(retval));
138  return NULL;
139  } else {
140  // copy-construct a fresh manifest object because ManifestEnsemble will
141  // free manifest_ensemble.manifest when it goes out of scope
142  manifest = new manifest::Manifest(*manifest_ensemble.manifest);
143  }
144 
145  // check if manifest fetching was successful
146  if (!manifest.IsValid()) {
147  LogCvmfs(kLogCvmfs, kLogStderr, "failed to load repository manifest");
148  return NULL;
149  }
150 
151  // check the provided base hash of the repository if provided
152  if (!base_hash.IsNull() && manifest->catalog_hash() != base_hash) {
154  "base hash does not match manifest "
155  "(found: %s expected: %s)",
156  manifest->catalog_hash().ToString().c_str(),
157  base_hash.ToString().c_str());
158  return NULL;
159  }
160 
161  // return the fetched manifest (releasing pointer ownership)
162  return manifest.Release();
163 }
164 
166  const std::string &temp_directory, const std::string &repo_name) {
167  // create a new Reflog if there was none found yet
168  const std::string tmp_path_prefix = temp_directory + "/new_reflog";
169  const std::string tmp_path = CreateTempPath(tmp_path_prefix, 0600);
170 
171  LogCvmfs(kLogCvmfs, kLogDebug, "creating new reflog '%s' for %s",
172  tmp_path.c_str(), repo_name.c_str());
173  return manifest::Reflog::Create(tmp_path, repo_name);
174 }
bool IsNull() const
Definition: hash.h:383
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:42
std::string ToString(const bool with_suffix=false) const
Definition: hash.h:249
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:1045
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:123
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:113
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:103
download::DownloadManager * download_manager() const
Definition: server_tool.cc:98
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:43
UniquePtr< signature::SignatureManager > signature_manager_
Definition: server_tool.h:52
manifest::Manifest * OpenLocalManifest(const std::string path) const
Definition: server_tool.cc:108
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:44
virtual ~ServerTool()
Definition: server_tool.cc:11
manifest::Reflog * CreateEmptyReflog(const std::string &temp_directory, const std::string &repo_name)
Definition: server_tool.cc:165
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:92
const char * Code2Ascii(const Failures error)
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)
Definition: logging.cc:528