-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
HttpsTest.kt
251 lines (208 loc) · 8.31 KB
/
HttpsTest.kt
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
248
249
250
251
package com.hexagonkt.http.test.examples
import com.hexagonkt.core.fail
import com.hexagonkt.core.require
import com.hexagonkt.core.security.getPrivateKey
import com.hexagonkt.core.security.getPublicKey
import com.hexagonkt.core.security.loadKeyStore
import com.hexagonkt.core.urlOf
import com.hexagonkt.http.SslSettings
import com.hexagonkt.http.client.HttpClient
import com.hexagonkt.http.client.HttpClientPort
import com.hexagonkt.http.client.HttpClientSettings
import com.hexagonkt.http.model.Header
import com.hexagonkt.http.model.HttpProtocol.HTTP2
import com.hexagonkt.http.model.HttpProtocol.HTTPS
import com.hexagonkt.http.server.*
import com.hexagonkt.http.handlers.HttpHandler
import com.hexagonkt.http.handlers.path
import com.hexagonkt.http.test.BaseTest
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
import kotlin.test.assertFails
import kotlin.test.assertNotNull
@Suppress("FunctionName") // This class's functions are intended to be used only in tests
abstract class HttpsTest(
final override val clientAdapter: () -> HttpClientPort,
final override val serverAdapter: () -> HttpServerPort,
final override val serverSettings: HttpServerSettings = HttpServerSettings(),
) : BaseTest() {
private val identity = "hexagontk.p12"
private val trust = "trust.p12"
private val keyStore = urlOf("classpath:ssl/$identity")
private val trustStore = urlOf("classpath:ssl/$trust")
private val keyStorePassword = identity.reversed()
private val trustStorePassword = trust.reversed()
private val sslSettings = SslSettings(
keyStore = keyStore,
keyStorePassword = keyStorePassword,
trustStore = trustStore,
trustStorePassword = trustStorePassword,
clientAuth = true
)
private val http2ServerSettings = serverSettings.copy(
bindPort = 0,
protocol = HTTP2,
sslSettings = sslSettings
)
private val clientSettings = HttpClientSettings(sslSettings = sslSettings)
private val router = path {
get("/hello") {
val certificateSubject = request.certificate()?.subjectX500Principal?.name ?: fail
val headers = response.headers + Header("cert", certificateSubject)
ok("Hello World!", headers = headers)
}
}
override val handler: HttpHandler = router
@Test fun `Serve HTTPS example`() {
// https
// Key store files
val identity = "hexagontk.p12"
val trust = "trust.p12"
// Default passwords are file name reversed
val keyStorePassword = identity.reversed()
val trustStorePassword = trust.reversed()
// Key stores can be set as URIs to classpath resources (the triple slash is needed)
val keyStore = urlOf("classpath:ssl/$identity")
val trustStore = urlOf("classpath:ssl/$trust")
val sslSettings = SslSettings(
keyStore = keyStore,
keyStorePassword = keyStorePassword,
trustStore = trustStore,
trustStorePassword = trustStorePassword,
clientAuth = true // Requires a valid certificate from the client (mutual TLS)
)
val serverSettings = HttpServerSettings(
bindPort = 0,
protocol = HTTPS, // You can also use HTTP2
sslSettings = sslSettings
)
val server = HttpServer(serverAdapter(), serverSettings) {
get("/hello") {
// We can access the certificate used by the client from the request
val subjectDn = request.certificate()?.subjectX500Principal?.name ?: ""
val h = response.headers + Header("cert", subjectDn)
ok("Hello World!", headers = h)
}
}
server.start()
// We'll use the same certificate for the client (in a real scenario it would be different)
val clientSettings = HttpClientSettings(sslSettings = sslSettings)
// Create an HTTP client and make an HTTPS request
val client = HttpClient(clientAdapter(), clientSettings.copy(baseUrl = server.binding))
client.start()
client.get("/hello").apply {
// Assure the certificate received (and returned) by the server is correct
assert(headers.require("cert").string()?.startsWith("CN=hexagontk.com") ?: false)
assertEquals("Hello World!", body)
}
// https
client.stop()
server.stop()
}
@Test fun `Serve HTTPS works properly`() {
val server = serve(serverAdapter(), handler, http2ServerSettings.copy(protocol = HTTPS))
val client = HttpClient(clientAdapter(), clientSettings.copy(baseUrl = server.binding))
client.start()
client.get("/hello").apply {
assert(headers.require("cert").string()?.startsWith("CN=hexagontk.com") ?: false)
assertEquals("Hello World!", body)
}
client.stop()
server.stop()
}
@Test fun `Serve HTTP2 works properly`() {
val server = serve(serverAdapter(), handler, http2ServerSettings)
val client = HttpClient(clientAdapter(), clientSettings.copy(baseUrl = server.binding))
client.start()
client.get("/hello").apply {
assert(headers.require("cert").string()?.startsWith("CN=hexagontk.com") ?: false)
assertEquals("Hello World!", body)
}
client.stop()
server.stop()
}
@Test fun `Serve insecure HTTPS example`() {
val identity = "hexagontk.p12"
val trust = "trust.p12"
// keyStoreSettings
val keyStoreSettings = SslSettings(
keyStore = urlOf("classpath:ssl/$identity"),
keyStorePassword = identity.reversed()
)
// keyStoreSettings
val serverSettings = serverSettings.copy(
bindPort = 0,
protocol = HTTPS,
sslSettings = keyStoreSettings
)
val server = HttpServer(serverAdapter(), serverSettings) {
get("/hello") {
ok("Hello World!")
}
}
server.start()
// trustStoreSettings
val trustStoreSettings = SslSettings(
trustStore = urlOf("classpath:ssl/$trust"),
trustStorePassword = trust.reversed()
)
// trustStoreSettings
val clientSettings = HttpClientSettings(
sslSettings = trustStoreSettings
)
// Create an HTTP client and make an HTTPS request
val contextPath = server.binding
val client = HttpClient(clientAdapter(), clientSettings.copy(baseUrl = contextPath))
client.start()
client.get("/hello").apply {
assertEquals("Hello World!", body)
}
assertFails {
val adapter = clientAdapter()
val noTrustStore = HttpClientSettings()
HttpClient(adapter, noTrustStore.copy(baseUrl = contextPath)).use {
it.start()
it.get("/hello")
}
}
assertFails {
val adapter = clientAdapter()
val contextPath1 = urlOf("https://127.0.0.1:${server.runtimePort}")
HttpClient(adapter, clientSettings.copy(baseUrl = contextPath1)).use {
it.start()
it.get("/hello")
}
}
val insecureClient = HttpClient(
clientAdapter(),
clientSettings.copy(baseUrl = contextPath, insecure = true, sslSettings = SslSettings())
)
insecureClient.use {
it.start()
it.get("/hello").apply {
assertEquals("Hello World!", body)
}
}
val settings = clientSettings.copy(
baseUrl = contextPath,
insecure = false,
sslSettings = SslSettings()
)
HttpClient(clientAdapter(), settings).use {
it.start()
val throwable = assertFails { it.get("/hello") }
throwable.printStackTrace()
}
client.stop()
server.stop()
}
@Test fun `Key stores contains the proper aliases`() {
loadKeyStore(keyStore, keyStorePassword).apply {
assertNotNull(getPrivateKey("hexagontk", keyStorePassword))
assertNotNull(getPublicKey("hexagontk"))
}
loadKeyStore(trustStore, trustStorePassword).apply {
assertNotNull(getPublicKey("ca"))
}
}
}