-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_user_key_test.go
210 lines (192 loc) · 6.54 KB
/
session_user_key_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
package cassh
import (
"context"
"crypto/rand"
"crypto/rsa"
"math/big"
"net/http"
"net/url"
"testing"
gocmp "github.com/google/go-cmp/cmp"
"golang.org/x/crypto/ssh"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
"github.com/krostar/httpclient"
httpclienttest "github.com/krostar/httpclient/test"
"github.com/krostar/sshx"
)
func Test_SessionUserKey_Set(t *testing.T) {
rsaPrivKey, err := rsa.GenerateKey(rand.Reader, 2048)
assert.NilError(t, err)
privKey, err := sshx.WrapPrivateKey(rsaPrivKey)
assert.NilError(t, err)
srv := httpclienttest.NewServer(func(u url.URL, httpDoer httpclient.Doer, checkCallback any) error {
client, err := NewClient(u.String(), ClientOptionHTTPClient(httpDoer), ClientOptionTolerateInsecureProtocols())
if err != nil {
return err
}
session := client.
SessionUser("awesomeuser", SessionUserOptionAuthenticationMechanismForTesting()).
Key(privKey.PublicKey())
err = session.Set(context.Background())
(checkCallback.(func(error)))(err)
return nil
})
reqMatcher := httpclienttest.
NewRequestMatcherBuilder().
Method(http.MethodPut).
URLPath("/client").
BodyForm(url.Values{
"testAuthPropagated": {"true"},
"username": {"awesomeuser"},
"pubkey": {string(ssh.MarshalAuthorizedKey(privKey.PublicKey()))},
}, true)
for name, test := range map[string]struct {
form url.Values
matcher httpclienttest.RequestMatcher
writer func(http.ResponseWriter) error
check func(err error)
}{
"ok": {
matcher: reqMatcher,
writer: func(rw http.ResponseWriter) error {
rw.WriteHeader(http.StatusOK)
return nil
},
check: func(err error) { assert.Check(t, err == nil) },
},
"ko - unsuficient privileges": {
matcher: reqMatcher,
writer: func(rw http.ResponseWriter) error {
rw.WriteHeader(http.StatusUnauthorized)
return nil
},
check: func(err error) { assert.Check(t, cmp.ErrorIs(err, ErrInsufficientPrivileges)) },
},
"ko - unhandled status": {
matcher: reqMatcher,
writer: func(rw http.ResponseWriter) error {
rw.WriteHeader(http.StatusInternalServerError)
return nil
},
check: func(err error) {
assert.Check(t, cmp.ErrorContains(err, "failed with status 500: unhandled request status"))
},
},
} {
t.Run(name, func(t *testing.T) { assert.Check(t, srv.AssertRequest(test.matcher, test.writer, test.check)) })
}
}
func Test_SessionUserKey_Sign(t *testing.T) {
createCert := func(t *testing.T, privKey sshx.PrivateKey, signer ssh.Signer, setups ...func(cert *ssh.Certificate)) *ssh.Certificate {
cert := &ssh.Certificate{
Nonce: []byte{}, // To pass reflect.DeepEqual after marshal & parse, this must be non-nil.
Key: privKey.PublicKey(),
CertType: ssh.HostCert,
ValidPrincipals: []string{"foo", "bar"},
Permissions: ssh.Permissions{ // To pass reflect.DeepEqual after marshal & parse, this must be non-nil.
CriticalOptions: make(map[string]string),
Extensions: make(map[string]string),
},
Reserved: []byte{}, // To pass reflect.DeepEqual after marshal & parse, this must be non-nil.
SignatureKey: privKey.PublicKey(),
}
for _, setup := range setups {
setup(cert)
}
assert.NilError(t, cert.SignCert(rand.Reader, signer))
return cert
}
rsaPrivKey, err := rsa.GenerateKey(rand.Reader, 2048)
assert.NilError(t, err)
privKey, err := sshx.WrapPrivateKey(rsaPrivKey)
assert.NilError(t, err)
caCert := createCert(t, privKey, privKey.Signer())
srv := httpclienttest.NewServer(func(u url.URL, httpDoer httpclient.Doer, checkCallback any) error {
client, err := NewClient(u.String(), ClientOptionHTTPClient(httpDoer), ClientOptionTolerateInsecureProtocols())
if err != nil {
return err
}
session := client.
SessionUser("awesomeuser", SessionUserOptionAuthenticationMechanismForTesting()).
Key(privKey.PublicKey())
certificate, err := session.Sign(context.Background(), SessionUserKeySignOptionForce())
(checkCallback.(func(*ssh.Certificate, error)))(certificate, err)
return nil
})
reqMatcher := httpclienttest.
NewRequestMatcherBuilder().
Method(http.MethodPost).
URLPath("/client").
BodyForm(url.Values{
"testAuthPropagated": {"true"},
"username": {"awesomeuser"},
"pubkey": {string(ssh.MarshalAuthorizedKey(privKey.PublicKey()))},
"admin_force": {"true"},
}, true)
for name, test := range map[string]struct {
form url.Values
matcher httpclienttest.RequestMatcher
writer func(http.ResponseWriter) error
check func(cert *ssh.Certificate, err error)
}{
"ok": {
matcher: reqMatcher,
writer: func(rw http.ResponseWriter) error {
rw.WriteHeader(http.StatusOK)
_, err := rw.Write(ssh.MarshalAuthorizedKey(caCert))
return err
},
check: func(cert *ssh.Certificate, err error) {
assert.Check(t, err == nil)
assert.Check(t, sshx.WrapSSHPublicKey(cert.Key).Equal(sshx.WrapSSHPublicKey(caCert.Key)))
assert.Check(t, cmp.DeepEqual(cert, caCert,
gocmp.AllowUnexported(big.Int{}),
gocmp.FilterPath(func(path gocmp.Path) bool { return path.String() == "Key" }, gocmp.Ignore()),
))
},
},
"ko - unable to parse authorized key": {
matcher: reqMatcher,
writer: func(rw http.ResponseWriter) error {
rw.WriteHeader(http.StatusOK)
_, err := rw.Write([]byte("hello"))
return err
},
check: func(cert *ssh.Certificate, err error) {
assert.Check(t, cmp.ErrorContains(err, "unable to parse ssh certificate"))
},
},
"ko - authorized key is not a certificate": {
matcher: reqMatcher,
writer: func(rw http.ResponseWriter) error {
rw.WriteHeader(http.StatusOK)
_, err := rw.Write(ssh.MarshalAuthorizedKey(privKey.PublicKey()))
return err
},
check: func(cert *ssh.Certificate, err error) {
assert.Check(t, cmp.ErrorContains(err, "authorized key is not a certificate"))
},
},
"ko - unsuficient privileges": {
matcher: reqMatcher,
writer: func(rw http.ResponseWriter) error {
rw.WriteHeader(http.StatusUnauthorized)
return nil
},
check: func(cert *ssh.Certificate, err error) { assert.Check(t, cmp.ErrorIs(err, ErrInsufficientPrivileges)) },
},
"ko - unhandled status": {
matcher: reqMatcher,
writer: func(rw http.ResponseWriter) error {
rw.WriteHeader(http.StatusInternalServerError)
return nil
},
check: func(cert *ssh.Certificate, err error) {
assert.Check(t, cmp.ErrorContains(err, "failed with status 500: unhandled request status"))
},
},
} {
t.Run(name, func(t *testing.T) { assert.Check(t, srv.AssertRequest(test.matcher, test.writer, test.check)) })
}
}