-
Notifications
You must be signed in to change notification settings - Fork 26
/
handlers.go
162 lines (148 loc) · 4.46 KB
/
handlers.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
package auth
import (
"context"
"crypto/rsa"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"os"
"strings"
log "github.com/sirupsen/logrus"
"github.com/dgrijalva/jwt-go"
"github.com/appbaseio/reactivesearch-api/util"
)
func (a *Auth) savePublicKey(ctx context.Context, indexName string, record publicKey) (interface{}, error) {
if strings.TrimSpace(record.RoleKey) == "" {
record.RoleKey = "role"
}
// Update es index
_, err := a.es.savePublicKey(ctx, indexName, record)
if err != nil {
log.Errorln(logTag, ": error indexing public key record", logTag)
return false, err
}
return true, nil
}
func (a *Auth) getPublicKey() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
record, _ := a.es.getPublicKey(req.Context())
rawPermission, err := json.Marshal(record)
if err != nil {
msg := "public key record not found"
util.WriteBackError(w, msg, http.StatusNotFound)
return
}
util.WriteBackRaw(w, rawPermission, http.StatusOK)
}
}
func (a *Auth) setPublicKey() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
reqBody, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Errorln(logTag, ":", err)
util.WriteBackError(w, "Can't read request body", http.StatusBadRequest)
return
}
defer req.Body.Close()
var body publicKey
err = json.Unmarshal(reqBody, &body)
if err != nil {
log.Errorln(logTag, ":", err)
util.WriteBackError(w, "Can't parse request body", http.StatusBadRequest)
return
}
publicKeyIndex := os.Getenv(envPublicKeyEsIndex)
if publicKeyIndex == "" {
publicKeyIndex = defaultPublicKeyEsIndex
}
jwtRsaPublicKey, err := getJWTPublickKey(body)
if err != nil {
log.Errorln(logTag, ":", err)
util.WriteBackError(w, err.Error(), http.StatusBadRequest)
return
}
// To decide whether to just update the local state
isLocal := req.URL.Query().Get("local")
if isLocal == "true" {
// update public key locally
a.updateLocalPublicKey(jwtRsaPublicKey, body.RoleKey)
util.WriteBackMessage(w, "Public key saved successfully.", http.StatusOK)
return
}
_, err = a.savePublicKey(req.Context(), publicKeyIndex, body)
if err != nil {
log.Errorln(logTag, ":", err)
util.WriteBackError(w, err.Error(), http.StatusBadRequest)
return
}
// Invoke ACCAPI
// unmarshal body
var bodyJSON map[string]interface{}
err2 := json.Unmarshal(reqBody, &bodyJSON)
if err2 != nil {
log.Errorln(logTag, ":", err2)
util.WriteBackError(w, err2.Error(), http.StatusBadRequest)
return
}
// Only update local state when proxy API has not been called
// If proxy API would get called then it would automatically update the
// state for all machines
// Updating the local state again can cause insconsistency issues
if util.ShouldProxyToACCAPI() {
res, err := util.ProxyACCAPI(util.ProxyConfig{
Method: http.MethodPut,
URL: "/_public_key",
Body: bodyJSON, // forward body
})
if err != nil {
log.Errorln(logTag, ":", err)
util.WriteBackError(w, err.Error(), http.StatusInternalServerError)
return
}
// Failed to update all nodes, return error response
if res != nil {
log.Errorln(logTag, ":", "error encountered updating public key")
bodyBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Errorln(logTag, ":", err)
util.WriteBackError(w, err.Error(), http.StatusInternalServerError)
return
}
util.WriteBackRaw(w, bodyBytes, res.StatusCode)
return
}
} else {
// Update local state
a.updateLocalPublicKey(jwtRsaPublicKey, body.RoleKey)
}
util.WriteBackMessage(w, "Public key saved successfully.", http.StatusOK)
}
}
func (a *Auth) updateLocalPublicKey(jwtRsaPublicKey *rsa.PublicKey, role string) {
if strings.TrimSpace(role) == "" {
role = "role"
}
// Update cached public key
if jwtRsaPublicKey != nil {
a.jwtRsaPublicKey = jwtRsaPublicKey
a.jwtRoleKey = role
}
}
func getJWTPublickKey(record publicKey) (*rsa.PublicKey, error) {
var jwtRsaPublicKey *rsa.PublicKey
if record.PublicKey != "" {
publicKeyBuf, err := util.DecodeBase64Key(record.PublicKey)
if err != nil {
log.Errorln(logTag, ": error indexing public key record", err)
return nil, err
}
jwtRsaPublicKey, err = jwt.ParseRSAPublicKeyFromPEM(publicKeyBuf)
if err != nil {
log.Errorln(logTag, ": error indexing public key record", err)
return jwtRsaPublicKey, err
}
return jwtRsaPublicKey, nil
}
return nil, errors.New("public key is missing in the request body")
}