3 #include "authenticate.h" 7 #include "logging/logging.h" 8 #include "ostree_http_repo.h" 9 #include "ostree_object.h" 10 #include "rate_controller.h" 11 #include "request_pool.h" 12 #include "treehub_server.h" 14 #include "utilities/utils.h" 17 static size_t writeString(
void *contents,
size_t size,
size_t nmemb,
void *userp) {
20 (
static_cast<std::string *
>(userp))->append(static_cast<char *>(contents), size * nmemb);
26 int CheckRefValid(
TreehubServer &treehub,
const std::string &ref,
RunMode mode,
int max_curl_requests,
27 const boost::filesystem::path &tree_dir) {
32 if (curl.get() ==
nullptr) {
33 LOG_FATAL <<
"Error initializing curl";
36 curlEasySetoptWrapper(curl.get(), CURLOPT_VERBOSE, get_curlopt_verbose());
37 curlEasySetoptWrapper(curl.get(), CURLOPT_NOBODY, 1L);
39 treehub.InjectIntoCurl(
"objects/" + ref.substr(0, 2) +
"/" + ref.substr(2) +
".commit", curl.get());
41 CURLcode
result = curl_easy_perform(curl.get());
42 if (result != CURLE_OK) {
43 LOG_FATAL <<
"Error connecting to treehub: " << result <<
": " << curl_easy_strerror(result);
49 curl_easy_getinfo(curl.get(), CURLINFO_RESPONSE_CODE, &http_code);
50 if (http_code == 404) {
52 LOG_FATAL <<
"OSTree commit " << ref <<
" is missing in treehub";
55 type = OstreeObjectType::OSTREE_OBJECT_TYPE_UNKNOWN;
57 }
else if (http_code != 200) {
58 LOG_FATAL <<
"Error " << http_code <<
" getting OSTree ref " << ref <<
" from treehub";
62 LOG_INFO <<
"OSTree commit " << ref <<
" is found on treehub";
69 OSTreeObject::ptr input_object = dest_repo.GetObject(hash, type);
71 RequestPool request_pool(treehub, max_curl_requests, mode);
74 request_pool.AddQuery(input_object);
82 }
while (!request_pool.is_idle() && !request_pool.is_stopped());
84 if (input_object->is_on_server() == PresenceOnServer::kObjectPresent) {
85 LOG_INFO <<
"Dry run. No objects uploaded.";
87 LOG_ERROR <<
"One or more errors while pushing";
92 if (type == OstreeObjectType::OSTREE_OBJECT_TYPE_COMMIT) {
93 curlEasySetoptWrapper(curl.get(), CURLOPT_VERBOSE, get_curlopt_verbose());
94 curlEasySetoptWrapper(curl.get(), CURLOPT_HTTPGET, 1L);
95 curlEasySetoptWrapper(curl.get(), CURLOPT_NOBODY, 0L);
96 treehub.InjectIntoCurl(
"/api/v1/user_repo/targets.json", curl.get(),
true);
98 std::string targets_str;
99 curlEasySetoptWrapper(curl.get(), CURLOPT_WRITEFUNCTION, writeString);
100 curlEasySetoptWrapper(curl.get(), CURLOPT_WRITEDATA,
static_cast<void *
>(&targets_str));
101 result = curl_easy_perform(curl.get());
103 if (result != CURLE_OK) {
104 LOG_FATAL <<
"Error connecting to TUF repo: " << result <<
": " << curl_easy_strerror(result);
108 curl_easy_getinfo(curl.get(), CURLINFO_RESPONSE_CODE, &http_code);
109 if (http_code != 200) {
110 LOG_FATAL <<
"Error " << http_code <<
" getting targets.json from TUF repo: " << targets_str;
114 Json::Value targets_json = Utils::parseJSON(targets_str);
115 std::string expiry_time_str = targets_json[
"signed"][
"expires"].asString();
118 if (timestamp.IsExpiredAt(TimeStamp::Now())) {
119 LOG_FATAL <<
"targets.json has been expired.";
123 Json::Value target_list = targets_json[
"signed"][
"targets"];
124 for (
auto t_it = target_list.begin(); t_it != target_list.end(); t_it++) {
125 if ((*t_it)[
"hashes"][
"sha256"].asString() == ref) {
126 LOG_INFO <<
"OSTree commit " << ref <<
" is found in targets.json";
130 LOG_FATAL <<
"OSTree ref " << ref <<
" was not found in targets.json";
133 LOG_INFO <<
"OSTree ref " << ref <<
" is not a commit object. Skipping targets.json check.";
static OSTreeHash Parse(const std::string &hash)
Parse an OSTree hash from a string.
OstreeObjectType
Types of OSTree objects, borrowed from libostree/ostree-core.h.
Walk the entire tree (without uploading).
Results of libaktualizr API calls.
RunMode
Execution mode to run garage tools in.