forked from koding/kite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
301 lines (246 loc) · 7.23 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
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
package kontrol
import (
"errors"
"fmt"
"strings"
"time"
jwt "github.com/dgrijalva/jwt-go"
"github.com/koding/kite"
"github.com/koding/kite/dnode"
"github.com/koding/kite/kitekey"
"github.com/koding/kite/kontrol/onceevery"
kontrolprotocol "github.com/koding/kite/kontrol/protocol"
"github.com/koding/kite/protocol"
)
func (k *Kontrol) handleRegister(r *kite.Request) (interface{}, error) {
k.log.Info("Register request from: %s", r.Client.Kite)
if r.Args.One().MustMap()["url"].MustString() == "" {
return nil, errors.New("invalid url")
}
var args struct {
URL string `json:"url"`
}
r.Args.One().MustUnmarshal(&args)
if args.URL == "" {
return nil, errors.New("empty url")
}
// Only accept requests with kiteKey because we need this info
// for generating tokens for this kite.
if r.Auth.Type != "kiteKey" {
return nil, fmt.Errorf("Unexpected authentication type: %s", r.Auth.Type)
}
t, err := jwt.Parse(r.Auth.Key, kitekey.GetKontrolKey)
if err != nil {
return nil, err
}
publicKey, ok := t.Claims["kontrolKey"].(string)
if !ok {
return nil, errors.New("public key is not passed")
}
var keyPair *KeyPair
var newKey bool
// check if the key is valid and is stored in the key pair storage, if not
// check if there is a new key we can use.
keyPair, err = k.keyPair.GetKeyFromPublic(strings.TrimSpace(publicKey))
if err != nil {
newKey = true
keyPair, err = k.pickKey(r)
if err != nil {
return nil, err // nothing to do here ..
}
}
kiteURL := args.URL
remote := r.Client
if err := validateKiteKey(&remote.Kite); err != nil {
return nil, err
}
value := &kontrolprotocol.RegisterValue{
URL: kiteURL,
KeyID: keyPair.ID,
}
// Register first by adding the value to the storage. Return if there is
// any error.
if err := k.storage.Upsert(&remote.Kite, value); err != nil {
k.log.Error("storage add '%s' error: %s", remote.Kite, err)
return nil, errors.New("internal error - register")
}
every := onceevery.New(UpdateInterval)
ping := make(chan struct{}, 1)
closed := false
updaterFunc := func() {
for {
select {
case <-ping:
k.log.Debug("Kite is active, got a ping %s", remote.Kite)
every.Do(func() {
k.log.Debug("Kite is active, updating the value %s", remote.Kite)
err := k.storage.Update(&remote.Kite, value)
if err != nil {
k.log.Error("storage update '%s' error: %s", remote.Kite, err)
}
})
case <-time.After(HeartbeatInterval + HeartbeatDelay):
k.log.Debug("Kite didn't sent any heartbeat %s.", remote.Kite)
every.Stop()
closed = true
return
}
}
}
go updaterFunc()
heartbeatArgs := []interface{}{
HeartbeatInterval / time.Second,
dnode.Callback(func(args *dnode.Partial) {
k.log.Debug("Kite send us an heartbeat. %s", remote.Kite)
k.clientLocks.Get(remote.Kite.ID).Lock()
defer k.clientLocks.Get(remote.Kite.ID).Unlock()
select {
case ping <- struct{}{}:
default:
}
// seems we miss a heartbeat, so start it again!
if closed {
closed = false
k.log.Warning("Updater was closed, but we are still getting heartbeats. Starting again %s",
remote.Kite)
// it might be removed because the ttl cleaner would come
// before us, so try to add it again, the updater will than
// continue to update it afterwards.
k.storage.Upsert(&remote.Kite, value)
go updaterFunc()
}
}),
}
// now trigger the remote kite so it sends us periodically an heartbeat
remote.GoWithTimeout("kite.heartbeat", 4*time.Second, heartbeatArgs...)
k.log.Info("Kite registered: %s", remote.Kite)
remote.OnDisconnect(func() {
k.log.Info("Kite disconnected: %s", remote.Kite)
every.Stop()
})
// send response back to the kite, also send the new public Key if it's exist
p := &protocol.RegisterResult{URL: args.URL}
if newKey {
p.PublicKey = keyPair.Public
}
return p, nil
}
func (k *Kontrol) handleGetKites(r *kite.Request) (interface{}, error) {
var args protocol.GetKitesArgs
if err := r.Args.One().Unmarshal(&args); err != nil {
return nil, err
}
// Get kites from the storage
kites, err := k.storage.Get(args.Query)
if err != nil {
return nil, err
}
for _, kite := range kites {
// audience will go into the token as "aud" claim.
audience := getAudience(args.Query)
keyPair, err := k.keyPair.GetKeyFromID(kite.KeyID)
if err != nil {
return nil, err
}
// Generate token once here because we are using the same token for every
// kite we return and generating many tokens is really slow.
token, err := generateToken(audience, r.Username,
k.Kite.Kite().Username, keyPair.Private)
if err != nil {
return nil, err
}
kite.Token = token
}
return &protocol.GetKitesResult{
Kites: kites,
}, nil
}
func (k *Kontrol) handleGetToken(r *kite.Request) (interface{}, error) {
var query *protocol.KontrolQuery
err := r.Args.One().Unmarshal(&query)
if err != nil {
return nil, errors.New("Invalid query")
}
// check if it's exist
kites, err := k.storage.Get(query)
if err != nil {
return nil, err
}
if len(kites) > 1 {
return nil, errors.New("query matches more than one kite")
}
if len(kites) == 0 {
return nil, errors.New("no kites found")
}
kite := kites[0]
audience := getAudience(query)
keyPair, err := k.keyPair.GetKeyFromID(kite.KeyID)
if err != nil {
return nil, err
}
return generateToken(audience, r.Username, k.Kite.Kite().Username, keyPair.Private)
}
func (k *Kontrol) handleMachine(r *kite.Request) (interface{}, error) {
var args struct {
AuthType string
}
err := r.Args.One().Unmarshal(&args)
if err != nil {
return nil, err
}
if k.MachineAuthenticate != nil {
// an empty authType is ok, the implementer is responsible of it. It
// can care of it or it can return an error
if err := k.MachineAuthenticate(args.AuthType, r); err != nil {
return nil, errors.New("cannot authenticate user")
}
}
keyPair, err := k.pickKey(r)
if err != nil {
return nil, err
}
return k.registerUser(r.Client.Kite.Username, keyPair.Public, keyPair.Private)
}
func (k *Kontrol) handleGetKey(r *kite.Request) (interface{}, error) {
// Only accept requests with kiteKey because we need this info
// for checking if the key is valid and needs to be regenerated
if r.Auth.Type != "kiteKey" {
return nil, fmt.Errorf("Unexpected authentication type: %s", r.Auth.Type)
}
t, err := jwt.Parse(r.Auth.Key, kitekey.GetKontrolKey)
if err != nil {
return nil, err
}
publicKey, ok := t.Claims["kontrolKey"].(string)
if !ok {
return nil, errors.New("public key is not passed")
}
err = k.keyPair.IsValid(publicKey)
if err == nil {
// everything is ok, just return the old one
return publicKey, nil
}
keyPair, err := k.pickKey(r)
if err != nil {
return nil, err
}
return keyPair.Public, nil
}
func (k *Kontrol) pickKey(r *kite.Request) (*KeyPair, error) {
if k.MachineKeyPicker != nil {
keyPair, err := k.MachineKeyPicker(r)
if err != nil {
return nil, err
}
return keyPair, nil
}
if len(k.lastPublic) != 0 && len(k.lastPrivate) != 0 {
return &KeyPair{
Public: k.lastPublic[len(k.lastPublic)-1],
Private: k.lastPrivate[len(k.lastPrivate)-1],
ID: k.lastIDs[len(k.lastIDs)-1],
}, nil
}
k.log.Error("neither machineKeyPicker neither public/private keys are available")
return nil, errors.New("internal error - 1")
}