-
Notifications
You must be signed in to change notification settings - Fork 53
/
api_notices.go
171 lines (147 loc) · 4.84 KB
/
api_notices.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright (c) 2023 Canonical Ltd
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package daemon
import (
"context"
"encoding/json"
"errors"
"net/http"
"regexp"
"github.com/canonical/x-go/strutil"
"github.com/canonical/pebble/internals/overlord/state"
)
// Ensure custom keys are in the form "domain.com/key" (but somewhat more restrictive).
var customKeyRegexp = regexp.MustCompile(
`^[a-z0-9]+(-[a-z0-9]+)*(\.[a-z0-9]+(-[a-z0-9]+)*)+(/[a-z0-9]+(-[a-z0-9]+)*)+$`)
const (
maxNoticeKeyLength = 256
maxNoticeDataSize = 4 * 1024
)
type addedNotice struct {
ID string `json:"id"`
}
func v1GetNotices(c *Command, r *http.Request, _ *UserState) Response {
query := r.URL.Query()
typeStrs := strutil.MultiCommaSeparatedList(query["types"])
types := make([]state.NoticeType, 0, len(typeStrs))
for _, typeStr := range typeStrs {
noticeType := state.NoticeType(typeStr)
if !noticeType.Valid() {
// Ignore invalid notice types (so requests from newer clients
// with unknown types succeed).
continue
}
types = append(types, noticeType)
}
keys := strutil.MultiCommaSeparatedList(query["keys"])
after, err := parseOptionalTime(query.Get("after"))
if err != nil {
return statusBadRequest(`invalid "after" timestamp: %v`, err)
}
filter := &state.NoticeFilter{
Types: types,
Keys: keys,
After: after,
}
var notices []*state.Notice
st := c.d.overlord.State()
st.Lock()
defer st.Unlock()
timeout, err := parseOptionalDuration(query.Get("timeout"))
if err != nil {
return statusBadRequest("invalid timeout: %v", err)
}
if timeout != 0 {
// Wait up to timeout for notices matching given filter to occur
ctx, cancel := context.WithTimeout(r.Context(), timeout)
defer cancel()
notices, err = st.WaitNotices(ctx, filter)
if errors.Is(err, context.Canceled) {
return statusBadRequest("request canceled")
}
// DeadlineExceeded will occur if timeout elapses; in that case return
// an empty list of notices, not an error.
if err != nil && !errors.Is(err, context.DeadlineExceeded) {
return statusInternalError("cannot wait for notices: %s", err)
}
} else {
// No timeout given, fetch currently-available notices
notices = st.Notices(filter)
}
if notices == nil {
notices = []*state.Notice{} // avoid null result
}
return SyncResponse(notices)
}
func v1PostNotices(c *Command, r *http.Request, _ *UserState) Response {
var payload struct {
Action string `json:"action"`
Type string `json:"type"`
Key string `json:"key"`
RepeatAfter string `json:"repeat-after"`
DataJSON json.RawMessage `json:"data"`
}
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&payload); err != nil {
return statusBadRequest("cannot decode request body: %v", err)
}
if payload.Action != "add" {
return statusBadRequest("invalid action %q", payload.Action)
}
if payload.Type != "custom" {
return statusBadRequest(`invalid type %q (can only add "custom" notices)`, payload.Type)
}
if !customKeyRegexp.MatchString(payload.Key) {
return statusBadRequest(`invalid key %q (must be in "domain.com/key" format)`, payload.Key)
}
if len(payload.Key) > maxNoticeKeyLength {
return statusBadRequest("key must be %d bytes or less", maxNoticeKeyLength)
}
repeatAfter, err := parseOptionalDuration(payload.RepeatAfter)
if err != nil {
return statusBadRequest("invalid repeat-after: %v", err)
}
if len(payload.DataJSON) > maxNoticeDataSize {
return statusBadRequest("total size of data must be %d bytes or less", maxNoticeDataSize)
}
var data map[string]string
if len(payload.DataJSON) > 0 {
err = json.Unmarshal(payload.DataJSON, &data)
if err != nil {
return statusBadRequest("cannot decode notice data: %v", err)
}
}
st := c.d.overlord.State()
st.Lock()
defer st.Unlock()
noticeId, err := st.AddNotice(state.CustomNotice, payload.Key, &state.AddNoticeOptions{
Data: data,
RepeatAfter: repeatAfter,
})
if err != nil {
return statusInternalError("%v", err)
}
return SyncResponse(addedNotice{ID: noticeId})
}
func v1GetNotice(c *Command, r *http.Request, _ *UserState) Response {
noticeID := muxVars(r)["id"]
st := c.d.overlord.State()
st.Lock()
defer st.Unlock()
notice := st.Notice(noticeID)
if notice == nil {
return statusNotFound("cannot find notice with id %q", noticeID)
}
return SyncResponse(notice)
}