Skip to content

Commit

Permalink
daemon: fix handling of invalid notice types in filter
Browse files Browse the repository at this point in the history
Invalid notice types are ignored when parsing the API request to
construct the notice filter. However, if only invalid notice types are
included in the request, discarding all types results in a filter
indistinguishable from one which never had any types in the first place,
and thus should return notices of any type. This commit fixes this
behaviour, so no notices are returned if only invalid notice types are
requested.

Signed-off-by: Oliver Calder <[email protected]>
  • Loading branch information
olivercalder committed Nov 16, 2023
1 parent 05d782c commit c40bd32
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internals/daemon/api_notices.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ func v1GetNotices(c *Command, r *http.Request, _ *UserState) Response {
}
types = append(types, noticeType)
}
if len(types) == 0 && len(typeStrs) > 0 {
// Only requested invalid notice types. Return no notices, rather than
// all, the latter of which would occur if the types filter was empty.
return SyncResponse([]*state.Notice{})
}

keys := strutil.MultiCommaSeparatedList(query["keys"])

Expand Down
41 changes: 41 additions & 0 deletions internals/daemon/api_notices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,47 @@ func (s *apiSuite) TestNoticesFilterMultipleKeys(c *C) {
c.Assert(n["key"], Equals, "danger")
}

func (s *apiSuite) TestNoticesFilterInvalidTypes(c *C) {
s.daemon(c)

st := s.d.Overlord().State()
st.Lock()
addNotice(c, st, state.ChangeUpdateNotice, "123", nil)
time.Sleep(time.Microsecond)
addNotice(c, st, state.WarningNotice, "danger", nil)
st.Unlock()

// Check that invalid types are discarded, and notices with remaining
// types are requested as expected, without error.
req, err := http.NewRequest("GET", "/v1/notices?types=foo&types=warning&types=bar,baz", nil)
c.Assert(err, IsNil)
noticesCmd := apiCmd("/v1/notices")
rsp, ok := noticesCmd.GET(noticesCmd, req, nil).(*resp)
c.Assert(ok, Equals, true)

c.Check(rsp.Type, Equals, ResponseTypeSync)
c.Check(rsp.Status, Equals, http.StatusOK)
notices, ok := rsp.Result.([]*state.Notice)
c.Assert(ok, Equals, true)
c.Assert(notices, HasLen, 1)
n := noticeToMap(c, notices[0])
c.Assert(n["type"], Equals, "warning")

// Check that if all types are invalid, no notices are returned, and there
// is no error.
req, err = http.NewRequest("GET", "/v1/notices?types=foo&types=bar,baz", nil)
c.Assert(err, IsNil)
noticesCmd = apiCmd("/v1/notices")
rsp, ok = noticesCmd.GET(noticesCmd, req, nil).(*resp)
c.Assert(ok, Equals, true)

c.Check(rsp.Type, Equals, ResponseTypeSync)
c.Check(rsp.Status, Equals, http.StatusOK)
notices, ok = rsp.Result.([]*state.Notice)
c.Assert(ok, Equals, true)
c.Assert(notices, HasLen, 0)
}

func (s *apiSuite) TestNoticesWait(c *C) {
s.daemon(c)

Expand Down

0 comments on commit c40bd32

Please sign in to comment.