From e77deb1ef29201ec2a957e17b613958823cca8bb Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Tue, 16 Apr 2019 11:19:43 -0400 Subject: Ssh.userKeys, Ssh.userKeyAt: Create .ssh directory when it does not yet exist. * Ssh.userKeys, Ssh.userKeyAt: Create .ssh directory when it does not yet exist. * Ssh.userKeyAt: When a relative filepath is provided, it's put inside the user's .ssh directory. --- src/Propellor/Property/Ssh.hs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/Propellor/Property/Ssh.hs b/src/Propellor/Property/Ssh.hs index 59698ec0..ca20e68c 100644 --- a/src/Propellor/Property/Ssh.hs +++ b/src/Propellor/Property/Ssh.hs @@ -279,9 +279,12 @@ userKeys user@(User name) context ks = combineProperties desc $ toProps $ -- | Sets up a user with the specified pubic key, and a private -- key from the privdata. -- --- A file can be specified to write the key to somewhere other than +-- A FilePath can be specified to write the key to somewhere other than -- the default locations. Allows a user to have multiple keys for -- different roles. +-- +-- When the FilePath is relative, is put inside the User's +-- ~/.ssh/ directory. userKeyAt :: IsContext c => Maybe FilePath -> User -> c -> (SshKeyType, PubKeyText) -> Property (HasInfo + UnixLike) userKeyAt dest user@(User u) context (keytype, pubkeytext) = combineProperties desc $ props @@ -306,14 +309,18 @@ userKeyAt dest user@(User u) context (keytype, pubkeytext) = installprop writer ext key = do f <- liftIO $ keyfile ext return $ combineProperties desc $ props + & File.dirExists (takeDirectory f) & writer f (keyFileContent key) & File.ownerGroup f user (userGroup user) & File.ownerGroup (takeDirectory f) user (userGroup user) keyfile ext = case dest of - Nothing -> do - home <- homeDirectory <$> getUserEntryForName u - return $ home ".ssh" "id_" ++ fromKeyType keytype ++ ext - Just f -> return $ f ++ ext + Nothing -> relhomessh $ "id_" ++ fromKeyType keytype ++ ext + Just f + | isRelative f -> relhomessh (f ++ ext) + | otherwise -> return (f ++ ext) + relhomessh f = do + home <- homeDirectory <$> getUserEntryForName u + return $ home ".ssh" f fromKeyType :: SshKeyType -> String fromKeyType SshRsa = "rsa" -- cgit v1.2.3