-
Notifications
You must be signed in to change notification settings - Fork 97
/
state.go
267 lines (250 loc) · 6.79 KB
/
state.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright 2023 - MinIO, Inc. All rights reserved.
// Use of this source code is governed by the AGPLv3
// license that can be found in the LICENSE file.
package kes
import (
"fmt"
"log/slog"
"maps"
"net"
"net/http"
"time"
"aead.dev/mem"
"github.com/minio/kes-go"
"github.com/minio/kes/internal/api"
"github.com/minio/kes/internal/metric"
)
type serverState struct {
Addr net.Addr
StartTime time.Time
Admin kes.Identity
Keys *keyCache
Policies map[string]*kes.Policy
Identities map[kes.Identity]identityEntry
Metrics *metric.Metrics
Routes map[string]api.Route
LogHandler *logHandler
Log *slog.Logger
Audit *auditLogger
}
type identityEntry struct {
Name string
*kes.Policy
}
func initRoutes(s *Server, routeConfig map[string]RouteConfig) (*http.ServeMux, map[string]api.Route) {
routes := map[string]api.Route{
api.PathVersion: {
Method: http.MethodGet,
Path: api.PathVersion,
MaxBody: 0,
Timeout: 10 * time.Second,
Auth: api.InsecureSkipVerify,
Handler: api.HandlerFunc(s.version),
},
api.PathReady: {
Method: http.MethodGet,
Path: api.PathReady,
MaxBody: 0,
Timeout: 15 * time.Second,
Auth: (*verifyIdentity)(&s.state),
Handler: api.HandlerFunc(s.ready),
},
api.PathStatus: {
Method: http.MethodGet,
Path: api.PathStatus,
MaxBody: 0,
Timeout: 15 * time.Second,
Auth: (*verifyIdentity)(&s.state),
Handler: api.HandlerFunc(s.status),
},
api.PathMetrics: {
Method: http.MethodGet,
Path: api.PathMetrics,
MaxBody: 0,
Timeout: 15 * time.Second,
Auth: (*verifyIdentity)(&s.state),
Handler: api.HandlerFunc(s.metrics),
},
api.PathListAPIs: {
Method: http.MethodGet,
Path: api.PathListAPIs,
MaxBody: 0,
Timeout: 10 * time.Second,
Auth: (*verifyIdentity)(&s.state),
Handler: api.HandlerFunc(s.listAPIs),
},
api.PathKeyCreate: {
Method: http.MethodPut,
Path: api.PathKeyCreate,
MaxBody: 0,
Timeout: 15 * time.Second,
Auth: (*verifyIdentity)(&s.state),
Handler: api.HandlerFunc(s.createKey),
},
api.PathKeyImport: {
Method: http.MethodPut,
Path: api.PathKeyImport,
MaxBody: 1 * mem.MB,
Timeout: 15 * time.Second,
Auth: (*verifyIdentity)(&s.state),
Handler: api.HandlerFunc(s.importKey),
},
api.PathKeyDescribe: {
Method: http.MethodGet,
Path: api.PathKeyDescribe,
MaxBody: 0,
Timeout: 15 * time.Second,
Auth: (*verifyIdentity)(&s.state),
Handler: api.HandlerFunc(s.describeKey),
},
api.PathKeyList: {
Method: http.MethodGet,
Path: api.PathKeyList,
MaxBody: 0,
Timeout: 15 * time.Second,
Auth: (*verifyIdentity)(&s.state),
Handler: api.HandlerFunc(s.listKeys),
},
api.PathKeyDelete: {
Method: http.MethodDelete,
Path: api.PathKeyDelete,
MaxBody: 0,
Timeout: 15 * time.Second,
Auth: (*verifyIdentity)(&s.state),
Handler: api.HandlerFunc(s.deleteKey),
},
api.PathKeyEncrypt: {
Method: http.MethodPut,
Path: api.PathKeyEncrypt,
MaxBody: 1 * mem.MB,
Timeout: 15 * time.Second,
Auth: (*verifyIdentity)(&s.state),
Handler: api.HandlerFunc(s.encryptKey),
},
api.PathKeyGenerate: {
Method: http.MethodPut,
Path: api.PathKeyGenerate,
MaxBody: 1 * mem.MB,
Timeout: 15 * time.Second,
Auth: (*verifyIdentity)(&s.state),
Handler: api.HandlerFunc(s.generateKey),
},
api.PathKeyDecrypt: {
Method: http.MethodPut,
Path: api.PathKeyDecrypt,
MaxBody: 1 * mem.MB,
Timeout: 15 * time.Second,
Auth: (*verifyIdentity)(&s.state),
Handler: api.HandlerFunc(s.decryptKey),
},
api.PathPolicyDescribe: {
Method: http.MethodGet,
Path: api.PathPolicyDescribe,
MaxBody: 0,
Timeout: 15 * time.Second,
Auth: (*verifyIdentity)(&s.state),
Handler: api.HandlerFunc(s.describePolicy),
},
api.PathPolicyRead: {
Method: http.MethodGet,
Path: api.PathPolicyRead,
MaxBody: 0,
Timeout: 15 * time.Second,
Auth: (*verifyIdentity)(&s.state),
Handler: api.HandlerFunc(s.readPolicy),
},
api.PathPolicyList: {
Method: http.MethodGet,
Path: api.PathPolicyList,
MaxBody: 0,
Timeout: 15 * time.Second,
Auth: (*verifyIdentity)(&s.state),
Handler: api.HandlerFunc(s.listPolicies),
},
api.PathIdentityDescribe: {
Method: http.MethodGet,
Path: api.PathIdentityDescribe,
MaxBody: 0,
Timeout: 15 * time.Second,
Auth: (*verifyIdentity)(&s.state),
Handler: api.HandlerFunc(s.describeIdentity),
},
api.PathIdentityList: {
Method: http.MethodGet,
Path: api.PathIdentityList,
MaxBody: 0,
Timeout: 15 * time.Second,
Auth: (*verifyIdentity)(&s.state),
Handler: api.HandlerFunc(s.listIdentities),
},
api.PathIdentitySelfDescribe: {
Method: http.MethodGet,
Path: api.PathIdentitySelfDescribe,
MaxBody: 0,
Timeout: 15 * time.Second,
Auth: insecureIdentifyOnly{}, // Anyone can use the self-describe API as long as a client cert is provided
Handler: api.HandlerFunc(s.selfDescribeIdentity),
},
api.PathLogError: {
Method: http.MethodGet,
Path: api.PathLogError,
MaxBody: 0,
Timeout: 0, // No timeout
Auth: (*verifyIdentity)(&s.state),
Handler: api.HandlerFunc(s.logError),
},
api.PathLogAudit: {
Method: http.MethodGet,
Path: api.PathLogAudit,
MaxBody: 0,
Timeout: 0, // No timeout
Auth: (*verifyIdentity)(&s.state),
Handler: api.HandlerFunc(s.logAudit),
},
}
for path, conf := range routeConfig { // apply API customization
route, ok := routes[path]
if !ok {
continue
}
if conf.InsecureSkipAuth {
route.Auth = insecureIdentifyOnly{}
}
if conf.Timeout > 0 {
route.Timeout = conf.Timeout
}
routes[path] = route
}
mux := http.NewServeMux()
for path, route := range routes {
mux.Handle(path, route)
}
return mux, routes
}
func initPolicies(policies map[string]Policy) (map[string]*kes.Policy, map[kes.Identity]identityEntry, error) {
policySet := make(map[string]*kes.Policy, len(policies))
identitySet := make(map[kes.Identity]identityEntry, len(policies))
for name, policy := range policies {
if !validName(name) {
return nil, nil, fmt.Errorf("kes: policy name '%s' is empty, too long or contains invalid characters", name)
}
p := &kes.Policy{
Allow: maps.Clone(policy.Allow),
Deny: maps.Clone(policy.Deny),
}
policySet[name] = p
for _, id := range policy.Identities {
if !validName(id.String()) {
return nil, nil, fmt.Errorf("kes: identity '%s' is empty, too long or contains invalid characters", id)
}
if _, ok := identitySet[id]; ok {
return nil, nil, fmt.Errorf("kes: cannot assign policy '%s' to '%v': identity already has a policy", name, id)
}
identitySet[id] = identityEntry{
Name: name,
Policy: p,
}
}
}
return policySet, identitySet, nil
}