-
Notifications
You must be signed in to change notification settings - Fork 10
/
auth_test.go
244 lines (205 loc) · 6.15 KB
/
auth_test.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
package pgsrv
import (
"bytes"
"crypto/md5"
"github.com/panoplyio/pgsrv/protocol"
"github.com/stretchr/testify/require"
"testing"
)
var authOKMessage = protocol.Message{'R', 0, 0, 0, 8, 0, 0, 0, 0}
var fatalMarker = []byte{
83, 70, 65, 84, 65, 76,
}
func TestAuthOKMsg(t *testing.T) {
actualResult := authOKMsg()
expectedResult := authOKMessage
require.Equal(t, expectedResult, actualResult)
}
func TestNoPassword_authenticate(t *testing.T) {
rw := &mockMessageReadWriter{output: []protocol.Message{}}
args := map[string]interface{}{
"user": "this-is-user",
}
np := &noPasswordAuthenticator{}
err := np.authenticate(rw, args)
require.NoError(t, err)
require.Equal(t, []protocol.Message{authOKMessage}, rw.messages)
}
func TestAuthenticationClearText_authenticate(t *testing.T) {
passwordRequest := protocol.Message{
'R',
0, 0, 0, 8, // length
0, 0, 0, 3, // clear text auth type
}
passwordMessage := protocol.Message{
'p',
0, 0, 0, 8,
109, 101, 104, 0, // 'meh'
}
rw := &mockMessageReadWriter{output: []protocol.Message{passwordMessage}}
args := map[string]interface{}{
"user": "this-is-user",
}
pp := &constantPasswordProvider{password: []byte("meh")}
a := &clearTextAuthenticator{pp}
t.Run("valid password", func(t *testing.T) {
defer rw.Reset()
err := a.authenticate(rw, args)
require.NoError(t, err)
expectedMessages := []protocol.Message{
passwordRequest,
authOKMessage,
}
require.Equal(t, expectedMessages, rw.messages)
})
t.Run("invalid password", func(t *testing.T) {
defer rw.Reset()
pp.password = []byte("shtoot")
err := a.authenticate(rw, args)
require.Equal(t, passwordRequest, rw.messages[0])
require.True(t, bytes.Contains(rw.messages[1], fatalMarker))
require.EqualError(t, err, "password does not match for user \"this-is-user\"")
})
t.Run("invalid message type", func(t *testing.T) {
defer rw.Reset()
rw = &mockMessageReadWriter{output: []protocol.Message{
{'q', 0, 0, 0, 5, 1},
}}
err := a.authenticate(rw, args)
require.Equal(t, passwordRequest, rw.messages[0])
require.True(t, bytes.Contains(rw.messages[1], fatalMarker))
require.EqualError(t, err, "expected password response, got message type 'q'")
})
}
func TestAuthenticationMD5_authenticate(t *testing.T) {
passwordRequest := protocol.Message{
'R',
0, 0, 0, 12, // length
0, 0, 0, 5, // md5 auth type
}
rw := &mockMD5MessageReadWriter{
user: "postgres",
pass: []byte("test"),
salt: []byte{},
}
args := map[string]interface{}{
"user": "postgres",
}
pp := &md5ConstantPasswordProvider{password: []byte("test")}
a := &md5Authenticator{pp}
t.Run("valid password", func(t *testing.T) {
defer rw.Reset()
err := a.authenticate(rw, args)
require.NoError(t, err)
require.True(t, bytes.Contains(rw.messages[0], passwordRequest))
require.Equal(t, authOKMessage, rw.messages[1])
})
t.Run("invalid password", func(t *testing.T) {
defer rw.Read()
pp.password = []byte("shtoot")
err := a.authenticate(rw, args)
require.True(t, bytes.Contains(rw.messages[0], passwordRequest))
require.True(t, bytes.Contains(rw.messages[1], fatalMarker))
require.EqualError(t, err, "password does not match for user \"postgres\"")
})
t.Run("invalid message type", func(t *testing.T) {
defer rw.Reset()
rw := &mockMessageReadWriter{output: []protocol.Message{
{'q', 0, 0, 0, 5, 1},
}}
err := a.authenticate(rw, args)
require.True(t, bytes.Contains(rw.messages[0], passwordRequest))
require.True(t, bytes.Contains(rw.messages[1], fatalMarker))
require.EqualError(t, err, "expected password response, got message type 'q'")
})
}
func TestHashWithSalt(t *testing.T) {
user := "postgres"
pass := []byte("test")
salt := []byte{196, 53, 49, 235}
hash := md5.Sum(append(pass, []byte(user)...))
// actual hash received from psql using the above variables
expectedHash := []byte{
109, 100, 53, 97, 97, 51, 102, 56, 98, 56,
55, 97, 57, 51, 52, 97, 52, 53, 48, 52,
52, 101, 49, 102, 98, 50, 100, 57, 48, 55,
48, 99, 98, 56, 48,
}
actualHash := hashWithSalt(hash[:], salt)
require.Equal(t, expectedHash, actualHash)
}
func TestGetRandomSalt(t *testing.T) {
var lastSalt []byte
for i := 0; i < 100; i++ {
salt := getRandomSalt()
require.Equal(t, len(salt), 4)
require.NotEqual(t, lastSalt, salt)
lastSalt = salt
}
}
func TestExtractPassword(t *testing.T) {
t.Run("regular password", func(t *testing.T) {
passwordMessage := protocol.Message{
'p',
0, 0, 0, 9,
42, 42, 42, 42,
0,
}
expectedResult := []byte{42, 42, 42, 42}
actualResult := extractPassword(passwordMessage)
require.Equal(t, expectedResult, actualResult)
})
t.Run("empty password", func(t *testing.T) {
passwordMessage := protocol.Message{
'p',
0, 0, 0, 5,
0,
}
expectedResult := []byte{}
actualResult := extractPassword(passwordMessage)
require.Equal(t, expectedResult, actualResult)
})
}
// mockMessageReadWriter implements messageReadWriter and outputs the provided output
// message by message, looped.
type mockMessageReadWriter struct {
output []protocol.Message
currentOutput int
messages []protocol.Message
}
func (rw *mockMessageReadWriter) Read() (protocol.Message, error) {
return rw.output[rw.currentOutput%len(rw.output)], nil
}
func (rw *mockMessageReadWriter) Write(m protocol.Message) error {
rw.messages = append(rw.messages, m)
return nil
}
func (rw *mockMessageReadWriter) Reset() {
rw.messages = make([]protocol.Message, 0)
}
// mockMD5MessageReadWriter implements messageReadWriter and outputs password
// hashed with the salt received in Write() method
type mockMD5MessageReadWriter struct {
user string
pass []byte
salt []byte
messages []protocol.Message
}
func (rw *mockMD5MessageReadWriter) Read() (protocol.Message, error) {
message := protocol.Message{
'p',
0, 0, 0, 25,
}
hash := md5.Sum(append(rw.pass, []byte(rw.user)...))
message = append(message, hashWithSalt(hash[:], rw.salt)...)
message = append(message, 0)
return message, nil
}
func (rw *mockMD5MessageReadWriter) Write(m protocol.Message) error {
rw.salt = m[9:]
rw.messages = append(rw.messages, m)
return nil
}
func (rw *mockMD5MessageReadWriter) Reset() {
rw.messages = make([]protocol.Message, 0)
}