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) {
31 if (curl.get() ==
nullptr) {
32 LOG_FATAL <<
"Error initializing curl";
35 curlEasySetoptWrapper(curl.get(), CURLOPT_VERBOSE, get_curlopt_verbose());
36 curlEasySetoptWrapper(curl.get(), CURLOPT_NOBODY, 1L);
38 treehub.InjectIntoCurl(
"objects/" + ref.substr(0, 2) +
"/" + ref.substr(2) +
".commit", curl.get());
40 CURLcode
result = curl_easy_perform(curl.get());
42 LOG_FATAL <<
"Error connecting to treehub: " <<
result <<
": " << curl_easy_strerror(
result);
48 curl_easy_getinfo(curl.get(), CURLINFO_RESPONSE_CODE, &http_code);
49 if (http_code == 404) {
51 LOG_FATAL <<
"OSTree commit " << ref <<
" is missing in treehub";
54 type = OstreeObjectType::OSTREE_OBJECT_TYPE_UNKNOWN;
56 }
else if (http_code != 200) {
57 LOG_FATAL <<
"Error " << http_code <<
" getting OSTree ref " << ref <<
" from treehub";
61 LOG_INFO <<
"OSTree commit " << ref <<
" is found on treehub";
68 OSTreeObject::ptr input_object = dest_repo.GetObject(hash, type);
70 RequestPool request_pool(treehub, max_curl_requests, mode);
73 request_pool.AddQuery(input_object);
81 }
while (!request_pool.is_idle() && !request_pool.is_stopped());
83 if (input_object->is_on_server() == PresenceOnServer::kObjectPresent) {
84 LOG_INFO <<
"Dry run. No objects uploaded.";
86 LOG_ERROR <<
"One or more errors while pushing";
91 if (type == OstreeObjectType::OSTREE_OBJECT_TYPE_COMMIT) {
92 curlEasySetoptWrapper(curl.get(), CURLOPT_VERBOSE, get_curlopt_verbose());
93 curlEasySetoptWrapper(curl.get(), CURLOPT_HTTPGET, 1L);
94 curlEasySetoptWrapper(curl.get(), CURLOPT_NOBODY, 0L);
95 treehub.InjectIntoCurl(
"/api/v1/user_repo/targets.json", curl.get(),
true);
97 std::string targets_str;
98 curlEasySetoptWrapper(curl.get(), CURLOPT_WRITEFUNCTION, writeString);
99 curlEasySetoptWrapper(curl.get(), CURLOPT_WRITEDATA, static_cast<void *>(&targets_str));
100 result = curl_easy_perform(curl.get());
103 LOG_FATAL <<
"Error connecting to TUF repo: " <<
result <<
": " << curl_easy_strerror(
result);
107 curl_easy_getinfo(curl.get(), CURLINFO_RESPONSE_CODE, &http_code);
108 if (http_code != 200) {
109 LOG_FATAL <<
"Error " << http_code <<
" getting targets.json from TUF repo: " << targets_str;
113 Json::Value targets_json = Utils::parseJSON(targets_str);
114 std::string expiry_time_str = targets_json[
"signed"][
"expires"].asString();
117 if (timestamp.IsExpiredAt(TimeStamp::Now())) {
118 LOG_FATAL <<
"targets.json has been expired.";
122 Json::Value target_list = targets_json[
"signed"][
"targets"];
123 for (
auto t_it = target_list.begin(); t_it != target_list.end(); t_it++) {
124 if ((*t_it)[
"hashes"][
"sha256"].asString() == ref) {
125 LOG_INFO <<
"OSTree commit " << ref <<
" is found in targets.json";
129 LOG_FATAL <<
"OSTree ref " << ref <<
" was not found in targets.json";
132 LOG_INFO <<
"OSTree ref " << ref <<
" is not a commit object. Skipping targets.json check.";