generated from theparanoids/.github
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandler_test.go
195 lines (180 loc) · 4.63 KB
/
handler_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
// Copyright 2022 Yahoo Inc.
// Licensed under the terms of the Apache License 2.0. Please see LICENSE file in project root for terms.
package regular
import (
"crypto/rand"
"crypto/rsa"
"os"
"path"
"testing"
"github.com/theparanoids/ysshra/common"
"github.com/theparanoids/ysshra/csr"
"github.com/theparanoids/ysshra/csr/transid"
"github.com/theparanoids/ysshra/message"
"github.com/theparanoids/ysshra/sshutils/version"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
func newSSHKeyPair(t *testing.T) (*rsa.PrivateKey, ssh.PublicKey) {
t.Helper()
const (
bit = 2048
)
priv, err := rsa.GenerateKey(rand.Reader, bit)
if err != nil {
t.Fatal(err)
}
pub, err := ssh.NewPublicKey(&priv.PublicKey)
if err != nil {
t.Fatal(err)
}
return priv, pub
}
func newSSHKeyPairInAgent(t *testing.T) (*rsa.PrivateKey, ssh.PublicKey, agent.Agent) {
t.Helper()
priv, pub := newSSHKeyPair(t)
ag := agent.NewKeyring()
addedKey := agent.AddedKey{PrivateKey: priv}
if err := ag.Add(addedKey); err != nil {
t.Fatal(err)
}
return priv, pub, ag
}
func writePubKeyFile(t *testing.T, logName string, pubBytes []byte) string {
t.Helper()
tmpDir := t.TempDir()
if err := os.WriteFile(path.Join(tmpDir, logName), pubBytes, 0400); err != nil {
t.Fatal(err)
}
return tmpDir
}
func TestHandler_Authenticate(t *testing.T) {
t.Parallel()
goodParam := &csr.ReqParam{
NamespacePolicy: common.NoNamespace,
HandlerName: "Regular",
ClientIP: "1.2.3.4",
LogName: "dummy",
ReqUser: "dummy",
ReqHost: "dummy.com",
TransID: transid.Generate(),
SSHClientVersion: version.New(8, 1),
Attrs: &message.Attributes{
Username: "dummy",
Hostname: "dummy.com",
SSHClientVersion: "8.1",
HardKey: false,
Touch2SSH: false,
TouchlessSudo: nil,
},
}
tests := map[string]struct {
params *csr.ReqParam
GetHandler func(t *testing.T) Handler
wantErr bool
}{
"happy path": {
params: goodParam,
GetHandler: func(t *testing.T) Handler {
_, pub, ag := newSSHKeyPairInAgent(t)
tmpDir := writePubKeyFile(t, "dummy", ssh.MarshalAuthorizedKey(pub))
return Handler{
agent: ag,
conf: &conf{PubKeyDir: tmpDir},
}
},
},
"nil param": {
GetHandler: func(t *testing.T) Handler {
_, pub, ag := newSSHKeyPairInAgent(t)
tmpDir := writePubKeyFile(t, "dummy", ssh.MarshalAuthorizedKey(pub))
return Handler{
agent: ag,
conf: &conf{PubKeyDir: tmpDir},
}
},
wantErr: true,
},
}
for name, test := range tests {
name, test := name, test
t.Run(name, func(t *testing.T) {
t.Parallel()
h := test.GetHandler(t)
if err := h.Authenticate(test.params); (err != nil) != test.wantErr {
t.Errorf("challengePubKey() error = %v, wantErr %v", err, test.wantErr)
}
})
}
}
func TestHandler_challengePubKey(t *testing.T) {
t.Parallel()
tests := []struct {
name string
GetHandler func(t *testing.T, logName string) Handler
logName string
wantErr bool
}{
{
name: "happy path",
GetHandler: func(t *testing.T, logName string) Handler {
_, pub, ag := newSSHKeyPairInAgent(t)
tmpDir := writePubKeyFile(t, logName, ssh.MarshalAuthorizedKey(pub))
return Handler{
agent: ag,
conf: &conf{PubKeyDir: tmpDir},
}
},
logName: "dummy",
},
{
name: "pubKey mismatch (cannot sign the challenge)",
GetHandler: func(t *testing.T, logName string) Handler {
_, _, ag := newSSHKeyPairInAgent(t)
_, mismatchedPub := newSSHKeyPair(t)
tmpDir := writePubKeyFile(t, logName, ssh.MarshalAuthorizedKey(mismatchedPub))
return Handler{
agent: ag,
conf: &conf{PubKeyDir: tmpDir},
}
},
logName: "dummy",
wantErr: true,
},
{
name: "missing pubKey File",
GetHandler: func(t *testing.T, logName string) Handler {
_, _, ag := newSSHKeyPairInAgent(t)
return Handler{
agent: ag,
conf: &conf{PubKeyDir: "invalidPath"},
}
},
logName: "dummy",
wantErr: true,
},
{
name: "invalid public key",
GetHandler: func(t *testing.T, logName string) Handler {
_, _, ag := newSSHKeyPairInAgent(t)
tmpDir := writePubKeyFile(t, logName, []byte("invalidPubKey"))
return Handler{
agent: ag,
conf: &conf{PubKeyDir: tmpDir},
}
},
logName: "dummy",
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
h := tt.GetHandler(t, tt.logName)
if err := h.challengePubKey(&csr.ReqParam{LogName: tt.logName}); (err != nil) != tt.wantErr {
t.Errorf("challengePubKey() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}