File: jetty9_config_test.clj

package info (click to toggle)
trapperkeeper-webserver-jetty9-clojure 1.7.0-2%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,156 kB
  • sloc: xml: 531; java: 120; makefile: 24; sh: 10
file content (471 lines) | stat: -rw-r--r-- 20,518 bytes parent folder | download
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
(ns puppetlabs.trapperkeeper.services.webserver.jetty9-config-test
  (:import (clojure.lang ExceptionInfo)
           (java.util Arrays))
  (:require [clojure.test :refer :all]
            [clojure.java.io :refer [resource]]
            [me.raynes.fs :as fs]
            [puppetlabs.ssl-utils.core :as ssl]
            [puppetlabs.kitchensink.core :as ks]
            [puppetlabs.trapperkeeper.services.webserver.jetty9-config :refer :all]
            [puppetlabs.trapperkeeper.testutils.logging :refer [with-test-logging]]
            [puppetlabs.trapperkeeper.services.webserver.jetty9-core :as jetty9]
            [puppetlabs.trapperkeeper.services.webserver.jetty9-service
             :refer [jetty9-service add-ring-handler]]
            [puppetlabs.trapperkeeper.app :as tk-app]
            [puppetlabs.trapperkeeper.testutils.bootstrap :refer [with-app-with-config]]
            [puppetlabs.trapperkeeper.testutils.webserver.common :refer [http-get]]
            [schema.test :as schema-test]
            [puppetlabs.trapperkeeper.testutils.webserver :as testutils]))

(use-fixtures :once
  schema-test/validate-schemas
  testutils/assert-clean-shutdown)

(def valid-ssl-pem-config
  {:ssl-cert    "./dev-resources/config/jetty/ssl/certs/localhost.pem"
   :ssl-key     "./dev-resources/config/jetty/ssl/private_keys/localhost.pem"
   :ssl-ca-cert "./dev-resources/config/jetty/ssl/certs/ca.pem"})

(def valid-ssl-keystore-config
  {:keystore        "./dev-resources/config/jetty/ssl/keystore.jks"
   :truststore      "./dev-resources/config/jetty/ssl/truststore.jks"
   :key-password    "Kq8lG9LkISky9cDIYysiadxRx"
   :trust-password  "Kq8lG9LkISky9cDIYysiadxRx"})

(defn munge-actual-http-config
  [config]
  (process-config config))

(defn munge-expected-common-config
  [expected scheme]
  (-> expected
      (update-in [:max-threads] identity)
      (update-in [:queue-max-size] identity)
      (update-in [:jmx-enable] (fnil ks/parse-bool default-jmx-enable))
      (update-in [scheme :request-header-max-size] identity)
      (update-in [scheme :so-linger-milliseconds] identity)
      (update-in [scheme :idle-timeout-milliseconds] identity)
      (update-in [scheme :acceptor-threads] identity)
      (update-in [scheme :selector-threads] identity)))

(defn munge-expected-http-config
  [expected]
  (munge-expected-common-config expected :http))

(defn munge-actual-https-config
  [config]
  (let [actual (process-config config)]
    (-> actual
        (update-in [:https] dissoc :keystore-config))))

(defn munge-expected-https-config
  [expected]
  (-> (munge-expected-common-config expected :https)
      (update-in [:https :cipher-suites] (fnil identity acceptable-ciphers))
      (update-in [:https :protocols] (fnil identity default-protocols))
      (update-in [:https :client-auth] (fnil identity default-client-auth))
      (update-in [:https :ssl-crl-path] identity)))

(deftest process-config-http-test
  (testing "process-config successfully builds a WebserverConfig for plaintext connector"
    (is (= (munge-actual-http-config
             {:port 8000})
           (munge-expected-http-config
             {:http {:host default-host :port 8000}})))

    (is (= (munge-actual-http-config
             {:port 8000 :host "foo.local"})
           (munge-expected-http-config
             {:http {:host "foo.local" :port 8000}})))

    (is (= (munge-actual-http-config
             {:host "foo.local"})
           (munge-expected-http-config
             {:http {:host "foo.local" :port default-http-port}})))

    (is (= (munge-actual-http-config
             {:port 8000 :request-header-max-size 16192})
           (munge-expected-http-config
             {:http {:host                    default-host
                     :port                    8000
                     :request-header-max-size 16192}})))

    (is (= (munge-actual-http-config
             {:port 8000 :so-linger-seconds 7})
           (munge-expected-http-config
             {:http {:host                   default-host
                     :port                   8000
                     :so-linger-milliseconds 7000}})))

    (is (= (munge-actual-http-config
             {:port 8000 :max-threads 500})
           (munge-expected-http-config
             {:http        {:host default-host :port 8000}
              :max-threads 500})))

    (is (= (munge-actual-http-config
             {:port 8000 :queue-max-size 123})
           (munge-expected-http-config
             {:http           {:host default-host :port 8000}
              :queue-max-size 123})))

    (is (= (munge-actual-http-config
             {:port 8000 :idle-timeout-milliseconds 6000})
           (munge-expected-http-config
             {:http {:host                      default-host
                     :port                      8000
                     :idle-timeout-milliseconds 6000}})))

    (is (= (munge-actual-http-config
             {:port 8000 :acceptor-threads 32})
           (munge-expected-http-config
             {:http {:host             default-host
                     :port             8000
                     :acceptor-threads 32}})))

    (is (= (munge-actual-http-config
             {:port 8000 :selector-threads 52})
           (munge-expected-http-config
             {:http {:host      default-host
                     :port      8000
                     :selector-threads 52}})))))

