feat: Add request and response transformer wasm plugin (#549)

This commit is contained in:
WeixinX
2023-10-07 15:47:20 +08:00
committed by GitHub
parent be8563765e
commit 1ded5322a5
12 changed files with 2402 additions and 4 deletions

View File

@@ -0,0 +1,121 @@
// 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.
package tests
import (
"testing"
"github.com/alibaba/higress/test/e2e/conformance/utils/http"
"github.com/alibaba/higress/test/e2e/conformance/utils/suite"
)
func init() {
Register(WasmPluginsTransformer)
}
// TODO(WeixinX): Request and response body conformance check is not supported now
var WasmPluginsTransformer = suite.ConformanceTest{
ShortName: "WasmPluginTransformer",
Description: "The Ingress in the higress-conformance-infra namespace test the transformer WASM plugin.",
Features: []suite.SupportedFeature{suite.WASMGoConformanceFeature},
Manifests: []string{"tests/go-wasm-transformer.yaml"},
Test: func(t *testing.T, suite *suite.ConformanceTestSuite) {
testcases := []http.Assertion{
{
Meta: http.AssertionMeta{
TestCaseName: "case 1: request transformer",
TargetBackend: "infra-backend-v1",
TargetNamespace: "higress-conformance-infra",
},
Request: http.AssertionRequest{
ActualRequest: http.Request{
Host: "foo1.com",
Path: "/get?k1=v11&k1=v12&k2=v2",
Headers: map[string]string{
"X-remove": "exist",
"X-not-renamed": "test",
"X-replace": "not-replaced",
"X-dedupe-first": "1,2,3",
"X-dedupe-last": "a,b,c",
"X-dedupe-unique": "1,2,3,3,2,1",
},
},
ExpectedRequest: &http.ExpectedRequest{
Request: http.Request{
Host: "foo1.com",
Path: "/get?k2-new=v2-new&k3=v31&k3=v32&k4=v31", // url.Value.Encode() is ordered by key
Headers: map[string]string{
"X-renamed": "test",
"X-replace": "replaced",
"X-add-append": "add,append", // header with same name
"X-map": "add,append",
"X-dedupe-first": "1",
"X-dedupe-last": "c",
"X-dedupe-unique": "1,2,3",
},
},
AbsentHeaders: []string{"X-remove"},
},
},
Response: http.AssertionResponse{
ExpectedResponse: http.Response{
StatusCode: 200,
},
},
},
{
Meta: http.AssertionMeta{
TestCaseName: "case 2: response transformer",
TargetBackend: "infra-backend-v1",
TargetNamespace: "higress-conformance-infra",
},
Request: http.AssertionRequest{
ActualRequest: http.Request{
Host: "foo2.com",
Path: "/get/index.html",
},
ExpectedRequest: &http.ExpectedRequest{
Request: http.Request{
Host: "foo2.com",
Path: "/get/index.html",
},
},
},
Response: http.AssertionResponse{
AdditionalResponseHeaders: map[string]string{
"X-remove": "exist",
"X-not-renamed": "test",
"X-replace": "not-replaced",
},
ExpectedResponse: http.Response{
StatusCode: 200,
Headers: map[string]string{
"X-renamed": "test",
"X-replace": "replace-get", // regexp matches path and replace "replace-$1"
"X-add-append": "add-foo2,append-index", // regexp matches host and replace "add-$1"
"X-map": "add-foo2,append-index",
},
AbsentHeaders: []string{"X-remove"},
},
},
},
}
t.Run("WasmPlugin transformer", func(t *testing.T) {
for _, testcase := range testcases {
http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, suite.GatewayAddress, testcase)
}
})
},
}

View File

@@ -0,0 +1,155 @@
# 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.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
name: wasmplugin-transform-request
namespace: higress-conformance-infra
spec:
ingressClassName: higress
rules:
- host: "foo1.com"
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: infra-backend-v1
port:
number: 8080
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
name: wasmplugin-transform-response
namespace: higress-conformance-infra
spec:
ingressClassName: higress
rules:
- host: "foo2.com"
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: infra-backend-v1
port:
number: 8080
---
apiVersion: extensions.higress.io/v1alpha1
kind: WasmPlugin
metadata:
name: transformer
namespace: higress-system
spec:
matchRules:
# request transformer
- ingress:
- higress-conformance-infra/wasmplugin-transform-request
configDisable: false
config:
type: request
rules:
- operate: remove
headers:
- key: X-remove
querys:
- key: k1
- operate: rename
headers:
- key: X-not-renamed
value: X-renamed
querys:
- key: k2
value: k2-new
- operate: replace
headers:
- key: X-replace
value: replaced
querys:
- key: k2-new
value: v2-new
- operate: add
headers:
- key: X-add-append
value: add
querys:
- key: k3
value: v31
- operate: append
headers:
- key: X-add-append
value: append
querys:
- key: k3
value: v32
- operate: map
headers:
- key: X-add-append
value: X-map
querys:
- key: k3
value: k4
- operate: dedupe
headers:
- key: X-dedupe-first
value: RETAIN_FIRST
- key: X-dedupe-last
value: RETAIN_LAST
- key: X-dedupe-unique
value: RETAIN_UNIQUE
querys:
- key: k4
value: RETAIN_FIRST
# response transformer
- ingress:
- higress-conformance-infra/wasmplugin-transform-response
configDisable: false
config:
type: response
rules:
- operate: remove
headers:
- key: X-remove
- operate: rename
headers:
- key: X-not-renamed
value: X-renamed
- operate: replace
headers:
- key: X-replace
value: replace-$1
path_pattern: ^.*?\/(\w+)[\?]{0,1}.*$
- operate: add
headers:
- key: X-add-append
value: add-$1
host_pattern: ^(.*)\.com$
- operate: append
headers:
- key: X-add-append
value: append-$1
path_pattern: ^\/get\/(.*)\.html$
- operate: map
headers:
- key: X-add-append
value: X-map
url: file:///opt/plugins/wasm-go/extensions/transformer/plugin.wasm