Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Confd / Utils.hs @ 3add7574

History | View | Annotate | Download (3.3 kB)

1
{-| Implementation of the Ganeti confd utilities.
2

    
3
This holds a few utility functions that could be useful in both
4
clients and servers.
5

    
6
-}
7

    
8
{-
9

    
10
Copyright (C) 2011, 2012 Google Inc.
11

    
12
This program is free software; you can redistribute it and/or modify
13
it under the terms of the GNU General Public License as published by
14
the Free Software Foundation; either version 2 of the License, or
15
(at your option) any later version.
16

    
17
This program is distributed in the hope that it will be useful, but
18
WITHOUT ANY WARRANTY; without even the implied warranty of
19
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20
General Public License for more details.
21

    
22
You should have received a copy of the GNU General Public License
23
along with this program; if not, write to the Free Software
24
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25
02110-1301, USA.
26

    
27
-}
28

    
29
module Ganeti.Confd.Utils
30
  ( getClusterHmac
31
  , parseSignedMessage
32
  , parseRequest
33
  , parseReply
34
  , signMessage
35
  , getCurrentTime
36
  ) where
37

    
38
import qualified Data.ByteString as B
39
import qualified Text.JSON as J
40
import System.Time
41

    
42
import Ganeti.BasicTypes
43
import Ganeti.Confd.Types
44
import Ganeti.Hash
45
import qualified Ganeti.Constants as C
46
import qualified Ganeti.Path as Path
47
import Ganeti.JSON
48
import Ganeti.Utils
49

    
50
-- | Type-adjusted max clock skew constant.
51
maxClockSkew :: Integer
52
maxClockSkew = fromIntegral C.confdMaxClockSkew
53

    
54
-- | Returns the HMAC key.
55
getClusterHmac :: IO HashKey
56
getClusterHmac = Path.confdHmacKey >>= fmap B.unpack . B.readFile
57

    
58
-- | Parses a signed message.
59
parseSignedMessage :: (J.JSON a) => HashKey -> String
60
                   -> Result (String, String, a)
61
parseSignedMessage key str = do
62
  (SignedMessage hmac msg salt) <- fromJResult "parsing signed message"
63
    $ J.decode str
64
  parsedMsg <- if verifyMac key (Just salt) msg hmac
65
           then fromJResult "parsing message" $ J.decode msg
66
           else Bad "HMAC verification failed"
67
  return (salt, msg, parsedMsg)
68

    
69
-- | Message parsing. This can either result in a good, valid request
70
-- message, or fail in the Result monad.
71
parseRequest :: HashKey -> String -> Integer
72
             -> Result (String, ConfdRequest)
73
parseRequest hmac msg curtime = do
74
  (salt, origmsg, request) <- parseSignedMessage hmac msg
75
  ts <- tryRead "Parsing timestamp" salt::Result Integer
76
  if abs (ts - curtime) > maxClockSkew
77
    then fail "Too old/too new timestamp or clock skew"
78
    else return (origmsg, request)
79

    
80
-- | Message parsing. This can either result in a good, valid reply
81
-- message, or fail in the Result monad.
82
-- It also checks that the salt in the message corresponds to the one
83
-- that is expected
84
parseReply :: HashKey -> String -> String -> Result (String, ConfdReply)
85
parseReply hmac msg expSalt = do
86
  (salt, origmsg, reply) <- parseSignedMessage hmac msg
87
  if salt /= expSalt
88
    then fail "The received salt differs from the expected salt"
89
    else return (origmsg, reply)
90

    
91
-- | Signs a message with a given key and salt.
92
signMessage :: HashKey -> String -> String -> SignedMessage
93
signMessage key salt msg =
94
  SignedMessage { signedMsgMsg  = msg
95
                , signedMsgSalt = salt
96
                , signedMsgHmac = hmac
97
                }
98
    where hmac = computeMac key (Just salt) msg
99

    
100
-- | Returns the current time.
101
getCurrentTime :: IO Integer
102
getCurrentTime = do
103
  TOD ctime _ <- getClockTime
104
  return ctime