(deftest process-config-https-test
  (testing "process-config successfully builds a WebserverConfig for ssl connector"
    (is (= (munge-actual-https-config
             (merge valid-ssl-pem-config
                    {:ssl-host "foo.local"}))
           (munge-expected-https-config
             {:https {:host "foo.local" :port default-https-port}})))

    (is (= (munge-actual-https-config
             (merge valid-ssl-pem-config
                    {:ssl-port 8001}))
           (munge-expected-https-config
             {:https {:host default-host :port 8001}})))

    (is (= (munge-actual-https-config
             (merge valid-ssl-pem-config
                    {:ssl-host "foo.local" :ssl-port 8001}))
           (munge-expected-https-config
             {:https {:host "foo.local" :port 8001}})))

    (is (= (munge-actual-https-config
             (merge valid-ssl-pem-config
                    {:ssl-host                "foo.local"
                     :ssl-port                8001
                     :request-header-max-size 16192}))
           (munge-expected-https-config
             {:https {:host                    "foo.local"
                      :port                    8001
                      :request-header-max-size 16192}})))

    (is (= (munge-actual-https-config
             (merge valid-ssl-pem-config
                    {:ssl-host          "foo.local"
                     :ssl-port          8001
                     :so-linger-seconds 22}))
           (munge-expected-https-config
             {:https {:host                   "foo.local"
                      :port                   8001
                      :so-linger-milliseconds 22000}})))

    (is (= (munge-actual-https-config
             (merge valid-ssl-pem-config
                    {:ssl-host    "foo.local"
                     :ssl-port    8001
                     :max-threads 93}))
           (munge-expected-https-config
             {:https       {:host "foo.local" :port 8001}
              :max-threads 93})))

    (is (= (munge-actual-https-config
             (merge valid-ssl-pem-config
                    {:ssl-host       "foo.local"
                     :ssl-port       8001
                     :queue-max-size 99}))
           (munge-expected-https-config
             {:https          {:host "foo.local" :port 8001}
              :queue-max-size 99})))

    (is (= (munge-actual-https-config
             (merge valid-ssl-pem-config
                    {:ssl-host                  "foo.local"
                     :ssl-port                  8001
                     :idle-timeout-milliseconds 4200}))
           (munge-expected-https-config
             {:https {:host                      "foo.local"
                      :port                      8001
                      :idle-timeout-milliseconds 4200}})))

    (is (= (munge-actual-https-config
             (merge valid-ssl-pem-config
                    {:ssl-host            "foo.local"
                     :ssl-port             8001
                     :ssl-selector-threads 4242}))
           (munge-expected-https-config
             {:https {:host             "foo.local"
                      :port             8001
                      :selector-threads 4242}})))

    (is (= (munge-actual-https-config
             (merge valid-ssl-pem-config
                    {:ssl-host             "foo.local"
                     :ssl-port             8001
                     :ssl-acceptor-threads 9193}))
           (munge-expected-https-config
             {:https {:host             "foo.local"
                      :port             8001
                      :acceptor-threads 9193}})))))

(deftest process-config-jks-test
  (testing "jks ssl config"
    (is (= (munge-actual-https-config
             (merge valid-ssl-keystore-config
                    {:ssl-port 8001}))
           (munge-expected-https-config
             {:https {:host default-host :port 8001}})))))

(deftest process-config-ciphers-test
  (testing "cipher suites"
    (is (= (munge-actual-https-config
             (merge valid-ssl-pem-config
                    {:ssl-port 8001 :cipher-suites ["FOO" "BAR"]}))
           (munge-expected-https-config
             {:https
              {:host          default-host
               :port          8001
               :cipher-suites ["FOO" "BAR"]}}))))

  (testing "cipher suites as a comma and space-separated string"
    (is (= (munge-actual-https-config
             (merge valid-ssl-pem-config
                    {:ssl-port 8001 :cipher-suites "FOO, BAR"}))
           (munge-expected-https-config
             {:https
              {:host          default-host
               :port          8001
               :cipher-suites ["FOO" "BAR"]}})))))

