diff options
author | Andrew Cady <d@jerkface.net> | 2016-10-28 18:11:30 -0400 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2016-10-28 18:57:10 -0400 |
commit | d489dcbc4ed286c54e4d1fc6502cf7e074a60dc2 (patch) | |
tree | f0fe866b8af1e46573dd5ab3a3ded475ca20b147 | |
parent | 1a03d33cb840b5484f5d3f0954e29643332d5993 (diff) |
factor out function 'configGetCertReqs'
-rw-r--r-- | acme-certify.hs | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/acme-certify.hs b/acme-certify.hs index b246a66..71309fb 100644 --- a/acme-certify.hs +++ b/acme-certify.hs | |||
@@ -233,17 +233,29 @@ readSignedObject = | |||
233 | B.readFile >=> return . | 233 | B.readFile >=> return . |
234 | either error (map (X509.decodeSignedObject . pemContent)) . pemParseBS | 234 | either error (map (X509.decodeSignedObject . pemContent)) . pemParseBS |
235 | 235 | ||
236 | configGetCertReqs :: Config -> IO [(String, DomainName, [VHostSpec])] | ||
237 | configGetCertReqs hostsConfig = do | ||
238 | fmap concat $ forM (Config.keys hostsConfig) $ \host -> | ||
239 | do | ||
240 | hostParts <- (Config.subconfig host hostsConfig >>= Config.subconfig "domains") <&> extractObject | ||
241 | return $ flip map (HashMap.keys hostParts) $ \domain -> | ||
242 | (unpack host, domainName' $ unpack domain, combineSubdomains domain hostParts) | ||
243 | |||
244 | combineSubdomains :: AsPrimitive v => Text -> HashMap.HashMap Text v -> [VHostSpec] | ||
245 | combineSubdomains domain subs = | ||
246 | map (makeVHostSpec (domainName' $ unpack domain)) $ | ||
247 | sort -- '.' sorts first | ||
248 | $ concat $ HashMap.lookup domain subs & toListOf (_Just . _String . to (words . unpack)) | ||
249 | |||
250 | extractObject :: Config -> Object | ||
251 | extractObject (Config _ o) = o | ||
236 | 252 | ||
237 | runUpdate :: UpdateOpts -> IO () | 253 | runUpdate :: UpdateOpts -> IO () |
238 | runUpdate UpdateOpts { .. } = do | 254 | runUpdate UpdateOpts { .. } = do |
239 | issuerCert <- readX509 letsEncryptX3CrossSigned | 255 | issuerCert <- readX509 letsEncryptX3CrossSigned |
240 | 256 | ||
241 | config <- Config.load $ fromMaybe defaultUpdateConfigFile updateConfigFile | 257 | config <- Config.load $ fromMaybe defaultUpdateConfigFile updateConfigFile |
242 | hostsConfig <- Config.subconfig "hosts" config | 258 | certReqDomains <- configGetCertReqs =<< Config.subconfig "hosts" config |
243 | certReqDomains <- fmap concat $ forM (Config.keys hostsConfig) $ \host -> do | ||
244 | hostParts <- (Config.subconfig host hostsConfig >>= Config.subconfig "domains") <&> extractObject | ||
245 | return $ flip map (HashMap.keys hostParts) $ \domain -> | ||
246 | (unpack host, domainName' $ unpack domain, combineSubdomains domain hostParts) | ||
247 | 259 | ||
248 | globalCertificateDir <- getHomeDirectory <&> (</> if updateStaging then ".acme/fake-certs" else ".acme/certs") | 260 | globalCertificateDir <- getHomeDirectory <&> (</> if updateStaging then ".acme/fake-certs" else ".acme/certs") |
249 | createDirectoryIfMissing True globalCertificateDir | 261 | createDirectoryIfMissing True globalCertificateDir |
@@ -281,8 +293,6 @@ runUpdate UpdateOpts { .. } = do | |||
281 | else print =<< fetchCertificate directoryUrl terms email issuerCert spec | 293 | else print =<< fetchCertificate directoryUrl terms email issuerCert spec |
282 | 294 | ||
283 | where | 295 | where |
284 | extractObject :: Config -> Object | ||
285 | extractObject (Config _ o) = o | ||
286 | 296 | ||
287 | elemOrNull :: Eq a => [a] -> a -> Bool | 297 | elemOrNull :: Eq a => [a] -> a -> Bool |
288 | elemOrNull xs x = null xs || x `elem` xs | 298 | elemOrNull xs x = null xs || x `elem` xs |
@@ -312,12 +322,6 @@ runUpdate UpdateOpts { .. } = do | |||
312 | csUserKeys = keys | 322 | csUserKeys = keys |
313 | csCertificateDir = baseDir </> host </> domainToString domain | 323 | csCertificateDir = baseDir </> host </> domainToString domain |
314 | 324 | ||
315 | combineSubdomains :: AsPrimitive v => Text -> HashMap.HashMap Text v -> [VHostSpec] | ||
316 | combineSubdomains domain subs = | ||
317 | map (makeVHostSpec (domainName' $ unpack domain)) $ | ||
318 | sort -- '.' sorts first | ||
319 | $ concat $ HashMap.lookup domain subs & toListOf (_Just . _String . to (words . unpack)) | ||
320 | |||
321 | domainToString :: DomainName -> String | 325 | domainToString :: DomainName -> String |
322 | domainToString = unpack . decodeUtf8 . Text.Domain.Validate.toByteString | 326 | domainToString = unpack . decodeUtf8 . Text.Domain.Validate.toByteString |
323 | 327 | ||