From e40f1533f9a597b9399283a0b2f75e751f71445c Mon Sep 17 00:00:00 2001 From: Abhishek Shah Date: Mon, 21 Sep 2015 16:32:51 -0700 Subject: [PATCH] netexec shellCommand endpoint returns the error if shell command fails, instead of killing netexec --- contrib/for-tests/netexec/Makefile | 4 +--- contrib/for-tests/netexec/netexec.go | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/contrib/for-tests/netexec/Makefile b/contrib/for-tests/netexec/Makefile index 73c540e6df6..57098585af3 100644 --- a/contrib/for-tests/netexec/Makefile +++ b/contrib/for-tests/netexec/Makefile @@ -1,12 +1,10 @@ .PHONY: all netexec image push clean -TAG = 1.0 +TAG = 1.1 PREFIX = gcr.io/google_containers all: push -TAG = 1.0 - netexec: netexec.go CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' ./netexec.go diff --git a/contrib/for-tests/netexec/netexec.go b/contrib/for-tests/netexec/netexec.go index 92bd75f7f77..cfca4a12971 100644 --- a/contrib/for-tests/netexec/netexec.go +++ b/contrib/for-tests/netexec/netexec.go @@ -191,9 +191,20 @@ func dialUDP(request string, remoteAddress *net.UDPAddr) (string, error) { func shellHandler(w http.ResponseWriter, r *http.Request) { log.Println(r.FormValue("shellCommand")) - output, err := exec.Command(shellPath, "-c", r.FormValue("shellCommand")).CombinedOutput() - assertNoError(err) - fmt.Fprintf(w, string(output)) + cmdOut, err := exec.Command(shellPath, "-c", r.FormValue("shellCommand")).CombinedOutput() + output := map[string]string{} + if len(cmdOut) > 0 { + output["output"] = string(cmdOut) + } + if err != nil { + output["error"] = fmt.Sprintf("%v", err) + } + bytes, err := json.Marshal(output) + if err == nil { + fmt.Fprintf(w, string(bytes)) + } else { + http.Error(w, fmt.Sprintf("response could not be serialized. %v", err), http.StatusExpectationFailed) + } } func hostNameHandler(w http.ResponseWriter, r *http.Request) {