Skip to content

Commit

Permalink
fix(daemon): handle invalid notice types in filter (#329)
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.
  • Loading branch information
olivercalder authored Nov 20, 2023
1 parent 010e042 commit f3a718c
Show file tree
Hide file tree
Showing 2 changed files with 45 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 {
// Caller did provide a types filter, but they're all invalid notice types.
// Return no notices, rather than the default of all notices.
return SyncResponse([]*state.Notice{})
}

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

Expand Down
40 changes: 40 additions & 0 deletions internals/daemon/api_notices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,46 @@ 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)
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 f3a718c

Please sign in to comment.