From 34116e204cd1e33193ed04da48255748d9b02777 Mon Sep 17 00:00:00 2001 From: jiangshaohua Date: Sun, 30 Oct 2022 16:44:16 +0800 Subject: [PATCH 1/8] remove supportCORSRequest and support both CORS pre-flight and CORS requests by default --- doc/reference/filters.md | 1 - pkg/filters/corsadaptor/corsadaptor.go | 6 ------ pkg/filters/corsadaptor/corsadaptor_test.go | 1 - 3 files changed, 8 deletions(-) diff --git a/doc/reference/filters.md b/doc/reference/filters.md index 1f5eecb75c..d22142960e 100644 --- a/doc/reference/filters.md +++ b/doc/reference/filters.md @@ -262,7 +262,6 @@ allowedMethods: [GET] | allowCredentials | bool | Indicates whether the request can include user credentials like cookies, HTTP authentication, or client-side SSL certificates | No | | exposedHeaders | []string | Indicates which headers are safe to expose to the API of a CORS API specification | No | | maxAge | int | Indicates how long (in seconds) the results of a preflight request can be cached. The default is 0 stands for no max age | No | -| supportCORSRequest | bool | When true, support CORS request and CORS preflight requests. By default, support only preflight requests. | No | ### Results diff --git a/pkg/filters/corsadaptor/corsadaptor.go b/pkg/filters/corsadaptor/corsadaptor.go index 33e30f818e..1c0e40f661 100644 --- a/pkg/filters/corsadaptor/corsadaptor.go +++ b/pkg/filters/corsadaptor/corsadaptor.go @@ -69,9 +69,6 @@ type ( AllowCredentials bool `json:"allowCredentials" jsonschema:"omitempty"` ExposedHeaders []string `json:"exposedHeaders" jsonschema:"omitempty"` MaxAge int `json:"maxAge" jsonschema:"omitempty"` - // If true, handle requests with 'Origin' header. https://fetch.spec.whatwg.org/#http-requests - // By default, only CORS-preflight requests are handled. - SupportCORSRequest bool `json:"supportCORSRequest" jsonschema:"omitempty"` } ) @@ -122,9 +119,6 @@ func (a *CORSAdaptor) Handle(ctx *context.Context) string { isPreflight := req.HTTPHeader().Get("Access-Control-Request-Method") != "" isPreflight = isPreflight && (req.Method() == http.MethodOptions) - if !a.spec.SupportCORSRequest && !isPreflight { - return "" - } rw := httptest.NewRecorder() a.cors.HandlerFunc(rw, req.Std()) diff --git a/pkg/filters/corsadaptor/corsadaptor_test.go b/pkg/filters/corsadaptor/corsadaptor_test.go index ca60a43e39..f1dab6c270 100644 --- a/pkg/filters/corsadaptor/corsadaptor_test.go +++ b/pkg/filters/corsadaptor/corsadaptor_test.go @@ -85,7 +85,6 @@ name: cors const yamlConfig = ` kind: CORSAdaptor name: cors -supportCORSRequest: true allowedOrigins: - test.orig.test ` From a1830b817115d661757395302e13bdef926ed6c5 Mon Sep 17 00:00:00 2001 From: jiangshaohua Date: Thu, 22 Dec 2022 11:53:07 +0800 Subject: [PATCH 2/8] enhance object delete add -f,-a flag to delete object from a yaml file or delete all object --- cmd/client/command/object.go | 30 ++++++++++++++++++++---------- pkg/api/object.go | 17 +++++++++++++++++ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/cmd/client/command/object.go b/cmd/client/command/object.go index 4586f1ac96..3ffaa36542 100644 --- a/cmd/client/command/object.go +++ b/cmd/client/command/object.go @@ -96,23 +96,33 @@ func updateObjectCmd() *cobra.Command { } func deleteObjectCmd() *cobra.Command { + var specFile string + var allFlag bool cmd := &cobra.Command{ - Use: "delete", - Short: "Delete an object", - Example: "egctl object delete ", - Args: func(cmd *cobra.Command, args []string) error { - if len(args) != 1 { - return errors.New("requires one object name to be deleted") + Use: "delete", + Short: "Delete an object from a yaml file or name", + Run: func(cmd *cobra.Command, args []string) { + + if allFlag { + handleRequest(http.MethodDelete, makeURL(objectsURL), nil, cmd) + return } - return nil - }, + if len(specFile) != 0 { + visitor := buildSpecVisitor(specFile, cmd) + visitor.Visit(func(s *spec) error { + handleRequest(http.MethodDelete, makeURL(objectURL, s.Name), nil, cmd) + return nil + }) + visitor.Close() + return + } - Run: func(cmd *cobra.Command, args []string) { handleRequest(http.MethodDelete, makeURL(objectURL, args[0]), nil, cmd) }, } - + cmd.Flags().StringVarP(&specFile, "file", "f", "", "A yaml file specifying the object.") + cmd.Flags().BoolVarP(&allFlag, "all", "a", false, "Delete all object.") return cmd } diff --git a/pkg/api/object.go b/pkg/api/object.go index 4c4aa8f81d..caa9336acc 100644 --- a/pkg/api/object.go +++ b/pkg/api/object.go @@ -72,6 +72,11 @@ func (s *Server) objectAPIEntries() []*Entry { Method: "DELETE", Handler: s.deleteObject, }, + { + Path: ObjectPrefix, + Method: "DELETE", + Handler: s.deleteAllObject, + }, { Path: StatusObjectPrefix, Method: "GET", @@ -151,6 +156,18 @@ func (s *Server) deleteObject(w http.ResponseWriter, r *http.Request) { s.upgradeConfigVersion(w, r) } +func (s *Server) deleteAllObject(w http.ResponseWriter, r *http.Request) { + s.Lock() + defer s.Unlock() + + specs := s._listObjects() + for _, spec := range specs { + s._deleteObject(spec.Name()) + } + + s.upgradeConfigVersion(w, r) +} + func (s *Server) getObject(w http.ResponseWriter, r *http.Request) { name := chi.URLParam(r, "name") From 0772ee5eca3353d060142b92b4d0f06309b383ac Mon Sep 17 00:00:00 2001 From: codeRabbit <61609783+coderabbit214@users.noreply.github.com> Date: Fri, 23 Dec 2022 09:21:53 +0800 Subject: [PATCH 3/8] Update cmd/client/command/object.go don't use the short version to help users avoid faulty operation. Co-authored-by: Bomin Zhang --- cmd/client/command/object.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/client/command/object.go b/cmd/client/command/object.go index 3ffaa36542..31c04f2862 100644 --- a/cmd/client/command/object.go +++ b/cmd/client/command/object.go @@ -122,7 +122,7 @@ func deleteObjectCmd() *cobra.Command { }, } cmd.Flags().StringVarP(&specFile, "file", "f", "", "A yaml file specifying the object.") - cmd.Flags().BoolVarP(&allFlag, "all", "a", false, "Delete all object.") + cmd.Flags().BoolVarP(&allFlag, "all", "", false, "Delete all object.") return cmd } From c3b106a77258b4d72cb419232352a05db7fd71c4 Mon Sep 17 00:00:00 2001 From: jiangshaohua Date: Fri, 23 Dec 2022 09:47:00 +0800 Subject: [PATCH 4/8] enhance object delete add parameter check --- cmd/client/command/object.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/cmd/client/command/object.go b/cmd/client/command/object.go index 31c04f2862..cecb99448d 100644 --- a/cmd/client/command/object.go +++ b/cmd/client/command/object.go @@ -101,6 +101,23 @@ func deleteObjectCmd() *cobra.Command { cmd := &cobra.Command{ Use: "delete", Short: "Delete an object from a yaml file or name", + Args: func(cmd *cobra.Command, args []string) error { + + if allFlag { + if len(specFile) != 0 { + return errors.New("--all and --file cannot be used together") + } + if len(args) != 0 { + return errors.New("--all and cannot be used together") + } + } + + if len(args) != 0 && len(specFile) != 0 { + return errors.New("--file and cannot be used together") + } + + return nil + }, Run: func(cmd *cobra.Command, args []string) { if allFlag { From 7b3f6c33c2b38d8eb40f13c85e4015054ffc8739 Mon Sep 17 00:00:00 2001 From: jiangshaohua Date: Fri, 23 Dec 2022 10:41:43 +0800 Subject: [PATCH 5/8] enhance object delete update delete all,avoid misusing curl --- cmd/client/command/object.go | 3 ++- pkg/api/object.go | 20 ++++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/cmd/client/command/object.go b/cmd/client/command/object.go index cecb99448d..f8b51a2441 100644 --- a/cmd/client/command/object.go +++ b/cmd/client/command/object.go @@ -19,6 +19,7 @@ package command import ( "errors" + "fmt" "net/http" "github.com/spf13/cobra" @@ -121,7 +122,7 @@ func deleteObjectCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { if allFlag { - handleRequest(http.MethodDelete, makeURL(objectsURL), nil, cmd) + handleRequest(http.MethodDelete, makeURL(objectsURL+fmt.Sprintf("?all=%v", true)), nil, cmd) return } diff --git a/pkg/api/object.go b/pkg/api/object.go index caa9336acc..4446d3e142 100644 --- a/pkg/api/object.go +++ b/pkg/api/object.go @@ -75,7 +75,7 @@ func (s *Server) objectAPIEntries() []*Entry { { Path: ObjectPrefix, Method: "DELETE", - Handler: s.deleteAllObject, + Handler: s.deleteObjects, }, { Path: StatusObjectPrefix, @@ -156,16 +156,20 @@ func (s *Server) deleteObject(w http.ResponseWriter, r *http.Request) { s.upgradeConfigVersion(w, r) } -func (s *Server) deleteAllObject(w http.ResponseWriter, r *http.Request) { - s.Lock() - defer s.Unlock() +func (s *Server) deleteObjects(w http.ResponseWriter, r *http.Request) { + allFlag := r.URL.Query().Get("all") + if allFlag == "true" { + s.Lock() + defer s.Unlock() - specs := s._listObjects() - for _, spec := range specs { - s._deleteObject(spec.Name()) + specs := s._listObjects() + for _, spec := range specs { + s._deleteObject(spec.Name()) + } + + s.upgradeConfigVersion(w, r) } - s.upgradeConfigVersion(w, r) } func (s *Server) getObject(w http.ResponseWriter, r *http.Request) { From 4fa8fcdd3782ec4df496a08965dd2934037d12ca Mon Sep 17 00:00:00 2001 From: codeRabbit <61609783+coderabbit214@users.noreply.github.com> Date: Fri, 23 Dec 2022 16:13:52 +0800 Subject: [PATCH 6/8] Delete blank line Co-authored-by: Bomin Zhang --- pkg/api/object.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/api/object.go b/pkg/api/object.go index 4446d3e142..33193252e0 100644 --- a/pkg/api/object.go +++ b/pkg/api/object.go @@ -169,7 +169,6 @@ func (s *Server) deleteObjects(w http.ResponseWriter, r *http.Request) { s.upgradeConfigVersion(w, r) } - } func (s *Server) getObject(w http.ResponseWriter, r *http.Request) { From 383bcb3a474bddab33d9ca40959854ed65cfd2c1 Mon Sep 17 00:00:00 2001 From: codeRabbit <61609783+coderabbit214@users.noreply.github.com> Date: Fri, 23 Dec 2022 16:14:00 +0800 Subject: [PATCH 7/8] Delete blank line Co-authored-by: Bomin Zhang --- cmd/client/command/object.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/client/command/object.go b/cmd/client/command/object.go index f8b51a2441..f8cf105aba 100644 --- a/cmd/client/command/object.go +++ b/cmd/client/command/object.go @@ -120,7 +120,6 @@ func deleteObjectCmd() *cobra.Command { return nil }, Run: func(cmd *cobra.Command, args []string) { - if allFlag { handleRequest(http.MethodDelete, makeURL(objectsURL+fmt.Sprintf("?all=%v", true)), nil, cmd) return From 5e3b4be5bbf2d9829c6659c1628562c45a1eb943 Mon Sep 17 00:00:00 2001 From: codeRabbit <61609783+coderabbit214@users.noreply.github.com> Date: Fri, 23 Dec 2022 16:14:08 +0800 Subject: [PATCH 8/8] Delete blank line Co-authored-by: Bomin Zhang --- cmd/client/command/object.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/client/command/object.go b/cmd/client/command/object.go index f8cf105aba..ba35dcbaf9 100644 --- a/cmd/client/command/object.go +++ b/cmd/client/command/object.go @@ -103,7 +103,6 @@ func deleteObjectCmd() *cobra.Command { Use: "delete", Short: "Delete an object from a yaml file or name", Args: func(cmd *cobra.Command, args []string) error { - if allFlag { if len(specFile) != 0 { return errors.New("--all and --file cannot be used together")