(deftest process-config-protocols-test
  (testing "protocols"
    (is (= (munge-actual-https-config
             (merge valid-ssl-pem-config
                    {:ssl-port 8001 :ssl-protocols ["FOO" "BAR"]}))
           (munge-expected-https-config
             {:https
              {:host      default-host
               :port      8001
               :protocols ["FOO" "BAR"]}}))))

  (testing "protocols as a comma and space-separated string"
    (is (= (munge-actual-https-config
             (merge valid-ssl-pem-config
                    {:ssl-port 8001 :ssl-protocols "FOO, BAR"}))
           (munge-expected-https-config
             {:https
              {:host      default-host
               :port      8001
               :protocols ["FOO" "BAR"]}})))))

(deftest process-config-crl-test
  (testing "ssl-crl-path"
    (is (= (munge-actual-https-config
             (merge valid-ssl-pem-config
                    {:ssl-port 8001
                     :ssl-crl-path
                               "./dev-resources/config/jetty/ssl/certs/ca.pem"}))
           (munge-expected-https-config
             {:https
              {:host         default-host
               :port         8001
               :ssl-crl-path "./dev-resources/config/jetty/ssl/certs/ca.pem"}})))))

(deftest process-config-client-auth-test
  (testing "client auth"
    (letfn [(get-client-auth [config]
                             (-> config
                                 (merge valid-ssl-pem-config)
                                 process-config
                                 (get-in [:https :client-auth])))]
      (testing "configure-web-server should set client-auth to a value of :need
        if not specified in options"
        (is (= :need (get-client-auth {:ssl-port 8001}))))

      (testing "configure-web-server should convert client-auth string to
            appropriate corresponding keyword value in configure-web-server
            options"
        (is (= :need (get-client-auth {:ssl-port 8081 :client-auth "need"})))
        (is (= :want (get-client-auth {:ssl-port 8081 :client-auth "want"})))
        (is (= :none (get-client-auth {:ssl-port 8081 :client-auth "none"}))))

      (testing "configure-web-server should throw IllegalArgumentException if an
            unsupported value is specified for the client-auth option"
        (is (thrown-with-msg? java.lang.IllegalArgumentException
                              #"Unexpected value found for client auth config option: bogus.  Expected need, want, or none."
                              (get-client-auth {:ssl-port 8081 :client-auth "bogus"})))))))

(deftest process-config-http-plus-https-test
  (testing "process-config successfully builds a WebserverConfig for plaintext+ssl"
    (is (= (munge-actual-https-config
             (merge valid-ssl-pem-config
                    {:ssl-host "foo.local" :port 8000}))
           (munge-expected-https-config
             {:http  {:host                      default-host
                      :port                      8000
                      :request-header-max-size   nil
                      :so-linger-milliseconds    nil
                      :idle-timeout-milliseconds nil
                      :acceptor-threads          nil
                      :selector-threads          nil}
              :https {:host "foo.local" :port default-https-port}})))))

