From 84304821bebf9b794fae56f616b50ae1d06014d2 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Tue, 11 Nov 2014 12:58:53 -0400 Subject: propellor spin --- src/Propellor/Gpg.hs | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/Propellor/Gpg.hs (limited to 'src/Propellor/Gpg.hs') diff --git a/src/Propellor/Gpg.hs b/src/Propellor/Gpg.hs new file mode 100644 index 00000000..c65d06ec --- /dev/null +++ b/src/Propellor/Gpg.hs @@ -0,0 +1,101 @@ +module Propellor.Gpg where + +import Control.Applicative +import System.IO +import System.FilePath +import System.Directory +import Data.Maybe +import Data.List.Utils + +import Propellor.PrivData.Paths +import Utility.SafeCommand +import Utility.Process +import Utility.Monad +import Utility.Misc +import Utility.Tmp + +type KeyId = String + +keyring :: FilePath +keyring = privDataDir "keyring.gpg" + +listPubKeys :: IO [KeyId] +listPubKeys = parse . lines <$> readProcess "gpg" listopts + where + listopts = useKeyringOpts ++ ["--with-colons", "--list-public-keys"] + parse = mapMaybe (keyIdField . split ":") + keyIdField ("pub":_:_:_:f:_) = Just f + keyIdField _ = Nothing + +useKeyringOpts :: [String] +useKeyringOpts = + [ "--options" + , "/dev/null" + , "--no-default-keyring" + , "--keyring", keyring + ] + +addKey :: KeyId -> IO () +addKey keyid = exitBool =<< allM id + [ gpg, gitadd keyring, reencryptprivdata, gitconfig, gitcommit ] + where + gpg = do + createDirectoryIfMissing True privDataDir + boolSystem "sh" + [ Param "-c" + , Param $ "gpg --export " ++ keyid ++ " | gpg " ++ + unwords (useKeyringOpts ++ ["--import"]) + ] + + reencryptprivdata = ifM (doesFileExist privDataFile) + ( do + gpgEncrypt privDataFile =<< gpgDecrypt privDataFile + gitadd privDataFile + , return True + ) + + gitadd f = boolSystem "git" + [ Param "add" + , File f + ] + + gitconfig = boolSystem "git" + [ Param "config" + , Param "user.signingkey" + , Param keyid + ] + + gitcommit = gitCommit + [ File keyring + , Param "-m" + , Param "propellor addkey" + ] + +{- Automatically sign the commit if there'a a keyring. -} +gitCommit :: [CommandParam] -> IO Bool +gitCommit ps = do + k <- doesFileExist keyring + boolSystem "git" $ catMaybes $ + [ Just (Param "commit") + , if k then Just (Param "--gpg-sign") else Nothing + ] ++ map Just ps + +gpgDecrypt :: FilePath -> IO String +gpgDecrypt f = ifM (doesFileExist f) + ( readProcess "gpg" ["--decrypt", f] + , return "" + ) + +gpgEncrypt :: FilePath -> String -> IO () +gpgEncrypt f s = do + keyids <- listPubKeys + let opts = + [ "--default-recipient-self" + , "--armor" + , "--encrypt" + ] ++ concatMap (\k -> ["--recipient", k]) keyids + encrypted <- writeReadProcessEnv "gpg" opts + Nothing + (Just $ flip hPutStr s) + Nothing + viaTmp writeFile f encrypted -- cgit v1.3-2-g0d8e From 7ea0f460e6e3cd82e0ec3d4b1c2d6006b7c24619 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Tue, 11 Nov 2014 13:26:31 -0400 Subject: use --trust-model always to avoid prompt from gpg gpg prompts when encrypting to an untrusted key, but if propellor has been told to add a key, we implicitly trust it. --- src/Propellor/Gpg.hs | 1 + 1 file changed, 1 insertion(+) (limited to 'src/Propellor/Gpg.hs') diff --git a/src/Propellor/Gpg.hs b/src/Propellor/Gpg.hs index c65d06ec..e478f610 100644 --- a/src/Propellor/Gpg.hs +++ b/src/Propellor/Gpg.hs @@ -93,6 +93,7 @@ gpgEncrypt f s = do [ "--default-recipient-self" , "--armor" , "--encrypt" + , "--trust-model", "always" ] ++ concatMap (\k -> ["--recipient", k]) keyids encrypted <- writeReadProcessEnv "gpg" opts Nothing -- cgit v1.3-2-g0d8e From e4f9df8404b8a7a2358e920dc2a231a3df823d6d Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Tue, 11 Nov 2014 13:41:25 -0400 Subject: avoid configuring git signing key when there's no secret key Also, nice display for --add-key steps --- doc/todo/multi_gpg_key_privdata.mdwn | 3 ++- src/Propellor/Gpg.hs | 31 ++++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 10 deletions(-) (limited to 'src/Propellor/Gpg.hs') diff --git a/doc/todo/multi_gpg_key_privdata.mdwn b/doc/todo/multi_gpg_key_privdata.mdwn index 1d9b05a4..f7475c13 100644 --- a/doc/todo/multi_gpg_key_privdata.mdwn +++ b/doc/todo/multi_gpg_key_privdata.mdwn @@ -8,4 +8,5 @@ To support multiple gpg keys added with --add-key, propellor should so that this new key can access it. * When --add-key on behalf of another user, do not modify the signing key for local git. This entails either splitting this command in two, `--add-key` and - `--set-signing-key`, or adding another command `--add-foreign-key`. + `--set-signing-key`, or adding another command `--add-foreign-key`, + or perhaps determining if the key being added has a known secret key. diff --git a/src/Propellor/Gpg.hs b/src/Propellor/Gpg.hs index e478f610..572be190 100644 --- a/src/Propellor/Gpg.hs +++ b/src/Propellor/Gpg.hs @@ -8,6 +8,7 @@ import Data.Maybe import Data.List.Utils import Propellor.PrivData.Paths +import Propellor.Message import Utility.SafeCommand import Utility.Process import Utility.Monad @@ -19,6 +20,7 @@ type KeyId = String keyring :: FilePath keyring = privDataDir "keyring.gpg" +-- Lists the keys in propellor's keyring. listPubKeys :: IO [KeyId] listPubKeys = parse . lines <$> readProcess "gpg" listopts where @@ -36,10 +38,15 @@ useKeyringOpts = ] addKey :: KeyId -> IO () -addKey keyid = exitBool =<< allM id - [ gpg, gitadd keyring, reencryptprivdata, gitconfig, gitcommit ] +addKey keyid = exitBool =<< allM (uncurry actionMessage) + [ ("adding key to propellor's keyring", addkeyring) + , ("staging propellor's keyring", gitadd keyring) + , ("updating encryption of any privdata", reencryptprivdata) + , ("configuring git signing to use key", gitconfig) + , ("committing changes", gitcommit) + ] where - gpg = do + addkeyring = do createDirectoryIfMissing True privDataDir boolSystem "sh" [ Param "-c" @@ -59,11 +66,16 @@ addKey keyid = exitBool =<< allM id , File f ] - gitconfig = boolSystem "git" - [ Param "config" - , Param "user.signingkey" - , Param keyid - ] + gitconfig = ifM (snd <$> processTranscript "gpg" ["--list-secret-keys", keyid] Nothing) + ( boolSystem "git" + [ Param "config" + , Param "user.signingkey" + , Param keyid + ] + , do + warningMessage $ "Cannot find a secret key for key " ++ keyid ++ ", so not configuring git user.signingkey to use this key." + return True + ) gitcommit = gitCommit [ File keyring @@ -71,7 +83,7 @@ addKey keyid = exitBool =<< allM id , Param "propellor addkey" ] -{- Automatically sign the commit if there'a a keyring. -} +-- Automatically sign the commit if there'a a keyring. gitCommit :: [CommandParam] -> IO Bool gitCommit ps = do k <- doesFileExist keyring @@ -86,6 +98,7 @@ gpgDecrypt f = ifM (doesFileExist f) , return "" ) +-- Encrypt file to all keys in propellor's keyring. gpgEncrypt :: FilePath -> String -> IO () gpgEncrypt f s = do keyids <- listPubKeys -- cgit v1.3-2-g0d8e