-
Notifications
You must be signed in to change notification settings - Fork 1
/
sasl_transport.go
202 lines (173 loc) · 4.8 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
package hive
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"git.apache.org/thrift.git/lib/go/thrift"
"github.com/emersion/go-sasl"
"io"
)
const (
saslStatusStart int8 = 1
saslStatusOK = 2
saslStatusBad = 3
saslStatusError = 4
saslStatusComplete = 5
)
// All implementations are copied from TFramedTransport except `Open`.
type TSaslClientTransport struct {
transport thrift.TTransport
clientFactory func() sasl.Client
client sasl.Client
buf bytes.Buffer
reader *bufio.Reader
frameSize uint32 //Current remaining size of the frame. if ==0 read next frame header
buffer [4]byte
maxLength uint32
}
func NewTSaslClientTransport(transport thrift.TTransport, factory func() sasl.Client) thrift.TTransport {
return &TSaslClientTransport{
transport: transport,
clientFactory: factory,
reader: bufio.NewReader(transport),
maxLength: thrift.DEFAULT_MAX_LENGTH,
}
}
func (c *TSaslClientTransport) Read(p []byte) (n int, err error) {
if c.frameSize == 0 {
c.frameSize, err = c.readFrameHeader()
if err != nil {
return
}
}
if c.frameSize < uint32(len(p)) {
frameSize := c.frameSize
tmp := make([]byte, c.frameSize)
n, err = c.Read(tmp)
copy(p, tmp)
if err == nil {
err = thrift.NewTTransportExceptionFromError(fmt.Errorf("Not enough frame size %d to read %d bytes", frameSize, len(p)))
return
}
}
got, err := c.reader.Read(p)
c.frameSize = c.frameSize - uint32(got)
//sanity check
if c.frameSize < 0 {
return 0, thrift.NewTTransportException(thrift.UNKNOWN_TRANSPORT_EXCEPTION, "Negative frame size")
}
return got, thrift.NewTTransportExceptionFromError(err)
}
func (c *TSaslClientTransport) readFrameHeader() (uint32, error) {
buf := c.buffer[:4]
if _, err := io.ReadFull(c.reader, buf); err != nil {
return 0, err
}
size := binary.BigEndian.Uint32(buf)
if size < 0 || size > c.maxLength {
return 0, thrift.NewTTransportException(thrift.UNKNOWN_TRANSPORT_EXCEPTION, fmt.Sprintf("Incorrect frame size (%d)", size))
}
return size, nil
}
func (c *TSaslClientTransport) Write(p []byte) (n int, err error) {
n, err = c.buf.Write(p)
return n, thrift.NewTTransportExceptionFromError(err)
}
func (c *TSaslClientTransport) Close() error {
c.client = nil
return c.transport.Close()
}
func (c *TSaslClientTransport) Flush() (err error) {
size := c.buf.Len()
buf := c.buffer[:4]
binary.BigEndian.PutUint32(buf, uint32(size))
_, err = c.transport.Write(buf)
if err != nil {
c.buf.Truncate(0)
return thrift.NewTTransportExceptionFromError(err)
}
if size > 0 {
if n, err := c.buf.WriteTo(c.transport); err != nil {
print("Error while flushing write buffer of size ", size, " to transport, only wrote ", n, " bytes: ", err.Error(), "\n")
c.buf.Truncate(0)
return thrift.NewTTransportExceptionFromError(err)
}
}
err = c.transport.Flush()
return thrift.NewTTransportExceptionFromError(err)
}
func (c *TSaslClientTransport) RemainingBytes() uint64 {
return uint64(c.frameSize)
}
func (c *TSaslClientTransport) Open() error {
if !c.IsOpen() {
if err := c.transport.Open(); err != nil {
return err
}
}
if c.client != nil {
return thrift.NewTTransportException(thrift.NOT_OPEN, "sasl transport is already opened")
}
c.client = c.clientFactory()
mech, ir, err := c.client.Start()
if err != nil {
return err
}
// Send initial response
if err := c.sendMessage(saslStatusStart, []byte(mech)); err != nil {
return err
}
if err := c.sendMessage(saslStatusOK, ir); err != nil {
return err
}
// SASL negotiation loop
for {
status, payload, err := c.recvMessage()
if err != nil {
return err
}
if status != saslStatusOK && status != saslStatusComplete {
return thrift.NewTTransportException(thrift.NOT_OPEN, fmt.Sprintf("Bad status: %d (%s)", status, payload))
}
if status == saslStatusComplete {
break
}
response, err := c.client.Next(payload)
if err != nil {
return err
}
if err := c.sendMessage(saslStatusOK, response); err != nil {
return err
}
}
return nil
}
func (c *TSaslClientTransport) sendMessage(status int8, body []byte) error {
header := make([]byte, 5)
header[0] = byte(status)
binary.BigEndian.PutUint32(header[1:], uint32(len(body)))
if _, err := c.transport.Write(append(header, body...)); err != nil {
return err
}
return c.transport.Flush()
}
func (c *TSaslClientTransport) recvMessage() (int8, []byte, error) {
header := make([]byte, 5)
if _, err := c.transport.Read(header); err != nil {
return 0, nil, err
}
status := int8(header[0])
length := binary.BigEndian.Uint32(header[1:])
if length == 0 {
return status, nil, nil
}
payload := make([]byte, length)
if _, err := c.transport.Read(payload); err != nil {
return 0, nil, err
}
return status, payload, nil
}
func (c *TSaslClientTransport) IsOpen() bool {
return c.transport.IsOpen()
}