Skip to content

Commit 1a222de

Browse files
(PE-37634) ensure sign-all with empty list succeeds
Prior to this change, if the sign-all behavior was invoked for the CA service, an exception would be thrown because the function returns a result that wasn't expected. This adds a test that demonstrates the failure, and adds a fix to get the test to pass.
1 parent f1b5d91 commit 1a222de

File tree

2 files changed

+44
-31
lines changed

2 files changed

+44
-31
lines changed

src/clj/puppetlabs/puppetserver/certificate_authority.clj

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2483,35 +2483,35 @@
24832483
serial-lock serial-lock-timeout-seconds] :as ca-settings} :- CaSettings
24842484
report-activity]
24852485
(let [;; if part of a CA bundle, the intermediate CA will be first in the chain
2486-
cacert (utils/pem->ca-cert cacert cakey)
2486+
cacert (utils/pem->ca-cert cacert cakey)
24872487
casubject (utils/get-subject-from-x509-certificate cacert)
2488-
ca-private-key (utils/pem->private-key cakey)]
2489-
(when-not (empty? subjects)
2490-
;; since we are going to be manipulating the serial file and the inventory file for multiple entries,
2491-
;; acquire the locks to prevent lock thrashing
2492-
(common/with-safe-write-lock serial-lock serial-lock-descriptor serial-lock-timeout-seconds
2493-
(common/with-safe-write-lock inventory-lock inventory-lock-descriptor inventory-lock-timeout-seconds
2494-
(let [results
2495-
;; loop through the subjects, one at a time, and collect the results for success or failure.
2496-
(loop [s subjects
2497-
result {:signed []
2498-
:no-csr []
2499-
:signing-errors[]}]
2500-
(if-not (empty? s)
2501-
(let [subject (first s)
2502-
csr-path (path-to-cert-request csrdir subject)]
2503-
(if (fs/exists? csr-path)
2504-
(let [_ (log/trace (i18n/trs "File exists at {0}" csr-path))
2505-
one-result (maybe-sign-one subject csr-path cacert casubject ca-private-key ca-settings)]
2506-
;; one-result is either :signed or :signing-errors
2507-
(recur (rest s)
2508-
(update result one-result conj subject)))
2509-
(do
2510-
(log/trace (i18n/trs "File does not exist at {0}" csr-path))
2511-
(recur (rest s)
2512-
(update result :no-csr conj subject)))))
2513-
result))]
2514-
;; submit the signing activity as one entry for all the hosts.
2515-
(when-not (empty? (:signed results))
2516-
(report-activity (:signed results) "signed"))
2517-
results))))))
2488+
ca-private-key (utils/pem->private-key cakey)]
2489+
2490+
;; since we are going to be manipulating the serial file and the inventory file for multiple entries,
2491+
;; acquire the locks to prevent lock thrashing
2492+
(common/with-safe-write-lock serial-lock serial-lock-descriptor serial-lock-timeout-seconds
2493+
(common/with-safe-write-lock inventory-lock inventory-lock-descriptor inventory-lock-timeout-seconds
2494+
(let [results
2495+
;; loop through the subjects, one at a time, and collect the results for success or failure.
2496+
(loop [s subjects
2497+
result {:signed []
2498+
:no-csr []
2499+
:signing-errors []}]
2500+
(if-not (empty? s)
2501+
(let [subject (first s)
2502+
csr-path (path-to-cert-request csrdir subject)]
2503+
(if (fs/exists? csr-path)
2504+
(let [_ (log/trace (i18n/trs "File exists at {0}" csr-path))
2505+
one-result (maybe-sign-one subject csr-path cacert casubject ca-private-key ca-settings)]
2506+
;; one-result is either :signed or :signing-errors
2507+
(recur (rest s)
2508+
(update result one-result conj subject)))
2509+
(do
2510+
(log/trace (i18n/trs "File does not exist at {0}" csr-path))
2511+
(recur (rest s)
2512+
(update result :no-csr conj subject)))))
2513+
result))]
2514+
;; submit the signing activity as one entry for all the hosts.
2515+
(when-not (empty? (:signed results)))
2516+
(report-activity (:signed results) "signed")
2517+
results)))))

test/integration/puppetlabs/services/certificate_authority/certificate_authority_int_test.clj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,19 @@
12271227
:ssl-key (str bootstrap/server-conf-dir "/ssl/private_keys/localhost.pem")
12281228
:ssl-ca-cert (str bootstrap/server-conf-dir "/ca/ca_crt.pem")
12291229
:ssl-crl-path (str bootstrap/server-conf-dir "/ssl/crl.pem")}}
1230+
(testing "PE-37634 PE-sign-all with no pending certs returns 200 with expected payload"
1231+
(let [response (http-client/post
1232+
"https://localhost:8140/puppet-ca/v1/sign/all"
1233+
{:ssl-cert (str bootstrap/server-conf-dir "/ca/ca_crt.pem")
1234+
:ssl-key (str bootstrap/server-conf-dir "/ca/ca_key.pem")
1235+
:ssl-ca-cert (str bootstrap/server-conf-dir "/ca/ca_crt.pem")
1236+
:as :text
1237+
:headers {"Accept" "application/json"}})]
1238+
(is (= 200 (:status response)))
1239+
(is (= {:signed []
1240+
:no-csr []
1241+
:signing-errors []}
1242+
(json/parse-string (:body response) true)))))
12301243
(testing "returns 200 with valid payload"
12311244
;; note- more extensive testing of the behavior is done with the testing in sign-multiple-certificate-signing-requests!-test
12321245
(let [certname (ks/rand-str :alpha-lower 16)

0 commit comments

Comments
 (0)