@@ -17,7 +17,22 @@ module L = Debug.Make (struct let name = __MODULE__ end)
1717
1818let get_hostname () = try Unix. gethostname () with _ -> " "
1919
20- exception Unexpected_address_type of string
20+ type management_ip_error =
21+ | Interface_missing
22+ | Unexpected_address_type of string
23+ | IP_missing
24+ | Other of exn
25+
26+ let management_ip_error_to_string = function
27+ | Interface_missing ->
28+ " Management interface is missing"
29+ | IP_missing ->
30+ " Management IP is missing"
31+ | Unexpected_address_type s ->
32+ Printf. sprintf
33+ " Unexpected address type. Expected 'ipv4' or 'ipv6', got %s" s
34+ | Other e ->
35+ Printexc. to_string e
2136
2237(* Try to get all FQDNs, avoid localhost *)
2338let dns_names () =
@@ -46,32 +61,63 @@ let ipaddr_to_cstruct = function
4661 | Ipaddr. V6 addr ->
4762 Cstruct. of_string (Ipaddr.V6. to_octets addr)
4863
49- let list_head lst = List. nth_opt lst 0
50-
51- let get_management_ip_addr ~dbg =
64+ let get_management_ip_addrs ~dbg =
5265 let iface = Inventory. lookup Inventory. _management_interface in
5366 try
5467 if iface = " " || (not @@ Net.Interface. exists dbg iface) then
55- None
68+ Error Interface_missing
5669 else
57- let addrs =
70+ let ( let * ) = Result. bind in
71+ let * addrs =
72+ let ipv4 = Net.Interface. get_ipv4_addr dbg iface in
73+ let ipv6 = Net.Interface. get_ipv6_addr dbg iface in
5874 match
5975 String. lowercase_ascii
6076 (Inventory. lookup Inventory. _management_address_type ~default: " ipv4" )
6177 with
6278 | "ipv4" ->
63- Net.Interface. get_ipv4_addr dbg iface
79+ Ok (ipv4, ipv6)
6480 | "ipv6" ->
65- Net.Interface. get_ipv6_addr dbg iface
81+ Ok (ipv6, ipv4)
6682 | s ->
67- let msg = Printf. sprintf " Expected 'ipv4' or 'ipv6', got %s" s in
68- L. error " %s: %s" __FUNCTION__ msg ;
69- raise (Unexpected_address_type msg)
83+ Error (Unexpected_address_type s)
7084 in
71- addrs
72- |> List. map (fun (addr , _ ) -> Ipaddr_unix. of_inet_addr addr)
7385 (* Filter out link-local addresses *)
74- |> List. filter (fun addr -> Ipaddr. scope addr <> Ipaddr. Link )
75- |> List. map (fun ip -> (Ipaddr. to_string ip, ipaddr_to_cstruct ip))
76- |> list_head
77- with _ -> None
86+ let no_local (addr , _ ) =
87+ let addr = Ipaddr_unix. of_inet_addr addr in
88+ if Ipaddr. scope addr <> Ipaddr. Link then
89+ Some addr
90+ else
91+ None
92+ in
93+ Ok
94+ ( List. filter_map no_local (fst addrs)
95+ , List. filter_map no_local (snd addrs)
96+ )
97+ with e -> Error (Other e)
98+
99+ let get_management_ip_addr ~dbg =
100+ match get_management_ip_addrs ~dbg with
101+ | Ok (preferred , _ ) ->
102+ List. nth_opt preferred 0
103+ |> Option. map (fun addr -> (Ipaddr. to_string addr, ipaddr_to_cstruct addr))
104+ | Error _ ->
105+ None
106+
107+ let get_host_certificate_subjects ~dbg =
108+ let ( let * ) = Result. bind in
109+ let * ips, preferred_ip =
110+ match get_management_ip_addrs ~dbg with
111+ | Error e ->
112+ Error e
113+ | Ok (preferred , others ) ->
114+ let ips = List. (rev_append (rev preferred) others) in
115+ Option. fold ~none: (Error IP_missing )
116+ ~some: (fun ip -> Ok (List. map ipaddr_to_cstruct ips, ip))
117+ (List. nth_opt ips 0 )
118+ in
119+ let dns_names = dns_names () in
120+ let name =
121+ match dns_names with [] -> Ipaddr. to_string preferred_ip | dns :: _ -> dns
122+ in
123+ Ok (name, dns_names, ips)
0 commit comments