Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 22 additions & 4 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,33 @@ func (e *ErrorResponse) Error() string {
if msg == "" {
msg = "(no message)"
}
// A 401 is nearly always a bad key or a skewed clock, and the server's own
// message rarely says so. The hint belongs here rather than in Messages,
// which reports what the server said.
// The hint belongs here rather than in Messages, which reports what the
// server said.
if e.StatusCode == 401 {
msg += " (check the client key and that the local clock is in sync)"
msg += unauthorizedHint(e.ServerMessage())
}
return fmt.Sprintf("cinc: %s %s: %d: %s", e.Method, e.Path, e.StatusCode, msg)
}

// unauthorizedHint returns advice to follow a 401's server message, or "" when
// the message already names the cause. erchef and cinc-server-ng name it for
// an unknown actor ("Ensure that your node_name and client key are correct"),
// a skewed clock, bad signing headers, and a failed password login. erchef's
// "Invalid signature for user or client" leaves only the key: the clock is
// checked first. Any other message, or none, gets the general hint.
func unauthorizedHint(msg string) string {
lower := strings.ToLower(msg)
for _, named := range []string{"client key", "clock", "time stamp", "timestamp", "header", "password"} {
if strings.Contains(lower, named) {
return ""
}
}
if strings.Contains(lower, "invalid signature for user or client") {
return " (check that the client key is the one the server has for this client)"
}
return " (check the client key and that the local clock is in sync)"
}

// Unwrap maps the status code to a sentinel error for errors.Is.
func (e *ErrorResponse) Unwrap() error {
switch e.StatusCode {
Expand Down
36 changes: 36 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,39 @@ func TestErrorResponse_ServerMessage(t *testing.T) {
})
}
}

// The 401 hint is added only where the server's message leaves the cause
// open, and names the cause where it is known. The messages are erchef's
// (oc_chef_wm_base:verify_request_message, chef_wm_malformed) and
// cinc-server-ng's (server/auth.go).
func TestErrorResponse_401HintMatchesCause(t *testing.T) {
const generic = " (check the client key and that the local clock is in sync)"
const key = " (check that the client key is the one the server has for this client)"
cases := []struct {
name, msg, hint string
}{
{"unknown actor", "Failed to authenticate as 'web1'. Ensure that your node_name and client key are correct.", ""},
{"bad signature", "Invalid signature for user or client 'web1'", key},
{"erchef clock", "Failed to authenticate as web1. Synchronize the clock on your host.", ""},
{"cinc-server-ng clock", "Original request time stamp too far in the future or past.", ""},
{"bad timestamp", "invalid X-Ops-Timestamp", ""},
{"missing headers", "missing required authentication header(s) X-Ops-UserId", ""},
{"password login", "Failed to authenticate: Username and password incorrect", ""},
{"unrecognized", "bad sig", generic},
{"no message", "", generic},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
e := &ErrorResponse{Method: "GET", Path: "/x", StatusCode: 401}
shown := "(no message)"
if tc.msg != "" {
e.Messages = []string{tc.msg}
shown = tc.msg
}
want := "cinc: GET /x: 401: " + shown + tc.hint
if got := e.Error(); got != want {
t.Errorf("Error() = %q, want %q", got, want)
}
})
}
}