summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoey Hess2019-04-16 11:19:43 -0400
committerJoey Hess2019-04-16 11:19:43 -0400
commite77deb1ef29201ec2a957e17b613958823cca8bb (patch)
treefd45c8fd879cc28fdbbf497682e6dbac56b25e2f
parent7b4a1e6149f1a04b2b66c22cca65514fe824782d (diff)
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.
-rw-r--r--debian/changelog4
-rw-r--r--doc/todo/Ssh.userKeyAt_should_create_the_.ssh_directory.mdwn9
-rw-r--r--doc/todo/Ssh.userKeyAt_should_create_the_.ssh_directory/comment_1_5883054e256c5970c425108bca106b58._comment11
-rw-r--r--src/Propellor/Property/Ssh.hs17
4 files changed, 34 insertions, 7 deletions
diff --git a/debian/changelog b/debian/changelog
index dd8f8fd2..b7dafc8c 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -10,6 +10,10 @@ propellor (5.8.0) UNRELEASED; urgency=medium
since it's namespaced.
* Add User.ownsWithPrimaryGroup
Thanks, Sean Whitton
+ * 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.
-- Joey Hess <id@joeyh.name> Mon, 08 Apr 2019 11:09:04 -0400
diff --git a/doc/todo/Ssh.userKeyAt_should_create_the_.ssh_directory.mdwn b/doc/todo/Ssh.userKeyAt_should_create_the_.ssh_directory.mdwn
index 0c333c0c..d61c809d 100644
--- a/doc/todo/Ssh.userKeyAt_should_create_the_.ssh_directory.mdwn
+++ b/doc/todo/Ssh.userKeyAt_should_create_the_.ssh_directory.mdwn
@@ -1,5 +1,10 @@
Hello,
-Maybe this should not create the directory as the given path is absolute, but unless my understanding is wrong, `Ssh.userKeys` does not create the directory either.
+Maybe this should not create the directory as the given path is absolute,
+but unless my understanding is wrong, `Ssh.userKeys` does not create the
+directory either.
-Could there be a `Ssh.userKeyNamed` for example which would prepend `$HOME/.ssh` to the given name?
+Could there be a `Ssh.userKeyNamed` for example which would prepend
+`$HOME/.ssh` to the given name?
+
+> [[done]] --[[Joey]]
diff --git a/doc/todo/Ssh.userKeyAt_should_create_the_.ssh_directory/comment_1_5883054e256c5970c425108bca106b58._comment b/doc/todo/Ssh.userKeyAt_should_create_the_.ssh_directory/comment_1_5883054e256c5970c425108bca106b58._comment
new file mode 100644
index 00000000..fc26779e
--- /dev/null
+++ b/doc/todo/Ssh.userKeyAt_should_create_the_.ssh_directory/comment_1_5883054e256c5970c425108bca106b58._comment
@@ -0,0 +1,11 @@
+[[!comment format=mdwn
+ username="joey"
+ subject="""comment 1"""
+ date="2019-04-16T15:12:20Z"
+ content="""
+Well spotted, I've fixed it to create the directory.
+
+userKeyAt's documentation doesn't actually specify that the filepath
+provided can't be relative, and if it were relative, it seems to make sense
+for it to be relative to the usual .ssh directory. Done so.
+"""]]
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"