This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userauth.go
324 lines (278 loc) · 9.58 KB
/
userauth.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
package main
import (
"bufio"
"crypto/x509"
"encoding/csv"
"encoding/json"
"encoding/pem"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/golang-jwt/jwt/v4"
"github.com/lestrrat/go-jwx/jwk"
"github.com/minio/minio-go/v6/pkg/s3signer"
log "github.com/sirupsen/logrus"
)
// Authenticator is an interface that takes care of authenticating users to the
// S3 proxy. It contains only one method, Authenticate.
type Authenticator interface {
// Authenticate inspects an http.Request and returns nil if the user is
// authenticated, otherwise an error is returned.
Authenticate(r *http.Request) (jwt.MapClaims, error)
}
// AlwaysAllow is an Authenticator that always authenticates
type AlwaysAllow struct{}
// NewAlwaysAllow returns a new AlwaysAllow authenticator.
func NewAlwaysAllow() *AlwaysAllow {
return &AlwaysAllow{}
}
// Authenticate authenticates everyone.
func (u *AlwaysAllow) Authenticate(r *http.Request) (jwt.MapClaims, error) {
return nil, nil
}
// ValidateFromFile is an Authenticator that reads client ids and secret ids
// from a file.
type ValidateFromFile struct {
filename string
}
// NewValidateFromFile returns a new ValidateFromFile, reading users from the
// supplied file.
func NewValidateFromFile(filename string) *ValidateFromFile {
return &ValidateFromFile{filename}
}
// ValidateFromToken is an Authenticator that reads the public key from
// supplied file
type ValidateFromToken struct {
pubkeys map[string][]byte
}
// NewValidateFromToken returns a new ValidateFromToken, reading the key from
// the supplied file.
func NewValidateFromToken(pubkeys map[string][]byte) *ValidateFromToken {
return &ValidateFromToken{pubkeys}
}
// Authenticate checks whether the http.Request is signed by any of the users
// in the supplied file.
func (u *ValidateFromFile) Authenticate(r *http.Request) error {
re := regexp.MustCompile("Credential=([^/]+)/")
auth := r.Header.Get("Authorization")
curAccessKey := ""
if tmp := re.FindStringSubmatch(auth); tmp != nil {
// Check if user requested own bucket
curAccessKey = tmp[1]
re := regexp.MustCompile("/([^/]+)/")
if curAccessKey != re.FindStringSubmatch(r.URL.Path)[1] {
return fmt.Errorf("user not authorized to access location")
}
} else {
log.Debugf("No credentials in Authorization header (%s)", auth)
return fmt.Errorf("authorization header had no credentials")
}
//nolint:nestif
if curSecretKey, err := u.secretFromID(curAccessKey); err == nil {
if r.Method == http.MethodGet {
re := regexp.MustCompile("Signature=(.*)")
signature := re.FindStringSubmatch(auth)
if signature == nil || len(signature) < 2 {
return fmt.Errorf("signature not found in Authorization header (%s)", auth)
}
// Create signing request
nr, e := http.NewRequest(r.Method, r.URL.String(), r.Body)
if e != nil {
log.Debug("error creating the new request")
log.Debug(e)
}
// Add required headers
nr.Header.Set("X-Amz-Date", r.Header.Get("X-Amz-Date"))
nr.Header.Set("X-Amz-Content-Sha256", r.Header.Get("X-Amz-Content-Sha256"))
nr.Host = r.Host
nr.URL.RawQuery = r.URL.RawQuery
// Sign the new request
s3signer.SignV4(*nr, curAccessKey, curSecretKey, "", "us-east-1")
curSignature := re.FindStringSubmatch(nr.Header.Get("Authorization"))
if curSignature == nil || len(signature) < 2 {
return fmt.Errorf("generated outgoing signature not found or unexpected (header wass %s)",
nr.Header.Get("Authorization"))
}
// Compare signatures
if curSignature[1] != signature[1] {
return fmt.Errorf("signature for outgoing (%s)request does not match incoming (%s",
curSignature[1], signature[1])
}
}
} else {
log.Debugf("Found no secret for user %s", curAccessKey)
return fmt.Errorf("no secret for user %s found", curAccessKey)
}
return nil
}
func (u *ValidateFromFile) secretFromID(id string) (string, error) {
f, e := os.Open(u.filename)
if e != nil {
log.Panicf("Error opening users file (%s): %v",
u.filename,
e)
}
defer func() {
if err := f.Close(); err != nil {
log.Debugf("Error on close: %v", err)
}
}()
r := csv.NewReader(bufio.NewReader(f))
for {
record, e := r.Read()
if e == io.EOF {
break
}
if record[0] == id {
log.Debugf("Returning secret for id %s", id)
return record[1], nil
}
}
log.Debugf("No secret found for id %s in %s", id, u.filename)
return "", fmt.Errorf("cannot find id %s in %s", id, u.filename)
}
// Authenticate verifies that the token included in the http.Request
// is valid
func (u *ValidateFromToken) Authenticate(r *http.Request) (claims jwt.MapClaims, err error) {
var ok bool
// Verify signature by parsing the token with the given key
tokenStr := r.Header.Get("X-Amz-Security-Token")
if tokenStr == "" {
return nil, fmt.Errorf("no access token supplied")
}
token, err := jwt.Parse(tokenStr, func(tokenStr *jwt.Token) (interface{}, error) { return nil, nil })
// Return error if token is broken (without claims)
if claims, ok = token.Claims.(jwt.MapClaims); !ok {
return nil, fmt.Errorf("broken token (claims are empty): %v\nerror: %s", claims, err)
}
strIss := fmt.Sprintf("%v", claims["iss"])
// Poor string unescaper for elixir
strIss = strings.ReplaceAll(strIss, "\\", "")
log.Debugf("Looking for key for %s", strIss)
re := regexp.MustCompile(`//([^/]*)`)
//nolint:nestif
if token.Header["alg"] == "ES256" {
key, err := jwt.ParseECPublicKeyFromPEM(u.pubkeys[re.FindStringSubmatch(strIss)[1]])
if err != nil {
return nil, fmt.Errorf("failed to parse EC public key (%v)", err)
}
_, err = jwt.Parse(tokenStr, func(tokenStr *jwt.Token) (interface{}, error) { return key, nil })
// Validate the error
v, _ := err.(*jwt.ValidationError)
// If error is for expired token continue
if err != nil && v.Errors != jwt.ValidationErrorExpired {
return nil, fmt.Errorf("signed token (ES256) not valid: %v, (token was %s)", err, tokenStr)
}
} else if token.Header["alg"] == "RS256" {
key, err := jwt.ParseRSAPublicKeyFromPEM(u.pubkeys[re.FindStringSubmatch(strIss)[1]])
if err != nil {
return nil, fmt.Errorf("failed to parse RSA256 public key (%v)", err)
}
_, err = jwt.Parse(tokenStr, func(tokenStr *jwt.Token) (interface{}, error) { return key, nil })
// Validate the error
v, _ := err.(*jwt.ValidationError)
// If error is for expired token continue
if err != nil && v.Errors != jwt.ValidationErrorExpired {
return nil, fmt.Errorf("signed token (RS256) not valid: %v, (token was %s)", err, tokenStr)
}
} else {
return nil, fmt.Errorf("unsupported algorithm %s", token.Header["alg"])
}
// Check whether token username and filepath match
re = regexp.MustCompile("/([^/]+)/")
username := re.FindStringSubmatch(r.URL.Path)[1]
//nolint:nestif
// Case for Elixir and CEGA usernames: Replace @ with _ character
if strings.Contains(fmt.Sprintf("%v", claims["sub"]), "@") {
claimString := fmt.Sprintf("%v", claims["sub"])
if strings.ReplaceAll(claimString, "@", "_") != username {
return nil, fmt.Errorf("token supplied username %s but URL had %s",
claims["sub"], username)
}
} else {
if claims["sub"] != username {
return nil, fmt.Errorf("token supplied username %s but URL had %s",
claims["sub"], username)
}
}
return claims, nil
}
// Function for reading the ega key in []byte
func (u *ValidateFromToken) getjwtkey(jwtpubkeypath string) error {
re := regexp.MustCompile(`(.*)\.+`)
err := filepath.Walk(jwtpubkeypath,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.Mode().IsRegular() {
log.Debug("Reading file: ", filepath.Join(filepath.Clean(jwtpubkeypath), info.Name()))
keyData, err := os.ReadFile(filepath.Join(filepath.Clean(jwtpubkeypath), info.Name()))
if err != nil {
return fmt.Errorf("token file error: %v", err)
}
nameMatch := re.FindStringSubmatch(info.Name())
if nameMatch == nil || len(nameMatch) < 2 {
return fmt.Errorf("unexpected lack of substring match in filename %s", info.Name())
}
u.pubkeys[nameMatch[1]] = keyData
}
return nil
})
if err != nil {
return fmt.Errorf("failed to get public key files (%v)", err)
}
return nil
}
// Function for fetching the elixir key from the JWK and transform it to []byte
func (u *ValidateFromToken) getjwtpubkey(jwtpubkeyurl string) error {
re := regexp.MustCompile("/([^/]+)/")
keyMatch := re.FindStringSubmatch(jwtpubkeyurl)
if keyMatch == nil {
return fmt.Errorf("not valid link for key %s", jwtpubkeyurl)
}
if len(keyMatch) < 2 {
return fmt.Errorf("unexpected lack of submatches in %s", jwtpubkeyurl)
}
key := keyMatch[1]
set, err := jwk.Fetch(jwtpubkeyurl)
if err != nil {
return fmt.Errorf("jwk.Fetch failed (%v) for %s", err, jwtpubkeyurl)
}
keyEl, err := set.Keys[0].Materialize()
if err != nil {
return fmt.Errorf("failed to materialize public key (%v)", err)
}
pkeyBytes, err := x509.MarshalPKIXPublicKey(keyEl)
if err != nil {
return fmt.Errorf("failed to marshal public key (%v)", err)
}
log.Debugf("Getting key from %s", jwtpubkeyurl)
r, err := http.Get(jwtpubkeyurl)
if err != nil {
return fmt.Errorf("failed to get JWK (%v)", err)
}
b, err := io.ReadAll(r.Body)
if err != nil {
return fmt.Errorf("failed to read key response (%v)", err)
}
defer r.Body.Close()
var keytype map[string][]map[string]string
err = json.Unmarshal(b, &keytype)
if err != nil {
return fmt.Errorf("failed to unmarshal key response (%v, response was %s)", err, b)
}
keyData := pem.EncodeToMemory(
&pem.Block{
Type: keytype["keys"][0]["kty"] + " PUBLIC KEY",
Bytes: pkeyBytes,
},
)
u.pubkeys[key] = keyData
log.Debugf("Registered public key for %s", key)
return nil
}