From a8f38271488942b666098151e316b76e3cdab969 Mon Sep 17 00:00:00 2001 From: vaikas-google Date: Mon, 25 Jan 2016 13:44:26 -0800 Subject: [PATCH] address CR comments --- manager/deployments.go | 83 ++++++++++++++---------------------- registry/registryprovider.go | 6 ++- 2 files changed, 38 insertions(+), 51 deletions(-) diff --git a/manager/deployments.go b/manager/deployments.go index 68714803d..c2440fca3 100644 --- a/manager/deployments.go +++ b/manager/deployments.go @@ -246,30 +246,9 @@ func getPathVariable(w http.ResponseWriter, r *http.Request, variable, handler s func getTemplate(w http.ResponseWriter, r *http.Request, handler string) *common.Template { util.LogHandlerEntry(handler, r) - b := io.LimitReader(r.Body, *maxLength*1024) - y, err := ioutil.ReadAll(b) - if err != nil { - util.LogAndReturnError(handler, http.StatusBadRequest, err, w) - return nil - } - - // Reject the input if it exceeded the length limit, - // since we may not have read all of it into the buffer. - if _, err = b.Read(make([]byte, 0, 1)); err != io.EOF { - e := fmt.Errorf("template exceeds maximum length of %d KB", *maxLength) - util.LogAndReturnError(handler, http.StatusBadRequest, e, w) - return nil - } + j, err := getJsonFromRequest(w, r, handler) - if err := r.Body.Close(); err != nil { - util.LogAndReturnError(handler, http.StatusInternalServerError, err, w) - return nil - } - - j, err := yaml.YAMLToJSON(y) if err != nil { - e := fmt.Errorf("%v\n%v", err, string(y)) - util.LogAndReturnError(handler, http.StatusBadRequest, e, w) return nil } @@ -450,30 +429,8 @@ func getDownloadURLsHandlerFunc(w http.ResponseWriter, r *http.Request) { func getCredential(w http.ResponseWriter, r *http.Request, handler string) *common.RegistryCredential { util.LogHandlerEntry(handler, r) - b := io.LimitReader(r.Body, *maxLength*1024) - y, err := ioutil.ReadAll(b) + j, err := getJsonFromRequest(w, r, handler) if err != nil { - util.LogAndReturnError(handler, http.StatusBadRequest, err, w) - return nil - } - - // Reject the input if it exceeded the length limit, - // since we may not have read all of it into the buffer. - if _, err = b.Read(make([]byte, 0, 1)); err != io.EOF { - e := fmt.Errorf("template exceeds maximum length of %d KB", *maxLength) - util.LogAndReturnError(handler, http.StatusBadRequest, e, w) - return nil - } - - if err := r.Body.Close(); err != nil { - util.LogAndReturnError(handler, http.StatusInternalServerError, err, w) - return nil - } - - j, err := yaml.YAMLToJSON(y) - if err != nil { - e := fmt.Errorf("%v\n%v", err, string(y)) - util.LogAndReturnError(handler, http.StatusBadRequest, e, w) return nil } @@ -497,11 +454,12 @@ func createCredentialHandlerFunc(w http.ResponseWriter, r *http.Request) { } c := getCredential(w, r, handler) - - err = backend.CreateCredential(credentialName, c) - if err != nil { - util.LogAndReturnError(handler, http.StatusBadRequest, err, w) - return + if c != nil { + err = backend.CreateCredential(credentialName, c) + if err != nil { + util.LogAndReturnError(handler, http.StatusBadRequest, err, w) + return + } } util.LogHandlerExitWithJSON(handler, w, c, http.StatusOK) @@ -523,3 +481,28 @@ func getCredentialHandlerFunc(w http.ResponseWriter, r *http.Request) { util.LogHandlerExitWithJSON(handler, w, c, http.StatusOK) } + +func getJsonFromRequest(w http.ResponseWriter, r *http.Request, handler string) ([]byte, error) { + util.LogHandlerEntry(handler, r) + b := io.LimitReader(r.Body, *maxLength*1024) + y, err := ioutil.ReadAll(b) + if err != nil { + util.LogAndReturnError(handler, http.StatusBadRequest, err, w) + return []byte{}, err + } + + // Reject the input if it exceeded the length limit, + // since we may not have read all of it into the buffer. + if _, err = b.Read(make([]byte, 0, 1)); err != io.EOF { + e := fmt.Errorf("template exceeds maximum length of %d KB", *maxLength) + util.LogAndReturnError(handler, http.StatusBadRequest, e, w) + return []byte{}, err + } + + if err := r.Body.Close(); err != nil { + util.LogAndReturnError(handler, http.StatusInternalServerError, err, w) + return []byte{}, err + } + + return yaml.YAMLToJSON(y) +} diff --git a/registry/registryprovider.go b/registry/registryprovider.go index d99799708..bb625839d 100644 --- a/registry/registryprovider.go +++ b/registry/registryprovider.go @@ -169,7 +169,11 @@ func (grp githubRegistryProvider) createGithubClient(credentialName string) (*gi return github.NewClient(tc), nil } if c.BasicAuth.Username != "" && c.BasicAuth.Password != "" { - + tp := github.BasicAuthTransport{ + Username: c.BasicAuth.Username, + Password: c.BasicAuth.Password, + } + return github.NewClient(tp.Client()), nil } }