Skip to content

Commit f11657e

Browse files
authored
Merge pull request #5670 from snwoods/private/stevenwo/CP-49116
CP-49116: Add Sha1 support to external_certificate_thumbprint_of_master.
2 parents 2008661 + 5e51f8e commit f11657e

File tree

10 files changed

+77
-44
lines changed

10 files changed

+77
-44
lines changed

ocaml/idl/datamodel_certificate.ml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,16 @@ let t =
6464
; field ~qualifier:StaticRO ~lifecycle ~ty:DateTime "not_after"
6565
~default_value:(Some (VDateTime Date.never))
6666
"Date before which the certificate is valid"
67-
; field ~qualifier:StaticRO ~lifecycle ~ty:String "fingerprint"
67+
; field ~qualifier:StaticRO
68+
~lifecycle:
69+
[(Published, rel_stockholm, ""); (Deprecated, "24.19.0", "")]
70+
~ty:String "fingerprint" ~default_value:(Some (VString ""))
71+
"Use fingerprint_sha256 instead"
72+
; field ~qualifier:StaticRO ~lifecycle ~ty:String "fingerprint_sha256"
6873
~default_value:(Some (VString ""))
6974
"The certificate's SHA256 fingerprint / hash"
75+
; field ~qualifier:StaticRO ~lifecycle ~ty:String "fingerprint_sha1"
76+
~default_value:(Some (VString ""))
77+
"The certificate's SHA1 fingerprint / hash"
7078
]
7179
~messages:[] ()

ocaml/idl/datamodel_common.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ open Datamodel_roles
1010
to leave a gap for potential hotfixes needing to increment the schema version.*)
1111
let schema_major_vsn = 5
1212

13-
let schema_minor_vsn = 778
13+
let schema_minor_vsn = 779
1414

1515
(* Historical schema versions just in case this is useful later *)
1616
let rio_schema_major_vsn = 5

ocaml/idl/schematest.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ let hash x = Digest.string x |> Digest.to_hex
33
(* BEWARE: if this changes, check that schema has been bumped accordingly in
44
ocaml/idl/datamodel_common.ml, usually schema_minor_vsn *)
55

6-
let last_known_schema_hash = "6566a4091ecb3200649185730e4f185d"
6+
let last_known_schema_hash = "e34cd0d32cdcec7805c2d3ed4e4a0c25"
77

88
let current_schema_hash : string =
99
let open Datamodel_types in

ocaml/xapi-cli-server/records.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5237,6 +5237,12 @@ let certificate_record rpc session_id certificate =
52375237
; make_field ~name:"fingerprint"
52385238
~get:(fun () -> (x ()).API.certificate_fingerprint)
52395239
()
5240+
; make_field ~name:"fingerprint_sha256"
5241+
~get:(fun () -> (x ()).API.certificate_fingerprint_sha256)
5242+
()
5243+
; make_field ~name:"fingerprint_sha1"
5244+
~get:(fun () -> (x ()).API.certificate_fingerprint_sha1)
5245+
()
52405246
]
52415247
}
52425248

ocaml/xapi/api_server.ml

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -250,24 +250,28 @@ let is_host_is_slave_error (response : Rpc.response) =
250250
false
251251

252252
let create_thumbprint_header req response =
253-
let include_thumbprint =
253+
let hash_type_opt =
254254
match
255255
List.assoc_opt
256256
!Xapi_globs.cert_thumbprint_header_request
257257
req.Http.Request.additional_headers
258258
with
259-
| Some x when x = !Xapi_globs.cert_thumbprint_header_value ->
260-
true
259+
| Some x when x = !Xapi_globs.cert_thumbprint_header_value_sha256 ->
260+
Some `Sha256
261+
| Some x when x = !Xapi_globs.cert_thumbprint_header_value_sha1 ->
262+
Some `Sha1
261263
| _ ->
262-
false
264+
None
263265
in
264-
if include_thumbprint && is_host_is_slave_error response then
265-
Helpers.external_certificate_thumbprint_of_master ()
266-
|> Option.fold ~none:[] ~some:(fun x ->
267-
[(!Xapi_globs.cert_thumbprint_header_response, x)]
268-
)
269-
else
270-
[]
266+
Option.bind hash_type_opt (fun hash_type ->
267+
if is_host_is_slave_error response then
268+
Helpers.external_certificate_thumbprint_of_master ~hash_type
269+
else
270+
None
271+
)
272+
|> Option.fold ~none:[] ~some:(fun x ->
273+
[(!Xapi_globs.cert_thumbprint_header_response, x)]
274+
)
271275

272276
module Unixext = Xapi_stdext_unix.Unixext
273277

ocaml/xapi/certificates.ml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ let update_ca_bundle () = Helpers.update_ca_bundle ()
6666
let to_string = function CA_Certificate -> "CA certificate" | CRL -> "CRL"
6767

