Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: output a more helpful error message when root is not found #1893

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func Root(w http.ResponseWriter, r *http.Request) {
// Load root certificate with the
cert, err := mustAuthority(r.Context()).Root(sum)
if err != nil {
render.Error(w, r, errs.Wrapf(http.StatusNotFound, err, "%s was not found", r.RequestURI))
render.Error(w, r, errs.NotFoundErr(err, errs.WithMessage("root with fingerprint %s was not found", sum)))
marten-seemann marked this conversation as resolved.
Show resolved Hide resolved
return
}

Expand Down
21 changes: 13 additions & 8 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,20 +835,22 @@ func Test_Health(t *testing.T) {
}

func Test_Root(t *testing.T) {
const sha = "efc7d6b475a56fe587650bcdb999a4a308f815ba44db4bf0371ea68a786ccd36"
tests := []struct {
name string
root *x509.Certificate
err error
statusCode int
name string
root *x509.Certificate
err error
expectedMsg string
statusCode int
}{
{"ok", parseCertificate(rootPEM), nil, 200},
{"fail", nil, fmt.Errorf("not found"), 404},
{"ok", parseCertificate(rootPEM), nil, "", 200},
{"fail", nil, fmt.Errorf("not found"), fmt.Sprintf("root with fingerprint %s was not found", sha), 404},
marten-seemann marked this conversation as resolved.
Show resolved Hide resolved
}

// Request with chi context
chiCtx := chi.NewRouteContext()
chiCtx.URLParams.Add("sha", "efc7d6b475a56fe587650bcdb999a4a308f815ba44db4bf0371ea68a786ccd36")
req := httptest.NewRequest("GET", "http://example.com/root/efc7d6b475a56fe587650bcdb999a4a308f815ba44db4bf0371ea68a786ccd36", http.NoBody)
chiCtx.URLParams.Add("sha", sha)
req := httptest.NewRequest("GET", "http://example.com/root/"+sha, http.NoBody)
req = req.WithContext(context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx))

expected := []byte(`{"ca":"` + strings.ReplaceAll(rootPEM, "\n", `\n`) + `\n"}`)
Expand All @@ -866,13 +868,16 @@ func Test_Root(t *testing.T) {

body, err := io.ReadAll(res.Body)
res.Body.Close()
fmt.Println("body:", string(body))
marten-seemann marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
t.Errorf("caHandler.Root unexpected error = %v", err)
}
if tt.statusCode == 200 {
if !bytes.Equal(bytes.TrimSpace(body), expected) {
t.Errorf("caHandler.Root Body = %s, wants %s", body, expected)
}
} else {
require.Contains(t, string(body), tt.expectedMsg)
}
})
}
Expand Down
6 changes: 3 additions & 3 deletions test/integration/requestid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ func Test_reflectRequestID(t *testing.T) {
var firstErr *errs.Error
if assert.ErrorAs(t, err, &firstErr) {
assert.Equal(t, 404, firstErr.StatusCode())
assert.Equal(t, "The requested resource could not be found. Please see the certificate authority logs for more info.", firstErr.Err.Error())
assert.Equal(t, "root with fingerprint invalid was not found", firstErr.Err.Error())
marten-seemann marked this conversation as resolved.
Show resolved Hide resolved
assert.NotEmpty(t, firstErr.RequestID)

// TODO: include the below error in the JSON? It's currently only output to the CA logs. Also see https://github.com/smallstep/certificates/pull/759
marten-seemann marked this conversation as resolved.
Show resolved Hide resolved
//assert.Equal(t, "/root/invalid was not found: certificate with fingerprint invalid was not found", apiErr.Msg)
// assert.Equal(t, "/root/invalid was not found: certificate with fingerprint invalid was not found", apiErr.Msg)
marten-seemann marked this conversation as resolved.
Show resolved Hide resolved
}
assert.Nil(t, rootResponse)

Expand All @@ -159,7 +159,7 @@ func Test_reflectRequestID(t *testing.T) {
var secondErr *errs.Error
if assert.ErrorAs(t, err, &secondErr) {
assert.Equal(t, 404, secondErr.StatusCode())
assert.Equal(t, "The requested resource could not be found. Please see the certificate authority logs for more info.", secondErr.Err.Error())
assert.Equal(t, "root with fingerprint invalid was not found", secondErr.Err.Error())
marten-seemann marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(t, "reqID", secondErr.RequestID)
}
assert.Nil(t, rootResponse)
Expand Down
Loading