Skip to content

Commit

Permalink
Version 0.7.1 (#117)
Browse files Browse the repository at this point in the history
* Change a bunch of HTTP exception codes from 502 to 400

A 400 exception (BadRequest) does not trigger `knife` to do a retry, which is a lot of cases makes good sense.

* Version 0.7.1
  • Loading branch information
Sander van Harmelen committed May 26, 2017
1 parent 1ecb409 commit bbb660e
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ _testmain.go
*.test

# Compiled binary
bin
chef-guard
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
Chef-Guard CHANGELOG
====================

0.7.1 (UNRELEASED)
0.7.1
------------------
- Make sure the private token is not exposed
- Fix a Gitlab bug and add additional Gitlab support
- Extend Ruby stack size while running Foodcritic
- Add vendoring of dependencies
- Fix 2 file descriptor leaks
- Change a bunch of HTTP exception codes from 502 to 400

0.7.0
-----
Expand All @@ -13,7 +18,7 @@ Chef-Guard CHANGELOG

0.6.2
-----
- Do not log empty error messages but instead try to capture a more useful message based on the given error
- Do not log empty error messages but instead try to capture a more useful message based on the given error
- Add a shared `insecureTransport` transport that can be used by all connections, instead of creating seperate ones for each connection
- Fix a bug that could cause a nil pointer exception when the Git diff was too large to include the patch in the commit details
- Replaced the multisyncer package with an updated version hosted outside the Chef-Guard repo
Expand Down
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## VARIABLES
VERSION=`cat VERSION`

release:
@mkdir -p bin/x86
@GOOS=linux GOARCH=386 go build -o bin/x86/chef-guard
tar zcvf chef-guard-v$(VERSION)-linux-x86.tar.gz -C examples . -C ../bin/x86 .
@mkdir -p bin/x64
@GOOS=linux GOARCH=amd64 go build -o bin/x64/chef-guard
tar zcvf chef-guard-v$(VERSION)-linux-x64.tar.gz -C examples . -C ../bin/x64 .

2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.7.1 (UNRELEASED)
0.7.1
12 changes: 6 additions & 6 deletions changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ func processChange(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Req
cg, err := newChefGuard(r)
if err != nil {
errorHandler(w, fmt.Sprintf(
"Failed to create a new ChefGuard structure: %s", err), http.StatusBadGateway)
"Failed to create a new ChefGuard structure: %s", err), http.StatusInternalServerError)
return
}

reqBody, err := dumpBody(r)
if err != nil {
errorHandler(w, fmt.Sprintf(
"Failed to get body from call to %s: %s", r.URL.String(), err), http.StatusBadGateway)
"Failed to get body from call to %s: %s", r.URL.String(), err), http.StatusBadRequest)
return
}

Expand Down Expand Up @@ -94,14 +94,14 @@ func processChange(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Req

r.URL, err = url.Parse(u)
if err != nil {
errorHandler(w, fmt.Sprintf("Failed to parse URL %s: %s", u, err), http.StatusBadGateway)
errorHandler(w, fmt.Sprintf("Failed to parse URL %s: %s", u, err), http.StatusBadRequest)
return
}

resp, err := http.DefaultTransport.RoundTrip(r)
if err != nil {
errorHandler(w, fmt.Sprintf(
"Call to %s failed: %s", r.URL.String(), err), http.StatusBadGateway)
"Call to %s failed: %s", r.URL.String(), err), http.StatusBadRequest)
return
}
defer resp.Body.Close()
Expand All @@ -118,14 +118,14 @@ func processChange(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Req
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
errorHandler(w, fmt.Sprintf(
"Failed to get body from call to %s: %s", r.URL.String(), err), http.StatusBadGateway)
"Failed to get body from call to %s: %s", r.URL.String(), err), http.StatusBadRequest)
return
}

cg.ChangeDetails, err = getChangeDetails(r, reqBody)
if err != nil {
errorHandler(w, fmt.Sprintf(
"Failed to parse variables from %s: %s", r.URL.String(), err), http.StatusBadGateway)
"Failed to parse variables from %s: %s", r.URL.String(), err), http.StatusBadRequest)
return
}