6868
(** {pp_hash hash} outputs the hexadecimal representation of the {hash}
69-
adding a semicolon between every octet, in uppercase.
69+
adding a colon between every octet, in uppercase.
7070
*)
7171
let pp_hash hash =
7272
let hex = Hex.(show @@ of_cstruct hash) in
@@ -218,13 +218,17 @@ end = struct
218218
let not_before, not_after =
219219
dates_of_ptimes (X509.Certificate.validity certificate)
220220
in
221-
let fingerprint =
221+
let fingerprint_sha256 =
222222
X509.Certificate.fingerprint `SHA256 certificate |> pp_hash
223223
in
224+
let fingerprint_sha1 =
225+
X509.Certificate.fingerprint `SHA1 certificate |> pp_hash
226+
in
224227
let uuid = Uuidx.(to_string (make ())) in
225228
let ref' = Ref.make () in
226229
Db.Certificate.create ~__context ~ref:ref' ~uuid ~host ~not_before
227-
~not_after ~fingerprint ~name ~_type ;
230+
~not_after ~fingerprint:fingerprint_sha256 ~fingerprint_sha256
231+
~fingerprint_sha1 ~name ~_type ;
228232
debug "added cert %s under uuid=%s ref=%s" name uuid (Ref.string_of ref') ;
229233
post_action () ;
230234
ref'

ocaml/xapi/certificates_sync.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ let install ~__context ~host:_ ~type' cert =
3232
(** determine if the database is up to date by comparing the fingerprint
3333
of xapi-ssl.pem with the entry in the database *)
3434
let is_unchanged ~__context cert_ref cert =
35-
let ref_hash = Db.Certificate.get_fingerprint ~__context ~self:cert_ref in
35+
let ref_hash =
36+
Db.Certificate.get_fingerprint_sha256 ~__context ~self:cert_ref
37+
in
3638
let cert_hash =
3739
X509.Certificate.fingerprint `SHA256 cert |> Certificates.pp_hash
3840
in

ocaml/xapi/helpers.ml

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,30 +2041,35 @@ let update_ca_bundle =
20412041
)
20422042
)
20432043

2044-
let external_certificate_thumbprint_of_master ?(hash_type = `Sha256) () =
2045-
match hash_type with
2046-
| `Sha256 ->
2047-
Server_helpers.exec_with_new_task
2048-
"Get master's external certificate thumbprint" (fun __context ->
2049-
let master_ref = get_master ~__context in
2050-
let certs =
2051-
Db.Certificate.get_records_where ~__context
2052-
~expr:
2053-
(And
2054-
( Eq (Field "host", Literal (Ref.string_of master_ref))
2055-
, Eq (Field "type", Literal "host")
2056-
)
2057-
)
2058-
in
2059-
match certs with
2060-
| [] ->
2061-
debug "Failed to fetch master's external certificate" ;
2062-
None
2063-
| (_, cert_record) :: _ ->
2064-
Some cert_record.certificate_fingerprint
2065-
)
2066-
| _ ->
2067-
None
2044+
let external_certificate_thumbprint_of_master ~hash_type =
2045+
if List.mem hash_type [`Sha256; `Sha1] then
2046+
Server_helpers.exec_with_new_task
2047+
"Get master's external certificate thumbprint" (fun __context ->
2048+
let master_ref = get_master ~__context in
2049+
let certs =
2050+
Db.Certificate.get_records_where ~__context
2051+
~expr:
2052+
(And
2053+
( Eq (Field "host", Literal (Ref.string_of master_ref))
2054+
, Eq (Field "type", Literal "host")
2055+
)
2056+
)
2057+
in
2058+
match certs with
2059+
| [] ->
2060+
debug "%s: Failed to fetch master's external certificate"
2061+
__FUNCTION__ ;
2062+
None
2063+
| (_, cert_record) :: _ -> (
2064+
match hash_type with
2065+
| `Sha256 ->
2066+
Some cert_record.certificate_fingerprint_sha256
2067+
| `Sha1 ->
2068+
Some cert_record.certificate_fingerprint_sha1
2069+
)
2070+
)
2071+
else
2072+
None
20682073

20692074
let unit_test ~__context : bool =
20702075
Pool_role.is_unit_test ()

ocaml/xapi/xapi_globs.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,9 @@ let max_observer_file_size = ref (1 lsl 20)
10201020
let cert_thumbprint_header_request =
10211021
ref "x-xenapi-request-host-certificate-thumbprint"
10221022

1023-
let cert_thumbprint_header_value = ref "sha-256:master"
1023+
let cert_thumbprint_header_value_sha256 = ref "sha-256:master"
1024+
1025+
let cert_thumbprint_header_value_sha1 = ref "sha-1:master"
10241026

10251027
let cert_thumbprint_header_response =
10261028
ref "x-xenapi-response-host-certificate-thumbprint"

ocaml/xapi/xapi_pool.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,9 @@ let pre_join_checks ~__context ~rpc ~session_id ~force =
775775
list
776776
|> List.to_seq
777777
|> Seq.map (fun (_, record) ->
778-
(record.API.certificate_name, record.API.certificate_fingerprint)
778+
( record.API.certificate_name
779+
, record.API.certificate_fingerprint_sha256
780+
)
779781
)
780782
|> CertMap.of_seq
781783
in

0 commit comments

Comments
 (0)