Use VoidLibrary strict JSON and component releases
This commit is contained in:
4 files changed
+148
-381
No files matched your search
@@ -0,0 +1,56 @@
|
||||
name: Independent component release
|
||||
on:
|
||||
push:
|
||||
tags: [v*]
|
||||
schedule:
|
||||
- cron: '17 2 * * *'
|
||||
workflow_dispatch:
|
||||
jobs:
|
||||
publish:
|
||||
runs-on: echoion-release-serial
|
||||
steps:
|
||||
- name: Check out component
|
||||
uses: https://github.com/actions/checkout@11d5960a326750d5838078e36cf38b85af677262
|
||||
with:
|
||||
fetch-depth: 0
|
||||
ssh-key: ${{ secrets.RELEASE_CHECKOUT_SSH_KEY }}
|
||||
persist-credentials: false
|
||||
- name: Check out pinned public release tools
|
||||
uses: https://github.com/actions/checkout@11d5960a326750d5838078e36cf38b85af677262
|
||||
with:
|
||||
repository: Void/Echoion
|
||||
ref: ${{ vars.ECHOION_RELEASE_TOOLS_REF }}
|
||||
path: .echoion-release-tools
|
||||
fetch-depth: 0
|
||||
ssh-key: ${{ secrets.RELEASE_CHECKOUT_SSH_KEY }}
|
||||
persist-credentials: false
|
||||
- name: Build, publish, and merge feed transaction
|
||||
shell: bash
|
||||
env:
|
||||
AWS_ACCESS_KEY_ID: ${{ secrets.COMPONENT_OBJECT_STORAGE_ACCESS_KEY }}
|
||||
AWS_SECRET_ACCESS_KEY: ${{ secrets.COMPONENT_OBJECT_STORAGE_SECRET_KEY }}
|
||||
AWS_DEFAULT_REGION: ${{ vars.COMPONENT_OBJECT_STORAGE_REGION }}
|
||||
ECHOION_COMPONENT_SSH_KEY: ${{ secrets.RELEASE_CHECKOUT_SSH_KEY }}
|
||||
ECHOION_DISTRIBUTION_BASE_URL: ${{ vars.ECHOION_DISTRIBUTION_BASE_URL }}
|
||||
ECHOION_FEED_BASE_URL: ${{ vars.ECHOION_FEED_BASE_URL }}
|
||||
ECHOION_FEED_COMMIT_SIGNING_KEY: ${{ secrets.RELEASE_FEED_GIT_SIGNING_KEY }}
|
||||
ECHOION_FEED_PRIVATE_KEY: ${{ secrets.RELEASE_FEED_ED25519_PRIVATE_KEY }}
|
||||
ECHOION_FEED_REPOSITORY: ssh://gitea@git.faumaray.ru/Void/Echoion.git
|
||||
ECHOION_FEED_SIGNING_KEY_ID: ${{ vars.ECHOION_FEED_SIGNING_KEY_ID }}
|
||||
ECHOION_OBJECT_STORAGE_BUCKET: ${{ vars.COMPONENT_OBJECT_STORAGE_BUCKET }}
|
||||
ECHOION_OBJECT_STORAGE_ENDPOINT: ${{ vars.COMPONENT_OBJECT_STORAGE_ENDPOINT }}
|
||||
ECHOION_OBJECT_STORAGE_PREFIX: ${{ vars.COMPONENT_OBJECT_STORAGE_PREFIX }}
|
||||
ECHOION_RELEASE_TAG_ALLOWED_SIGNERS: ${{ secrets.RELEASE_TAG_ALLOWED_SIGNERS }}
|
||||
GITEA_API_URL: ${{ gitea.api_url }}
|
||||
GITEA_EVENT_NAME: ${{ gitea.event_name }}
|
||||
GITEA_REF_NAME: ${{ gitea.ref_name }}
|
||||
GITEA_RUN_NUMBER: ${{ gitea.run_number }}
|
||||
GITEA_SHA: ${{ gitea.sha }}
|
||||
GITEA_REPOSITORY: ${{ gitea.repository }}
|
||||
GITEA_RELEASE_TOKEN: ${{ secrets.RELEASE_BOT_TOKEN }}
|
||||
RELEASE_TOOLS_REF: ${{ vars.ECHOION_RELEASE_TOOLS_REF }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
[[ "$RELEASE_TOOLS_REF" =~ ^[0-9a-f]{40}$ ]]
|
||||
test "$(git -C .echoion-release-tools rev-parse HEAD)" = "$RELEASE_TOOLS_REF"
|
||||
.echoion-release-tools/scripts/release-component.sh component.release.json
|
||||
+12
-4
@@ -1,8 +1,11 @@
|
||||
cmake_minimum_required(VERSION 4.0.0)
|
||||
if(NOT DEFINED ECHOION_PRODUCT_VERSION)
|
||||
set(ECHOION_PRODUCT_VERSION 2.0.0)
|
||||
set(ECHOION_PRODUCT_VERSION 0.0.0)
|
||||
endif()
|
||||
project(EchoionUpdater VERSION "${ECHOION_PRODUCT_VERSION}" LANGUAGES CXX)
|
||||
if(NOT DEFINED ECHOION_COMPONENT_VERSION)
|
||||
set(ECHOION_COMPONENT_VERSION "${ECHOION_PRODUCT_VERSION}")
|
||||
endif()
|
||||
project(EchoionUpdater VERSION "${ECHOION_COMPONENT_VERSION}" LANGUAGES CXX)
|
||||
|
||||
include(GNUInstallDirs)
|
||||
|
||||
@@ -27,7 +30,10 @@ endif()
|
||||
find_package(OpenSSL REQUIRED COMPONENTS Crypto)
|
||||
|
||||
add_executable(EchoionUpdater echoion-updater.cpp ReleaseTree.cpp)
|
||||
target_link_libraries(EchoionUpdater PRIVATE OpenSSL::Crypto Void::Update)
|
||||
target_link_libraries(EchoionUpdater PRIVATE
|
||||
OpenSSL::Crypto
|
||||
Void::Data
|
||||
Void::Update)
|
||||
set_target_properties(EchoionUpdater PROPERTIES
|
||||
OUTPUT_NAME echoion-updater
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
|
||||
@@ -46,7 +52,9 @@ if(BUILD_TESTS)
|
||||
add_executable(EchoionReleaseTreeSecurityTests
|
||||
ReleaseTreeSecurityTests.cpp
|
||||
ReleaseTree.cpp)
|
||||
target_link_libraries(EchoionReleaseTreeSecurityTests PRIVATE OpenSSL::Crypto)
|
||||
target_link_libraries(EchoionReleaseTreeSecurityTests PRIVATE
|
||||
OpenSSL::Crypto
|
||||
Void::Data)
|
||||
|
||||
add_test(NAME EchoionReleaseTreeSecurity
|
||||
COMMAND EchoionReleaseTreeSecurityTests)
|
||||
|
||||
+73
-377
@@ -1,9 +1,10 @@
|
||||
#include "ReleaseTree.hpp"
|
||||
|
||||
#include <Void/Data/StrictJson.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cerrno>
|
||||
#include <charconv>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <exception>
|
||||
@@ -35,7 +36,6 @@ namespace Echoion::Updater
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
constexpr std::size_t kMaximumJsonBytes{16U * 1024U * 1024U};
|
||||
constexpr std::size_t kMaximumJsonDepth{64U};
|
||||
constexpr std::size_t kMaximumReleaseEntries{50'000U};
|
||||
constexpr std::size_t kMaximumManifestEntries{kMaximumReleaseEntries};
|
||||
constexpr std::size_t kMaximumSymlinkBytes{4096U};
|
||||
@@ -63,13 +63,13 @@ namespace Echoion::Updater
|
||||
}
|
||||
}
|
||||
|
||||
FileDescriptor(const FileDescriptor&) = delete;
|
||||
FileDescriptor& operator=(const FileDescriptor&) = delete;
|
||||
FileDescriptor(FileDescriptor&& other) noexcept
|
||||
FileDescriptor(const FileDescriptor &) = delete;
|
||||
FileDescriptor &operator=(const FileDescriptor &) = delete;
|
||||
FileDescriptor(FileDescriptor &&other) noexcept
|
||||
: descriptor_{std::exchange(other.descriptor_, -1)}
|
||||
{
|
||||
}
|
||||
FileDescriptor& operator=(FileDescriptor&& other) noexcept
|
||||
FileDescriptor &operator=(FileDescriptor &&other) noexcept
|
||||
{
|
||||
if (this != &other) {
|
||||
if (descriptor_ >= 0) {
|
||||
@@ -88,7 +88,7 @@ namespace Echoion::Updater
|
||||
};
|
||||
|
||||
struct DirectoryCloser {
|
||||
void operator()(DIR* directory) const noexcept
|
||||
void operator()(DIR *directory) const noexcept
|
||||
{
|
||||
if (directory != nullptr) {
|
||||
::closedir(directory);
|
||||
@@ -105,13 +105,7 @@ namespace Echoion::Updater
|
||||
std::strerror(errno)};
|
||||
}
|
||||
|
||||
struct Json {
|
||||
using Array = std::vector<Json>;
|
||||
using Object = std::map<std::string, Json, std::less<>>;
|
||||
std::variant<std::nullptr_t, bool, std::int64_t, std::string,
|
||||
Array, Object>
|
||||
value;
|
||||
};
|
||||
using Json = Void::Json::Strict::Value;
|
||||
|
||||
class JsonParser final
|
||||
{
|
||||
@@ -122,325 +116,27 @@ namespace Echoion::Updater
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] Json parse()
|
||||
[[nodiscard]] Json parse() const
|
||||
{
|
||||
skipWhitespace();
|
||||
Json result = parseValue(0U);
|
||||
skipWhitespace();
|
||||
if (position_ != input_.size()) {
|
||||
fail("trailing data");
|
||||
auto result = Void::Json::Strict::parse(input_);
|
||||
if (!result) {
|
||||
throw std::runtime_error{
|
||||
"invalid JSON at byte " +
|
||||
std::to_string(result.error().offset) + ": " +
|
||||
result.error().message};
|
||||
}
|
||||
return result;
|
||||
return std::move(*result);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
[[noreturn]] void fail(const std::string_view message) const
|
||||
{
|
||||
throw std::runtime_error{"invalid JSON at byte " +
|
||||
std::to_string(position_) + ": " +
|
||||
std::string{message}};
|
||||
}
|
||||
|
||||
void skipWhitespace()
|
||||
{
|
||||
while (position_ < input_.size() &&
|
||||
(input_[position_] == ' ' ||
|
||||
input_[position_] == '\t' ||
|
||||
input_[position_] == '\r' ||
|
||||
input_[position_] == '\n')) {
|
||||
++position_;
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] char take()
|
||||
{
|
||||
if (position_ >= input_.size()) {
|
||||
fail("unexpected end of input");
|
||||
}
|
||||
return input_[position_++];
|
||||
}
|
||||
|
||||
void expect(const char expected)
|
||||
{
|
||||
if (take() != expected) {
|
||||
fail("unexpected character");
|
||||
}
|
||||
}
|
||||
|
||||
void expectLiteral(const std::string_view literal)
|
||||
{
|
||||
if (input_.substr(position_, literal.size()) != literal) {
|
||||
fail("invalid literal");
|
||||
}
|
||||
position_ += literal.size();
|
||||
}
|
||||
|
||||
[[nodiscard]] static int hexValue(const char value)
|
||||
{
|
||||
if (value >= '0' && value <= '9') {
|
||||
return value - '0';
|
||||
}
|
||||
if (value >= 'a' && value <= 'f') {
|
||||
return value - 'a' + 10;
|
||||
}
|
||||
if (value >= 'A' && value <= 'F') {
|
||||
return value - 'A' + 10;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::uint32_t unicodeEscape()
|
||||
{
|
||||
std::uint32_t value{};
|
||||
for (int index{}; index < 4; ++index) {
|
||||
const int digit = hexValue(take());
|
||||
if (digit < 0) {
|
||||
fail("invalid unicode escape");
|
||||
}
|
||||
value = value * 16U + static_cast<std::uint32_t>(digit);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
static void appendUtf8(std::string& output,
|
||||
const std::uint32_t codepoint)
|
||||
{
|
||||
if (codepoint <= 0x7FU) {
|
||||
output.push_back(static_cast<char>(codepoint));
|
||||
} else if (codepoint <= 0x7FFU) {
|
||||
output.push_back(
|
||||
static_cast<char>(0xC0U | (codepoint >> 6U)));
|
||||
output.push_back(
|
||||
static_cast<char>(0x80U | (codepoint & 0x3FU)));
|
||||
} else if (codepoint <= 0xFFFFU) {
|
||||
output.push_back(
|
||||
static_cast<char>(0xE0U | (codepoint >> 12U)));
|
||||
output.push_back(static_cast<char>(
|
||||
0x80U | ((codepoint >> 6U) & 0x3FU)));
|
||||
output.push_back(
|
||||
static_cast<char>(0x80U | (codepoint & 0x3FU)));
|
||||
} else {
|
||||
output.push_back(
|
||||
static_cast<char>(0xF0U | (codepoint >> 18U)));
|
||||
output.push_back(static_cast<char>(
|
||||
0x80U | ((codepoint >> 12U) & 0x3FU)));
|
||||
output.push_back(static_cast<char>(
|
||||
0x80U | ((codepoint >> 6U) & 0x3FU)));
|
||||
output.push_back(
|
||||
static_cast<char>(0x80U | (codepoint & 0x3FU)));
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string parseString()
|
||||
{
|
||||
expect('"');
|
||||
std::string output;
|
||||
while (true) {
|
||||
const unsigned char value =
|
||||
static_cast<unsigned char>(take());
|
||||
if (value == '"') {
|
||||
return output;
|
||||
}
|
||||
if (value < 0x20U) {
|
||||
fail("control character in string");
|
||||
}
|
||||
if (value != '\\') {
|
||||
output.push_back(static_cast<char>(value));
|
||||
continue;
|
||||
}
|
||||
switch (take()) {
|
||||
case '"':
|
||||
output.push_back('"');
|
||||
break;
|
||||
case '\\':
|
||||
output.push_back('\\');
|
||||
break;
|
||||
case '/':
|
||||
output.push_back('/');
|
||||
break;
|
||||
case 'b':
|
||||
output.push_back('\b');
|
||||
break;
|
||||
case 'f':
|
||||
output.push_back('\f');
|
||||
break;
|
||||
case 'n':
|
||||
output.push_back('\n');
|
||||
break;
|
||||
case 'r':
|
||||
output.push_back('\r');
|
||||
break;
|
||||
case 't':
|
||||
output.push_back('\t');
|
||||
break;
|
||||
case 'u':
|
||||
{
|
||||
std::uint32_t codepoint = unicodeEscape();
|
||||
if (codepoint >= 0xD800U &&
|
||||
codepoint <= 0xDBFFU) {
|
||||
if (take() != '\\' || take() != 'u') {
|
||||
fail("missing low surrogate");
|
||||
}
|
||||
const std::uint32_t low =
|
||||
unicodeEscape();
|
||||
if (low < 0xDC00U || low > 0xDFFFU) {
|
||||
fail("invalid low surrogate");
|
||||
}
|
||||
codepoint =
|
||||
0x10000U +
|
||||
((codepoint - 0xD800U) << 10U) +
|
||||
low - 0xDC00U;
|
||||
} else if (codepoint >= 0xDC00U &&
|
||||
codepoint <= 0xDFFFU) {
|
||||
fail("unexpected low surrogate");
|
||||
}
|
||||
appendUtf8(output, codepoint);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
fail("invalid string escape");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] Json parseNumber()
|
||||
{
|
||||
const std::size_t start = position_;
|
||||
if (input_[position_] == '-') {
|
||||
++position_;
|
||||
}
|
||||
if (position_ >= input_.size()) {
|
||||
fail("incomplete number");
|
||||
}
|
||||
if (input_[position_] == '0') {
|
||||
++position_;
|
||||
} else {
|
||||
if (input_[position_] < '1' ||
|
||||
input_[position_] > '9') {
|
||||
fail("invalid number");
|
||||
}
|
||||
while (position_ < input_.size() &&
|
||||
input_[position_] >= '0' &&
|
||||
input_[position_] <= '9') {
|
||||
++position_;
|
||||
}
|
||||
}
|
||||
if (position_ < input_.size() &&
|
||||
(input_[position_] == '.' || input_[position_] == 'e' ||
|
||||
input_[position_] == 'E')) {
|
||||
fail("floating-point numbers are not supported");
|
||||
}
|
||||
std::int64_t value{};
|
||||
const auto result =
|
||||
std::from_chars(input_.data() + start,
|
||||
input_.data() + position_, value);
|
||||
if (result.ec != std::errc{} ||
|
||||
result.ptr != input_.data() + position_) {
|
||||
fail("integer is out of range");
|
||||
}
|
||||
return Json{value};
|
||||
}
|
||||
|
||||
[[nodiscard]] Json parseArray(const std::size_t depth)
|
||||
{
|
||||
expect('[');
|
||||
skipWhitespace();
|
||||
Json::Array result;
|
||||
if (position_ < input_.size() && input_[position_] == ']') {
|
||||
++position_;
|
||||
return Json{std::move(result)};
|
||||
}
|
||||
while (true) {
|
||||
result.push_back(parseValue(depth + 1U));
|
||||
skipWhitespace();
|
||||
const char separator = take();
|
||||
if (separator == ']') {
|
||||
return Json{std::move(result)};
|
||||
}
|
||||
if (separator != ',') {
|
||||
fail("array requires comma or closing bracket");
|
||||
}
|
||||
skipWhitespace();
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] Json parseObject(const std::size_t depth)
|
||||
{
|
||||
expect('{');
|
||||
skipWhitespace();
|
||||
Json::Object result;
|
||||
if (position_ < input_.size() && input_[position_] == '}') {
|
||||
++position_;
|
||||
return Json{std::move(result)};
|
||||
}
|
||||
while (true) {
|
||||
if (position_ >= input_.size() ||
|
||||
input_[position_] != '"') {
|
||||
fail("object key must be a string");
|
||||
}
|
||||
std::string key = parseString();
|
||||
skipWhitespace();
|
||||
expect(':');
|
||||
skipWhitespace();
|
||||
auto [iterator, inserted] = result.emplace(
|
||||
std::move(key), parseValue(depth + 1U));
|
||||
static_cast<void>(iterator);
|
||||
if (!inserted) {
|
||||
fail("duplicate object key");
|
||||
}
|
||||
skipWhitespace();
|
||||
const char separator = take();
|
||||
if (separator == '}') {
|
||||
return Json{std::move(result)};
|
||||
}
|
||||
if (separator != ',') {
|
||||
fail("object requires comma or closing brace");
|
||||
}
|
||||
skipWhitespace();
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] Json parseValue(const std::size_t depth)
|
||||
{
|
||||
if (depth > kMaximumJsonDepth ||
|
||||
position_ >= input_.size()) {
|
||||
fail("nesting limit or unexpected end of input");
|
||||
}
|
||||
switch (input_[position_]) {
|
||||
case '{':
|
||||
return parseObject(depth);
|
||||
case '[':
|
||||
return parseArray(depth);
|
||||
case '"':
|
||||
return Json{parseString()};
|
||||
case 't':
|
||||
expectLiteral("true");
|
||||
return Json{true};
|
||||
case 'f':
|
||||
expectLiteral("false");
|
||||
return Json{false};
|
||||
case 'n':
|
||||
expectLiteral("null");
|
||||
return Json{nullptr};
|
||||
default:
|
||||
if (input_[position_] == '-' ||
|
||||
(input_[position_] >= '0' &&
|
||||
input_[position_] <= '9')) {
|
||||
return parseNumber();
|
||||
}
|
||||
fail("invalid value");
|
||||
}
|
||||
}
|
||||
|
||||
std::string_view input_;
|
||||
std::size_t position_{};
|
||||
};
|
||||
|
||||
[[nodiscard]] const Json::Object&
|
||||
object(const Json& value, const std::string_view description)
|
||||
[[nodiscard]] const Json::Object &
|
||||
object(const Json &value, const std::string_view description)
|
||||
{
|
||||
const auto* result = std::get_if<Json::Object>(&value.value);
|
||||
const auto *result = std::get_if<Json::Object>(&value.value);
|
||||
if (result == nullptr) {
|
||||
throw std::runtime_error{std::string{description} +
|
||||
" must be an object"};
|
||||
@@ -448,10 +144,10 @@ namespace Echoion::Updater
|
||||
return *result;
|
||||
}
|
||||
|
||||
[[nodiscard]] const Json::Array&
|
||||
array(const Json& value, const std::string_view description)
|
||||
[[nodiscard]] const Json::Array &
|
||||
array(const Json &value, const std::string_view description)
|
||||
{
|
||||
const auto* result = std::get_if<Json::Array>(&value.value);
|
||||
const auto *result = std::get_if<Json::Array>(&value.value);
|
||||
if (result == nullptr) {
|
||||
throw std::runtime_error{std::string{description} +
|
||||
" must be an array"};
|
||||
@@ -459,7 +155,7 @@ namespace Echoion::Updater
|
||||
return *result;
|
||||
}
|
||||
|
||||
[[nodiscard]] const Json& required(const Json::Object& value,
|
||||
[[nodiscard]] const Json &required(const Json::Object &value,
|
||||
const std::string_view key)
|
||||
{
|
||||
const auto iterator = value.find(key);
|
||||
@@ -470,10 +166,10 @@ namespace Echoion::Updater
|
||||
return iterator->second;
|
||||
}
|
||||
|
||||
[[nodiscard]] const std::string& string(const Json::Object& value,
|
||||
[[nodiscard]] const std::string &string(const Json::Object &value,
|
||||
const std::string_view key)
|
||||
{
|
||||
const auto* result =
|
||||
const auto *result =
|
||||
std::get_if<std::string>(&required(value, key).value);
|
||||
if (result == nullptr) {
|
||||
throw std::runtime_error{"JSON field must be a string: " +
|
||||
@@ -482,10 +178,10 @@ namespace Echoion::Updater
|
||||
return *result;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::int64_t integer(const Json::Object& value,
|
||||
[[nodiscard]] std::int64_t integer(const Json::Object &value,
|
||||
const std::string_view key)
|
||||
{
|
||||
const auto* result =
|
||||
const auto *result =
|
||||
std::get_if<std::int64_t>(&required(value, key).value);
|
||||
if (result == nullptr) {
|
||||
throw std::runtime_error{"JSON field must be an integer: " +
|
||||
@@ -494,13 +190,13 @@ namespace Echoion::Updater
|
||||
return *result;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::uint32_t retainedVersions(const Json::Object& value)
|
||||
[[nodiscard]] std::uint32_t retainedVersions(const Json::Object &value)
|
||||
{
|
||||
const auto found = value.find("retainHealthyVersions");
|
||||
if (found == value.end()) {
|
||||
return 2U;
|
||||
}
|
||||
const auto* result =
|
||||
const auto *result =
|
||||
std::get_if<std::int64_t>(&found->second.value);
|
||||
if (result == nullptr || *result < 1 || *result > 10) {
|
||||
throw std::runtime_error{
|
||||
@@ -540,7 +236,7 @@ namespace Echoion::Updater
|
||||
});
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string readBounded(const fs::path& path,
|
||||
[[nodiscard]] std::string readBounded(const fs::path &path,
|
||||
const std::size_t maximumBytes)
|
||||
{
|
||||
std::error_code error;
|
||||
@@ -569,12 +265,12 @@ namespace Echoion::Updater
|
||||
return result;
|
||||
}
|
||||
|
||||
[[nodiscard]] unsigned int mode(const fs::file_status& status)
|
||||
[[nodiscard]] unsigned int mode(const fs::file_status &status)
|
||||
{
|
||||
return static_cast<unsigned int>(status.permissions()) & 07777U;
|
||||
}
|
||||
|
||||
void rejectWritable(const fs::path& path, const fs::file_status& status)
|
||||
void rejectWritable(const fs::path &path, const fs::file_status &status)
|
||||
{
|
||||
if ((mode(status) & 0022U) != 0U) {
|
||||
throw std::runtime_error{
|
||||
@@ -615,8 +311,8 @@ namespace Echoion::Updater
|
||||
}
|
||||
|
||||
[[nodiscard]] fs::path
|
||||
joinedPath(const fs::path& root,
|
||||
const std::vector<std::string_view>& parts)
|
||||
joinedPath(const fs::path &root,
|
||||
const std::vector<std::string_view> &parts)
|
||||
{
|
||||
fs::path result = root;
|
||||
for (const auto part : parts) {
|
||||
@@ -625,7 +321,7 @@ namespace Echoion::Updater
|
||||
return result;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool within(const fs::path& path, const fs::path& root)
|
||||
[[nodiscard]] bool within(const fs::path &path, const fs::path &root)
|
||||
{
|
||||
const auto mismatch = std::mismatch(root.begin(), root.end(),
|
||||
path.begin(), path.end());
|
||||
@@ -671,7 +367,7 @@ namespace Echoion::Updater
|
||||
++entries;
|
||||
}
|
||||
|
||||
void accountFile(const struct stat& metadata,
|
||||
void accountFile(const struct stat &metadata,
|
||||
const std::string_view logicalPath)
|
||||
{
|
||||
if (metadata.st_size < 0) {
|
||||
@@ -699,8 +395,8 @@ namespace Echoion::Updater
|
||||
}
|
||||
};
|
||||
|
||||
[[nodiscard]] bool sameOpenedNode(const struct stat& before,
|
||||
const struct stat& after) noexcept
|
||||
[[nodiscard]] bool sameOpenedNode(const struct stat &before,
|
||||
const struct stat &after) noexcept
|
||||
{
|
||||
return before.st_dev == after.st_dev &&
|
||||
before.st_ino == after.st_ino &&
|
||||
@@ -738,7 +434,7 @@ namespace Echoion::Updater
|
||||
}
|
||||
|
||||
template <typename Visitor>
|
||||
void visitDirectoryEntries(const int descriptor, Visitor&& visitor)
|
||||
void visitDirectoryEntries(const int descriptor, Visitor &&visitor)
|
||||
{
|
||||
const int duplicate = ::fcntl(descriptor, F_DUPFD_CLOEXEC, 0);
|
||||
if (duplicate < 0) {
|
||||
@@ -755,7 +451,7 @@ namespace Echoion::Updater
|
||||
std::size_t entries{};
|
||||
while (true) {
|
||||
errno = 0;
|
||||
const dirent* entry = ::readdir(directory.get());
|
||||
const dirent *entry = ::readdir(directory.get());
|
||||
if (entry == nullptr) {
|
||||
if (errno != 0) {
|
||||
throw systemError("cannot enumerate staged directory");
|
||||
@@ -807,7 +503,7 @@ namespace Echoion::Updater
|
||||
return result;
|
||||
}
|
||||
|
||||
void requireOwnedSource(const struct stat& metadata, const uid_t owner,
|
||||
void requireOwnedSource(const struct stat &metadata, const uid_t owner,
|
||||
const std::string_view logicalPath)
|
||||
{
|
||||
if (metadata.st_uid != owner) {
|
||||
@@ -823,7 +519,7 @@ namespace Echoion::Updater
|
||||
}
|
||||
}
|
||||
|
||||
void writeAll(const int descriptor, const char* bytes,
|
||||
void writeAll(const int descriptor, const char *bytes,
|
||||
const std::size_t size)
|
||||
{
|
||||
std::size_t offset{};
|
||||
@@ -847,7 +543,7 @@ namespace Echoion::Updater
|
||||
void copyRegularAt(
|
||||
const int sourceParent, const std::string_view name,
|
||||
const int destinationParent, const std::string_view logicalPath,
|
||||
const uid_t owner, const std::size_t depth, CopyBudget& budget,
|
||||
const uid_t owner, const std::size_t depth, CopyBudget &budget,
|
||||
const std::uintmax_t maximumBytes = kMaximumReleaseFileBytes)
|
||||
{
|
||||
requireEntryName(name);
|
||||
@@ -958,7 +654,7 @@ namespace Echoion::Updater
|
||||
const int destinationParent,
|
||||
const std::string_view logicalPath,
|
||||
const uid_t owner, const std::size_t depth,
|
||||
CopyBudget& budget)
|
||||
CopyBudget &budget)
|
||||
{
|
||||
const std::string ownedName{name};
|
||||
struct stat before{};
|
||||
@@ -990,7 +686,7 @@ namespace Echoion::Updater
|
||||
void copyNodeAt(const int sourceParent, const std::string_view name,
|
||||
const int destinationParent,
|
||||
const std::string_view logicalPath, const uid_t owner,
|
||||
const std::size_t depth, CopyBudget& budget)
|
||||
const std::size_t depth, CopyBudget &budget)
|
||||
{
|
||||
requireEntryName(name);
|
||||
const std::string ownedName{name};
|
||||
@@ -1026,7 +722,7 @@ namespace Echoion::Updater
|
||||
throw systemError("cannot set release directory mode");
|
||||
}
|
||||
visitDirectoryEntries(
|
||||
source.get(), [&](const std::string& child) {
|
||||
source.get(), [&](const std::string &child) {
|
||||
const std::string childPath =
|
||||
childLogicalPath(logicalPath, child);
|
||||
copyNodeAt(source.get(), child, destination.get(),
|
||||
@@ -1055,7 +751,7 @@ namespace Echoion::Updater
|
||||
}
|
||||
|
||||
void removeTreeAt(const int parent, const std::string_view name,
|
||||
const std::size_t depth, std::size_t& entries)
|
||||
const std::size_t depth, std::size_t &entries)
|
||||
{
|
||||
requireEntryName(name);
|
||||
if (depth > kMaximumReleaseDepth + 1U ||
|
||||
@@ -1081,7 +777,7 @@ namespace Echoion::Updater
|
||||
"incomplete release cleanup exceeds traversal limit"};
|
||||
}
|
||||
entries += children.size();
|
||||
for (const auto& child : children) {
|
||||
for (const auto &child : children) {
|
||||
removeTreeAt(directory.get(), child, depth + 1U, entries);
|
||||
}
|
||||
if (::unlinkat(parent, ownedName.c_str(), AT_REMOVEDIR) != 0) {
|
||||
@@ -1094,7 +790,7 @@ namespace Echoion::Updater
|
||||
}
|
||||
|
||||
void syncTreeAt(const int directoryDescriptor, const std::size_t depth,
|
||||
std::size_t& entries)
|
||||
std::size_t &entries)
|
||||
{
|
||||
if (depth > kMaximumReleaseDepth + 1U ||
|
||||
entries > kMaximumReleaseEntries + 1U) {
|
||||
@@ -1107,7 +803,7 @@ namespace Echoion::Updater
|
||||
"release sync exceeds traversal limit"};
|
||||
}
|
||||
entries += children.size();
|
||||
for (const auto& child : children) {
|
||||
for (const auto &child : children) {
|
||||
struct stat metadata{};
|
||||
if (::fstatat(directoryDescriptor, child.c_str(), &metadata,
|
||||
AT_SYMLINK_NOFOLLOW) != 0) {
|
||||
@@ -1150,12 +846,12 @@ namespace Echoion::Updater
|
||||
throw std::runtime_error{"activation request exceeds size limit"};
|
||||
}
|
||||
const Json requestValue = JsonParser{json}.parse();
|
||||
const auto& request = object(requestValue, "privileged request");
|
||||
const auto &request = object(requestValue, "privileged request");
|
||||
static const std::set<std::string_view> allowed{
|
||||
"schemaVersion", "id", "createdAt",
|
||||
"operation", "stagingId", "version",
|
||||
"manifestSha256", "origin", "retainHealthyVersions"};
|
||||
for (const auto& [key, value] : request) {
|
||||
for (const auto &[key, value] : request) {
|
||||
static_cast<void>(value);
|
||||
if (!allowed.contains(key)) {
|
||||
throw std::runtime_error{"unknown activation request field: " +
|
||||
@@ -1171,14 +867,14 @@ namespace Echoion::Updater
|
||||
}
|
||||
if (const auto origin = request.find("origin");
|
||||
origin != request.end()) {
|
||||
const auto* originValue =
|
||||
const auto *originValue =
|
||||
std::get_if<std::string>(&origin->second.value);
|
||||
if (originValue == nullptr || !safeIdentifier(*originValue)) {
|
||||
throw std::runtime_error{
|
||||
"privileged request origin is invalid"};
|
||||
}
|
||||
}
|
||||
const std::string& operation = string(request, "operation");
|
||||
const std::string &operation = string(request, "operation");
|
||||
if (operation == "activate-runtime") {
|
||||
RuntimeActivation result{
|
||||
.stagingId = string(request, "stagingId"),
|
||||
@@ -1200,7 +896,7 @@ namespace Echoion::Updater
|
||||
throw std::runtime_error{
|
||||
"runtime selection request has activation-only fields"};
|
||||
}
|
||||
const std::string& version = string(request, "version");
|
||||
const std::string &version = string(request, "version");
|
||||
if (!safeIdentifier(version)) {
|
||||
throw std::runtime_error{
|
||||
"runtime selection request contains an unsafe version"};
|
||||
@@ -1224,12 +920,12 @@ namespace Echoion::Updater
|
||||
"pending runtime journal exceeds size limit"};
|
||||
}
|
||||
const Json value = JsonParser{json}.parse();
|
||||
const auto& pending = object(value, "pending runtime journal");
|
||||
const auto &pending = object(value, "pending runtime journal");
|
||||
static const std::set<std::string_view> allowed{
|
||||
"schemaVersion", "candidateId", "version",
|
||||
"previousVersion", "manifestSha256", "manifestPath",
|
||||
"activatedAt", "state", "retainHealthyVersions"};
|
||||
for (const auto& [key, child] : pending) {
|
||||
for (const auto &[key, child] : pending) {
|
||||
static_cast<void>(child);
|
||||
if (!allowed.contains(key)) {
|
||||
throw std::runtime_error{"unknown pending runtime field: " +
|
||||
@@ -1265,14 +961,14 @@ namespace Echoion::Updater
|
||||
RuntimeActivation parseActivationRequest(const std::string_view json)
|
||||
{
|
||||
PrivilegedRequest request = parsePrivilegedRequest(json);
|
||||
const auto* activation = std::get_if<RuntimeActivation>(&request);
|
||||
const auto *activation = std::get_if<RuntimeActivation>(&request);
|
||||
if (activation == nullptr) {
|
||||
throw std::runtime_error{"request is not a runtime activation"};
|
||||
}
|
||||
return *activation;
|
||||
}
|
||||
|
||||
std::string sha256File(const fs::path& path)
|
||||
std::string sha256File(const fs::path &path)
|
||||
{
|
||||
const std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_free)> context{
|
||||
EVP_MD_CTX_new(), &EVP_MD_CTX_free};
|
||||
@@ -1314,9 +1010,9 @@ namespace Echoion::Updater
|
||||
return result;
|
||||
}
|
||||
|
||||
void validateRelease(const fs::path& release,
|
||||
const std::string& expectedVersion,
|
||||
const std::string& expectedManifestSha256)
|
||||
void validateRelease(const fs::path &release,
|
||||
const std::string &expectedVersion,
|
||||
const std::string &expectedManifestSha256)
|
||||
{
|
||||
const fs::path root = fs::canonical(release);
|
||||
const auto rootStatus = fs::symlink_status(release);
|
||||
@@ -1332,7 +1028,7 @@ namespace Echoion::Updater
|
||||
const std::string manifestBytes =
|
||||
readBounded(manifestPath, kMaximumJsonBytes);
|
||||
const Json manifestValue = JsonParser{manifestBytes}.parse();
|
||||
const auto& manifest = object(manifestValue, "release manifest");
|
||||
const auto &manifest = object(manifestValue, "release manifest");
|
||||
if (integer(manifest, "schemaVersion") != 2 ||
|
||||
string(manifest, "version") != expectedVersion ||
|
||||
string(manifest, "executable") != "bin/echoiond" ||
|
||||
@@ -1340,16 +1036,16 @@ namespace Echoion::Updater
|
||||
string(manifest, "core") != "lib/Echoion.ts") {
|
||||
throw std::runtime_error{"release manifest identity is invalid"};
|
||||
}
|
||||
const auto& entries =
|
||||
const auto &entries =
|
||||
array(required(manifest, "files"), "manifest files");
|
||||
if (entries.empty() || entries.size() > kMaximumManifestEntries) {
|
||||
throw std::runtime_error{"release manifest entry count is invalid"};
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> declared;
|
||||
for (const Json& entryValue : entries) {
|
||||
const auto& entry = object(entryValue, "manifest entry");
|
||||
const std::string& relative = string(entry, "path");
|
||||
for (const Json &entryValue : entries) {
|
||||
const auto &entry = object(entryValue, "manifest entry");
|
||||
const std::string &relative = string(entry, "path");
|
||||
const auto parts = pathParts(relative);
|
||||
if (!declared.emplace(relative, string(entry, "type")).second) {
|
||||
throw std::runtime_error{"duplicate manifest path: " +
|
||||
@@ -1377,7 +1073,7 @@ namespace Echoion::Updater
|
||||
throw std::runtime_error{"manifest symlink is missing: " +
|
||||
relative};
|
||||
}
|
||||
const std::string& target = string(entry, "target");
|
||||
const std::string &target = string(entry, "target");
|
||||
static_cast<void>(
|
||||
pathParts(std::string{parts.front()} + "/" + target));
|
||||
if (fs::read_symlink(path).generic_string() != target) {
|
||||
@@ -1404,7 +1100,7 @@ namespace Echoion::Updater
|
||||
"immutable release directory is missing"};
|
||||
}
|
||||
rejectWritable(directory, status);
|
||||
for (const auto& entry :
|
||||
for (const auto &entry :
|
||||
fs::recursive_directory_iterator{directory}) {
|
||||
const auto entryStatus = entry.symlink_status();
|
||||
if (fs::is_directory(entryStatus)) {
|
||||
@@ -1437,7 +1133,7 @@ namespace Echoion::Updater
|
||||
{"service/echoion-updater.service.in", "file"},
|
||||
{"service/echoion-updater.timer.in", "file"},
|
||||
};
|
||||
for (const auto& [path, type] : requiredFiles) {
|
||||
for (const auto &[path, type] : requiredFiles) {
|
||||
if (!declared.contains(path) || declared.at(path) != type) {
|
||||
throw std::runtime_error{"required release file is missing: " +
|
||||
path};
|
||||
@@ -1450,8 +1146,8 @@ namespace Echoion::Updater
|
||||
}
|
||||
|
||||
void copyAndValidateRelease(const int sourceDirectoryDescriptor,
|
||||
const fs::path& destination,
|
||||
const RuntimeActivation& activation)
|
||||
const fs::path &destination,
|
||||
const RuntimeActivation &activation)
|
||||
{
|
||||
struct stat sourceMetadata{};
|
||||
if (sourceDirectoryDescriptor < 0 ||
|
||||
@@ -1549,7 +1245,7 @@ namespace Echoion::Updater
|
||||
}
|
||||
}
|
||||
|
||||
void syncReleaseTree(const fs::path& root)
|
||||
void syncReleaseTree(const fs::path &root)
|
||||
{
|
||||
FileDescriptor directory{::open(
|
||||
root.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW)};
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"schemaVersion": 1,
|
||||
"componentId": "EchoionUpdater",
|
||||
"productCompatibility": ">=2.0.0 <3.0.0",
|
||||
"publishNightly": true,
|
||||
"cmakeOptions": {}
|
||||
}
|
||||
Reference in new issue
Block a user