Commit 180d02f0 authored by Geoff Simmons's avatar Geoff Simmons

REST API returns error response body on Not Found for PUT Secret.

parent 13ab77c6
...@@ -30,6 +30,8 @@ package rest ...@@ -30,6 +30,8 @@ package rest
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt"
"net/http" "net/http"
"os" "os"
"regexp" "regexp"
...@@ -94,6 +96,11 @@ var ( ...@@ -94,6 +96,11 @@ var (
Title: "Error deleting PEM file", Title: "Error deleting PEM file",
Detail: "", Detail: "",
} }
errPemSecretNotFound = ErrorDetails{
Type: "/errors/pems/secretNotFound",
Title: "Matching Secret not found",
Detail: "",
}
) )
// Problem Details object per RFC7807 // Problem Details object per RFC7807
...@@ -286,9 +293,20 @@ func (h *pemsHndlr) ServeHTTP(resp http.ResponseWriter, req *http.Request) { ...@@ -286,9 +293,20 @@ func (h *pemsHndlr) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
return return
} }
if found, valid, err := h.files.Write(ns, name, uid, version); !found { if found, valid, err := h.files.Write(ns, name, uid, version); !found {
status = http.StatusNotFound msg := fmt.Sprintf("Secret matching %s/%s", ns, name)
resp.WriteHeader(status) if uid != "" {
reqLog(h.log, req, now, status, bytes) msg += " uid=" + uid
}
if version != "" {
msg += " version=" + version
}
msg += " not found"
if err != nil {
msg += ": " + err.Error()
}
errr := errors.New(msg)
h.errorResponse(resp, req, now, http.StatusNotFound,
errPemSecretNotFound, errr)
return return
} else if !valid || err != nil { } else if !valid || err != nil {
// XXX problem description in body // XXX problem description in body
......
...@@ -38,6 +38,7 @@ import ( ...@@ -38,6 +38,7 @@ import (
"os" "os"
"regexp" "regexp"
"strconv" "strconv"
"strings"
"testing" "testing"
"code.uplex.de/k8s/k8s-crt-dnldr/pkg/crt" "code.uplex.de/k8s/k8s-crt-dnldr/pkg/crt"
...@@ -351,10 +352,40 @@ func TestPutPem(t *testing.T) { ...@@ -351,10 +352,40 @@ func TestPutPem(t *testing.T) {
t.Errorf("PUT /v1/pems/foo/bar status: got %d want %d", t.Errorf("PUT /v1/pems/foo/bar status: got %d want %d",
rr.Code, http.StatusNotFound) rr.Code, http.StatusNotFound)
} }
if rr.Result().Header.Get("Content-Length") != "0" { if rr.Header().Get("Content-Type") != problemContentType {
t.Errorf("PUT /v1/pems/foo/bar Content-Length: "+ t.Errorf("PUT /v1/pems/foo/bar Content-Type: got %s want %s",
"got %s want 0", rr.Header().Get("Content-Type"), problemContentType)
rr.Result().Header.Get("Content-Length")) }
bodylen := len(rr.Body.String())
if rr.Header().Get("Content-Length") != strconv.Itoa(bodylen) {
t.Errorf("PUT /v1/pems/foo/bar Content-Length: got %s want %d",
rr.Header().Get("Content-Length"), bodylen)
}
problem := &Problem{}
if err = json.Unmarshal(rr.Body.Bytes(), problem); err != nil {
t.Fatalf("PUT /v1/pems/foo/bar body unmarshal: %v", err)
}
if problem.Type != errPemSecretNotFound.Type {
t.Errorf("PUT /v1/pems/foo/bar problem type: got %s want %s",
problem.Type, errPemSecretNotFound.Type)
}
if problem.Title != errPemSecretNotFound.Title {
t.Errorf("PUT /v1/pems/foo/bar problem title: got %s want %s",
problem.Title, errPemSecretNotFound.Title)
}
errMsg := "Secret matching foo/bar not found"
if !strings.HasPrefix(problem.Detail, errMsg) {
t.Errorf("PUT /v1/pems/foo/bar problem detail: "+
"got \"%s\" want preifx \"%s\"",
problem.Detail, errMsg)
}
if problem.Status != http.StatusNotFound {
t.Errorf("PUT /v1/pems/foo/bar problem status: got %d want %d",
problem.Status, http.StatusNotFound)
}
if !errInstancePattern.Match([]byte(problem.Instance)) {
t.Errorf("PUT /v1/pems/foo/bar problem instance: "+
"got %s want /log/errors/N", problem.Instance)
} }
req = httptest.NewRequest(http.MethodPut, req = httptest.NewRequest(http.MethodPut,
......
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