remove codeconv usage

This commit is contained in:
Bartosz Wieczorek 2025-06-13 12:51:58 +02:00
parent 29890305c6
commit 6f3da4edb6
3 changed files with 69 additions and 47 deletions

View File

@ -182,7 +182,7 @@ class CheckApplication {
status.updateAppVersion(RUBLON_VERSION_STRING); status.updateAppVersion(RUBLON_VERSION_STRING);
status.updateSystemVersion(details::osName(&mr)); status.updateSystemVersion(details::osName(&mr));
status.updateSSHDConfig(); status.updateSSHDConfig();
status.updateRublonConfig(); // status.updateRublonConfig();
if(status.updated()) { if(status.updated()) {
auto & alloc = status.data().GetAllocator(); auto & alloc = status.data().GetAllocator();

View File

@ -33,7 +33,7 @@ class Configuration {
std::pmr::string apiServer{memoryResource}; std::pmr::string apiServer{memoryResource};
int prompt{}; int prompt{};
bool enablePasswdEmail{}; bool enablePasswdEmail{}; // obsolete
bool logging{}; bool logging{};
bool autopushPrompt{}; bool autopushPrompt{};
FailMode failMode{}; FailMode failMode{};
@ -86,6 +86,8 @@ class ConfigurationReader {
tl::expected< bool, ConfigurationError > applyTo(Configuration & config) const { tl::expected< bool, ConfigurationError > applyTo(Configuration & config) const {
using string = std::pmr::string; using string = std::pmr::string;
auto logRequiredFieldNotAvailable = [](auto fieldname) { log(LogLevel::Error, "%s field is not set", fieldname); };
// Helper lambdas for conversion // Helper lambdas for conversion
auto getStringOpt = [&](const string & key) -> std::optional< std::pmr::string > { auto getStringOpt = [&](const string & key) -> std::optional< std::pmr::string > {
auto it = keyValues.find(key); auto it = keyValues.find(key);
@ -215,20 +217,26 @@ class ConfigurationReader {
// * configuration value is empty -> RequiredValueEmpty // * configuration value is empty -> RequiredValueEmpty
// Reading required fields // Reading required fields
if(auto val = getStringReq("systemToken"); not val.has_value()) if(auto val = getStringReq("systemToken"); not val.has_value()) {
logRequiredFieldNotAvailable("systemToken");
return tl::unexpected{val.error()}; return tl::unexpected{val.error()};
else } else {
config.systemToken = std::move(val.value()); config.systemToken = std::move(val.value());
}
if(auto val = getStringReq("secretKey"); not val.has_value()) if(auto val = getStringReq("secretKey"); not val.has_value()) {
logRequiredFieldNotAvailable("secretKey");
return tl::unexpected{val.error()}; return tl::unexpected{val.error()};
else } else {
config.secretKey = std::move(val.value()); config.secretKey = std::move(val.value());
}
if(auto val = getStringReq("rublonApiServer"); not val.has_value()) if(auto val = getStringReq("rublonApiServer"); not val.has_value()) {
logRequiredFieldNotAvailable("rublonApiServer");
return tl::unexpected{val.error()}; return tl::unexpected{val.error()};
else } else {
config.apiServer = std::move(val.value()); config.apiServer = std::move(val.value());
}
// optional configuration options // optional configuration options
config.prompt = getInt("prompt").value_or(1); config.prompt = getInt("prompt").value_or(1);
@ -242,22 +250,22 @@ class ConfigurationReader {
config.proxyEnabled = getBool("proxyEnabled").value_or(false); config.proxyEnabled = getBool("proxyEnabled").value_or(false);
config.proxyType = getStringOpt("proxyType"); config.proxyType = getStringOpt("proxyType");
config.proxyServer = getStringOpt("proxyServer"); config.proxyServer = getStringOpt("proxyServer");
// Apply fallback if no config is set // Apply fallback if no config is set
if (config.proxyEnabled && (!config.proxyType || config.proxyType->empty()) && (!config.proxyServer || config.proxyServer->empty())) { if(config.proxyEnabled && (!config.proxyType || config.proxyType->empty()) &&
(!config.proxyServer || config.proxyServer->empty())) {
log(LogLevel::Info, "Proxy is enabled but no configuration for it is provided, trying to read from env"); log(LogLevel::Info, "Proxy is enabled but no configuration for it is provided, trying to read from env");
if (const char* https_proxy = std::getenv("https_proxy"); https_proxy && *https_proxy) { if(const char * https_proxy = std::getenv("https_proxy"); https_proxy && *https_proxy) {
if (parseProxyURL(https_proxy)) { if(parseProxyURL(https_proxy)) {
log(LogLevel::Info, "Loaded proxy config from HTTPS_PROXY"); log(LogLevel::Info, "Loaded proxy config from HTTPS_PROXY");
} }
} else if (const char* http_proxy = std::getenv("http_proxy"); http_proxy && *http_proxy) { } else if(const char * http_proxy = std::getenv("http_proxy"); http_proxy && *http_proxy) {
if (parseProxyURL(http_proxy)) { if(parseProxyURL(http_proxy)) {
log(LogLevel::Info, "Loaded proxy config from HTTP_PROXY"); log(LogLevel::Info, "Loaded proxy config from HTTP_PROXY");
} }
} }
} }
if(config.proxyEnabled) { if(config.proxyEnabled) {
if(not config.proxyType or config.proxyType->empty()) { if(not config.proxyType or config.proxyType->empty()) {
log(LogLevel::Error, "Proxy is enabled but proxy type is not present or empty"); log(LogLevel::Error, "Proxy is enabled but proxy type is not present or empty");

View File

@ -1,17 +1,20 @@
#pragma once #pragma once
#include <charconv> #include <tl/expected.hpp>
#include <cstdint>
#include <limits>
#include <optional> #include <optional>
#include <string>
#include <utility>
#include <rublon/memory.hpp> #include <rublon/memory.hpp>
#include <rublon/static_string.hpp> #include <rublon/static_string.hpp>
#include <rublon/stdlib.hpp> #include <rublon/stdlib.hpp>
#include <fcntl.h> #include <fcntl.h>
#include <string>
#include <sys/stat.h> #include <sys/stat.h>
#include <syslog.h> #include <syslog.h>
#include <unistd.h> #include <unistd.h>
#include <utility>
#include <security/pam_appl.h> #include <security/pam_appl.h>
#include <security/pam_modules.h> #include <security/pam_modules.h>
@ -150,23 +153,33 @@ namespace conv {
std::transform(userinput.cbegin(), userinput.cend(), buf.data(), asciitolower); std::transform(userinput.cbegin(), userinput.cend(), buf.data(), asciitolower);
return strcmp(buf.data(), "true") == 0; return strcmp(buf.data(), "true") == 0;
} }
inline std::optional< std::uint32_t> to_uint32opt(std::string_view userinput) noexcept { inline std::optional< std::uint32_t > to_uint32opt(std::string_view userinput) noexcept {
int out; constexpr auto max = std::numeric_limits< uint32_t >::digits10 + 1;
const std::from_chars_result result = std::from_chars(userinput.data(), userinput.data() + userinput.size(), out); if(userinput.empty() || userinput.size() >= max)
if(result.ec == std::errc::invalid_argument || result.ec == std::errc::result_out_of_range) { return std::nullopt; // Avoid large or empty inputs
char buffer[max]={0};
std::memcpy(buffer, userinput.data(), userinput.size());
buffer[userinput.size()] = '\0'; // Ensure null termination
char * endptr = nullptr;
errno = 0;
long result = std::strtol(buffer, &endptr, 10);
if(errno == ERANGE || endptr != buffer + userinput.size() || result < 0 || result > std::numeric_limits<uint32_t>::max()) {
return std::nullopt; return std::nullopt;
} }
return out;
return static_cast< std::uint32_t >(result);
} }
inline tl::expected< std::uint32_t, Error > to_uint32(std::string_view userinput) noexcept { inline tl::expected< std::uint32_t, Error > to_uint32(std::string_view userinput) noexcept {
int out; auto val = to_uint32opt(userinput);
const std::from_chars_result result = std::from_chars(userinput.data(), userinput.data() + userinput.size(), out); if(val)
if(result.ec == std::errc::invalid_argument || result.ec == std::errc::result_out_of_range) { return *val;
return tl::make_unexpected(Error::NotANumber); return tl::unexpected{Error::NotANumber};
}
return out;
} }
} // namespace conv } // namespace conv
@ -186,33 +199,33 @@ namespace details {
static inline std::string_view trim(std::string_view s) { static inline std::string_view trim(std::string_view s) {
return ltrim(rtrim(s)); return ltrim(rtrim(s));
} }
template<typename StrT> template < typename StrT >
void trimInPlace(StrT & s) { void trimInPlace(StrT & s) {
// Remove leading whitespace // Remove leading whitespace
size_t start = 0; size_t start = 0;
while(start < s.size() && isspace(static_cast< unsigned char >(s[start]))) while(start < s.size() && isspace(static_cast< unsigned char >(s[start])))
++start; ++start;
// Remove trailing whitespace // Remove trailing whitespace
size_t end = s.size(); size_t end = s.size();
while(end > start && isspace(static_cast< unsigned char >(s[end - 1]))) while(end > start && isspace(static_cast< unsigned char >(s[end - 1])))
--end; --end;
if(start > 0 || end < s.size()) { if(start > 0 || end < s.size()) {
s = s.substr(start, end - start); s = s.substr(start, end - start);
} }
} }
template < typename Headers > template < typename Headers >
inline void headers(std::string_view data, Headers & headers) { inline void headers(std::string_view data, Headers & headers) {
memory::Monotonic_4k_Resource stackResource; memory::Monotonic_4k_Resource stackResource;
std::pmr::string tmp{&stackResource}; std::pmr::string tmp{&stackResource};
tmp.reserve(300); tmp.reserve(300);
std::istringstream resp{}; std::istringstream resp{};
resp.rdbuf()->pubsetbuf(const_cast< char * >(data.data()), data.size()); resp.rdbuf()->pubsetbuf(const_cast< char * >(data.data()), data.size());
while(std::getline(resp, tmp)) { while(std::getline(resp, tmp)) {
if(tmp == "\r") if(tmp == "\r")
continue; continue;
@ -250,9 +263,8 @@ namespace details {
std::pmr::string _key{&memoryResource}; std::pmr::string _key{&memoryResource};
std::pmr::string _value{&memoryResource}; std::pmr::string _value{&memoryResource};
line.reserve(100); line.reserve(100);
while(std::getline(file, line)) {
while(std::getline(file, line)) {
if(!line.length()) if(!line.length())
continue; continue;
@ -299,19 +311,21 @@ constexpr std::array< Out, sizeof...(Types) > make_array(Types... names) {
return {std::forward< Types >(names)...}; return {std::forward< Types >(names)...};
} }
template <typename T> std::size_t size_buffer(const T &item) { template < typename T >
using U = std::decay_t<T>; std::size_t size_buffer(const T & item) {
if constexpr (std::is_same_v<U, const char *>) { using U = std::decay_t< T >;
if constexpr(std::is_same_v< U, const char * >) {
return strlen(item); return strlen(item);
} else if constexpr(std::is_same_v< U, std::pmr::string > || std::is_same_v< U, std::string >) { } else if constexpr(std::is_same_v< U, std::pmr::string > || std::is_same_v< U, std::string >) {
return item.size(); return item.size();
} else if constexpr(std::is_integral_v< U > || std::is_floating_point_v< U >) { } else if constexpr(std::is_integral_v< U > || std::is_floating_point_v< U >) {
return std::numeric_limits<U>::digits; return std::numeric_limits< U >::digits;
} }
return 0; return 0;
} }
template <typename T> std::size_t size_buffer(const std::optional<T> &item) { template < typename T >
std::size_t size_buffer(const std::optional< T > & item) {
if(item.has_value()) if(item.has_value())
return size_buffer(*item); return size_buffer(*item);
return 0; return 0;