From 5370ed32b53758b8036f6ad6f5b1a7c0fafa39e7 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Wed, 10 Jul 2019 05:46:15 -0400 Subject: use a type (FileWriter) --- lib/Kiki.hs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/Kiki.hs b/lib/Kiki.hs index 8ceda99..3a1028b 100644 --- a/lib/Kiki.hs +++ b/lib/Kiki.hs @@ -384,7 +384,12 @@ writeL077 f bs = do writeL f bs setFileCreationMask old_umask -getMkPathAndCommit :: FilePath -> IO (FilePath -> FilePath, IO ()) +data FileWriter = FileWriter { + pathMaker :: FilePath -> FilePath, + fileWriterCommit :: IO () +} + +getMkPathAndCommit :: FilePath -> IO (FileWriter) getMkPathAndCommit destdir = do let cachedir = takeDirectory destdir unslash ('/':xs) = xs @@ -414,7 +419,7 @@ getMkPathAndCommit destdir = do -- copyFile (destdir unslash (makeRelative destdir pth) (mkpath pth) -- doesFileExist (mkpath pth) >>= flip when copyIt -- return (mkpath pth) - return (mkpath, commit {-, readyReadBeforeWrite -}) + return $ FileWriter mkpath commit generateHostsFile :: (FilePath -> FilePath) -> KeyRingRuntime -> IO () generateHostsFile mkpath rt = do @@ -475,7 +480,7 @@ installIpsecConf mkpath wkaddr (certBasename) cs = do refreshCache :: KeyRingRuntime -> Maybe FilePath -> IO () refreshCache rt rootdir = do - (mkpath, commit) <- getMkPathAndCommit (fromMaybe "" rootdir ++ "/var/cache/kiki/config") + fw@(FileWriter mkpath commit) <- getMkPathAndCommit (fromMaybe "" rootdir ++ "/var/cache/kiki/config") generateHostsFile mkpath rt fromMaybe (error "No working key.") $ do Hostnames wkaddr onames _ _ <- names rt @@ -494,14 +499,14 @@ refreshCache rt rootdir = do where passphrases = mempty { opPassphrases = [PassphraseMemoizer (rtPassphrases rt)] } pemSecrets = mempty { opFiles = Map.fromList - [ send "ipsec" (mkpath $ ipsecKeyPath oname) "missing ipsec key?" - , send "ssh-client" (mkpath "root/.ssh/id_rsa") "missing ssh-client key?" - , send "ssh-server" (mkpath "ssh_host_rsa_key") "missing ssh host key?" - , send "tor" (mkpath "tor/private_key") "missing tor key?" + [ send "ipsec" (ipsecKeyPath oname) "missing ipsec key?" + , send "ssh-client" ("root/.ssh/id_rsa") "missing ssh-client key?" + , send "ssh-server" ("ssh_host_rsa_key") "missing ssh host key?" + , send "tor" ("tor/private_key") "missing tor key?" ] } send usage path warning = - ( ArgFile path, StreamInfo { typ = PEMFile + ( ArgFile (mkpath path), StreamInfo { typ = PEMFile , fill = KF_Match usage , spill = KF_None , access = Sec @@ -512,15 +517,15 @@ refreshCache rt rootdir = do outputReport report -- outputReport $ map (first $ resolveForReport ctx) $ filter ((/=ExportedSubkey) . snd) report rt'' <- rethrowKikiErrors rt' - writePublicKeyFiles rt'' mkpath grip oname wkaddr commit + writePublicKeyFiles rt'' fw grip oname wkaddr rethrowKikiErrors :: KikiCondition a -> IO a rethrowKikiErrors BadPassphrase = error "Operation requires correct passphrase. (Hint: Use --passphrase-fd=0 to input it on stdin.)" rethrowKikiErrors rt = unconditionally $ return rt -writePublicKeyFiles :: KeyRingRuntime -> (FilePath -> FilePath) -> String -> Char8.ByteString -> SockAddr -> IO () -> IO () -writePublicKeyFiles rt mkpath grip oname wkaddr commit = do +writePublicKeyFiles :: KeyRingRuntime -> FileWriter -> String -> Char8.ByteString -> SockAddr -> IO () +writePublicKeyFiles rt (FileWriter mkpath commit) grip oname wkaddr = do -- Finally, export public keys if they do not exist. either warn (write $ mkpath "root/.ssh/id_rsa.pub") $ show_ssh' "ssh-client" grip (rtKeyDB rt) -- cgit v1.2.3