You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2022/11/25 04:46:14 UTC

[GitHub] [apisix-ingress-controller] tao12345666333 commented on a diff in pull request #1471: feat: add control http method using kubernetes ingress by annotations

tao12345666333 commented on code in PR #1471:
URL: https://github.com/apache/apisix-ingress-controller/pull/1471#discussion_r1031998345


##########
pkg/providers/ingress/translation/annotations/plugins/http_method.go:
##########
@@ -0,0 +1,83 @@
+// 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 (
+	"github.com/apache/apisix-ingress-controller/pkg/providers/ingress/translation/annotations"
+	apisixv1 "github.com/apache/apisix-ingress-controller/pkg/types/apisix/v1"
+)
+
+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.AnnotationsHttpAllowMethod)
+	blockMethods := e.GetStringsAnnotation(annotations.AnnotationsHttpBlockMethod)
+
+	plugin.StatusCode = 405
+	var methodExpr = []apisixv1.Expr{}
+
+	if len(allowMethods) > 0 {
+		//plugin.LuaRestyExpr = []interface{}{
+		//	[]interface{}{
+		//		"request_method", "!", "in", allowMethods,
+		//	},
+		//}
+		for _, method := range allowMethods {
+			methodExpr = append(methodExpr, apisixv1.Expr{StringVal: method})
+		}
+
+		plugin.LuaRestyExpr = []apisixv1.Expr{
+			{
+				ArrayVal: []apisixv1.Expr{
+					{StringVal: "request_method"},
+					{StringVal: "!"},
+					{StringVal: "in"},
+					{ArrayVal: methodExpr},
+				},
+			},
+		}
+
+	} else if len(blockMethods) > 0 {

Review Comment:
   This means that the two annotations are mutually exclusive. We should make it clear in the docs, thanks



##########
pkg/providers/ingress/translation/annotations/types.go:
##########
@@ -62,6 +62,10 @@ const (
 	AnnotationsAllowlistSourceRange = AnnotationsPrefix + "allowlist-source-range"
 	AnnotationsBlocklistSourceRange = AnnotationsPrefix + "blocklist-source-range"
 
+	// http-method plugin
+	AnnotationsHttpAllowMethod = AnnotationsPrefix + "http-allow-method"
+	AnnotationsHttpBlockMethod = AnnotationsPrefix + "http-block-method"

Review Comment:
   We should use plurals, right?
   
   `http-allow-methods` and `http-block-methods`



##########
docs/en/latest/concepts/annotations.md:
##########
@@ -99,6 +99,28 @@ k8s.apisix.apache.org/blocklist-source-range: "127.0.0.1,172.17.0.0/16"
 
 The default value is empty which means no IP addresses are blocked.
 
+## Allow http method
+
+This annotation can be used to specify which http method are allowed. Multiple methods can also be specified by separating them with commas.
+
+```yaml
+k8s.apisix.apache.org/http-allow-method: "GET,POST"
+```
+
+The default value is empty which means all methods are allowed.

Review Comment:
   I think it needs to be explained here that once this configuration is added, **it means that except for the allowed methods, the rest of the methods are not allowed**



##########
pkg/providers/ingress/translation/annotations/plugins/http_method.go:
##########
@@ -0,0 +1,83 @@
+// 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 (
+	"github.com/apache/apisix-ingress-controller/pkg/providers/ingress/translation/annotations"
+	apisixv1 "github.com/apache/apisix-ingress-controller/pkg/types/apisix/v1"
+)
+
+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.AnnotationsHttpAllowMethod)
+	blockMethods := e.GetStringsAnnotation(annotations.AnnotationsHttpBlockMethod)
+
+	plugin.StatusCode = 405
+	var methodExpr = []apisixv1.Expr{}
+
+	if len(allowMethods) > 0 {
+		//plugin.LuaRestyExpr = []interface{}{

Review Comment:
   +1



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org