From 311352a4cad53acba6bd2a563d12b40738c0af95 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 16 May 2016 12:24:50 +0200 Subject: [PATCH] first try of graph.json --- api/aliases.go | 22 ++++++++++++++-------- api/lib.go | 31 +++++++++++++++++++++++++++++++ models/config.go | 1 + 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/api/aliases.go b/api/aliases.go index 96b2e9e..e7b8a5b 100644 --- a/api/aliases.go +++ b/api/aliases.go @@ -22,10 +22,13 @@ func NewAliases (config *models.Config, router *httprouter.Router,prefix string, } router.GET(prefix, api.GetAll) router.GET(prefix+"/ansible", api.AnsibleDiff) - router.GET(prefix+"/alias/:nodeid", api.GetOne) - router.POST(prefix+"/alias/:nodeid", api.SaveOne) + router.GET(prefix+"/cleanup", api.Cleanup) + router.GET(prefix+"/alias/:nodeid", BasicAuth(api.GetOne, []byte(config.Webserver.Api.Passphrase))) + router.POST(prefix+"/alias/:nodeid", BasicAuth(api.SaveOne,[]byte(config.Webserver.Api.Passphrase))) +} +func (api *ApiAliases) cleaner(){ + // clean up the aliases by correct values in nodes } - func (api *ApiAliases) GetAll(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { jsonOutput(w,api.aliases.List) } @@ -40,8 +43,8 @@ func (api *ApiAliases) GetOne(w http.ResponseWriter, r *http.Request, ps httprou func (api *ApiAliases) SaveOne(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { var alias models.Alias - err := json.NewDecoder(r.Body).Decode(&alias) + err := json.NewDecoder(r.Body).Decode(&alias) if err != nil{ http.Error(w, err.Error(), http.StatusInternalServerError) fmt.Fprint(w, "Decode: ", ps.ByName("nodeid"),"\n") @@ -51,8 +54,11 @@ func (api *ApiAliases) SaveOne(w http.ResponseWriter, r *http.Request, ps httpro jsonOutput(w,alias) } -func (api *ApiAliases) AnsibleDiff(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { - diff := api.aliases.List - //TODO diff between List and api.nodes (for run not at all) - jsonOutput(w,models.GenerateAnsible(api.nodes,diff)) +func (api *ApiAliases) Cleanup(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { + api.cleaner() + jsonOutput(w,api.aliases.List) +} +func (api *ApiAliases) AnsibleDiff(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { + api.cleaner() + jsonOutput(w,models.GenerateAnsible(api.nodes,api.aliases.List)) } diff --git a/api/lib.go b/api/lib.go index 2f5aa2f..6f90b4c 100644 --- a/api/lib.go +++ b/api/lib.go @@ -1,8 +1,13 @@ package api import ( + "bytes" + "strings" "net/http" "encoding/json" + "encoding/base64" + + "github.com/julienschmidt/httprouter" ) func jsonOutput(w http.ResponseWriter,data interface{}){ @@ -15,3 +20,29 @@ func jsonOutput(w http.ResponseWriter,data interface{}){ w.Header().Set("Content-Type", "application/json") w.Write(js) } +func BasicAuth(h httprouter.Handle, pass []byte) httprouter.Handle { + return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + const basicAuthPrefix string = "Basic " + + // Get the Basic Authentication credentials + auth := r.Header.Get("Authorization") + if strings.HasPrefix(auth, basicAuthPrefix) { + // Check credentials + payload, err := base64.StdEncoding.DecodeString(auth[len(basicAuthPrefix):]) + if err == nil { + pair := bytes.SplitN(payload, []byte(":"), 2) + if len(pair) == 2 && + bytes.Equal(pair[1], pass) { + + // Delegate request to the given handle + h(w, r, ps) + return + } + } + } + + // Request Basic Authentication otherwise + w.Header().Set("WWW-Authenticate", "Basic realm=Restricted") + http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) + } +} diff --git a/models/config.go b/models/config.go index e1044b0..b18ba4d 100644 --- a/models/config.go +++ b/models/config.go @@ -21,6 +21,7 @@ type Config struct { Address string `yaml:"address"` Webroot string `yaml:"webroot"` Api struct { + Passphrase string `yaml:"passphrase"` NewNode bool `yaml:"newnode"` Aliases bool `yaml:"aliases"` } `yaml:"api"`