mirror of
https://github.com/alibaba/higress.git
synced 2026-05-10 13:57:27 +08:00
Add plugins (#27)
This commit is contained in:
55
plugins/wasm-cpp/extensions/custom_response/BUILD
Normal file
55
plugins/wasm-cpp/extensions/custom_response/BUILD
Normal file
@@ -0,0 +1,55 @@
|
||||
load("@proxy_wasm_cpp_sdk//bazel/wasm:wasm.bzl", "wasm_cc_binary")
|
||||
load("//bazel:wasm.bzl", "declare_wasm_image_targets")
|
||||
|
||||
wasm_cc_binary(
|
||||
name = "custom_response.wasm",
|
||||
srcs = [
|
||||
"plugin.cc",
|
||||
"plugin.h",
|
||||
],
|
||||
deps = [
|
||||
"@com_google_absl//absl/strings",
|
||||
"@com_google_absl//absl/time",
|
||||
"//common:json_util",
|
||||
"@proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics",
|
||||
"//common:http_util",
|
||||
"//common:rule_util",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "custom_response_lib",
|
||||
srcs = [
|
||||
"plugin.cc",
|
||||
],
|
||||
hdrs = [
|
||||
"plugin.h",
|
||||
],
|
||||
copts = ["-DNULL_PLUGIN"],
|
||||
deps = [
|
||||
"@com_google_absl//absl/strings",
|
||||
"//common:json_util",
|
||||
"@proxy_wasm_cpp_host//:lib",
|
||||
"//common:http_util_nullvm",
|
||||
"//common:rule_util_nullvm",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "custom_response_test",
|
||||
srcs = [
|
||||
"plugin_test.cc",
|
||||
],
|
||||
copts = ["-DNULL_PLUGIN"],
|
||||
deps = [
|
||||
":custom_response_lib",
|
||||
"@com_google_googletest//:gtest",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
"@proxy_wasm_cpp_host//:lib",
|
||||
],
|
||||
)
|
||||
|
||||
declare_wasm_image_targets(
|
||||
name = "custom_response",
|
||||
wasm_file = ":custom_response.wasm",
|
||||
)
|
||||
194
plugins/wasm-cpp/extensions/custom_response/plugin.cc
Normal file
194
plugins/wasm-cpp/extensions/custom_response/plugin.cc
Normal file
@@ -0,0 +1,194 @@
|
||||
// Copyright (c) 2022 Alibaba Group Holding Ltd.
|
||||
//
|
||||
// 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 "extensions/custom_response/plugin.h"
|
||||
|
||||
#include <array>
|
||||
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "absl/strings/str_split.h"
|
||||
#include "common/json_util.h"
|
||||
|
||||
using ::nlohmann::json;
|
||||
using ::Wasm::Common::JsonArrayIterate;
|
||||
using ::Wasm::Common::JsonGetField;
|
||||
using ::Wasm::Common::JsonObjectIterate;
|
||||
using ::Wasm::Common::JsonValueAs;
|
||||
|
||||
#ifdef NULL_PLUGIN
|
||||
|
||||
namespace proxy_wasm {
|
||||
namespace null_plugin {
|
||||
namespace custom_response {
|
||||
|
||||
PROXY_WASM_NULL_PLUGIN_REGISTRY
|
||||
|
||||
#endif
|
||||
|
||||
static RegisterContextFactory register_CustomResponse(
|
||||
CONTEXT_FACTORY(PluginContext), ROOT_FACTORY(PluginRootContext));
|
||||
|
||||
bool PluginRootContext::parsePluginConfig(const json& configuration,
|
||||
CustomResponseConfigRule& rule) {
|
||||
if (!JsonArrayIterate(
|
||||
configuration, "enable_on_status", [&](const json& item) -> bool {
|
||||
auto status = JsonValueAs<int64_t>(item);
|
||||
if (status.second != Wasm::Common::JsonParserResultDetail::OK) {
|
||||
LOG_WARN("cannot parse enable_on_status");
|
||||
return false;
|
||||
}
|
||||
rule.enable_on_status.push_back(
|
||||
std::to_string(status.first.value()));
|
||||
return true;
|
||||
})) {
|
||||
LOG_WARN("failed to parse configuration for enable_on_status.");
|
||||
return false;
|
||||
}
|
||||
bool has_content_type = false;
|
||||
if (!JsonArrayIterate(
|
||||
configuration, "headers", [&](const json& item) -> bool {
|
||||
auto header = JsonValueAs<std::string>(item);
|
||||
if (header.second != Wasm::Common::JsonParserResultDetail::OK) {
|
||||
LOG_WARN("cannot parse header");
|
||||
return false;
|
||||
}
|
||||
std::vector<std::string> pair =
|
||||
absl::StrSplit(header.first.value(), absl::MaxSplits("=", 2));
|
||||
if (pair.size() != 2) {
|
||||
LOG_WARN("invalid header pair format");
|
||||
}
|
||||
if (absl::AsciiStrToLower(pair[0]) ==
|
||||
Wasm::Common::Http::Header::ContentLength) {
|
||||
return true;
|
||||
}
|
||||
if (absl::AsciiStrToLower(pair[0]) ==
|
||||
Wasm::Common::Http::Header::ContentType) {
|
||||
rule.content_type = pair[1];
|
||||
has_content_type = true;
|
||||
return true;
|
||||
}
|
||||
rule.headers.emplace_back(pair[0], pair[1]);
|
||||
return true;
|
||||
})) {
|
||||
LOG_WARN("failed to parse configuration for headers.");
|
||||
return false;
|
||||
}
|
||||
auto it = configuration.find("status_code");
|
||||
if (it != configuration.end()) {
|
||||
auto status_code = JsonValueAs<int64_t>(it.value());
|
||||
if (status_code.second != Wasm::Common::JsonParserResultDetail::OK) {
|
||||
LOG_WARN("cannot parse status code");
|
||||
return false;
|
||||
}
|
||||
rule.status_code = status_code.first.value();
|
||||
}
|
||||
it = configuration.find("body");
|
||||
if (it != configuration.end()) {
|
||||
auto body_string = JsonValueAs<std::string>(it.value());
|
||||
if (body_string.second != Wasm::Common::JsonParserResultDetail::OK) {
|
||||
LOG_WARN("cannot parse body");
|
||||
return false;
|
||||
}
|
||||
rule.body = body_string.first.value();
|
||||
}
|
||||
if (!rule.body.empty() && !has_content_type) {
|
||||
auto try_decode_json = Wasm::Common::JsonParse(rule.body);
|
||||
if (try_decode_json.has_value()) {
|
||||
rule.content_type = "application/json; charset=utf-8";
|
||||
// rule.headers.emplace_back(Wasm::Common::Http::Header::ContentType,
|
||||
// "application/json; charset=utf-8");
|
||||
} else {
|
||||
rule.content_type = "text/plain; charset=utf-8";
|
||||
// rule.headers.emplace_back(Wasm::Common::Http::Header::ContentType,
|
||||
// "text/plain; charset=utf-8");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
FilterHeadersStatus PluginRootContext::onRequest(
|
||||
const CustomResponseConfigRule& rule) {
|
||||
if (!rule.enable_on_status.empty()) {
|
||||
return FilterHeadersStatus::Continue;
|
||||
}
|
||||
sendLocalResponse(rule.status_code, "", rule.body, rule.headers);
|
||||
return FilterHeadersStatus::StopIteration;
|
||||
}
|
||||
|
||||
FilterHeadersStatus PluginRootContext::onResponse(
|
||||
const CustomResponseConfigRule& rule) {
|
||||
GET_RESPONSE_HEADER_VIEW(":status", status_code);
|
||||
bool hit = false;
|
||||
for (const auto& status : rule.enable_on_status) {
|
||||
if (status_code == status) {
|
||||
hit = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!hit) {
|
||||
return FilterHeadersStatus::Continue;
|
||||
}
|
||||
replaceResponseHeader(Wasm::Common::Http::Header::ContentType,
|
||||
rule.content_type);
|
||||
sendLocalResponse(rule.status_code, "", rule.body, rule.headers);
|
||||
return FilterHeadersStatus::StopIteration;
|
||||
}
|
||||
|
||||
bool PluginRootContext::onConfigure(size_t size) {
|
||||
// Parse configuration JSON string.
|
||||
if (size > 0 && !configure(size)) {
|
||||
LOG_WARN("configuration has errors initialization will not continue.");
|
||||
setInvalidConfig();
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PluginRootContext::configure(size_t configuration_size) {
|
||||
auto configuration_data = getBufferBytes(WasmBufferType::PluginConfiguration,
|
||||
0, configuration_size);
|
||||
// Parse configuration JSON string.
|
||||
auto result = ::Wasm::Common::JsonParse(configuration_data->view());
|
||||
if (!result.has_value()) {
|
||||
LOG_WARN(absl::StrCat("cannot parse plugin configuration JSON string: ",
|
||||
configuration_data->view()));
|
||||
return false;
|
||||
}
|
||||
if (!parseRuleConfig(result.value())) {
|
||||
LOG_WARN(absl::StrCat("cannot parse plugin configuration JSON string: ",
|
||||
configuration_data->view()));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
FilterHeadersStatus PluginContext::onRequestHeaders(uint32_t, bool) {
|
||||
auto* rootCtx = rootContext();
|
||||
return rootCtx->onHeaders(
|
||||
[rootCtx](const auto& config) { return rootCtx->onRequest(config); });
|
||||
}
|
||||
|
||||
FilterHeadersStatus PluginContext::onResponseHeaders(uint32_t, bool) {
|
||||
auto* rootCtx = rootContext();
|
||||
return rootCtx->onHeaders(
|
||||
[rootCtx](const auto& config) { return rootCtx->onResponse(config); });
|
||||
}
|
||||
|
||||
#ifdef NULL_PLUGIN
|
||||
|
||||
} // namespace custom_response
|
||||
} // namespace null_plugin
|
||||
} // namespace proxy_wasm
|
||||
|
||||
#endif
|
||||
85
plugins/wasm-cpp/extensions/custom_response/plugin.h
Normal file
85
plugins/wasm-cpp/extensions/custom_response/plugin.h
Normal file
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright (c) 2022 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* 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 <assert.h>
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "common/http_util.h"
|
||||
#include "common/route_rule_matcher.h"
|
||||
#define ASSERT(_X) assert(_X)
|
||||
|
||||
#ifndef NULL_PLUGIN
|
||||
|
||||
#include "proxy_wasm_intrinsics.h"
|
||||
|
||||
#else
|
||||
|
||||
#include "include/proxy-wasm/null_plugin.h"
|
||||
|
||||
namespace proxy_wasm {
|
||||
namespace null_plugin {
|
||||
namespace custom_response {
|
||||
|
||||
#endif
|
||||
|
||||
struct CustomResponseConfigRule {
|
||||
std::vector<std::string> enable_on_status;
|
||||
std::vector<std::pair<std::string, std::string>> headers;
|
||||
std::string content_type;
|
||||
int32_t status_code = 200;
|
||||
std::string body;
|
||||
};
|
||||
|
||||
// PluginRootContext is the root context for all streams processed by the
|
||||
// thread. It has the same lifetime as the worker thread and acts as target for
|
||||
// interactions that outlives individual stream, e.g. timer, async calls.
|
||||
class PluginRootContext : public RootContext,
|
||||
public RouteRuleMatcher<CustomResponseConfigRule> {
|
||||
public:
|
||||
PluginRootContext(uint32_t id, std::string_view root_id)
|
||||
: RootContext(id, root_id) {}
|
||||
~PluginRootContext() {}
|
||||
bool onConfigure(size_t) override;
|
||||
FilterHeadersStatus onRequest(const CustomResponseConfigRule&);
|
||||
FilterHeadersStatus onResponse(const CustomResponseConfigRule&);
|
||||
bool configure(size_t);
|
||||
|
||||
private:
|
||||
bool parsePluginConfig(const json&, CustomResponseConfigRule&) override;
|
||||
};
|
||||
|
||||
// Per-stream context.
|
||||
class PluginContext : public Context {
|
||||
public:
|
||||
explicit PluginContext(uint32_t id, RootContext* root) : Context(id, root) {}
|
||||
FilterHeadersStatus onRequestHeaders(uint32_t, bool) override;
|
||||
FilterHeadersStatus onResponseHeaders(uint32_t, bool) override;
|
||||
|
||||
private:
|
||||
inline PluginRootContext* rootContext() {
|
||||
return dynamic_cast<PluginRootContext*>(this->root());
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef NULL_PLUGIN
|
||||
|
||||
} // namespace custom_response
|
||||
} // namespace null_plugin
|
||||
} // namespace proxy_wasm
|
||||
|
||||
#endif
|
||||
186
plugins/wasm-cpp/extensions/custom_response/plugin_test.cc
Normal file
186
plugins/wasm-cpp/extensions/custom_response/plugin_test.cc
Normal file
@@ -0,0 +1,186 @@
|
||||
// Copyright (c) 2022 Alibaba Group Holding Ltd.
|
||||
//
|
||||
// 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 "extensions/custom_response/plugin.h"
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "include/proxy-wasm/context.h"
|
||||
#include "include/proxy-wasm/null.h"
|
||||
|
||||
namespace proxy_wasm {
|
||||
namespace null_plugin {
|
||||
namespace custom_response {
|
||||
|
||||
NullPluginRegistry* context_registry_;
|
||||
RegisterNullVmPluginFactory register_custom_response_plugin(
|
||||
"custom_response", []() {
|
||||
return std::make_unique<NullPlugin>(custom_response::context_registry_);
|
||||
});
|
||||
|
||||
class MockContext : public proxy_wasm::ContextBase {
|
||||
public:
|
||||
MockContext(WasmBase* wasm) : ContextBase(wasm) {}
|
||||
|
||||
MOCK_METHOD(BufferInterface*, getBuffer, (WasmBufferType));
|
||||
MOCK_METHOD(WasmResult, log, (uint32_t, std::string_view));
|
||||
MOCK_METHOD(WasmResult, getHeaderMapValue,
|
||||
(WasmHeaderMapType /* type */, std::string_view /* key */,
|
||||
std::string_view* /*result */));
|
||||
MOCK_METHOD(WasmResult, replaceHeaderMapValue,
|
||||
(WasmHeaderMapType /* type */, std::string_view /* key */,
|
||||
std::string_view /* value */));
|
||||
MOCK_METHOD(WasmResult, sendLocalResponse,
|
||||
(uint32_t /* response_code */, std::string_view /* body */,
|
||||
Pairs /* additional_headers */, uint32_t /* grpc_status */,
|
||||
std::string_view /* details */));
|
||||
MOCK_METHOD(WasmResult, getProperty, (std::string_view, std::string*));
|
||||
};
|
||||
|
||||
class CustomResponseTest : public ::testing::Test {
|
||||
protected:
|
||||
CustomResponseTest() {
|
||||
// Initialize test VM
|
||||
test_vm_ = createNullVm();
|
||||
wasm_base_ = std::make_unique<WasmBase>(
|
||||
std::move(test_vm_), "test-vm", "", "",
|
||||
std::unordered_map<std::string, std::string>{},
|
||||
AllowedCapabilitiesMap{});
|
||||
wasm_base_->load("custom_response");
|
||||
wasm_base_->initialize();
|
||||
|
||||
// Initialize host side context
|
||||
mock_context_ = std::make_unique<MockContext>(wasm_base_.get());
|
||||
current_context_ = mock_context_.get();
|
||||
|
||||
ON_CALL(*mock_context_, log(testing::_, testing::_))
|
||||
.WillByDefault([](uint32_t, std::string_view m) {
|
||||
std::cerr << m << "\n";
|
||||
return WasmResult::Ok;
|
||||
});
|
||||
|
||||
ON_CALL(*mock_context_, getHeaderMapValue(WasmHeaderMapType::RequestHeaders,
|
||||
testing::_, testing::_))
|
||||
.WillByDefault([&](WasmHeaderMapType, std::string_view header,
|
||||
std::string_view* result) {
|
||||
if (header == ":authority") {
|
||||
*result = authority_;
|
||||
}
|
||||
return WasmResult::Ok;
|
||||
});
|
||||
|
||||
ON_CALL(*mock_context_,
|
||||
replaceHeaderMapValue(WasmHeaderMapType::RequestHeaders, testing::_,
|
||||
testing::_))
|
||||
.WillByDefault([&](WasmHeaderMapType, std::string_view key,
|
||||
std::string_view value) { return WasmResult::Ok; });
|
||||
|
||||
ON_CALL(*mock_context_,
|
||||
getHeaderMapValue(WasmHeaderMapType::ResponseHeaders, testing::_,
|
||||
testing::_))
|
||||
.WillByDefault([&](WasmHeaderMapType, std::string_view header,
|
||||
std::string_view* result) {
|
||||
if (header == ":status") {
|
||||
*result = status_code_;
|
||||
}
|
||||
return WasmResult::Ok;
|
||||
});
|
||||
|
||||
ON_CALL(*mock_context_, getProperty(testing::_, testing::_))
|
||||
.WillByDefault([&](std::string_view path, std::string* result) {
|
||||
*result = route_name_;
|
||||
return WasmResult::Ok;
|
||||
});
|
||||
|
||||
// Initialize Wasm sandbox context
|
||||
root_context_ = std::make_unique<PluginRootContext>(0, "");
|
||||
context_ = std::make_unique<PluginContext>(1, root_context_.get());
|
||||
}
|
||||
~CustomResponseTest() override {}
|
||||
|
||||
std::unique_ptr<WasmBase> wasm_base_;
|
||||
std::unique_ptr<WasmVm> test_vm_;
|
||||
std::unique_ptr<MockContext> mock_context_;
|
||||
|
||||
std::unique_ptr<PluginRootContext> root_context_;
|
||||
std::unique_ptr<PluginContext> context_;
|
||||
|
||||
std::string authority_;
|
||||
std::string route_name_;
|
||||
std::string status_code_;
|
||||
};
|
||||
|
||||
TEST_F(CustomResponseTest, EnableOnStatus) {
|
||||
std::string configuration = R"(
|
||||
{
|
||||
"enable_on_status": [429],
|
||||
"headers": ["abc=123","zty=test"],
|
||||
"status_code": 233,
|
||||
"body": "{\"abc\":123}"
|
||||
})";
|
||||
|
||||
BufferBase buffer;
|
||||
buffer.set({configuration.data(), configuration.size()});
|
||||
|
||||
EXPECT_CALL(*mock_context_, getBuffer(WasmBufferType::PluginConfiguration))
|
||||
.WillOnce([&buffer](WasmBufferType) { return &buffer; });
|
||||
EXPECT_TRUE(root_context_->configure(configuration.size()));
|
||||
|
||||
status_code_ = "200";
|
||||
EXPECT_EQ(context_->onRequestHeaders(0, false),
|
||||
FilterHeadersStatus::Continue);
|
||||
EXPECT_EQ(context_->onResponseHeaders(0, false),
|
||||
FilterHeadersStatus::Continue);
|
||||
|
||||
status_code_ = "429";
|
||||
EXPECT_EQ(context_->onRequestHeaders(0, false),
|
||||
FilterHeadersStatus::Continue);
|
||||
EXPECT_CALL(*mock_context_, sendLocalResponse(233, testing::_, testing::_,
|
||||
testing::_, testing::_));
|
||||
EXPECT_EQ(context_->onResponseHeaders(0, false),
|
||||
FilterHeadersStatus::StopIteration);
|
||||
}
|
||||
|
||||
TEST_F(CustomResponseTest, NoGlobalRule) {
|
||||
std::string configuration = R"(
|
||||
{
|
||||
"_rules_": [{
|
||||
"_match_route_": ["test"],
|
||||
"headers": ["abc=123","zty=test"],
|
||||
"status_code": 233,
|
||||
"body": "{\"abc\":123}"
|
||||
}]
|
||||
})";
|
||||
|
||||
BufferBase buffer;
|
||||
buffer.set({configuration.data(), configuration.size()});
|
||||
|
||||
EXPECT_CALL(*mock_context_, getBuffer(WasmBufferType::PluginConfiguration))
|
||||
.WillOnce([&buffer](WasmBufferType) { return &buffer; });
|
||||
EXPECT_TRUE(root_context_->configure(configuration.size()));
|
||||
|
||||
EXPECT_EQ(context_->onRequestHeaders(0, false),
|
||||
FilterHeadersStatus::Continue);
|
||||
EXPECT_EQ(context_->onResponseHeaders(0, false),
|
||||
FilterHeadersStatus::Continue);
|
||||
|
||||
route_name_ = "test";
|
||||
EXPECT_CALL(*mock_context_, sendLocalResponse(233, testing::_, testing::_,
|
||||
testing::_, testing::_));
|
||||
EXPECT_EQ(context_->onRequestHeaders(0, false),
|
||||
FilterHeadersStatus::StopIteration);
|
||||
}
|
||||
} // namespace custom_response
|
||||
} // namespace null_plugin
|
||||
} // namespace proxy_wasm
|
||||
Reference in New Issue
Block a user