summaryrefslogtreecommitdiff
path: root/src/Propellor/Types/PrivData.hs
diff options
context:
space:
mode:
authorJoey Hess2015-09-14 20:11:25 -0400
committerJoey Hess2015-09-14 20:11:25 -0400
commit9a0169f0cbdf2470e149a32f5fab8ec2369686f3 (patch)
treec8d4dc9f3a970b7ce3622370a8ff8ee8869b413c /src/Propellor/Types/PrivData.hs
parent115baccc7761356ec6633202e69dfff65f53a993 (diff)
clean up privdata excess/lacking newline issue
* PrivData converted to newtype (API change). * Stopped stripping trailing newlines when setting PrivData; this was previously done to avoid mistakes when pasting eg passwords with an unwanted newline. Instead, PrivData consumers should use either privDataLines or privDataVal, to extract respectively lines or a value (without internal newlines) from PrivData.
Diffstat (limited to 'src/Propellor/Types/PrivData.hs')
-rw-r--r--src/Propellor/Types/PrivData.hs21
1 files changed, 20 insertions, 1 deletions
diff --git a/src/Propellor/Types/PrivData.hs b/src/Propellor/Types/PrivData.hs
index d713c7cf..c72838cb 100644
--- a/src/Propellor/Types/PrivData.hs
+++ b/src/Propellor/Types/PrivData.hs
@@ -1,6 +1,9 @@
module Propellor.Types.PrivData where
import Propellor.Types.OS
+import Utility.PartialPrelude
+
+import Data.Maybe
-- | Note that removing or changing constructors or changing types will
-- break the serialized privdata files, so don't do that!
@@ -89,7 +92,23 @@ anyContext = Context "any"
hostContext :: HostContext
hostContext = HostContext Context
-type PrivData = String
+-- | Contains the actual private data.
+--
+-- Note that this may contain exta newlines at the end, or they may have
+-- been stripped off, depending on how the user entered the privdata,
+-- and which version of propellor stored it. Use the accessor functions
+-- below to avoid newline problems.
+newtype PrivData = PrivData String
+
+-- | When PrivData is the content of a file, this is the lines thereof.
+privDataLines :: PrivData -> [String]
+privDataLines (PrivData s) = lines s
+
+-- | When the PrivData is a single value, like a password, this extracts
+-- it. Note that if multiple lines are present in the PrivData, only
+-- the first is returned; there is never a newline in the String.
+privDataVal :: PrivData -> String
+privDataVal (PrivData s) = fromMaybe "" (headMaybe (lines s))
data SshKeyType = SshRsa | SshDsa | SshEcdsa | SshEd25519
deriving (Read, Show, Ord, Eq, Enum, Bounded)