From b403d11969d41728ee1bc90e109a3de1e6a5531f Mon Sep 17 00:00:00 2001 From: Hannes Mehnert Date: Sat, 6 Jan 2018 14:20:23 +0100 Subject: [PATCH] vmm_provision: when private key is on disk, use it! --- provision/vmm_provision.ml | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/provision/vmm_provision.ml b/provision/vmm_provision.ml index 221bd1a..9579047 100644 --- a/provision/vmm_provision.ml +++ b/provision/vmm_provision.ml @@ -91,13 +91,18 @@ let sign ?dbname ?certname extensions issuer key csr delta = let priv_key ?(bits = 2048) fn name = let open Rresult.R.Infix in - match fn with - | None -> + let file = match fn with + | None -> Fpath.(v name + "key") + | Some f -> Fpath.v f + in + Bos.OS.File.exists file >>= function + | false -> + Logs.info (fun m -> m "creating new RSA key %a" Fpath.pp file) ; let priv = `RSA (Nocrypto.Rsa.generate bits) in - Bos.OS.File.write ~mode:0o400 Fpath.(v name + "key") (Cstruct.to_string (X509.Encoding.Pem.Private_key.to_pem_cstruct1 priv)) >>= fun () -> + Bos.OS.File.write ~mode:0o400 file (Cstruct.to_string (X509.Encoding.Pem.Private_key.to_pem_cstruct1 priv)) >>= fun () -> Ok priv - | Some fn -> - Bos.OS.File.read (Fpath.v fn) >>= fun s -> + | true -> + Bos.OS.File.read file >>= fun s -> Ok (X509.Encoding.Pem.Private_key.of_pem_cstruct1 (Cstruct.of_string s)) open Cmdliner