Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Rewrite Header Annotations #2285

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/en/latest/concepts/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,32 @@ spec:
number: 80
```

### Rewrite Headers

### Add header

This annotation configures to append the new headers in the upstream request.

```yaml
k8s.apisix.apache.org/rewrite-add-header: "testkey1:testval1,testkey2:testval2"
```

### Set header

This annotation configures to rewrite the new headers in the upstream request.

```yaml
k8s.apisix.apache.org/rewrite-set-header: "testkey1:testval1,testkey2:testval2"
```

### Remove header

This annotation configures to remove headers in the upstream request.

```yaml
k8s.apisix.apache.org/rewrite-remove-header: "testkey1,testkey2"
```

## HTTP to HTTPS

This annotation is used to redirect HTTP requests to HTTPS with a `301` status code and with the same URI as the original request.
Expand Down
31 changes: 19 additions & 12 deletions pkg/providers/gateway/translation/gateway_httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,28 @@ func (t *translator) generatePluginFromHTTPRequestHeaderFilter(plugins apisixv1.
if reqHeaderModifier == nil {
return
}
headers := map[string]any{}
// TODO: The current apisix plugin does not conform to the specification.
for _, header := range reqHeaderModifier.Add {
headers[string(header.Name)] = header.Value
}
for _, header := range reqHeaderModifier.Set {
headers[string(header.Name)] = header.Value
}
for _, header := range reqHeaderModifier.Remove {
headers[header] = ""
}
plugin := apisixv1.RewriteConfig{}

if len(reqHeaderModifier.Add) > 0 || len(reqHeaderModifier.Set) > 0 || len(reqHeaderModifier.Remove) > 0 {
headers := apisixv1.RewriteConfigHeaders{}

headers.Add = make(map[string]string, len(reqHeaderModifier.Add))
for _, header := range reqHeaderModifier.Add {
headers.Add[string(header.Name)] = header.Value
}

headers.Set = make(map[string]string, len(reqHeaderModifier.Set))
for _, header := range reqHeaderModifier.Set {
headers.Set[string(header.Name)] = header.Value
}

plugins["proxy-rewrite"] = apisixv1.RewriteConfig{
Headers: headers,
headers.Remove = reqHeaderModifier.Remove

plugin.Headers = &headers
}

plugins["proxy-rewrite"] = plugin
}

func (t *translator) generatePluginFromHTTPRequestMirrorFilter(namespace string, plugins apisixv1.Plugins, reqMirror *gatewayv1beta1.HTTPRequestMirrorFilter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,18 @@ func (c *responseRewrite) Handle(e annotations.Extractor) (interface{}, error) {
plugin.StatusCode, _ = strconv.Atoi(e.GetStringAnnotation(annotations.AnnotationsResponseRewriteStatusCode))
plugin.Body = e.GetStringAnnotation(annotations.AnnotationsResponseRewriteBody)
plugin.BodyBase64 = e.GetBoolAnnotation(annotations.AnnotationsResponseRewriteBodyBase64)
headers := make(apisixv1.Headers)
headers.Add(e.GetStringsAnnotation(annotations.AnnotationsResponseRewriteHeaderAdd))
headers.Set(e.GetStringsAnnotation(annotations.AnnotationsResponseRewriteHeaderSet))
headers.Remove(e.GetStringsAnnotation(annotations.AnnotationsResponseRewriteHeaderRemove))
plugin.Headers = headers

headersToAdd := e.GetStringsAnnotation(annotations.AnnotationsResponseRewriteHeaderAdd)
headersToSet := e.GetStringsAnnotation(annotations.AnnotationsResponseRewriteHeaderSet)
headersToRemove := e.GetStringsAnnotation(annotations.AnnotationsResponseRewriteHeaderRemove)

if len(headersToAdd) > 0 || len(headersToSet) > 0 || len(headersToRemove) > 0 {
headers := apisixv1.ResponseRewriteConfigHeaders{}
headers.SetAddHeaders(headersToAdd)
headers.SetSetHeaders(headersToSet)
headers.SetRemoveHeaders(headersToRemove)
plugin.Headers = &headers
}

return &plugin, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@ import (

func TestResponseRewriteHandler(t *testing.T) {
anno := map[string]string{
annotations.AnnotationsEnableResponseRewrite: "true",
annotations.AnnotationsResponseRewriteStatusCode: "200",
annotations.AnnotationsResponseRewriteBody: "bar_body",
annotations.AnnotationsResponseRewriteBodyBase64: "false",
annotations.AnnotationsResponseRewriteHeaderAdd: "testkey1:testval1,testkey2:testval2",
annotations.AnnotationsResponseRewriteHeaderRemove: "testkey1,testkey2",
annotations.AnnotationsResponseRewriteHeaderSet: "testkey1:testval1,testkey2:testval2",
annotations.AnnotationsEnableResponseRewrite: "true",
annotations.AnnotationsResponseRewriteStatusCode: "200",
annotations.AnnotationsResponseRewriteBody: "bar_body",
annotations.AnnotationsResponseRewriteBodyBase64: "false",
}
p := NewResponseRewriteHandler()
out, err := p.Handle(annotations.NewExtractor(anno))
Expand All @@ -41,12 +38,21 @@ func TestResponseRewriteHandler(t *testing.T) {
assert.Equal(t, "bar_body", config.Body)
assert.Equal(t, false, config.BodyBase64)
assert.Equal(t, "response-rewrite", p.PluginName())
assert.Equal(t, []string{"testkey1:testval1", "testkey2:testval2"}, config.Headers.GetAddedHeaders())
assert.Nil(t, config.Headers)

anno[annotations.AnnotationsResponseRewriteHeaderAdd] = "testkey1:testval1,testkey2:testval2"
anno[annotations.AnnotationsResponseRewriteHeaderRemove] = "testkey1,testkey2"
anno[annotations.AnnotationsResponseRewriteHeaderSet] = "testkey1:testval1,testkey2:testval2"
out, err = p.Handle(annotations.NewExtractor(anno))
assert.Nil(t, err, "checking given error")
config = out.(*apisixv1.ResponseRewriteConfig)
assert.Equal(t, []string{"testkey1:testval1", "testkey2:testval2"}, config.Headers.GetAddHeaders())
assert.Equal(t, []string{"testkey1", "testkey2"}, config.Headers.GetRemovedHeaders())
assert.Equal(t, map[string]string{
"testkey1": "testval1",
"testkey2": "testval2",
}, config.Headers.GetSetHeaders())

anno[annotations.AnnotationsEnableResponseRewrite] = "false"
out, err = p.Handle(annotations.NewExtractor(anno))
assert.Nil(t, err, "checking given error")
Expand Down
14 changes: 13 additions & 1 deletion pkg/providers/ingress/translation/annotations/plugins/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ func (i *rewrite) Handle(e annotations.Extractor) (interface{}, error) {
rewriteTarget := e.GetStringAnnotation(annotations.AnnotationsRewriteTarget)
rewriteTargetRegex := e.GetStringAnnotation(annotations.AnnotationsRewriteTargetRegex)
rewriteTemplate := e.GetStringAnnotation(annotations.AnnotationsRewriteTargetRegexTemplate)
if rewriteTarget != "" || rewriteTargetRegex != "" || rewriteTemplate != "" {

headersToAdd := e.GetStringsAnnotation(annotations.AnnotationsRewriteHeaderAdd)
headersToSet := e.GetStringsAnnotation(annotations.AnnotationsRewriteHeaderSet)
headersToRemove := e.GetStringsAnnotation(annotations.AnnotationsRewriteHeaderRemove)

if rewriteTarget != "" || rewriteTargetRegex != "" || rewriteTemplate != "" || len(headersToAdd) > 0 || len(headersToSet) > 0 || len(headersToRemove) > 0 {
plugin.RewriteTarget = rewriteTarget
if rewriteTargetRegex != "" && rewriteTemplate != "" {
_, err := regexp.Compile(rewriteTargetRegex)
Expand All @@ -47,6 +52,13 @@ func (i *rewrite) Handle(e annotations.Extractor) (interface{}, error) {
}
plugin.RewriteTargetRegex = []string{rewriteTargetRegex, rewriteTemplate}
}
if len(headersToAdd) > 0 || len(headersToSet) > 0 || len(headersToRemove) > 0 {
headers := apisixv1.RewriteConfigHeaders{}
headers.SetAddHeaders(headersToAdd)
headers.SetSetHeaders(headersToSet)
headers.SetRemoveHeaders(headersToRemove)
plugin.Headers = &headers
}
return &plugin, nil
}
return nil, nil
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You 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 plugins

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/apache/apisix-ingress-controller/pkg/providers/ingress/translation/annotations"
apisixv1 "github.com/apache/apisix-ingress-controller/pkg/types/apisix/v1"
)

func TestRewriteHandler(t *testing.T) {
anno := map[string]string{
annotations.AnnotationsRewriteTarget: "/sample",
annotations.AnnotationsRewriteTargetRegex: "/sample/(.*)",
annotations.AnnotationsRewriteTargetRegexTemplate: "/$1",
annotations.AnnotationsRewriteHeaderAdd: "testkey1:testval1,testkey2:testval2",
annotations.AnnotationsRewriteHeaderRemove: "testkey1,testkey2",
annotations.AnnotationsRewriteHeaderSet: "testsetkey1:testsetval1,testsetkey2:testsetval2",
}
p := NewRewriteHandler()
out, err := p.Handle(annotations.NewExtractor(anno))
assert.Nil(t, err, "checking given error")
config := out.(*apisixv1.RewriteConfig)
assert.Equal(t, "/sample", config.RewriteTarget)
assert.Equal(t, []string{"/sample/(.*)", "/$1"}, config.RewriteTargetRegex)
assert.Equal(t, "proxy-rewrite", p.PluginName())
assert.Equal(t, map[string]string{
"testkey1": "testval1",
"testkey2": "testval2",
}, config.Headers.GetAddHeaders())
assert.Equal(t, []string{"testkey1", "testkey2"}, config.Headers.GetRemovedHeaders())
assert.Equal(t, map[string]string{
"testsetkey1": "testsetval1",
"testsetkey2": "testsetval2",
}, config.Headers.GetSetHeaders())

anno = map[string]string{
annotations.AnnotationsRewriteTarget: "/sample",
}
out, err = p.Handle(annotations.NewExtractor(anno))
assert.Nil(t, err, "checking given error")
config = out.(*apisixv1.RewriteConfig)
assert.Equal(t, "/sample", config.RewriteTarget)
assert.Nil(t, config.RewriteTargetRegex)
assert.Nil(t, config.Headers)
}
3 changes: 3 additions & 0 deletions pkg/providers/ingress/translation/annotations/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ const (
AnnotationsRewriteTarget = AnnotationsPrefix + "rewrite-target"
AnnotationsRewriteTargetRegex = AnnotationsPrefix + "rewrite-target-regex"
AnnotationsRewriteTargetRegexTemplate = AnnotationsPrefix + "rewrite-target-regex-template"
AnnotationsRewriteHeaderAdd = AnnotationsPrefix + "rewrite-add-header"
AnnotationsRewriteHeaderSet = AnnotationsPrefix + "rewrite-set-header"
AnnotationsRewriteHeaderRemove = AnnotationsPrefix + "rewrite-remove-header"

// response-rewrite plugin
AnnotationsEnableResponseRewrite = AnnotationsPrefix + "enable-response-rewrite"
Expand Down
Loading
Loading