Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
K
k8s-crt-dnldr
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
k8s
k8s-crt-dnldr
Commits
180d02f0
Commit
180d02f0
authored
Jul 24, 2020
by
Geoff Simmons
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
REST API returns error response body on Not Found for PUT Secret.
parent
13ab77c6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
7 deletions
+56
-7
handlers.go
pkg/rest/handlers.go
+21
-3
handlers_test.go
pkg/rest/handlers_test.go
+35
-4
No files found.
pkg/rest/handlers.go
View file @
180d02f0
...
...
@@ -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
...
...
pkg/rest/handlers_test.go
View file @
180d02f0
...
...
@@ -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
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment