File: test_rbac.clj

package info (click to toggle)
rbac-client-clojure 1.1.5-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 288 kB
  • sloc: sh: 61; makefile: 27; xml: 11
file content (333 lines) | stat: -rw-r--r-- 17,663 bytes parent folder | download | duplicates (2)
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
(ns puppetlabs.rbac-client.services.test-rbac
  (:require [clojure.test :refer [are deftest is testing]]
            [puppetlabs.http.client.sync :refer [create-client]]
            [puppetlabs.kitchensink.json :as json]
            [puppetlabs.rbac-client.protocols.rbac :as rbac]
            [puppetlabs.rbac-client.services.rbac :refer [remote-rbac-consumer-service api-url->status-url perm-str->map parse-subject]]
            [puppetlabs.rbac-client.testutils.config :as cfg]
            [puppetlabs.rbac-client.testutils.http :as http]
            [puppetlabs.trapperkeeper.logging :refer [reset-logging]]
            [puppetlabs.trapperkeeper.testutils.logging :refer [with-test-logging]]
            [puppetlabs.trapperkeeper.app :as tk-app]
            [slingshot.test]
            [puppetlabs.trapperkeeper.testutils.bootstrap :refer [with-app-with-config]]
            [puppetlabs.trapperkeeper.testutils.webserver :refer [with-test-webserver-and-config]])
  (:import [java.util UUID]))

(def ^:private rand-subject {:login "rando@whoknows.net", :id (UUID/randomUUID)})

(def ^:private configs
  (let [server-cfg (cfg/jetty-ssl-config)]
    {:server server-cfg
     :client (cfg/rbac-client-config server-cfg)}))

(defn- wrap-test-handler-middleware
  [handler]
  (http/wrap-test-handler-middleware handler (:client configs)))

(deftest test-perm-str->map
  (testing "returns the correct value for a * permission"
    (is (= {:object_type "environment"
            :action "deploy_code"
            :instance "production"}
           (perm-str->map "environment:deploy_code:production"))))

  (testing "returns the correct value for a simple permission"
    (is (= {:object_type "console_page"
            :action "view"
            :instance "*"}
           (perm-str->map "console_page:view:*"))))

  (testing "returns the correct value for an instance containing colons"
    (is (= {:object_type "tasks"
            :action "run"
            :instance "package::install"}
         (perm-str->map "tasks:run:package::install")))))

(deftest test-is-permitted?
  (testing "is-permitted? returns the first result from RBAC's API"
    (with-app-with-config tk-app [remote-rbac-consumer-service] (:client configs)
      (let [consumer-svc (tk-app/get-service tk-app :RbacConsumerService)]
        (doseq [result [[true] [false]]]
          (let [handler (wrap-test-handler-middleware
                          (constantly (http/json-200-resp result)))]
            (with-test-webserver-and-config handler _ (:server configs)
              (is (= (first result)
                     (rbac/is-permitted? consumer-svc rand-subject "users:disable:1"))))))))))

