Skip to content

Commit 1cea371

Browse files
committed
Improve cert check error
Signed-off-by: Changlei Li <changlei.li@cloud.com>
1 parent ebcc64e commit 1cea371

5 files changed

Lines changed: 22 additions & 29 deletions

File tree

ocaml/libs/stunnel/stunnel.ml

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ exception Stunnel_binary_missing
2323

2424
exception Stunnel_error of string
2525

26-
exception Stunnel_verify_error of string
26+
exception Stunnel_verify_error of string list
2727

2828
let crl_path = "/etc/stunnel/crls"
2929

@@ -138,7 +138,7 @@ type t = {
138138
}
139139
140140
type stunnel_error =
141-
| Certificate_verify of string
141+
| Certificate_verify of string list
142142
| Stunnel of string
143143
| Unknown of string
144144
@@ -489,14 +489,7 @@ let with_client_proxy_systemd_service ~verify_cert ~remote_host ~remote_port
489489
)
490490
(fun () -> Unixext.unlink_safe conf_path)
491491
492-
let check_verify_error line =
493-
let sub_after i s =
494-
let len = String.length s in
495-
String.sub s i (len - i)
496-
in
497-
let split_1 c s =
498-
match Astring.String.cut ~sep:c s with Some (x, _) -> x | None -> s
499-
in
492+
let check_verify_error cert_errors line =
500493
(* When verified with a mismatched certificate, one line of log from stunnel
501494
* would look like:
502495
SSL_connect: ssl/statem/statem_clnt.c:1889: error:0A000086:SSL routines::certificate verify failed
@@ -505,17 +498,14 @@ let check_verify_error line =
505498
if Astring.String.is_infix ~affix:"certificate verify failed" line then
506499
match Astring.String.find_sub ~sub:"error:" line with
507500
| Some e ->
508-
raise
509-
(Stunnel_verify_error
510-
(split_1 "," (sub_after (e + String.length "error:") line))
511-
)
501+
raise (Stunnel_verify_error cert_errors)
512502
| None ->
513-
raise (Stunnel_verify_error "")
503+
raise (Stunnel_verify_error [])
514504
else if
515505
Astring.String.is_infix ~affix:"No certificate or private key specified"
516506
line
517507
then
518-
raise (Stunnel_verify_error "The specified certificate is corrupt")
508+
raise (Stunnel_verify_error ["The specified certificate is corrupt"])
519509
else
520510
()
521511
@@ -524,9 +514,12 @@ let check_error s line =
524514
raise (Stunnel_error s)
525515
526516
let check_stunnel_logfile logfile =
517+
let cert_errors = ref [] in
527518
let check_line line =
528519
!stunnel_logger line ;
529-
check_verify_error line ;
520+
if Astring.String.is_infix ~affix:"CERT:" line then
521+
cert_errors := line :: !cert_errors ;
522+
check_verify_error !cert_errors line ;
530523
check_error "Connection refused" line ;
531524
check_error "No host resolved" line ;
532525
check_error "No route to host" line ;
@@ -540,8 +533,8 @@ let check_stunnel_status logfile =
540533
match check_stunnel_logfile logfile with
541534
| () ->
542535
Ok ()
543-
| exception Stunnel_verify_error reason ->
544-
Error (Certificate_verify reason)
536+
| exception Stunnel_verify_error r ->
537+
Error (Certificate_verify r)
545538
| exception Stunnel_error reason ->
546539
Error (Stunnel reason)
547540
| exception exn ->

ocaml/libs/stunnel/stunnel.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ exception Stunnel_binary_missing
1717

1818
exception Stunnel_error of string
1919

20-
exception Stunnel_verify_error of string
20+
exception Stunnel_verify_error of string list
2121

2222
val crl_path : string
2323

@@ -56,7 +56,7 @@ type t = {
5656
}
5757

5858
type stunnel_error =
59-
| Certificate_verify of string
59+
| Certificate_verify of string list
6060
| Stunnel of string
6161
| Unknown of string
6262

ocaml/xapi/remote_requests.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,5 +211,5 @@ let send_test_post ~__context ~host ~port ~body =
211211
with
212212
| Timed_out ->
213213
raise (Api_errors.Server_error (Api_errors.wlb_timeout, ["30.0"]))
214-
| Stunnel.Stunnel_verify_error reason ->
215-
raise (Api_errors.Server_error (Api_errors.ssl_verify_error, [reason]))
214+
| Stunnel.Stunnel_verify_error reasons ->
215+
raise (Api_errors.Server_error (Api_errors.ssl_verify_error, reasons))

ocaml/xapi/repository.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ let sync ~__context ~self ~token ~token_id ~username ~password =
312312
with
313313
| Api_errors.Server_error (_, _) as e ->
314314
raise e
315-
| Stunnel.Stunnel_verify_error reason ->
316-
raise (Api_errors.Server_error (Api_errors.ssl_verify_error, [reason]))
315+
| Stunnel.Stunnel_verify_error reasons ->
316+
raise (Api_errors.Server_error (Api_errors.ssl_verify_error, reasons))
317317
| e ->
318318
error "Failed to sync with remote YUM repository: %s"
319319
(ExnHelper.string_of_exn e) ;

ocaml/xapi/workload_balancing.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ let raise_timeout timeout =
5656
raise
5757
(Api_errors.Server_error (Api_errors.wlb_timeout, [string_of_float timeout]))
5858

59-
let raise_verify_error reason =
60-
raise (Api_errors.Server_error (Api_errors.ssl_verify_error, [reason]))
59+
let raise_verify_error reasons =
60+
raise (Api_errors.Server_error (Api_errors.ssl_verify_error, reasons))
6161

6262
let raise_authentication_failed () =
6363
raise (Api_errors.Server_error (Api_errors.wlb_authentication_failed, []))
@@ -344,8 +344,8 @@ let wlb_request ~__context ~host ~port ~auth ~meth ~params ~handler ~enable_log
344344
raise_connection_reset ()
345345
| Xmlrpc_client.Connection_reset ->
346346
raise_connection_reset ()
347-
| Stunnel.Stunnel_verify_error reason ->
348-
raise_verify_error reason
347+
| Stunnel.Stunnel_verify_error reasons ->
348+
raise_verify_error reasons
349349
| Stunnel.Stunnel_error error_msg as exc -> (
350350
match error_msg with
351351
| "Connection refused" ->

0 commit comments

Comments
 (0)