diff --git a/BUILD b/BUILD index 69c9bdae5..f29f4de85 100644 --- a/BUILD +++ b/BUILD @@ -66,6 +66,8 @@ cc_library( "src/bytecode_util.cc", "src/context.cc", "src/exports.cc", + "src/hash.cc", + "src/hash.h", "src/pairs_util.cc", "src/shared_data.cc", "src/shared_data.h", diff --git a/include/proxy-wasm/context.h b/include/proxy-wasm/context.h index 652b86b6e..ab99cad70 100644 --- a/include/proxy-wasm/context.h +++ b/include/proxy-wasm/context.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #include "include/proxy-wasm/context_interface.h" @@ -53,8 +54,7 @@ struct PluginBase { std::string_view key) : name_(std::string(name)), root_id_(std::string(root_id)), vm_id_(std::string(vm_id)), engine_(std::string(engine)), plugin_configuration_(plugin_configuration), - fail_open_(fail_open), - key_(root_id_ + "||" + plugin_configuration_ + "||" + std::string(key)), + fail_open_(fail_open), key_(makePluginKey(root_id, plugin_configuration, key)), log_prefix_(makeLogPrefix()) {} const std::string name_; @@ -69,6 +69,8 @@ struct PluginBase { private: std::string makeLogPrefix() const; + static std::string makePluginKey(std::string_view root_id, std::string_view plugin_configuration, + std::string_view key); const std::string key_; const std::string log_prefix_; diff --git a/src/context.cc b/src/context.cc index 29037ae43..5353a52a5 100644 --- a/src/context.cc +++ b/src/context.cc @@ -22,6 +22,7 @@ #include "include/proxy-wasm/context.h" #include "include/proxy-wasm/wasm.h" +#include "src/hash.h" #include "src/shared_data.h" #include "src/shared_queue.h" @@ -85,6 +86,11 @@ std::string PluginBase::makeLogPrefix() const { return prefix; } +std::string PluginBase::makePluginKey(std::string_view root_id, + std::string_view plugin_configuration, std::string_view key) { + return Sha256String({root_id, "||", plugin_configuration, "||", key}); +} + ContextBase::ContextBase() : parent_context_(this) {} ContextBase::ContextBase(WasmBase *wasm) : wasm_(wasm), parent_context_(this) { diff --git a/src/hash.cc b/src/hash.cc new file mode 100644 index 000000000..f2d1ded13 --- /dev/null +++ b/src/hash.cc @@ -0,0 +1,54 @@ +// Copyright 2023 Google LLC +// +// 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 "src/hash.h" + +#include +#include + +#include + +namespace proxy_wasm { + +namespace { + +std::string BytesToHex(const std::vector &bytes) { + static const char *const hex = "0123456789ABCDEF"; + std::string result; + result.reserve(bytes.size() * 2); + for (auto byte : bytes) { + result.push_back(hex[byte >> 4]); + result.push_back(hex[byte & 0xf]); + } + return result; +} + +} // namespace + +std::vector Sha256(const std::vector &parts) { + uint8_t sha256[SHA256_DIGEST_LENGTH]; + SHA256_CTX sha_ctx; + SHA256_Init(&sha_ctx); + for (auto part : parts) { + SHA256_Update(&sha_ctx, part.data(), part.size()); + } + SHA256_Final(sha256, &sha_ctx); + return std::vector(std::begin(sha256), std::end(sha256)); +} + +std::string Sha256String(const std::vector &parts) { + return BytesToHex(Sha256(parts)); +} + +} // namespace proxy_wasm \ No newline at end of file diff --git a/src/hash.h b/src/hash.h new file mode 100644 index 000000000..40d03e9d4 --- /dev/null +++ b/src/hash.h @@ -0,0 +1,27 @@ +// Copyright 2023 Google LLC +// +// 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 + +namespace proxy_wasm { + +std::vector Sha256(const std::vector &parts); +std::string Sha256String(const std::vector &parts); + +} // namespace proxy_wasm diff --git a/src/wasm.cc b/src/wasm.cc index a7a22fc9c..15127c99a 100644 --- a/src/wasm.cc +++ b/src/wasm.cc @@ -27,11 +27,10 @@ #include #include -#include - #include "include/proxy-wasm/bytecode_util.h" #include "include/proxy-wasm/signature_util.h" #include "include/proxy-wasm/vm_id_handle.h" +#include "src/hash.h" namespace proxy_wasm { @@ -44,33 +43,11 @@ thread_local std::unordered_map> lo std::mutex base_wasms_mutex; std::unordered_map> *base_wasms = nullptr; -std::vector Sha256(const std::vector &parts) { - uint8_t sha256[SHA256_DIGEST_LENGTH]; - SHA256_CTX sha_ctx; - SHA256_Init(&sha_ctx); - for (auto part : parts) { - SHA256_Update(&sha_ctx, part.data(), part.size()); - } - SHA256_Final(sha256, &sha_ctx); - return std::vector(std::begin(sha256), std::end(sha256)); -} - -std::string BytesToHex(const std::vector &bytes) { - static const char *const hex = "0123456789ABCDEF"; - std::string result; - result.reserve(bytes.size() * 2); - for (auto byte : bytes) { - result.push_back(hex[byte >> 4]); - result.push_back(hex[byte & 0xf]); - } - return result; -} - } // namespace std::string makeVmKey(std::string_view vm_id, std::string_view vm_configuration, std::string_view code) { - return BytesToHex(Sha256({vm_id, vm_configuration, code})); + return Sha256String({vm_id, "||", vm_configuration, "||", code}); } class WasmBase::ShutdownHandle {