(deftest process-config-invalid-test
  (testing "process-config fails for invalid server config"
    (are [config]
      (thrown? ExceptionInfo
               (process-config config))
      {:port "foo"}
      {:port 8000 :badkey "hi"}))

  (testing "process-config fails for incomplete ssl context config"
    (are [config]
      (thrown? IllegalArgumentException
               (process-config config))
      {}
      {:ssl-port 8001}
      {:ssl-port 8001 :ssl-host "foo.local"}
      {:ssl-host "foo.local"}
      valid-ssl-pem-config
      (merge {:ssl-port 8001} (dissoc valid-ssl-pem-config :ssl-key))
      (merge {:ssl-port 8001} (dissoc valid-ssl-keystore-config :keystore))))

  (testing "should warn if both keystore-based and PEM-based SSL settings are found"
    (with-test-logging
      (process-config (merge {:ssl-port 8001}
                             valid-ssl-pem-config
                             valid-ssl-keystore-config))
      (is (logged? #"Found settings for both keystore-based and PEM-based SSL")))))

(defn- validate-cert-lists-equal
  [pem-with-expected-certs ssl-cert ssl-cert-chain]
  (let [expected-certs (ssl/pem->certs pem-with-expected-certs)
        actual-certs   (construct-ssl-x509-cert-chain ssl-cert ssl-cert-chain)]
    (is (= (count expected-certs) (count actual-certs))
        "Number of expected certs do not match number of actual certs")
    (dotimes [n (count expected-certs)]
      (is (Arrays/equals (.getEncoded (nth expected-certs n))
                         (.getEncoded (nth actual-certs n)))
        (str "Expected cert # " n " from  does not match actual cert")))))

(deftest construct-ssl-x509-cert-chain-test
  (testing "non-existent ssl-cert throws expected exception"
    (let [tmp-file (ks/temp-file)]
      (fs/delete tmp-file)
      (is (thrown-with-msg? IllegalArgumentException
                            #"^Unable to open 'ssl-cert' file:"
                            (construct-ssl-x509-cert-chain
                              (.getAbsolutePath tmp-file)
                              nil)))))

  (testing "no content in ssl-cert throws expected exception"
    (let [tmp-file (ks/temp-file)]
      (is (thrown-with-msg? Exception
                            #"^No certs found in 'ssl-cert' file:"
                            (construct-ssl-x509-cert-chain
                              (.getAbsolutePath tmp-file)
                              nil)))))

  (testing "non-existent ssl-cert-chain throws expected exception"
    (let [tmp-file (ks/temp-file)]
      (fs/delete tmp-file)
      (is (thrown-with-msg? IllegalArgumentException
                            #"^Unable to open 'ssl-cert-chain' file:"
                            (construct-ssl-x509-cert-chain
                              "./dev-resources/config/jetty/ssl/certs/localhost.pem"
                              (.getAbsolutePath tmp-file))))))

  (testing "ssl-cert with single cert loaded into list"
    (validate-cert-lists-equal
      "./dev-resources/config/jetty/ssl/certs/localhost.pem"
      "./dev-resources/config/jetty/ssl/certs/localhost.pem"
      nil))

  (testing "ssl-cert with multiple certs loaded into list"
    (validate-cert-lists-equal
      "./dev-resources/config/jetty/ssl/certs/master-with-all-cas.pem"
      "./dev-resources/config/jetty/ssl/certs/master-with-all-cas.pem"
      nil))

  (testing (str "ssl-cert with single cert and ssl-cert-chain with "
                "multiple certs loaded into list")
    (validate-cert-lists-equal
      "./dev-resources/config/jetty/ssl/certs/master-with-all-cas.pem"
      "./dev-resources/config/jetty/ssl/certs/master.pem"
      "./dev-resources/config/jetty/ssl/certs/ca-master-intermediate-and-root.pem"))

  (testing (str "for ssl-cert with multiple certs and ssl-cert-chain with "
                "with one cert, only the first cert from ssl-cert is "
                "loaded into list with cert from ssl-cert-chain")
    (validate-cert-lists-equal
      "./dev-resources/config/jetty/ssl/certs/master-with-root-ca.pem"
      "./dev-resources/config/jetty/ssl/certs/master-with-intermediate-ca.pem"
      "./dev-resources/config/jetty/ssl/certs/ca-root.pem")))

(deftest test-advanced-scripting-config
  (testing "Verify that we can use scripting to handle advanced configuration scenarios"
    (let [config {:webserver
                  {:port               9000
                   :host               "localhost"
                   :post-config-script (str "import org.eclipse.jetty.server.ServerConnector;"
                                            "ServerConnector c = (ServerConnector)(server.getConnectors()[0]);\n"
                                            "c.setPort(10000);")}}]
      (with-test-logging
        (with-app-with-config app
          [jetty9-service]
          config
          (let [s (tk-app/get-service app :WebserverService)
                add-ring-handler (partial add-ring-handler s)
                body "Hi World"
                path "/hi_world"
                ring-handler (fn [req] {:status 200 :body body})]
            (testing "A warning is logged when using post-config-script"
              (is (logged? #"The 'post-config-script' setting is for advanced use"
                           :warn)))

            (testing "scripted changes are executed properly"
              (add-ring-handler ring-handler path)
              (let [response (http-get
                               (format "http://localhost:10000/%s" path))]
                (is (= (:status response) 200))
                (is (= (:body response) body)))))))))

  (testing "Server fails to start with bad post-config-script"
    (let [base-config {:port 9000
                       :host "localhost"}]
      (testing "Throws an error if the script can't be compiled."
        (is (thrown-with-msg?
              IllegalArgumentException
              #"Invalid script string in webserver 'post-config-script' configuration"
              (let [context (jetty9/initialize-context)]
                (with-test-logging
                 (try
                   (jetty9/start-webserver!
                    context
                    (merge base-config
                           {:post-config-script (str "AHAHHHGHAHAHAHEASD!  OMG!")}))
                   (finally
                     (jetty9/shutdown context))))))))
      (testing "Throws an error if the script can't be executed."
        (is (thrown-with-msg?
              IllegalArgumentException
              #"Invalid script string in webserver 'post-config-script' configuration"
              (let [context (jetty9/initialize-context)]
                (with-test-logging
                 (try
                   (jetty9/start-webserver!
                    context
                    (merge base-config
                           {:post-config-script (str "Object x = null; x.toString();")}))
                   (finally
                     (jetty9/shutdown context)))))))))))