diff --git a/userspace/engine/falco_utils.cpp b/userspace/engine/falco_utils.cpp index 513d2e23..3502e7cf 100644 --- a/userspace/engine/falco_utils.cpp +++ b/userspace/engine/falco_utils.cpp @@ -16,6 +16,7 @@ See the License for the specific language governing permissions and limitations under the License. */ +#include #include "falco_utils.h" #include "banned.h" // This raises a compilation error when certain functions are used @@ -26,7 +27,7 @@ namespace falco namespace utils { -std::string wrap_text(const std::string &str, uint32_t initial_pos, uint32_t indent, uint32_t line_len) +std::string wrap_text(const std::string& str, uint32_t initial_pos, uint32_t indent, uint32_t line_len) { std::string ret; @@ -51,6 +52,36 @@ std::string wrap_text(const std::string &str, uint32_t initial_pos, uint32_t ind return ret; } -} // namespace utils +void readfile(const std::string& filename, std::string& data) +{ + std::ifstream file(filename.c_str(), std::ios::in); + if(file.is_open()) + { + std::stringstream ss; + ss << file.rdbuf(); + + file.close(); + + data = ss.str(); + } + + return; +} + +bool starts_with(const std::string& text, const std::string& prefix) +{ + return prefix.empty() || + (text.size() >= prefix.size() && + std::memcmp(text.data(), prefix.data(), prefix.size()) == 0); +} + +namespace network +{ +bool url_is_unix_scheme(const std::string& url) +{ + return starts_with(url, UNIX_SCHEME); +} +} // namespace network +} // namespace utils } // namespace falco diff --git a/userspace/engine/falco_utils.h b/userspace/engine/falco_utils.h index 0c812a05..dc1fd85e 100644 --- a/userspace/engine/falco_utils.h +++ b/userspace/engine/falco_utils.h @@ -17,6 +17,9 @@ limitations under the License. */ +#include +#include +#include #include #pragma once @@ -27,8 +30,14 @@ namespace falco namespace utils { -std::string wrap_text(const std::string &str, uint32_t initial_pos, uint32_t indent, uint32_t line_len); +std::string wrap_text(const std::string& str, uint32_t initial_pos, uint32_t indent, uint32_t line_len); +void readfile(const std::string& filename, std::string& data); +bool starts_with(const std::string& text, const std::string& prefix); +namespace network +{ +static const std::string UNIX_SCHEME{"unix://"}; +bool url_is_unix_scheme(const std::string& url); +} // namespace network } // namespace utils - } // namespace falco diff --git a/userspace/falco/grpc_server.cpp b/userspace/falco/grpc_server.cpp index cf727785..91d27130 100644 --- a/userspace/falco/grpc_server.cpp +++ b/userspace/falco/grpc_server.cpp @@ -23,7 +23,7 @@ limitations under the License. #include "logger.h" #include "grpc_server.h" #include "grpc_request_context.h" -#include "utils.h" +#include "falco_utils.h" #include "banned.h" // This raises a compilation error when certain functions are used #define REGISTER_STREAM(req, res, svc, rpc, impl, num) \ @@ -117,9 +117,9 @@ void falco::grpc::server::init_mtls_server_builder() string private_key; string cert_chain; string root_certs; - falco::utils::read(m_cert_chain, cert_chain); - falco::utils::read(m_private_key, private_key); - falco::utils::read(m_root_certs, root_certs); + falco::utils::readfile(m_cert_chain, cert_chain); + falco::utils::readfile(m_private_key, private_key); + falco::utils::readfile(m_root_certs, root_certs); ::grpc::SslServerCredentialsOptions::PemKeyCertPair cert_pair{private_key, cert_chain}; ::grpc::SslServerCredentialsOptions ssl_opts(GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY); ssl_opts.pem_root_certs = root_certs; diff --git a/userspace/falco/utils.cpp b/userspace/falco/utils.cpp deleted file mode 100644 index 76d20468..00000000 --- a/userspace/falco/utils.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/* -Copyright (C) 2019 The Falco Authors - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -#include - -#include "utils.h" -#include "banned.h" // This raises a compilation error when certain functions are used - -void falco::utils::read(const std::string& filename, std::string& data) -{ - std::ifstream file(filename.c_str(), std::ios::in); - - if(file.is_open()) - { - std::stringstream ss; - ss << file.rdbuf(); - - file.close(); - - data = ss.str(); - } - - return; -} - -bool falco::utils::starts_with(const std::string& text, const std::string& prefix) -{ - return prefix.empty() || - (text.size() >= prefix.size() && - std::memcmp(text.data(), prefix.data(), prefix.size()) == 0); -} - -bool falco::utils::network::url_is_unix_scheme(const std::string& url) -{ - return starts_with(url, UNIX_SCHEME); -} diff --git a/userspace/falco/utils.h b/userspace/falco/utils.h deleted file mode 100644 index a91ea978..00000000 --- a/userspace/falco/utils.h +++ /dev/null @@ -1,37 +0,0 @@ -/* -Copyright (C) 2019 The Falco Authors - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -#pragma once - -#include -#include -#include -#include - -namespace falco -{ -namespace utils -{ -void read(const std::string& filename, std::string& data); -bool starts_with(const std::string& text, const std::string& prefix); - -namespace network -{ -static const std::string UNIX_SCHEME{"unix://"}; -bool url_is_unix_scheme(const std::string& url); -} // namespace network -} // namespace utils -} // namespace falco