]> www.average.org Git - pdns-pipe-nmc.git/blob - NmcTransform.hs
23244a554aa9c25803cbf18b473275b29c7526bb
[pdns-pipe-nmc.git] / NmcTransform.hs
1 module NmcTransform ( seedNmcDom
2                     , descendNmcDom
3                     ) where
4
5 import Prelude hiding (lookup)
6 import Data.ByteString.Lazy (ByteString)
7 import Data.Map.Lazy (empty, lookup, delete, size, singleton
8                      , foldrWithKey, insert, insertWith)
9 import Control.Monad (foldM)
10 import Data.Aeson (decode)
11 import Data.Default.Class (def)
12
13 import NmcDom
14
15 -- | Perform query and return error string or parsed domain object
16 queryNmcDom ::
17   (String -> IO (Either String ByteString)) -- ^ query operation action
18   -> String                                 -- ^ key
19   -> IO (Either String NmcDom)              -- ^ error string or domain
20 queryNmcDom queryOp key = do
21   l <- queryOp key
22   case l of
23     Left estr -> return $ Left estr
24     Right str -> case decode str :: Maybe NmcDom of
25       Nothing  -> return $ Left $ "Unparseable value: " ++ (show str)
26       Just dom -> return $ Right dom
27
28 -- | Try to fetch "delegate" or "import" object and merge them into the
29 --   base domain. Original "import" element is removed, but newly
30 --   merged data may contain new "import" or "delegate", so the objects
31 --   that are about to be merged are processed recursively until there
32 --   are no more "import" and "deletage" attributes (or the depth gauge
33 --   reaches zero).
34 mergeIncl ::
35   (String -> IO (Either String ByteString)) -- ^ query operation action
36   -> Int                                    -- ^ recursion counter
37   -> NmcDom                                 -- ^ base domain
38   -> IO (Either String NmcDom)              -- ^ result with merged import
39 mergeIncl queryOp depth base = do
40   let
41     mbase = mergeSelf base
42     base' = mbase {domDelegate = Nothing, domImport = Nothing}
43   -- print base
44   if depth <= 0 then return $ Left "Nesting of imports is too deep"
45     else case ((domDelegate mbase), (domImport mbase)) of
46       (Nothing,  Nothing  ) -> return $ Right base'
47       (Nothing,  Just keys) -> foldM mergeIncl1 (Right base') keys
48       (Just key, _        ) -> mergeIncl1 (Right def) key
49   where
50     mergeIncl1 (Left  err) _   = return $ Left err -- can never happen
51     mergeIncl1 (Right acc) key = do
52       sub <- queryNmcDom queryOp key
53       case sub of
54         Left  err  -> return $ Left err
55         Right sub' -> mergeIncl queryOp (depth - 1) $ sub' `merge` acc
56
57 -- | If there is an element in the map with key "", merge the contents
58 --   and remove this element. Do this recursively.
59 mergeSelf :: NmcDom -> NmcDom
60 mergeSelf base =
61   let
62     map   = domSubmap base
63     base' = base {domSubmap = removeSelf map}
64     removeSelf Nothing    = Nothing
65     removeSelf (Just map) = if size map' == 0 then Nothing else Just map'
66       where map' = delete "" map
67   in
68     case map of
69       Nothing   -> base'
70       Just map' ->
71         case lookup "" map' of
72           Nothing  -> base'
73           Just sub -> (mergeSelf sub) `merge` base'
74         -- recursion depth limited by the size of the record
75
76 -- | transfer some elements of `base` into `sub`, notably TLSA
77 propagate :: NmcDom -> NmcDom -> NmcDom
78 propagate base sub = sub `merge` (pickglobals base)
79   where -- FIXME must do this on the map elements, not on the top level
80     pickglobals dom = def { domTlsa = fmap pickforcedtls (domTlsa dom) }
81     pickforcedtls = filter (\x -> tlsIncSubdoms x)
82  
83 -- | Presence of some elements require removal of some others
84 normalizeDom :: NmcDom -> NmcDom
85 normalizeDom dom = foldr id dom [ translateNormalizer
86                                 , nsNormalizer
87                                 ]
88   where
89     nsNormalizer dom = case domNs dom of
90       Nothing  -> dom
91       Just ns  -> def { domNs = domNs dom, domEmail = domEmail dom }
92     translateNormalizer dom = case domTranslate dom of
93       Nothing  -> dom
94       Just tr  -> dom { domSubmap = Nothing }
95
96 -- | Merge imports and Selfs and follow the maps tree to get dom
97 descendNmcDom ::
98   (String -> IO (Either String ByteString)) -- ^ query operation action
99   -> [String]                               -- ^ subdomain chain
100   -> NmcDom                                 -- ^ base domain
101   -> IO (Either String NmcDom)              -- ^ fully processed result
102 descendNmcDom queryOp subdom base = do
103   base' <- mergeIncl queryOp 10 base
104   case subdom of
105     []   -> return $ fmap normalizeDom base'
106     d:ds ->
107       case base' of
108         Left err     -> return base'
109         Right base'' ->
110           case domSubmap base'' of
111             Nothing  -> return $ Right def
112             Just map ->
113               case lookup d map of
114                 Nothing  -> return $ Right def
115                 Just sub -> descendNmcDom queryOp ds $ propagate base'' sub
116
117 -- | Initial NmcDom populated with "import" only, suitable for "descend"
118 seedNmcDom ::
119   String        -- ^ domain key (without namespace prefix)
120   -> NmcDom     -- ^ resulting seed domain
121 seedNmcDom dn = def { domImport = Just (["d/" ++ dn])}