-
Notifications
You must be signed in to change notification settings - Fork 21
/
sasl_transport.go
247 lines (216 loc) · 6.06 KB
/
sasl_transport.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
package impalathing
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"github.com/apache/thrift/lib/go/thrift"
"github.com/beltran/gosasl"
"io"
)
const (
START = 1
OK = 2
BAD = 3
ERROR = 4
COMPLETE = 5
)
const DEFAULT_MAX_LENGTH = 16384000
// TSaslTransport is a tranport thrift struct that uses SASL
type TSaslTransport struct {
service string
saslClient *gosasl.Client
tp thrift.TTransport
tpFramed thrift.TFramedTransport
mechanism string
writeBuf bytes.Buffer
readBuf bytes.Buffer
buffer [4]byte
rawFrameSize uint32 //Current remaining size of the frame. if ==0 read next frame header
frameSize int //Current remaining size of the frame. if ==0 read next frame header
maxLength uint32
principal string
OpeningContext context.Context
}
// NewTSaslTransport return a TSaslTransport
func NewTSaslTransport(trans thrift.TTransport, host string, configuration map[string]string) (*TSaslTransport, error) {
var mechanism gosasl.Mechanism
mechanismName, _ := configuration["mechanismName"]
if mechanismName == "PLAIN" {
mechanism = gosasl.NewPlainMechanism(configuration["username"], configuration["password"])
} else if mechanismName == "GSSAPI" {
var err error
mechanism, err = gosasl.NewGSSAPIMechanism(configuration["service"])
if err != nil {
return nil, err
}
} else {
panic("Mechanism not supported")
}
client := gosasl.NewSaslClient(host, mechanism)
return &TSaslTransport{
saslClient: client,
tp: trans,
mechanism: mechanismName,
maxLength: DEFAULT_MAX_LENGTH,
principal: configuration["principal"],
OpeningContext: context.Background(),
}, nil
}
// IsOpen opens a SASL connection
func (p *TSaslTransport) IsOpen() bool {
return p.tp.IsOpen() && p.saslClient.Complete()
}
// Open check if a SASL transport connection is opened
func (p *TSaslTransport) Open() (err error) {
if !p.tp.IsOpen() {
err = p.tp.Open()
if err != nil {
return err
}
}
if err = p.sendSaslMsg(p.OpeningContext, START, []byte(p.mechanism)); err != nil {
return nil
}
proccessed, err := p.saslClient.Start()
if err != nil {
return
}
if err = p.sendSaslMsg(p.OpeningContext, OK, proccessed); err != nil {
return nil
}
for true {
status, challenge := p.recvSaslMsg(p.OpeningContext)
if status == OK {
proccessed, err = p.saslClient.Step(challenge)
if err != nil {
return
}
p.sendSaslMsg(p.OpeningContext, OK, proccessed)
} else if status == COMPLETE {
if !p.saslClient.Complete() {
return thrift.NewTTransportException(thrift.NOT_OPEN, "The server erroneously indicated that SASL negotiation was complete")
}
break
} else {
return thrift.NewTTransportExceptionFromError(fmt.Errorf("Bad SASL negotiation status: %d (%s)", status, challenge))
}
}
return nil
}
// Close close a SASL transport connection
func (p *TSaslTransport) Close() (err error) {
p.saslClient.Dispose()
return p.tp.Close()
}
func (p *TSaslTransport) sendSaslMsg(ctx context.Context, status uint8, body []byte) error {
header := make([]byte, 5)
header[0] = status
length := uint32(len(body))
binary.BigEndian.PutUint32(header[1:], length)
_, err := p.tp.Write(append(header[:], body[:]...))
if err != nil {
return err
}
err = p.tp.Flush(ctx)
if err != nil {
return err
}
return nil
}
func (p *TSaslTransport) recvSaslMsg(ctx context.Context) (int8, []byte) {
header := make([]byte, 5)
_, err := io.ReadFull(p.tp, header)
if err != nil {
return ERROR, nil
}
status := int8(header[0])
length := binary.BigEndian.Uint32(header[1:])
if length > 0 {
payload := make([]byte, length)
_, err = io.ReadFull(p.tp, payload)
if err != nil {
return ERROR, nil
}
return status, payload
}
return status, nil
}
func (p *TSaslTransport) Read(buf []byte) (l int, err error) {
if p.rawFrameSize == 0 && p.frameSize == 0 {
p.rawFrameSize, err = p.readFrameHeader()
if err != nil {
return
}
}
var got int
if p.rawFrameSize > 0 {
rawBuf := make([]byte, p.rawFrameSize)
got, err = io.ReadFull(p.tp, rawBuf)
if err != nil {
return
}
p.rawFrameSize = p.rawFrameSize - uint32(got)
var unwrappedBuf []byte
unwrappedBuf, err = p.saslClient.Decode(rawBuf)
if err != nil {
return
}
p.frameSize += len(unwrappedBuf)
p.readBuf.Write(unwrappedBuf)
}
// totalBytes := p.readBuf.Len()
got, err = p.readBuf.Read(buf)
p.frameSize = p.frameSize - got
/*
if p.readBuf.Len() > 0 {
err = thrift.NewTTransportExceptionFromError(fmt.Errorf("Not enough frame size %d to read %d bytes", p.frameSize, totalBytes))
return
}
*/
if p.frameSize < 0 {
return 0, thrift.NewTTransportException(thrift.UNKNOWN_TRANSPORT_EXCEPTION, "Negative frame size")
}
return got, thrift.NewTTransportExceptionFromError(err)
}
func (p *TSaslTransport) readFrameHeader() (uint32, error) {
buf := p.buffer[:4]
if _, err := io.ReadFull(p.tp, buf); err != nil {
return 0, err
}
size := binary.BigEndian.Uint32(buf)
if size < 0 || size > p.maxLength {
return 0, thrift.NewTTransportException(thrift.UNKNOWN_TRANSPORT_EXCEPTION, fmt.Sprintf("Incorrect frame size (%d)", size))
}
return size, nil
}
func (p *TSaslTransport) Write(buf []byte) (int, error) {
n, err := p.writeBuf.Write(buf)
return n, thrift.NewTTransportExceptionFromError(err)
}
// Flush the bytes in the buffer
func (p *TSaslTransport) Flush(ctx context.Context) (err error) {
wrappedBuf, err := p.saslClient.Encode(p.writeBuf.Bytes())
if err != nil {
return thrift.NewTTransportExceptionFromError(err)
}
p.writeBuf.Reset()
size := len(wrappedBuf)
buf := p.buffer[:4]
binary.BigEndian.PutUint32(buf, uint32(size))
_, err = p.tp.Write(buf)
if err != nil {
return thrift.NewTTransportExceptionFromError(err)
}
if size > 0 {
if _, err := p.tp.Write(wrappedBuf); err != nil {
return thrift.NewTTransportExceptionFromError(err)
}
}
err = p.tp.Flush(ctx)
return thrift.NewTTransportExceptionFromError(err)
}
// RemainingBytes return the size of the unwrapped bytes
func (p *TSaslTransport) RemainingBytes() uint64 {
return uint64(p.frameSize)
}