{-# LANGUAGE FlexibleContexts, OverloadedStrings, RecordWildCards #-}
module Network.AWS.CloudFront.SignedCookies.CLI
( main, Opts (..), optsParser
) where
import Network.AWS.CloudFront.SignedCookies
import Data.Coerce (Coercible, coerce)
import Data.Foldable (for_)
import Data.Semigroup ((<>))
import qualified Options.Applicative as Opt
import qualified Data.Text as Text
import qualified Data.Text.IO as Text
import Data.Time.Clock (nominalDay)
main :: IO ()
main = do
Opts{..} <- Opt.execParser $
Opt.info (Opt.helper <*> optsParser) $
Opt.header "Generator of signed cookies for AWS CloudFront"
policy <- simplePolicy opt_resource opt_lifespan
key <- readPrivateKeyPemFile opt_pemFilePath
cookies <- createSignedCookies opt_keyPairId key policy
(Text.putStr . renderCookiesText) cookies
data Opts =
Opts
{ opt_pemFilePath :: PemFilePath
, opt_keyPairId :: KeyPairId
, opt_resource :: Resource
, opt_lifespan :: Lifespan
}
optsParser :: Opt.Parser Opts
optsParser =
Opts
<$> text "pem-file" "Location in the filesystem where a .pem file \
\containing an RSA secret key can be found"
<*> text "key-pair-id" "CloudFront key pair ID for the key pair that \
\you are using to generate signature"
<*> text "resource" "URL that the policy will grant access to, \
\optionally containing asterisks for wildcards"
<*> days "days" "Integer number of days until the policy expires"
where
text :: Coercible Text a => String -> String -> Opt.Parser a
text long help = fmap (coerce . Text.pack) $ Opt.strOption $ mod long help
days :: Coercible NominalDiffTime a => String -> String -> Opt.Parser a
days long help = fmap (coerce . (* nominalDay) . fromInteger) $
Opt.option Opt.auto $ mod long help
mod :: Opt.HasName f => String -> String -> Opt.Mod f a
mod long help = Opt.long long <> Opt.help help