-
Notifications
You must be signed in to change notification settings - Fork 81
/
auth.go
79 lines (64 loc) · 2.33 KB
/
auth.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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package apikey
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/esapi"
"github.com/elastic/fleet-server/v7/internal/pkg/es"
)
var (
ErrUnauthorized = errors.New("unauthorized")
ErrElasticsearchAuthLimit = errors.New("elasticsearch auth limit")
)
// SecurityInfo contains all related information about an APIKey that Elasticsearch tracks.
type SecurityInfo struct {
UserName string `json:"username"`
Roles []string `json:"roles"`
FullName string `json:"full_name"`
Email string `json:"email"`
Metadata json.RawMessage `json:"metadata"`
Enabled bool `json:"enabled"`
AuthRealm map[string]string `json:"authentication_realm"`
LookupRealm map[string]string `json:"lookup_realm"`
}
// Authenticate will return the SecurityInfo associated with the APIKey (retrieved from Elasticsearch).
// Note: Prefer the bulk wrapper on this API
func (k APIKey) Authenticate(ctx context.Context, client *elasticsearch.Client) (*SecurityInfo, error) {
token := fmt.Sprintf("%s%s", authPrefix, k.Token())
req := esapi.SecurityAuthenticateRequest{
Header: map[string][]string{AuthKey: []string{token}},
}
res, err := req.Do(ctx, client)
if err != nil {
return nil, fmt.Errorf("apikey auth request %s: %w", k.ID, err)
}
if res.Body != nil {
defer res.Body.Close()
}
if res.IsError() {
var returnError error
switch res.StatusCode {
case http.StatusUnauthorized:
returnError = ErrUnauthorized
case http.StatusTooManyRequests:
returnError = ErrElasticsearchAuthLimit
}
if returnError != nil {
return nil, fmt.Errorf("%w: %w", returnError, fmt.Errorf("apikey auth response %s: %s", k.ID, res.String()))
}
// body is not parsed to not give the caller too much information
return nil, es.TranslateError(res.StatusCode, nil)
}
var info SecurityInfo
decoder := json.NewDecoder(res.Body)
if err := decoder.Decode(&info); err != nil {
return nil, fmt.Errorf("apikey auth parse %s: %w", k.ID, err)
}
return &info, nil
}