Commit a8f38271 authored by vaikas-google's avatar vaikas-google

address CR comments

parent 0f8b3f77
...@@ -246,30 +246,9 @@ func getPathVariable(w http.ResponseWriter, r *http.Request, variable, handler s ...@@ -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 { func getTemplate(w http.ResponseWriter, r *http.Request, handler string) *common.Template {
util.LogHandlerEntry(handler, r) util.LogHandlerEntry(handler, r)
b := io.LimitReader(r.Body, *maxLength*1024) j, err := getJsonFromRequest(w, r, handler)
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
}
if err := r.Body.Close(); err != nil {
util.LogAndReturnError(handler, http.StatusInternalServerError, err, w)
return nil
}
j, err := yaml.YAMLToJSON(y)
if err != nil { if err != nil {
e := fmt.Errorf("%v\n%v", err, string(y))
util.LogAndReturnError(handler, http.StatusBadRequest, e, w)
return nil return nil
} }
...@@ -450,30 +429,8 @@ func getDownloadURLsHandlerFunc(w http.ResponseWriter, r *http.Request) { ...@@ -450,30 +429,8 @@ func getDownloadURLsHandlerFunc(w http.ResponseWriter, r *http.Request) {
func getCredential(w http.ResponseWriter, r *http.Request, handler string) *common.RegistryCredential { func getCredential(w http.ResponseWriter, r *http.Request, handler string) *common.RegistryCredential {
util.LogHandlerEntry(handler, r) util.LogHandlerEntry(handler, r)
b := io.LimitReader(r.Body, *maxLength*1024) j, err := getJsonFromRequest(w, r, handler)
y, err := ioutil.ReadAll(b)
if err != nil { 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 return nil
} }
...@@ -497,11 +454,12 @@ func createCredentialHandlerFunc(w http.ResponseWriter, r *http.Request) { ...@@ -497,11 +454,12 @@ func createCredentialHandlerFunc(w http.ResponseWriter, r *http.Request) {
} }
c := getCredential(w, r, handler) c := getCredential(w, r, handler)
if c != nil {
err = backend.CreateCredential(credentialName, c) err = backend.CreateCredential(credentialName, c)
if err != nil { if err != nil {
util.LogAndReturnError(handler, http.StatusBadRequest, err, w) util.LogAndReturnError(handler, http.StatusBadRequest, err, w)
return return
}
} }
util.LogHandlerExitWithJSON(handler, w, c, http.StatusOK) util.LogHandlerExitWithJSON(handler, w, c, http.StatusOK)
...@@ -523,3 +481,28 @@ func getCredentialHandlerFunc(w http.ResponseWriter, r *http.Request) { ...@@ -523,3 +481,28 @@ func getCredentialHandlerFunc(w http.ResponseWriter, r *http.Request) {
util.LogHandlerExitWithJSON(handler, w, c, http.StatusOK) 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)
}
...@@ -169,7 +169,11 @@ func (grp githubRegistryProvider) createGithubClient(credentialName string) (*gi ...@@ -169,7 +169,11 @@ func (grp githubRegistryProvider) createGithubClient(credentialName string) (*gi
return github.NewClient(tc), nil return github.NewClient(tc), nil
} }
if c.BasicAuth.Username != "" && c.BasicAuth.Password != "" { if c.BasicAuth.Username != "" && c.BasicAuth.Password != "" {
tp := github.BasicAuthTransport{
Username: c.BasicAuth.Username,
Password: c.BasicAuth.Password,
}
return github.NewClient(tp.Client()), nil
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment