From 612fcf120e2d1f13294403b7f1c38569d1cd24a4 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Wed, 31 Jul 2013 10:10:00 -0700 Subject: [PATCH] check keywords before set/testandset --- client_handlers.go | 14 ++++++++++---- error.go | 3 +++ store/keyword_test.go | 37 +++++++++++++++++++++++++++++++++++++ store/keywords.go | 1 - 4 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 store/keyword_test.go diff --git a/client_handlers.go b/client_handlers.go index 6c1330cc4..25193ab0f 100644 --- a/client_handlers.go +++ b/client_handlers.go @@ -35,6 +35,14 @@ func Multiplexer(w http.ResponseWriter, req *http.Request) { func SetHttpHandler(w *http.ResponseWriter, req *http.Request) { key := req.URL.Path[len("/v1/keys/"):] + if store.CheckKeyword(key) { + + (*w).WriteHeader(http.StatusBadRequest) + + (*w).Write(newJsonError(400, "Set")) + return + } + debug("[recv] POST http://%v/v1/keys/%s", raftServer.Name(), key) value := req.FormValue("value") @@ -57,6 +65,7 @@ func SetHttpHandler(w *http.ResponseWriter, req *http.Request) { (*w).WriteHeader(http.StatusBadRequest) (*w).Write(newJsonError(202, "Set")) + return } if len(prevValue) != 0 { @@ -227,7 +236,7 @@ func GetHttpHandler(w *http.ResponseWriter, req *http.Request) { (*w).WriteHeader(http.StatusInternalServerError) (*w).Write(newJsonError(300, "")) - return + } else { body, ok := body.([]byte) if !ok { @@ -237,7 +246,6 @@ func GetHttpHandler(w *http.ResponseWriter, req *http.Request) { (*w).WriteHeader(http.StatusOK) (*w).Write(body) - return } } @@ -274,7 +282,6 @@ func WatchHttpHandler(w http.ResponseWriter, req *http.Request) { if body, err := command.Apply(raftServer); err != nil { warn("Unable to do watch command: %v", err) w.WriteHeader(http.StatusInternalServerError) - return } else { w.WriteHeader(http.StatusOK) @@ -284,7 +291,6 @@ func WatchHttpHandler(w http.ResponseWriter, req *http.Request) { } w.Write(body) - return } } diff --git a/error.go b/error.go index 666187669..a07060d03 100644 --- a/error.go +++ b/error.go @@ -21,6 +21,9 @@ func init() { // raft related errors errors[300] = "Raft Internal Error" errors[301] = "During Leader Election" + + // keyword + errors[400] = "The prefix of the given key is a keyword in etcd" } type jsonError struct { diff --git a/store/keyword_test.go b/store/keyword_test.go new file mode 100644 index 000000000..064d0f6de --- /dev/null +++ b/store/keyword_test.go @@ -0,0 +1,37 @@ +package store + +import ( + "testing" +) + +func TestKeywords(t *testing.T) { + keyword := CheckKeyword("machines") + if !keyword { + t.Fatal("machines should be keyword") + } + + keyword = CheckKeyword("/machines") + + if !keyword { + t.Fatal("/machines should be keyword") + } + + keyword = CheckKeyword("/machines/") + + if !keyword { + t.Fatal("/machines/ contains keyword prefix") + } + + keyword = CheckKeyword("/machines/node1") + + if !keyword { + t.Fatal("/machines/* contains keyword prefix") + } + + keyword = CheckKeyword("/nokeyword/machines/node1") + + if keyword { + t.Fatal("this does not contain keyword prefix") + } + +} diff --git a/store/keywords.go b/store/keywords.go index 8332d04ea..2923090d2 100644 --- a/store/keywords.go +++ b/store/keywords.go @@ -1,7 +1,6 @@ package store import ( - "fmt" "path" "strings" )