-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add control http method using kubernetes ingress by annotations (…
- Loading branch information
1 parent
bccf762
commit 8e39e71
Showing
12 changed files
with
498 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
pkg/providers/ingress/translation/annotations/plugins/http_method.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// 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 ( | ||
"net/http" | ||
|
||
"github.com/apache/apisix-ingress-controller/pkg/providers/ingress/translation/annotations" | ||
apisixv1 "github.com/apache/apisix-ingress-controller/pkg/types/apisix/v1" | ||
|
||
"github.com/incubator4/go-resty-expr/expr" | ||
) | ||
|
||
type HttpMethod struct{} | ||
|
||
// NewHttpHandler creates a handler to convert annotations about | ||
// HttpMethod to APISIX cors plugin. | ||
func NewHttpMethodHandler() PluginAnnotationsHandler { | ||
return &HttpMethod{} | ||
} | ||
|
||
func (h HttpMethod) PluginName() string { | ||
return "response-rewrite" | ||
} | ||
|
||
func (h HttpMethod) Handle(e annotations.Extractor) (interface{}, error) { | ||
var plugin apisixv1.ResponseRewriteConfig | ||
|
||
allowMethods := e.GetStringsAnnotation(annotations.AnnotationsHttpAllowMethods) | ||
blockMethods := e.GetStringsAnnotation(annotations.AnnotationsHttpBlockMethods) | ||
|
||
plugin.StatusCode = http.StatusMethodNotAllowed | ||
|
||
if len(allowMethods) > 0 { | ||
plugin.LuaRestyExpr = []expr.Expr{ | ||
expr.StringExpr("request_method").Not().In( | ||
expr.ArrayExpr(expr.ExprArrayFromStrings(allowMethods)...), | ||
), | ||
} | ||
|
||
} else if len(blockMethods) > 0 { | ||
plugin.LuaRestyExpr = []expr.Expr{ | ||
expr.StringExpr("request_method").In( | ||
expr.ArrayExpr(expr.ExprArrayFromStrings(blockMethods)...), | ||
), | ||
} | ||
|
||
} else { | ||
return nil, nil | ||
} | ||
return &plugin, nil | ||
} |
93 changes: 93 additions & 0 deletions
93
pkg/providers/ingress/translation/annotations/plugins/http_method_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// 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 ( | ||
"net/http" | ||
"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" | ||
|
||
"github.com/incubator4/go-resty-expr/expr" | ||
) | ||
|
||
// annotations: | ||
// | ||
// k8s.apisix.apache.org/allow-http-methods: GET,POST | ||
func TestAnnotationsHttpAllowMethod(t *testing.T) { | ||
anno := map[string]string{ | ||
annotations.AnnotationsHttpAllowMethods: "GET,POST", | ||
} | ||
p := NewHttpMethodHandler() | ||
out, err := p.Handle(annotations.NewExtractor(anno)) | ||
assert.Nil(t, err, "checking given error") | ||
config := out.(*apisixv1.ResponseRewriteConfig) | ||
|
||
assert.Equal(t, http.StatusMethodNotAllowed, config.StatusCode) | ||
assert.Equal(t, []expr.Expr{ | ||
expr.StringExpr("request_method").Not().In(expr.ArrayExpr( | ||
expr.StringExpr("GET"), | ||
expr.StringExpr("POST"), | ||
)), | ||
}, config.LuaRestyExpr) | ||
} | ||
|
||
// annotations: | ||
// | ||
// k8s.apisix.apache.org/block-http-methods: GET,PUT | ||
func TestAnnotationsHttpBlockMethod(t *testing.T) { | ||
anno := map[string]string{ | ||
annotations.AnnotationsHttpBlockMethods: "GET,PUT", | ||
} | ||
p := NewHttpMethodHandler() | ||
out, err := p.Handle(annotations.NewExtractor(anno)) | ||
assert.Nil(t, err, "checking given error") | ||
config := out.(*apisixv1.ResponseRewriteConfig) | ||
|
||
assert.Equal(t, 405, config.StatusCode) | ||
assert.Equal(t, []expr.Expr{ | ||
expr.StringExpr("request_method").In(expr.ArrayExpr( | ||
expr.StringExpr("GET"), | ||
expr.StringExpr("PUT"), | ||
)), | ||
}, config.LuaRestyExpr) | ||
} | ||
|
||
// annotations: | ||
// | ||
// k8s.apisix.apache.org/allow-http-methods: GET | ||
// k8s.apisix.apache.org/block-http-methods: POST,PUT | ||
// | ||
// Only allow methods would be accepted, block methods would be ignored. | ||
func TestAnnotationsHttpBothMethod(t *testing.T) { | ||
anno := map[string]string{ | ||
annotations.AnnotationsHttpAllowMethods: "GET", | ||
annotations.AnnotationsHttpBlockMethods: "POST,PUT", | ||
} | ||
p := NewHttpMethodHandler() | ||
out, err := p.Handle(annotations.NewExtractor(anno)) | ||
assert.Nil(t, err, "checking given error") | ||
config := out.(*apisixv1.ResponseRewriteConfig) | ||
|
||
assert.Equal(t, http.StatusMethodNotAllowed, config.StatusCode) | ||
assert.Equal(t, []expr.Expr{ | ||
expr.StringExpr("request_method").Not().In(expr.ArrayExpr( | ||
expr.StringExpr("GET"), | ||
)), | ||
}, config.LuaRestyExpr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ var ( | |
NewBasicAuthHandler(), | ||
NewKeyAuthHandler(), | ||
NewCSRFHandler(), | ||
NewHttpMethodHandler(), | ||
} | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.