-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtenant.go
284 lines (221 loc) · 6.09 KB
/
tenant.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
package spice
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"errors"
"fmt"
"io"
"net"
"github.com/jsimonetti/go-spice/red"
)
type tenantHandshake struct {
proxy *Proxy
done bool
tenantAuthMethod red.AuthMethod
privateKey *rsa.PrivateKey
channelID uint8
channelType red.ChannelType
sessionID uint32
otp string // one time password
destination string // compute computeAddress
log Logger
}
func newTenantHandshake(p *Proxy, log Logger) (*tenantHandshake, error) {
handShake := &tenantHandshake{
proxy: p,
log: log,
}
rng := rand.Reader
key, err := rsa.GenerateKey(rng, 1024)
if err != nil {
return nil, err
}
handShake.privateKey = key
return handShake, nil
}
func (c *tenantHandshake) Done() bool {
return c.done
}
func (c *tenantHandshake) clientLinkStage(tenant io.ReadWriter) (net.Conn, error) {
// Handle first Tenant Link Message
if err := c.clientLinkMessage(tenant); err != nil {
return nil, err
}
c.otp = c.proxy.sessionTable.OTP(c.sessionID)
c.destination = c.proxy.sessionTable.Compute(c.sessionID)
// Handle 2nd Tenant auth method select
if err := c.clientAuthMethod(tenant); err != nil {
return nil, err
}
// Do compute handshake
handShake := &computeHandshake{
proxy: c.proxy,
channelType: c.channelType,
channelID: c.channelID,
sessionID: c.sessionID,
tenant: tenant,
log: c.log,
}
// Lookup destination in proxy.sessionTable
if c.proxy.sessionTable.Lookup(c.sessionID) {
var err error
c.destination, err = c.proxy.sessionTable.Connect(c.sessionID)
if err != nil {
return nil, err
}
}
handShake.log = c.log.WithFields("compute", c.destination)
for !handShake.Done() {
if err := handShake.clientLinkStage(c.destination); err != nil {
handShake.log.WithError(err).Error("compute handshake error")
return nil, err
}
}
c.log = handShake.log
c.sessionID = handShake.sessionID
c.proxy.sessionTable.Add(c.sessionID, c.destination, c.otp)
c.done = true
return handShake.compute, nil
}
func (c *tenantHandshake) clientAuthMethod(tenant io.ReadWriter) error {
var err error
b := make([]byte, 4)
if _, err = tenant.Read(b); err != nil {
c.log.WithError(err).Error("error reading client AuthMethod")
return err
}
c.log.Debug("received ClientAuthMethod")
c.tenantAuthMethod = red.AuthMethod(b[0])
var auth Authenticator
var ok bool
if auth, ok = c.proxy.authenticator[c.tenantAuthMethod]; !ok {
if err := sendServerTicket(red.ErrorPermissionDenied, tenant); err != nil {
c.log.WithError(err).Error("send ticket")
}
return fmt.Errorf("unavailable auth method %s", c.tenantAuthMethod)
}
c.log = c.log.WithFields("authmethod", c.tenantAuthMethod)
c.log.Debug("starting authentication")
var authCtx AuthContext
switch c.tenantAuthMethod {
case red.AuthMethodSpice:
authCtx = &authSpice{tenant: tenant, privateKey: c.privateKey, token: c.otp, computeAddress: c.destination}
case red.AuthMethodSASL:
return errors.New("SASL is not a supported authmethod")
default:
return errors.New("unsupported authmethod")
}
result, destination, err := auth.Next(authCtx)
if err != nil {
c.log.WithError(err).Error("authentication error")
return err
}
c.otp = authCtx.LoadToken()
c.destination = destination
if !result {
if err := sendServerTicket(red.ErrorPermissionDenied, tenant); err != nil {
c.log.WithError(err).Error("send ticket")
return err
}
return fmt.Errorf("authentication failed")
}
return sendServerTicket(red.ErrorOk, tenant)
}
func (c *tenantHandshake) clientLinkMessage(tenant io.ReadWriter) error {
var err error
var b []byte
if b, err = readLinkPacket(tenant); err != nil {
c.log.WithError(err).Error("error reading link packet")
return err
}
c.log.Debug("received ClientLinkMessage")
linkMessage := &red.ClientLinkMessage{}
if err := linkMessage.UnmarshalBinary(b); err != nil {
return err
}
c.channelType = linkMessage.ChannelType
c.channelID = linkMessage.ChannelID
c.sessionID = linkMessage.SessionID
c.log = c.log.WithFields("channel", c.channelID, "type", c.channelType, "session", c.sessionID)
return sendServerLinkPacket(tenant, c.privateKey.Public())
}
func redPubKey(key crypto.PublicKey) (ret red.PubKey, err error) {
cert, err := x509.MarshalPKIXPublicKey(key)
if err != nil {
return ret, err
}
copy(ret[:], cert[:])
return ret, nil
}
func sendServerLinkPacket(wr io.Writer, key crypto.PublicKey) error {
pubkey, err := redPubKey(key)
if err != nil {
return err
}
var channelCaps, commonCaps red.Capability
commonCaps.Set(red.CapabilityAuthSpice).Set(red.CapabilityAuthSelection).Set(red.CapabilityMiniHeader)
channelCaps.Set(red.CapabilityMainSeamlessMigrate).Set(red.CapabilityMainSemiSeamlessMigrate)
reply := red.ServerLinkMessage{
Error: red.ErrorOk,
PubKey: pubkey,
CommonCaps: 1,
ChannelCaps: 1,
CommonCapabilities: []red.Capability{commonCaps},
ChannelCapabilities: []red.Capability{channelCaps},
}
b, err := reply.MarshalBinary()
if err != nil {
return err
}
header := red.LinkHeader{
Size: reply.CapsOffset + 8,
}
b2, err := header.MarshalBinary()
if err != nil {
return err
}
data := append(b2, b...)
_, err = wr.Write(data)
if err != nil {
return err
}
return nil
}
func readLinkPacket(conn io.Reader) ([]byte, error) {
headerBytes := make([]byte, 16)
if _, err := conn.Read(headerBytes); err != nil {
return nil, err
}
header := &red.LinkHeader{}
if err := header.UnmarshalBinary(headerBytes); err != nil {
return nil, err
}
var messageBytes []byte
var n int
var err error
pending := int(header.Size)
for pending > 0 {
bytes := make([]byte, header.Size)
if n, err = conn.Read(bytes); err != nil {
return nil, err
}
pending = pending - n
messageBytes = append(messageBytes, bytes[:n]...)
}
return messageBytes[:int(header.Size)], nil
}
func sendServerTicket(result red.ErrorCode, writer io.Writer) error {
msg := red.ServerTicket{
Result: result,
}
b, err := msg.MarshalBinary()
if err != nil {
return err
}
if _, err := writer.Write(b); err != nil {
return err
}
return nil
}