From 8be417a4a12c85c448e1676da15e6ff1eea1a71d Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Sun, 11 Feb 2018 15:42:37 +0200 Subject: [PATCH] log the SHA1 of the received payload --- README.md | 2 +- pkg/server/handlers.go | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a5ec23b..894c874 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Web API: * `POST /readyz/enable` signals the Kubernetes LB that this instance is ready to receive traffic * `POST /readyz/disable` signals the Kubernetes LB to stop sending requests to this instance * `GET /panic` crashes the process with exit code 255 -* `POST /echo` echos the posted content +* `POST /echo` echos the posted content, logs the SHA1 hash of the content * `POST /job` long running job, json body: `{"wait":2}` * `POST /backend` forwards the call to the backend service on `http://backend-podinfo:9898/echo` diff --git a/pkg/server/handlers.go b/pkg/server/handlers.go index 169bdda..e4cee84 100644 --- a/pkg/server/handlers.go +++ b/pkg/server/handlers.go @@ -12,6 +12,8 @@ import ( "time" "os" "bytes" + "crypto/sha1" + "fmt" ) func (s *Server) index(w http.ResponseWriter, r *http.Request) { @@ -49,7 +51,8 @@ func (s *Server) echo(w http.ResponseWriter, r *http.Request) { w.Write([]byte(err.Error())) return } - glog.Infof("Payload received from %s: %s", r.RemoteAddr, string(body)) + sha1 := hash(string(body)) + glog.Infof("Payload received from %s hash %s", r.RemoteAddr, sha1) w.WriteHeader(http.StatusAccepted) w.Write(body) default: @@ -184,3 +187,8 @@ func (s *Server) disable(w http.ResponseWriter, r *http.Request) { func (s *Server) panic(w http.ResponseWriter, r *http.Request) { glog.Fatal("Kill switch triggered") } + +func hash(input string) string { + h := sha1.New() + return fmt.Sprintf("%x", h.Sum([]byte(input))) +}