Make the parseRequest function more general
authorMichele Tartara <mtartara@google.com>
Mon, 17 Dec 2012 14:34:17 +0000 (15:34 +0100)
committerMichele Tartara <mtartara@google.com>
Thu, 20 Dec 2012 16:16:39 +0000 (17:16 +0100)
The parseRequest function of the Confd utils can be used to parse both request
(in the server) and reply (in the client, soon to be implemented) signed
messages.
This patch changes the signature of the function to allow this, and its name
accordingly.

A unit test is updated as well.

Signed-off-by: Michele Tartara <mtartara@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>

htest/Test/Ganeti/Confd/Utils.hs
htools/Ganeti/Confd/Utils.hs

index 128a78e..38b9dc3 100644 (file)
@@ -82,7 +82,8 @@ prop_bad_key salt crq =
   let signed = Confd.Utils.signMessage key_sign salt (J.encode crq)
       encoded = J.encode signed
   in printTestCase ("Accepted message signed with different key" ++ encoded) $
-     Confd.Utils.parseRequest key_verify encoded ==?
+     (Confd.Utils.parseSignedMessage key_verify encoded
+      :: BasicTypes.Result (String, String, Confd.ConfdRequest)) ==?
        BasicTypes.Bad "HMAC verification failed"
 
 testSuite "Confd/Utils"
index d0e60a3..28fba06 100644 (file)
@@ -28,7 +28,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
 module Ganeti.Confd.Utils
   ( getClusterHmac
-  , parseRequest
+  , parseSignedMessage
   , parseMessage
   , signMessage
   , getCurrentTime
@@ -54,21 +54,23 @@ maxClockSkew = fromIntegral C.confdMaxClockSkew
 getClusterHmac :: IO HashKey
 getClusterHmac = Path.confdHmacKey >>= fmap B.unpack . B.readFile
 
--- | Parses a signed request.
-parseRequest :: HashKey -> String -> Result (String, String, ConfdRequest)
-parseRequest key str = do
-  (SignedMessage hmac msg salt) <- fromJResult "parsing request" $ J.decode str
-  req <- if verifyMac key (Just salt) msg hmac
+-- | Parses a signed message.
+parseSignedMessage :: (J.JSON a) => HashKey -> String
+                   -> Result (String, String, a)
+parseSignedMessage key str = do
+  (SignedMessage hmac msg salt) <- fromJResult "parsing signed message"
+    $ J.decode str
+  parsedMsg <- if verifyMac key (Just salt) msg hmac
            then fromJResult "parsing message" $ J.decode msg
            else Bad "HMAC verification failed"
-  return (salt, msg, req)
+  return (salt, msg, parsedMsg)
 
 -- | Message parsing. This can either result in a good, valid message,
 -- or fail in the Result monad.
 parseMessage :: HashKey -> String -> Integer
              -> Result (String, ConfdRequest)
 parseMessage hmac msg curtime = do
-  (salt, origmsg, request) <- parseRequest hmac msg
+  (salt, origmsg, request) <- parseSignedMessage hmac msg
   ts <- tryRead "Parsing timestamp" salt::Result Integer
   if abs (ts - curtime) > maxClockSkew
     then fail "Too old/too new timestamp or clock skew"