forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader_checks.go
252 lines (228 loc) · 6.72 KB
/
header_checks.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2015-2016 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 asserts
import (
"crypto"
"encoding/base64"
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
// common checks used when decoding/assembling assertions
func checkExistsString(headers map[string]interface{}, name string) (string, error) {
value, ok := headers[name]
if !ok {
return "", fmt.Errorf("%q header is mandatory", name)
}
s, ok := value.(string)
if !ok {
return "", fmt.Errorf("%q header must be a string", name)
}
return s, nil
}
func checkNotEmptyString(headers map[string]interface{}, name string) (string, error) {
s, err := checkExistsString(headers, name)
if err != nil {
return "", err
}
if len(s) == 0 {
return "", fmt.Errorf("%q header should not be empty", name)
}
return s, nil
}
func checkOptionalString(headers map[string]interface{}, name string) (string, error) {
value, ok := headers[name]
if !ok {
return "", nil
}
s, ok := value.(string)
if !ok {
return "", fmt.Errorf("%q header must be a string", name)
}
return s, nil
}
func checkPrimaryKey(headers map[string]interface{}, primKey string) (string, error) {
value, err := checkNotEmptyString(headers, primKey)
if err != nil {
return "", err
}
if strings.Contains(value, "/") {
return "", fmt.Errorf("%q primary key header cannot contain '/'", primKey)
}
return value, nil
}
func checkAssertType(assertType *AssertionType) error {
if assertType == nil {
return fmt.Errorf("internal error: assertion type cannot be nil")
}
// sanity check against known canonical
sanity := typeRegistry[assertType.Name]
switch sanity {
case assertType:
// fine, matches canonical
return nil
case nil:
return fmt.Errorf("internal error: unknown assertion type: %q", assertType.Name)
default:
return fmt.Errorf("internal error: unpredefined assertion type for name %q used (unexpected address %p)", assertType.Name, assertType)
}
}
// use 'defl' default if missing
func checkIntWithDefault(headers map[string]interface{}, name string, defl int) (int, error) {
value, ok := headers[name]
if !ok {
return defl, nil
}
s, ok := value.(string)
if !ok {
return -1, fmt.Errorf("%q header is not an integer: %v", name, value)
}
m, err := strconv.Atoi(s)
if err != nil {
return -1, fmt.Errorf("%q header is not an integer: %v", name, s)
}
return m, nil
}
func checkInt(headers map[string]interface{}, name string) (int, error) {
valueStr, err := checkNotEmptyString(headers, name)
if err != nil {
return -1, err
}
value, err := strconv.Atoi(valueStr)
if err != nil {
return -1, fmt.Errorf("%q header is not an integer: %v", name, valueStr)
}
return value, nil
}
func checkRFC3339Date(headers map[string]interface{}, name string) (time.Time, error) {
dateStr, err := checkNotEmptyString(headers, name)
if err != nil {
return time.Time{}, err
}
date, err := time.Parse(time.RFC3339, dateStr)
if err != nil {
return time.Time{}, fmt.Errorf("%q header is not a RFC3339 date: %v", name, err)
}
return date, nil
}
func checkRFC3339DateWithDefault(headers map[string]interface{}, name string, defl time.Time) (time.Time, error) {
value, ok := headers[name]
if !ok {
return defl, nil
}
dateStr, ok := value.(string)
if !ok {
return time.Time{}, fmt.Errorf("%q header must be a string", name)
}
date, err := time.Parse(time.RFC3339, dateStr)
if err != nil {
return time.Time{}, fmt.Errorf("%q header is not a RFC3339 date: %v", name, err)
}
return date, nil
}
func checkUint(headers map[string]interface{}, name string, bitSize int) (uint64, error) {
valueStr, err := checkNotEmptyString(headers, name)
if err != nil {
return 0, err
}
value, err := strconv.ParseUint(valueStr, 10, bitSize)
if err != nil {
return 0, fmt.Errorf("%q header is not an unsigned integer: %v", name, valueStr)
}
return value, nil
}
func checkDigest(headers map[string]interface{}, name string, h crypto.Hash) ([]byte, error) {
digestStr, err := checkNotEmptyString(headers, name)
if err != nil {
return nil, err
}
b, err := base64.RawURLEncoding.DecodeString(digestStr)
if err != nil {
return nil, fmt.Errorf("%q header cannot be decoded: %v", name, err)
}
if len(b) != h.Size() {
return nil, fmt.Errorf("%q header does not have the expected bit length: %d", name, len(b)*8)
}
return b, nil
}
var anyString = regexp.MustCompile("")
func checkStringListInMap(m map[string]interface{}, name, what string, pattern *regexp.Regexp) ([]string, error) {
value, ok := m[name]
if !ok {
return nil, nil
}
lst, ok := value.([]interface{})
if !ok {
return nil, fmt.Errorf("%s must be a list of strings", what)
}
if len(lst) == 0 {
return nil, nil
}
res := make([]string, len(lst))
for i, v := range lst {
s, ok := v.(string)
if !ok {
return nil, fmt.Errorf("%s must be a list of strings", what)
}
if !pattern.MatchString(s) {
return nil, fmt.Errorf("%s contains an invalid element: %q", what, s)
}
res[i] = s
}
return res, nil
}
func checkStringList(headers map[string]interface{}, name string) ([]string, error) {
return checkStringListMatches(headers, name, anyString)
}
func checkStringListMatches(headers map[string]interface{}, name string, pattern *regexp.Regexp) ([]string, error) {
return checkStringListInMap(headers, name, fmt.Sprintf("%q header", name), pattern)
}
func checkStringMatches(headers map[string]interface{}, name string, pattern *regexp.Regexp) (string, error) {
s, err := checkNotEmptyString(headers, name)
if err != nil {
return "", err
}
if !pattern.MatchString(s) {
return "", fmt.Errorf("%q header contains invalid characters: %q", name, s)
}
return s, nil
}
func checkOptionalBool(headers map[string]interface{}, name string) (bool, error) {
value, ok := headers[name]
if !ok {
return false, nil
}
s, ok := value.(string)
if !ok || (s != "true" && s != "false") {
return false, fmt.Errorf("%q header must be 'true' or 'false'", name)
}
return s == "true", nil
}
func checkMap(headers map[string]interface{}, name string) (map[string]interface{}, error) {
value, ok := headers[name]
if !ok {
return nil, nil
}
m, ok := value.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("%q header must be a map", name)
}
return m, nil
}