1 module NmcTransform ( seedNmcDom
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)
15 -- | Perform query and return error string or parsed domain object
17 (String -> IO (Either String ByteString)) -- ^ query operation action
19 -> IO (Either String NmcDom) -- ^ error string or domain
20 queryNmcDom queryOp key = do
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
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
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
41 mbase = mergeSelf base
42 base' = mbase {domDelegate = Nothing, domImport = Nothing}
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
50 mergeIncl1 (Left err) _ = return $ Left err -- can never happen
51 mergeIncl1 (Right acc) key = do
52 sub <- queryNmcDom queryOp key
54 Left err -> return $ Left err
55 Right sub' -> mergeIncl queryOp (depth - 1) $ sub' `merge` acc
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
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
71 case lookup "" map' of
73 Just sub -> (mergeSelf sub) `merge` base'
74 -- recursion depth limited by the size of the record
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)
83 -- | Presence of some elements require removal of some others
84 normalizeDom :: NmcDom -> NmcDom
85 normalizeDom dom = foldr id dom [ translateNormalizer
89 nsNormalizer dom = case domNs dom of
91 Just ns -> def { domNs = domNs dom, domEmail = domEmail dom }
92 translateNormalizer dom = case domTranslate dom of
94 Just tr -> dom { domSubmap = Nothing }
96 -- | Merge imports and Selfs and follow the maps tree to get dom
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
105 [] -> return $ fmap normalizeDom base'
108 Left err -> return base'
110 case domSubmap base'' of
111 Nothing -> return $ Right def
114 Nothing -> return $ Right def
115 Just sub -> descendNmcDom queryOp ds $ propagate base'' sub
117 -- | Initial NmcDom populated with "import" only, suitable for "descend"
119 String -- ^ domain key (without namespace prefix)
120 -> NmcDom -- ^ resulting seed domain
121 seedNmcDom dn = def { domImport = Just (["d/" ++ dn])}