-
Notifications
You must be signed in to change notification settings - Fork 2k
/
server.go
402 lines (367 loc) · 11.6 KB
/
server.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package server
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"net"
"net/http"
"regexp"
"sort"
"strings"
"time"
"github.com/cesanta/glog"
"github.com/docker/distribution/registry/auth/token"
"github.com/labring/sealos/pkg/client-go/kubernetes"
"github.com/labring/sealos/service/hub/api"
"github.com/labring/sealos/service/hub/auth"
"github.com/labring/sealos/service/hub/utils"
)
var (
hostPortRegex = regexp.MustCompile(`^(?:\[(.+)\]:\d+|([^:]+):\d+)$`)
scopeRegex = regexp.MustCompile(`([a-z0-9]+)(\([a-z0-9]+\))?`)
)
type AuthServer struct {
config *Config
authenticators api.Authenticator
authorizers api.Authorizer
reqLimiter *utils.Limiter
}
func NewAuthServer(c *Config) (*AuthServer, error) {
as := &AuthServer{
config: c,
authenticators: auth.NewSealosAuthn(),
authorizers: auth.NewSealosAuthz(),
// NewLimiter will start a go routine to reset reqLimiter
reqLimiter: utils.NewLimiter(c.Server.MaxRequestsPerIP, c.Server.MaxRequestsPerAccount, c.Server.ReqLimitersResetInterval),
}
return as, nil
}
type AuthRequest struct {
RemoteConnAddr string
RemoteAddr string
RemoteIP net.IP
User string
Password api.PasswordString
Account string
Service string
Scopes []AuthScope
Labels api.Labels
}
type AuthScope struct {
Type string
Class string
Name string
Actions []string
}
type AuthzResult struct {
scope AuthScope
autorizedActions []string
}
func (ar AuthRequest) String() string {
return fmt.Sprintf("{%s:%s@%s %s}", ar.User, ar.Password, ar.RemoteAddr, ar.Scopes)
}
func parseRemoteAddr(ra string) net.IP {
hp := hostPortRegex.FindStringSubmatch(ra)
if hp != nil {
if hp[1] != "" {
ra = hp[1]
} else if hp[2] != "" {
ra = hp[2]
}
}
res := net.ParseIP(ra)
return res
}
func parseScope(scope string) (string, string, error) {
parts := scopeRegex.FindStringSubmatch(scope)
if parts == nil {
return "", "", fmt.Errorf("malformed scope request")
}
switch len(parts) {
case 3:
return parts[1], "", nil
case 4:
return parts[1], parts[3], nil
default:
return "", "", fmt.Errorf("malformed scope request")
}
}
func (as *AuthServer) ParseRequest(req *http.Request) (*AuthRequest, error) {
ar := &AuthRequest{RemoteConnAddr: req.RemoteAddr, RemoteAddr: req.RemoteAddr}
ar.RemoteIP = parseRemoteAddr(ar.RemoteAddr)
if ar.RemoteIP == nil {
return nil, fmt.Errorf("unable to parse remote addr %s", ar.RemoteAddr)
}
user, password, haveBasicAuth := req.BasicAuth()
if haveBasicAuth {
ar.User = user
ar.Password = api.PasswordString(password)
} else if req.Method == "POST" {
// username and password could be part of form data
username := req.FormValue("username")
password := req.FormValue("password")
if username != "" && password != "" {
ar.User = username
ar.Password = api.PasswordString(password)
}
}
ar.Account = req.FormValue("account")
if ar.Account == "" {
ar.Account = ar.User
} else if haveBasicAuth && ar.Account != ar.User {
return nil, fmt.Errorf("user and account are not the same (%q vs %q)", ar.User, ar.Account)
}
ar.Service = req.FormValue("service")
if err := req.ParseForm(); err != nil {
return nil, fmt.Errorf("invalid form value")
}
// https://github.com/docker/distribution/blob/1b9ab303a477ded9bdd3fc97e9119fa8f9e58fca/docs/spec/auth/scope.md#resource-scope-grammar
if req.FormValue("scope") != "" {
for _, scopeValue := range req.Form["scope"] {
for _, scopeStr := range strings.Split(scopeValue, " ") {
parts := strings.Split(scopeStr, ":")
var scope AuthScope
scopeType, scopeClass, err := parseScope(parts[0])
if err != nil {
return nil, err
}
switch len(parts) {
case 3:
scope = AuthScope{
Type: scopeType,
Class: scopeClass,
Name: parts[1],
Actions: strings.Split(parts[2], ","),
}
case 4:
scope = AuthScope{
Type: scopeType,
Class: scopeClass,
Name: parts[1] + ":" + parts[2],
Actions: strings.Split(parts[3], ","),
}
default:
return nil, fmt.Errorf("invalid scope: %q", scopeStr)
}
sort.Strings(scope.Actions)
ar.Scopes = append(ar.Scopes, scope)
}
}
}
return ar, nil
}
func (as *AuthServer) Authenticate(ar *AuthRequest) (bool, api.Labels, kubernetes.Client, error) {
result, labels, client, err := as.authenticators.Authenticate(ar.Account, ar.Password)
glog.V(2).Infof("Authn %s -> %t, %+v, %v", ar.Account, result, labels, err)
if err != nil {
if err == api.ErrNoMatch {
return false, nil, nil, err
} else if err == api.ErrWrongPass {
glog.Warningf("Failed authentication with %s: %s", err, ar.Account)
return false, nil, nil, nil
}
err = fmt.Errorf("authentication returned error: %s", err)
glog.Errorf("%s: %s", ar, err)
return false, nil, nil, err
}
return result, labels, client, nil
}
func (as *AuthServer) authorizeScope(client kubernetes.Client, ai *api.AuthRequestInfo) ([]string, error) {
// if client is nil, authorize anonymously by using server.kubeconfig
if client == nil {
glog.V(2).Infof("Authorize anonymously")
client = k8sClient
}
result, err := as.authorizers.Authorize(client, ai)
glog.V(2).Infof("Authz %s -> %s, %s", *ai, result, err)
if err != nil {
if err == api.ErrNoMatch {
return nil, nil
}
err = fmt.Errorf("authz # returned error: %s", err)
glog.Errorf("%s: %s", *ai, err)
return nil, err
}
return result, nil
}
func (as *AuthServer) Authorize(client kubernetes.Client, ar *AuthRequest) ([]AuthzResult, error) {
var ares []AuthzResult
for _, scope := range ar.Scopes {
ai := &api.AuthRequestInfo{
Account: ar.Account,
Type: scope.Type,
Name: scope.Name,
Service: ar.Service,
IP: ar.RemoteIP,
Actions: scope.Actions,
Labels: ar.Labels,
}
actions, err := as.authorizeScope(client, ai)
if err != nil {
return nil, err
}
ares = append(ares, AuthzResult{scope: scope, autorizedActions: actions})
}
return ares, nil
}
// CreateToken https://github.com/docker/distribution/blob/master/docs/spec/auth/token.md#example
func (as *AuthServer) CreateToken(ar *AuthRequest, ares []AuthzResult) (string, error) {
now := time.Now().Unix()
tc := &as.config.Token
// Sign something dummy to find out which algorithm is used.
_, sigAlg, err := tc.privateKey.Sign(strings.NewReader("dummy"), 0)
if err != nil {
return "", fmt.Errorf("failed to sign: %s", err)
}
header := token.Header{
Type: "JWT",
SigningAlg: sigAlg,
KeyID: tc.publicKey.KeyID(),
}
headerJSON, err := json.Marshal(header)
if err != nil {
return "", fmt.Errorf("failed to marshal header: %s", err)
}
// @see: https://github.com/distribution/distribution/blob/main/registry/auth/token/token_test.go#L111
randomBytes := make([]byte, 15)
if _, err = rand.Read(randomBytes); err != nil {
return "", fmt.Errorf("unable to read random bytes for jwt id: %s", err)
}
claims := token.ClaimSet{
Issuer: tc.Issuer,
Subject: ar.Account,
Audience: ar.Service,
NotBefore: now - 10,
IssuedAt: now,
Expiration: now + tc.Expiration,
JWTID: base64.URLEncoding.EncodeToString(randomBytes),
Access: []*token.ResourceActions{},
}
for _, a := range ares {
ra := &token.ResourceActions{
Type: a.scope.Type,
Name: a.scope.Name,
Actions: a.autorizedActions,
}
if ra.Actions == nil {
ra.Actions = []string{}
}
sort.Strings(ra.Actions)
claims.Access = append(claims.Access, ra)
}
claimsJSON, err := json.Marshal(claims)
if err != nil {
return "", fmt.Errorf("failed to marshal claims: %s", err)
}
payload := fmt.Sprintf("%s%s%s", joseBase64UrlEncode(headerJSON), token.TokenSeparator, joseBase64UrlEncode(claimsJSON))
sig, sigAlg2, err := tc.privateKey.Sign(strings.NewReader(payload), 0)
if err != nil || sigAlg2 != sigAlg {
return "", fmt.Errorf("failed to sign token: %s", err)
}
glog.Infof("New token for %s %+v: %s", *ar, ar.Labels, claimsJSON)
return fmt.Sprintf("%s%s%s", payload, token.TokenSeparator, joseBase64UrlEncode(sig)), nil
}
func (as *AuthServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
glog.V(3).Infof("Request: %+v", req)
pathPrefix := as.config.Server.PathPrefix
switch {
case req.URL.Path == pathPrefix+"/auth/issuer":
as.doIndex(rw, req)
case req.URL.Path == pathPrefix+"/auth":
as.doAuth(rw, req)
default:
http.Error(rw, "Not found", http.StatusNotFound)
return
}
}
func (as *AuthServer) doIndex(rw http.ResponseWriter, _ *http.Request) {
rw.Header().Set("Content-Type", "text/html; charset=utf-8")
// nosemgrep go.lang.security.audit.xss.no-fprintf-to-responsewriter.no-fprintf-to-responsewriter
fmt.Fprintf(rw, "<h1>%s</h1>\n", as.config.Token.Issuer)
}
func (as *AuthServer) doAuth(rw http.ResponseWriter, req *http.Request) {
ar, err := as.ParseRequest(req)
var ares []AuthzResult
if err != nil {
glog.Warningf("Bad request: %s", err)
http.Error(rw, fmt.Sprintf("Bad request: %s", err), http.StatusBadRequest)
return
}
glog.V(2).Infof("Auth request: %+v", ar)
authnResult, labels, client, err := as.Authenticate(ar)
if err != nil {
http.Error(rw, fmt.Sprintf("Authentication failed (%s)", err), http.StatusInternalServerError)
return
}
if !authnResult {
glog.Warningf("Auth failed: %s", *ar)
rw.Header()["WWW-Authenticate"] = []string{fmt.Sprintf(`Basic realm="%s"`, as.config.Token.Issuer)}
http.Error(rw, "Auth failed.", http.StatusUnauthorized)
return
}
ar.Labels = labels
// Check if the request is coming from a valid IP and account
if !as.allowRequest(ar, (client == nil)) {
glog.Infof("Too many pull requests from %s, %s", ar.RemoteIP.String(), ar.Account)
http.Error(rw, "Too many pull requests", http.StatusTooManyRequests)
return
}
if len(ar.Scopes) > 0 {
ares, err = as.Authorize(client, ar)
if err != nil {
http.Error(rw, fmt.Sprintf("Authorization failed (%s)", err), http.StatusInternalServerError)
return
}
} else {
// Authentication-only request ("docker login"), pass through.
glog.Info("no scopes")
}
tk, err := as.CreateToken(ar, ares)
if err != nil {
msg := fmt.Sprintf("Failed to generate token %s", err)
http.Error(rw, msg, http.StatusInternalServerError)
glog.Errorf("%s: %s", ar, msg)
return
}
// https://www.oauth.com/oauth2-servers/access-tokens/access-token-response/
// describes that the response should have the token in `access_token`
// https://docs.docker.com/registry/spec/auth/token/#token-response-fields
// the token should also be in `token` to support older clients
result, _ := json.Marshal(&map[string]string{"access_token": tk, "token": tk})
glog.V(3).Infof("%s", result)
rw.Header().Set("Content-Type", "application/json")
// nosemgrep go.lang.security.audit.xss.no-direct-write-to-responsewriter.no-direct-write-to-responsewriter
_, _ = rw.Write(result)
}
func (as *AuthServer) Stop() {
as.authenticators.Stop()
as.authorizers.Stop()
glog.Infof("Server stopped")
}
// Copy-pasted from libtrust where it is private.
func joseBase64UrlEncode(b []byte) string {
return strings.TrimRight(base64.URLEncoding.EncodeToString(b), "=")
}
// allowRequest checks if the request is coming from a valid IP and account
func (as *AuthServer) allowRequest(ar *AuthRequest, isAnomaly bool) bool {
if len(ar.Scopes) == 0 {
return true
}
for _, cidr := range as.config.Server.WhiteIPCidrList {
if utils.IsIPInCIDR(ar.RemoteIP, cidr) {
return true
}
}
for _, user := range as.config.Server.WhiteUserList {
if ar.Account == user {
return true
}
}
// anomaly user pull/push request, check IP
if isAnomaly {
return as.reqLimiter.AllowIP(ar.RemoteIP.String())
}
// nomal user pull/push request, check account name
return as.reqLimiter.AllowUser(ar.User)
}