From 142ca2dbd5ad4407e97036ef16dbc43c0fbe6671 Mon Sep 17 00:00:00 2001 From: Vincent Fiduccia Date: Sun, 20 Mar 2016 22:21:20 -0700 Subject: [PATCH 1/2] Show version --- main.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 4da8cb0..e4283a0 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,7 @@ const ( ) var ( + showVersion = flag.Bool("version", false, "Show version") debug = flag.Bool("debug", false, "Debug") enableXff = flag.Bool("xff", false, "X-Forwarded-For header support") listen = flag.String("listen", ":80", "Address to listen to (TCP)") @@ -47,6 +48,7 @@ var ( router = mux.NewRouter() answers Versions + VERSION string wantRevision = 1 loadedRevision = 0 loading = false @@ -54,8 +56,14 @@ var ( ) func main() { - log.Info("Starting rancher-metadata") parseFlags() + + if *showVersion { + fmt.Printf("%s\n", VERSION) + os.Exit(0) + } + + log.Infof("Starting rancher-metadata %s", VERSION) err := loadAnswers() if err != nil { log.Fatal("Cannot startup without a valid Answers file") From 5bb68ca324abfef266d7db32eadca70768d6057d Mon Sep 17 00:00:00 2001 From: Vincent Fiduccia Date: Mon, 21 Mar 2016 00:44:03 -0700 Subject: [PATCH 2/2] Encode and decode slashes properly --- answers.go | 8 +++----- example/answers.json | 8 +++++++- main.go | 32 ++++++++++++++++++++++---------- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/answers.go b/answers.go index 5d97086..246b1a2 100644 --- a/answers.go +++ b/answers.go @@ -4,7 +4,6 @@ import ( log "github.com/Sirupsen/logrus" "reflect" "strconv" - "strings" ) func (answers *Versions) Versions() []string { @@ -16,7 +15,7 @@ func (answers *Versions) Versions() []string { return out } -func (answers *Versions) Matching(version string, ip string, path string) (interface{}, bool) { +func (answers *Versions) Matching(version string, ip string, path []string) (interface{}, bool) { var out interface{} all, ok := (*answers)[version] @@ -48,11 +47,10 @@ func (answers *Versions) Matching(version string, ip string, path string) (inter return nil, false } -func valueForPath(in *interface{}, path string) (interface{}, bool) { +func valueForPath(in *interface{}, path []string) (interface{}, bool) { out := *in - parts := strings.Split(path, "/") - for _, key := range parts { + for _, key := range path { valid := false switch v := out.(type) { diff --git a/example/answers.json b/example/answers.json index 19709eb..68431ec 100644 --- a/example/answers.json +++ b/example/answers.json @@ -1,7 +1,10 @@ { "2015-07-25": { "default": { - "host": null, + "host": null, + "sla/sh": { + "sub": "key" + }, "version": "20-aff454dabf2760385d247a5955488ff375b8230edefe079d3748ab44426edd9f", "hosts": [ { @@ -241,6 +244,9 @@ }, "2015-12-19": { "default": { + "sla/sh": { + "sub": "key" + }, "host": null, "version": "20-aff454dabf2760385d247a5955488ff375b8230edefe079d3748ab44426edd9f", "hosts": [ diff --git a/main.go b/main.go index e4283a0..dc5b2cf 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "net" "net/http" + "net/url" "os" "os/signal" "sort" @@ -278,11 +279,22 @@ func metadata(w http.ResponseWriter, req *http.Request) { } } - key := strings.TrimRight(vars["key"], "/") - displayKey := "/" + key + path := strings.TrimRight(req.URL.EscapedPath()[1:], "/") + pathSegments := strings.Split(path, "/")[1:] + displayKey := "" + var err error + for i := 0; err == nil && i < len(pathSegments); i++ { + displayKey += "/" + pathSegments[i] + pathSegments[i], err = url.QueryUnescape(pathSegments[i]) + } + + if err != nil { + respondError(w, req, err.Error(), http.StatusBadRequest) + return + } - log.WithFields(log.Fields{"version": version, "client": clientIp}).Debugf("Searching for: %s", key) - val, ok := answers.Matching(version, clientIp, key) + log.WithFields(log.Fields{"version": version, "client": clientIp}).Debugf("Searching for: %s", displayKey) + val, ok := answers.Matching(version, clientIp, pathSegments) if ok { log.WithFields(log.Fields{"version": version, "client": clientIp}).Infof("OK: %s", displayKey) @@ -359,9 +371,9 @@ func respondText(w http.ResponseWriter, req *http.Request, val interface{}) { _, isMap := vv.(map[string]interface{}) _, isArray := vv.([]interface{}) if isMap || isArray { - out[i] = fmt.Sprintf("%s/\n", k) + out[i] = fmt.Sprintf("%s/\n", url.QueryEscape(k)) } else { - out[i] = fmt.Sprintf("%s\n", k) + out[i] = fmt.Sprintf("%s\n", url.QueryEscape(k)) } i++ } @@ -376,19 +388,19 @@ func respondText(w http.ResponseWriter, req *http.Request, val interface{}) { _, isArray := vv.([]interface{}) if isMap { - // If the child is a map and has a "name" property, show "key=name" + // If the child is a map and has a "name" property, show index=name ("0=foo") name, ok := vvMap[MAGIC_ARRAY_KEY] if ok { - fmt.Fprintf(w, "%d=%s\n", k, name) + fmt.Fprintf(w, "%d=%s\n", k, url.QueryEscape(name.(string))) continue } } if isMap || isArray { - // If the child is a map or array, show "key/" + // If the child is a map or array, show index ("0/") fmt.Fprintf(w, "%d/\n", k) } else { - // Otherwise, show "key" + // Otherwise, show index ("0" ) fmt.Fprintf(w, "%d\n", k) } }