(deftest test-are-permitted?
  (testing "are-permitted? passes through the result from RBAC's API"
    (with-app-with-config tk-app [remote-rbac-consumer-service] (:client configs)
      (let [consumer-svc (tk-app/get-service tk-app :RbacConsumerService)]
        (dotimes [_ 10]
          (let [result (vec (repeatedly (rand-int 30) #(< (rand) 0.5)))
                handler (wrap-test-handler-middleware
                          (constantly (http/json-200-resp result)))]
            (with-test-webserver-and-config handler _ (:server configs)
              (is (= result
                     (rbac/are-permitted? consumer-svc rand-subject ["users:disable:1"]))))))))))

(deftest test-cert-whitelisted?
  (with-app-with-config tk-app [remote-rbac-consumer-service] (:client configs)
    (let [consumer-svc (tk-app/get-service tk-app :RbacConsumerService)]
      (testing "returns the result from RBAC using v2 endpoint"
        (doseq [result [true false]]
          (let [handler (wrap-test-handler-middleware
                          (fn [req]
                           (when (= "/rbac-api/v2/certs/foobar" (get req :uri))
                             (http/json-200-resp {:cn "foobar", :allowlisted result, :subject rand-subject}))))]
            (with-test-webserver-and-config handler _ (:server configs)
              (is (= result (rbac/cert-whitelisted? consumer-svc "foobar")))))))
      (testing "tries v2, falls back to v1, and v1 afterward"
        (let [subject rand-subject
              v2-count (atom 0)
              v1-count (atom 0)
              handler (wrap-test-handler-middleware
                        (fn [req]
                          (case (get req :uri)
                            "/rbac-api/v2/certs/foobar" (do
                                                          (swap! v2-count inc)
                                                          (http/json-resp 404 {}))
                            "/rbac-api/v1/certs/foobar" (do
                                                          (swap! v1-count inc)
                                                          (http/json-200-resp {:cn "foobar", :whitelisted true, :subject subject})))))]
          (with-test-webserver-and-config handler _ (:server configs)
            (is (= true (rbac/cert-whitelisted? consumer-svc "foobar")))
            (is (= 1 (deref v2-count)))
            (is (= 1 (deref v1-count)))
            (is (= true (rbac/cert-whitelisted? consumer-svc "foobar")))
            (is (= true (rbac/cert-whitelisted? consumer-svc "foobar")))
            (is (= true (rbac/cert-whitelisted? consumer-svc "foobar")))
            (is (= 1 (deref v2-count)))
            (is (= 4 (deref v1-count))))))
      (testing "returns false when no cert is supplied"
        (is (not (rbac/cert-whitelisted? consumer-svc nil)))))))

(deftest test-cert-allowed?
  (with-app-with-config tk-app [remote-rbac-consumer-service] (:client configs)
    (let [consumer-svc (tk-app/get-service tk-app :RbacConsumerService)]
      (testing "returns the result from RBAC using v2 endpoint"
        (doseq [result [true false]]
          (let [handler (wrap-test-handler-middleware
                         (fn [req]
                          (when (= "/rbac-api/v2/certs/foobar" (get req :uri))
                            (http/json-200-resp {:cn "foobar", :allowlisted result, :subject rand-subject}))))]
            (with-test-webserver-and-config handler _ (:server configs)
                                            (is (= result (rbac/cert-allowed? consumer-svc "foobar")))))))
      (testing "tries v2, falls back to v1, and v1 afterward"
        (let [subject rand-subject
              v2-count (atom 0)
              v1-count (atom 0)
              handler (wrap-test-handler-middleware
                       (fn [req]
                         (case (get req :uri)
                           "/rbac-api/v2/certs/foobar" (do
                                                         (swap! v2-count inc)
                                                         (http/json-resp 404 {}))
                           "/rbac-api/v1/certs/foobar" (do
                                                         (swap! v1-count inc)
                                                         (http/json-200-resp {:cn "foobar", :whitelisted true, :subject subject})))))]
          (with-test-webserver-and-config handler _ (:server configs)
                                          (is (= true (rbac/cert-allowed? consumer-svc "foobar")))
                                          (is (= 1 (deref v2-count)))
                                          (is (= 1 (deref v1-count)))
                                          (is (= true (rbac/cert-allowed? consumer-svc "foobar")))
                                          (is (= true (rbac/cert-allowed? consumer-svc "foobar")))
                                          (is (= true (rbac/cert-allowed? consumer-svc "foobar")))
                                          (is (= 1 (deref v2-count)))
                                          (is (= 4 (deref v1-count))))))
      (testing "returns false when no cert is supplied"
        (is (not (rbac/cert-allowed? consumer-svc nil)))))))

(deftest test-cert->subject
 (with-app-with-config tk-app [remote-rbac-consumer-service] (:client configs)
    (let [consumer-svc (tk-app/get-service tk-app :RbacConsumerService)]
      (testing "uses the v2 endpoint"
        (doseq [subject [rand-subject nil]]
          (let [handler (wrap-test-handler-middleware
                         (fn [req]
                           (when (= "/rbac-api/v2/certs/foobar" (get req :uri))
                            (http/json-200-resp {:cn "foobar", :allowlisted (some? subject), :subject subject}))))]
            (with-test-webserver-and-config handler _ (:server configs)
              (is (= subject (rbac/cert->subject consumer-svc "foobar")))))))
      (testing "tries v2, falls back to v1, and v1 afterward"
        (let [subject rand-subject
              v2-count (atom 0)
              v1-count (atom 0)
              handler (wrap-test-handler-middleware
                        (fn [req]
                          (case (get req :uri)
                            "/rbac-api/v2/certs/foobar" (do
                                                          (swap! v2-count inc)
                                                          (http/json-resp 404 {}))
                            "/rbac-api/v1/certs/foobar" (do
                                                          (swap! v1-count inc)
                                                          (http/json-200-resp {:cn "foobar", :whitelisted (some? subject), :subject subject})))))]
          (with-test-webserver-and-config handler _ (:server configs)
            (is (= subject (rbac/cert->subject consumer-svc "foobar")))
            (is (= 1 (deref v2-count)))
            (is (= 1 (deref v1-count)))
            (is (= subject (rbac/cert->subject consumer-svc "foobar")))
            (is (= subject (rbac/cert->subject consumer-svc "foobar")))
            (is (= subject (rbac/cert->subject consumer-svc "foobar")))
            (is (= 1 (deref v2-count)))
            (is (= 4 (deref v1-count))))))

      (testing "returns nil when no cert is supplied"
        (is (nil? (rbac/cert->subject consumer-svc nil)))))))

(deftest test-valid-token->subject
  (with-app-with-config tk-app [remote-rbac-consumer-service] (:client configs)
    (let [consumer-svc (tk-app/get-service tk-app :RbacConsumerService)
          !last-request (atom nil)
          handler (wrap-test-handler-middleware
                    (fn [req]
                      (reset! !last-request req)
                      (http/json-200-resp rand-subject)))]

      (with-test-webserver-and-config handler _ (:server configs)
        (testing "valid-token->subject"
          (let [returned-subject (rbac/valid-token->subject consumer-svc "token")
                {:keys [update_last_activity?]} (-> @!last-request
                                                 :body
                                                 (json/parse-string true))]
            (testing "returns the expected subject"
              (is (= rand-subject returned-subject)))
            (testing "updates the token's activity"
              (is (true? update_last_activity?))))

          (testing "doesn't update activity when the token is suffixed with '|no_keepalive'"
            (let [_ (rbac/valid-token->subject consumer-svc "token|no_keepalive")
                  {:keys [update_last_activity?]} (-> @!last-request
                                                      :body
                                                      (json/parse-string true))]
              (is (false? update_last_activity?)))))))))

(deftest test-list-permitted
  (with-app-with-config tk-app [remote-rbac-consumer-service] (:client configs)
    (let [consumer-svc (tk-app/get-service tk-app :RbacConsumerService)]
        (let [handler (wrap-test-handler-middleware
                       (fn [req]
                         (cond
                           (not (= "token" (get-in req [:headers "x-authentication"])))
                           (http/json-resp 400 {:kind "mismatched token"})

                           (not (= "/rbac-api/v1/permitted/numbers/count" (get req :uri)))
                           (http/json-resp 400 {:kind "incorrect url structure"})

                           :default
                           (http/json-200-resp ["one" "two" "three"]))))]
          (with-test-webserver-and-config handler _ (assoc (:server configs) :client-auth "want")
              (is (= ["one" "two" "three"] (rbac/list-permitted consumer-svc "token" "numbers" "count"))))))))

(deftest test-list-permitted-for
  (with-app-with-config tk-app [remote-rbac-consumer-service] (:client configs)
    (let [consumer-svc (tk-app/get-service tk-app :RbacConsumerService)]
      (let [mock-subj {:id "12345"
                       :login "login-string"}
            handler (wrap-test-handler-middleware
                     (fn [req]
                       (cond
                         (not (= "/rbac-api/v1/permitted/object-type/action/12345" (get req :uri)))
                         (http/json-resp 400 {:kind "incorrect url structure"})

                         :default
                         (http/json-200-resp ["four" "five" "six"]))))]
        (with-test-webserver-and-config handler _ (assoc (:server configs) :client-auth "want")
          (is (= ["four" "five" "six"] (rbac/list-permitted-for consumer-svc mock-subj "object-type" "action"))))))))

(deftest test-subject
  (with-app-with-config tk-app [remote-rbac-consumer-service] (:client configs)
    (let [consumer-svc (tk-app/get-service tk-app :RbacConsumerService)]
      (let [mock-subj {:id #uuid "2aa80edb-7f6a-4b94-b6ad-ce9a4cb453b2"
                       :login "login-string"}
            handler (wrap-test-handler-middleware
                     (fn [req]
                       (cond
                         (not (= "/rbac-api/v1/users/2aa80edb-7f6a-4b94-b6ad-ce9a4cb453b2" (get req :uri)))
                         (http/json-resp 400 {:kind "incorrect url structure"})

                         :default
                         (http/json-200-resp mock-subj))))]
        (with-test-webserver-and-config handler _ (assoc (:server configs) :client-auth "want")
          (is (= mock-subj (rbac/subject consumer-svc "2aa80edb-7f6a-4b94-b6ad-ce9a4cb453b2"))))))))

(deftest test-status-url
  (are [service-url rbac-api-url] (= service-url (api-url->status-url rbac-api-url))
    "https://foo.com:4444/status/v1/services"
    "https://foo.com:4444/rbac/rbac-api"

    "http://foo.com:4444/status/v1/services"
    "http://foo.com:4444/rbac/rbac-api"

    "https://foo.com/status/v1/services"
    "https://foo.com/rbac/rbac-api"

    "http://foo.com/status/v1/services"
    "http://foo.com/rbac/rbac-api"))

(deftest test-unconfigured
  (reset-logging)
  (with-test-logging
    (is (thrown+-with-msg? [:kind :puppetlabs.rbac-client/invalid-configuration]
                           #"'rbac-consumer' not configured with an 'api-url'"
          (with-app-with-config tk-app [remote-rbac-consumer-service]
            (assoc-in (:client configs) [:rbac-consumer :api-url] nil)
            nil)))))

(deftest test-status-check
  (with-app-with-config tk-app [remote-rbac-consumer-service] (:client configs)
    (let [consumer-svc (tk-app/get-service tk-app :RbacConsumerService)
          status-results {:activity-service
                          {:service_version "0.5.3",
                           :service_status_version 1,
                           :detail_level "info",
                           :state "running",
                           :status {:db_up true}}

                          :rbac-service
                          {:service_version "1.2.12",
                           :service_status_version 1,
                           :detail_level "info",
                           :state "running",
                           :status {:db_up true,
                                    :activity_up true}}}

          failed-response (http/malformed-json-400-resp "No 'level' parameter found in request")
          handler (wrap-test-handler-middleware
                   (fn [req]
                     (if (= "critical" (get-in req [:params "level"]))
                       (http/json-200-resp status-results)
                       failed-response)))
          error-handler (wrap-test-handler-middleware
                         (fn [req]
                           (if (= "critical" (get-in req [:params "level"]))
                             (http/json-200-resp
                              (-> status-results
                                  (assoc-in [:rbac-service :state] "error")
                                  (assoc-in [:rbac-service :status :db_up] false)))
                             failed-response)))]

      (with-test-webserver-and-config handler _ (:server configs)
        (is (= {:service_version "1.2.12",
                :service_status_version 1,
                :detail_level "info",
                :state :running,
                :status {:db_up true,
                         :activity_up true}}
               (rbac/status consumer-svc "critical"))))

      (with-test-webserver-and-config error-handler _ (:server configs)
        (is (= {:service_version "1.2.12",
                :service_status_version 1,
                :detail_level "info",
                :state :error,
                :status {:db_up false,
                         :activity_up true}}
               (rbac/status consumer-svc "critical")))))))

(deftest parse-subject-test
  (testing "parses subject with string id"
    (is (uuid? (:id (parse-subject (assoc rand-subject :id (.toString (UUID/randomUUID)))))))))
  (testing "parses subject with UUID id"
    (is (uuid? (:id (parse-subject rand-subject)))))