Skip to content
Closed
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
3 changes: 3 additions & 0 deletions websocket/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,9 @@ func (c *Client) handleStatus(msg json.RawMessage) error {
c.log.Debugf("authentication successful")
case "auth_failed":
// this is a fatal error so need to close the connection
if message := sanitize(cm.Message); message != "" {
return fmt.Errorf("authentication failed: %s: closing connection", message)
}
return errors.New("authentication failed: closing connection")
case "success":
c.log.Debugf("received a successful status message: %v", sanitize(cm.Message))
Expand Down
27 changes: 24 additions & 3 deletions websocket/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func connect(w http.ResponseWriter, r *http.Request) {
data, _ := json.Marshal(res)
err = c.WriteMessage(mt, data)
} else {
res := []models.ControlMessage{{EventType: models.EventType{EventType: "status"}, Status: "auth_failed"}}
res := []models.ControlMessage{{EventType: models.EventType{EventType: "status"}, Status: "auth_failed", Message: "missing entitlement"}}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you inver auth_failed correlates to only a bad entitlement or would (should) the backend provide this additional context?

data, _ := json.Marshal(res)
err = c.WriteMessage(mt, data)
}
Expand Down Expand Up @@ -127,8 +127,29 @@ func TestConnectAuthFailure(t *testing.T) {
err = c.Connect()
assert.Nil(t, err)

err = <-c.Error()
assert.NotNil(t, err)
select {
case err = <-c.Error():
assert.EqualError(t, err, "authentication failed: missing entitlement: closing connection")
case <-time.After(5 * time.Second):
t.Fatal("timed out waiting for authentication error")
}
}

func TestHandleStatusAuthFailure(t *testing.T) {
for _, tc := range []struct {
name, message, want string
}{
{"reason", "missing entitlement", "authentication failed: missing entitlement: closing connection"},
{"empty", "", "authentication failed: closing connection"},
{"newline", "invalid\n credentials", "authentication failed: invalid credentials: closing connection"},
} {
t.Run(tc.name, func(t *testing.T) {
msg, err := json.Marshal(models.ControlMessage{Status: "auth_failed", Message: tc.message})
assert.NoError(t, err)
c := &Client{log: &nopLogger{}}
assert.EqualError(t, c.handleStatus(msg), tc.want)
})
}
}

func TestConnectRetryFailure(t *testing.T) {
Expand Down