Expand Down
8 changes: 4 additions & 4 deletions checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import (
func (cg *ChefGuard) executeChecks() (int, error) {
if cfg.Tests.Foodcritic != "" {
if errCode, err := runFoodcritic(cg.ChefOrg, cg.CookbookPath); err != nil {
if errCode == http.StatusBadGateway || !cg.continueAfterFailedCheck("foodcritic") {
if errCode == http.StatusInternalServerError || !cg.continueAfterFailedCheck("foodcritic") {
return errCode, err
}
}
}
if cfg.Tests.Rubocop != "" {
if errCode, err := runRubocop(cg.CookbookPath); err != nil {
if errCode == http.StatusBadGateway || !cg.continueAfterFailedCheck("rubocop") {
if errCode == http.StatusInternalServerError || !cg.continueAfterFailedCheck("rubocop") {
return errCode, err
}
}
Expand All @@ -59,7 +59,7 @@ func runFoodcritic(org, cookbookPath string) (int, error) {

output, err := cmd.CombinedOutput()
if err != nil {
return http.StatusBadGateway, fmt.Errorf("Failed to execute foodcritic tests: %s - %s", output, err)
return http.StatusInternalServerError, fmt.Errorf("Failed to execute foodcritic tests: %s - %s", output, err)
}
if strings.TrimSpace(string(output)) != "" {
errText := strings.TrimSpace(strings.Replace(string(output), fmt.Sprintf("%s/", cookbookPath), "", -1))
Expand Down Expand Up @@ -94,7 +94,7 @@ func runRubocop(cookbookPath string) (int, error) {
errText := strings.TrimSpace(strings.Replace(string(output), fmt.Sprintf("%s/", cookbookPath), "", -1))
return http.StatusPreconditionFailed, fmt.Errorf("\n=== Rubocop errors found ===\n%s\n============================\n", errText)
}
return http.StatusBadGateway, fmt.Errorf("Failed to execute rubocop tests: %s - %s", output, err)
return http.StatusInternalServerError, fmt.Errorf("Failed to execute rubocop tests: %s - %s", output, err)
}
return 0, nil
}
4 changes: 2 additions & 2 deletions clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func processDownload(w http.ResponseWriter, r *http.Request) {

targetfile, err := getTargetFile(dir, r.FormValue("v"))
if err != nil {
errorHandler(w, err.Error(), http.StatusBadGateway)
errorHandler(w, err.Error(), http.StatusBadRequest)
}

if targetfile != "" {
Expand All @@ -91,7 +91,7 @@ func processDownload(w http.ResponseWriter, r *http.Request) {
if mux.Vars(r)["type"] == "metadata" {
data, err := ioutil.ReadFile(targetfile)
if err != nil {
errorHandler(w, "Failed to read client file: %s"+err.Error(), http.StatusBadGateway)
errorHandler(w, "Failed to read client file: %s"+err.Error(), http.StatusBadRequest)
}

targetmd5 := md5.Sum(data)
Expand Down
12 changes: 6 additions & 6 deletions cookbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ func processCookbook(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.R
}
cg, err := newChefGuard(r)
if err != nil {
errorHandler(w, fmt.Sprintf("Failed to create a new ChefGuard structure: %s", err), http.StatusBadGateway)
errorHandler(w, fmt.Sprintf("Failed to create a new ChefGuard structure: %s", err), http.StatusInternalServerError)
return
}
if r.Method != "DELETE" {
body, err := dumpBody(r)
if err != nil {
errorHandler(w, fmt.Sprintf("Failed to get body from call to %s: %s", r.URL.String(), err), http.StatusBadGateway)
errorHandler(w, fmt.Sprintf("Failed to get body from call to %s: %s", r.URL.String(), err), http.StatusBadRequest)
return
}
if err := json.Unmarshal(body, &cg.Cookbook); err != nil {
errorHandler(w, fmt.Sprintf("Failed to unmarshal body %s: %s", string(body), err), http.StatusBadGateway)
errorHandler(w, fmt.Sprintf("Failed to unmarshal body %s: %s", string(body), err), http.StatusBadRequest)
return
}
if getEffectiveConfig("Mode", cg.ChefOrg).(string) != "silent" {
Expand All @@ -74,7 +74,7 @@ func processCookbook(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.R
if cg.Cookbook.Frozen {
cg.CookbookPath = path.Join(cfg.Default.Tempdir, fmt.Sprintf("%s-%s", r.Header.Get("X-Ops-Userid"), cg.Cookbook.Name))
if err := cg.processCookbookFiles(); err != nil {
errorHandler(w, err.Error(), http.StatusBadGateway)
errorHandler(w, err.Error(), http.StatusBadRequest)
return
}
defer func() {
Expand Down Expand Up @@ -258,7 +258,7 @@ func (cg *ChefGuard) tagAndPublishCookbook() (int, error) {
mail := fmt.Sprintf("%s@%s", cg.User, getEffectiveConfig("MailDomain", cg.ChefOrg).(string))
err := tagCookbook(cg.SourceCookbook.gitConfig, cg.Cookbook.Name, tag, cg.User, mail)
if err != nil {
return http.StatusBadGateway, err
return http.StatusBadRequest, err
}
}
if getEffectiveConfig("PublishCookbook", cg.ChefOrg).(bool) && cg.SourceCookbook.private {
Expand All @@ -270,7 +270,7 @@ func (cg *ChefGuard) tagAndPublishCookbook() (int, error) {
errText = fmt.Sprintf("%s - NOTE: Failed to untag the repo during cleanup!", errText)
}
}
return http.StatusBadGateway, fmt.Errorf(errText)
return http.StatusBadRequest, fmt.Errorf(errText)
}
}
}
Expand Down
28 changes: 14 additions & 14 deletions validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func unmarshalConstraints(body []byte) (*Constraints, error) {
func (cg *ChefGuard) checkCookbookFrozen() (int, error) {
frozen, err := cg.cookbookFrozen(cg.Cookbook.Name, cg.Cookbook.Version)
if err != nil {
return http.StatusBadGateway, err
return http.StatusBadRequest, err
}
if frozen {
return http.StatusPreconditionFailed, fmt.Errorf("\n=== Cookbook Upload error found ===\n" +
Expand Down Expand Up @@ -140,7 +140,7 @@ func (cg *ChefGuard) validateCookbookStatus() (int, error) {
func (cg *ChefGuard) validateConstraints(body []byte) (int, error) {
c, err := unmarshalConstraints(body)
if err != nil {
return http.StatusBadGateway, fmt.Errorf("Failed to unmarshal body %s: %s", string(body), err)
return http.StatusBadRequest, fmt.Errorf("Failed to unmarshal body %s: %s", string(body), err)
}

devEnv := getEffectiveConfig("DevEnvironment", cg.ChefOrg).(string)
Expand Down Expand Up @@ -192,7 +192,7 @@ func (cg *ChefGuard) checkDependencies(constraints map[string][]string, validate
}
frozen, err := cg.cookbookFrozen(name, version)
if err != nil {
return http.StatusBadGateway, err
return http.StatusBadRequest, err
}
if !frozen {
errors = append(errors, fmt.Sprintf("%s version %s needs to be frozen", name, version))
Expand All @@ -219,7 +219,7 @@ func (cg *ChefGuard) cookbookFrozen(name, version string) (bool, error) {
func (cg *ChefGuard) compareCookbooks() (int, error) {
sh, err := cg.getSourceFileHashes()
if err != nil {
return http.StatusBadGateway, err
return http.StatusBadRequest, err
}
changed := []string{}
missing := []string{}
Expand All @@ -237,7 +237,7 @@ func (cg *ChefGuard) compareCookbooks() (int, error) {
} else {
ignore, err := cg.ignoreThisFile(file, true)
if err != nil {
return http.StatusBadGateway, err
return http.StatusBadRequest, err
}
if !ignore {
missing = append(missing, file)
Expand All @@ -258,7 +258,7 @@ func (cg *ChefGuard) compareCookbooks() (int, error) {
for file := range sh {
ignore, err := cg.ignoreThisFile(file, true)
if err != nil {
return http.StatusBadGateway, err
return http.StatusBadRequest, err
}
if !ignore {
missing = append(missing, file)
Expand Down Expand Up @@ -388,7 +388,7 @@ func searchCommunityCookbooks(name, version string) (*SourceCookbook, int, error
if cfg.Community.Forks != "" {
sc, err = searchGit(strings.Split(cfg.Community.Forks, ","), name, version, true)
if err != nil {
return nil, http.StatusBadGateway, err
return nil, http.StatusBadRequest, err
}
if sc != nil {
// Do additional tests to check for a PR!
Expand Down Expand Up @@ -432,7 +432,7 @@ func searchPrivateCookbooks(chefOrg, name, version string) (*SourceCookbook, int
}
sc, err := searchGit(strings.Split(gitConfigs, ","), name, version, false)
if err != nil {
return nil, http.StatusBadGateway, err
return nil, http.StatusBadRequest, err
}
if sc != nil {
sc.private = true
Expand All @@ -445,35 +445,35 @@ func searchPrivateCookbooks(chefOrg, name, version string) (*SourceCookbook, int
func searchSupermarket(supermarket, name, version string) (*SourceCookbook, int, error) {
u, err := url.Parse(fmt.Sprintf("%s/%s", supermarket, "universe"))
if err != nil {
return nil, http.StatusBadGateway, fmt.Errorf(
return nil, http.StatusBadRequest, fmt.Errorf(
"Failed to parse the community cookbooks URL %s: %s", supermarket, err)
}
resp, err := http.Get(u.String())
if err != nil {
return nil, http.StatusBadGateway, fmt.Errorf(
return nil, http.StatusBadRequest, fmt.Errorf(
"Failed to get cookbook list from %s: %s", u.String(), err)
}
defer resp.Body.Close()
if err := checkHTTPResponse(resp, []int{http.StatusOK}); err != nil {
return nil, http.StatusBadGateway, fmt.Errorf(
return nil, http.StatusBadRequest, fmt.Errorf(
"Failed to get cookbook list from %s: %s", u.String(), err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, http.StatusBadGateway, fmt.Errorf(
return nil, http.StatusBadRequest, fmt.Errorf(
"Failed to read the response body from %v: %s", resp, err)
}
results := make(map[string]map[string]*SourceCookbook)
if err := json.Unmarshal(body, &results); err != nil {
return nil, http.StatusBadGateway, fmt.Errorf(
return nil, http.StatusBadRequest, fmt.Errorf(
"Failed to unmarshal body %s: %s", string(body), err)
}
if cb, exists := results[name]; exists {
if sc, exists := cb[version]; exists {
sc.artifact = true
u, err := communityDownloadURL(sc.LocationPath, name, version)
if err != nil {
return nil, http.StatusBadGateway, err
return nil, http.StatusBadRequest, err
}
sc.DownloadURL = u
sc.sourceURL = strings.Split(u.String(), "&")[0]
Expand Down

0 comments on commit bbb660e

Please sign in to comment.