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
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"regexp"
......@@ -94,6 +96,11 @@ var (
Title: "Error deleting PEM file",
Detail: "",
}
errPemSecretNotFound = ErrorDetails{
Type: "/errors/pems/secretNotFound",
Title: "Matching Secret not found",
Detail: "",
}
)
// Problem Details object per RFC7807
......@@ -286,9 +293,20 @@ func (h *pemsHndlr) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
return
}
if found, valid, err := h.files.Write(ns, name, uid, version); !found {
status = http.StatusNotFound
resp.WriteHeader(status)
reqLog(h.log, req, now, status, bytes)
msg := fmt.Sprintf("Secret matching %s/%s", ns, name)
if uid != "" {
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
} else if !valid || err != nil {
// XXX problem description in body
......
......@@ -38,6 +38,7 @@ import (
"os"
"regexp"
"strconv"
"strings"
"testing"
"code.uplex.de/k8s/k8s-crt-dnldr/pkg/crt"
......@@ -351,10 +352,40 @@ func TestPutPem(t *testing.T) {
t.Errorf("PUT /v1/pems/foo/bar status: got %d want %d",
rr.Code, http.StatusNotFound)
}
if rr.Result().Header.Get("Content-Length") != "0" {
t.Errorf("PUT /v1/pems/foo/bar Content-Length: "+
"got %s want 0",
rr.Result().Header.Get("Content-Length"))
if rr.Header().Get("Content-Type") != problemContentType {
t.Errorf("PUT /v1/pems/foo/bar Content-Type: got %s want %s",
rr.Header().Get("Content-Type"), problemContentType)
}
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,
......
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