From 6454d7bb9fb8e96c299845327e12b5959047b72d Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Mon, 7 Mar 2016 11:26:54 -0800 Subject: [PATCH] Guard client/ api to ensure the client is running --- command/agent/fs_endpoint.go | 19 +++++++++++++++++++ command/agent/http.go | 4 +--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/command/agent/fs_endpoint.go b/command/agent/fs_endpoint.go index 4a8de569875e..584bf6cc6a56 100644 --- a/command/agent/fs_endpoint.go +++ b/command/agent/fs_endpoint.go @@ -11,8 +11,27 @@ import ( var ( allocIDNotPresentErr = fmt.Errorf("must provide a valid alloc id") fileNameNotPresentErr = fmt.Errorf("must provide a file name") + clientNotRunning = fmt.Errorf("node is not running a Nomad Client") ) +func (s *HTTPServer) FsRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { + if s.agent.client == nil { + return nil, clientNotRunning + } + + path := strings.TrimPrefix(req.URL.Path, "/v1/client/fs/") + switch { + case strings.HasPrefix(path, "ls/"): + return s.DirectoryListRequest(resp, req) + case strings.HasPrefix(path, "stat/"): + return s.FileStatRequest(resp, req) + case strings.HasPrefix(path, "readat/"): + return s.FileReadAtRequest(resp, req) + default: + return nil, CodedError(404, ErrInvalidMethod) + } +} + func (s *HTTPServer) DirectoryListRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { var allocID, path string diff --git a/command/agent/http.go b/command/agent/http.go index b81d26e71725..c755f3a52fc9 100644 --- a/command/agent/http.go +++ b/command/agent/http.go @@ -103,9 +103,7 @@ func (s *HTTPServer) registerHandlers(enableDebug bool) { s.mux.HandleFunc("/v1/evaluations", s.wrap(s.EvalsRequest)) s.mux.HandleFunc("/v1/evaluation/", s.wrap(s.EvalSpecificRequest)) - s.mux.HandleFunc("/v1/client/fs/ls/", s.wrap(s.DirectoryListRequest)) - s.mux.HandleFunc("/v1/client/fs/stat/", s.wrap(s.FileStatRequest)) - s.mux.HandleFunc("/v1/client/fs/readat/", s.wrap(s.FileReadAtRequest)) + s.mux.HandleFunc("/v1/client/fs/", s.wrap(s.FsRequest)) s.mux.HandleFunc("/v1/agent/self", s.wrap(s.AgentSelfRequest)) s.mux.HandleFunc("/v1/agent/join", s.wrap(s.AgentJoinRequest))