|
| 1 | +{-# LANGUAGE FlexibleContexts #-} |
| 2 | +{-# LANGUAGE OverloadedStrings #-} |
| 3 | +{-# LANGUAGE RankNTypes #-} |
| 4 | +{-# LANGUAGE TemplateHaskell #-} |
| 5 | + |
| 6 | +{-| |
| 7 | +Module : Stack.Sig.Sign |
| 8 | +Description : Signing Packages |
| 9 | +Copyright : (c) FPComplete.com, 2015 |
| 10 | +License : BSD3 |
| 11 | +Maintainer : Tim Dysinger <tim@fpcomplete.com> |
| 12 | +Stability : experimental |
| 13 | +Portability : POSIX |
| 14 | +-} |
| 15 | + |
| 16 | +module Stack.Sig.Sign (sign, signTarBytes) where |
| 17 | + |
| 18 | +import qualified Codec.Archive.Tar as Tar |
| 19 | +import qualified Codec.Compression.GZip as GZip |
| 20 | +import Control.Monad (when) |
| 21 | +import Control.Monad.Catch |
| 22 | +import Control.Monad.IO.Class |
| 23 | +import Control.Monad.Logger |
| 24 | +import Control.Monad.Trans.Control |
| 25 | +import qualified Data.ByteString.Lazy as BS |
| 26 | +import qualified Data.ByteString.Lazy as L |
| 27 | +import Data.Monoid ((<>)) |
| 28 | +import qualified Data.Text as T |
| 29 | +import Data.UUID (toString) |
| 30 | +import Data.UUID.V4 (nextRandom) |
| 31 | +import Network.HTTP.Conduit |
| 32 | + (Response(..), RequestBody(..), Request(..), httpLbs, newManager, |
| 33 | + tlsManagerSettings) |
| 34 | +import Network.HTTP.Download |
| 35 | +import Network.HTTP.Types (status200, methodPut) |
| 36 | +import Path |
| 37 | +import Path.IO |
| 38 | +import Stack.Package |
| 39 | +import qualified Stack.Sig.GPG as GPG |
| 40 | +import Stack.Types |
| 41 | +import qualified System.FilePath as FP |
| 42 | + |
| 43 | +-- | Sign a haskell package with the given url of the signature |
| 44 | +-- service and a path to a tarball. |
| 45 | +sign |
| 46 | + :: (MonadCatch m, MonadBaseControl IO m, MonadIO m, MonadMask m, MonadLogger m, MonadThrow m) |
| 47 | + => Maybe (Path Abs Dir) -> String -> Path Abs File -> m () |
| 48 | +sign Nothing _ _ = throwM SigNoProjectRootException |
| 49 | +sign (Just projectRoot) url filePath = do |
| 50 | + withStackWorkTempDir |
| 51 | + projectRoot |
| 52 | + (\tempDir -> |
| 53 | + do bytes <- |
| 54 | + liftIO |
| 55 | + (fmap |
| 56 | + GZip.decompress |
| 57 | + (BS.readFile (toFilePath filePath))) |
| 58 | + maybePath <- extractCabalFile tempDir (Tar.read bytes) |
| 59 | + case maybePath of |
| 60 | + Nothing -> throwM SigInvalidSDistTarBall |
| 61 | + Just cabalPath -> do |
| 62 | + pkg <- cabalFilePackageId (tempDir </> cabalPath) |
| 63 | + signPackage url pkg filePath) |
| 64 | + where |
| 65 | + extractCabalFile tempDir (Tar.Next entry entries) = do |
| 66 | + case Tar.entryContent entry of |
| 67 | + (Tar.NormalFile lbs _) -> |
| 68 | + case FP.splitFileName (Tar.entryPath entry) of |
| 69 | + (folder,file) |
| 70 | + | length (FP.splitDirectories folder) == 1 && |
| 71 | + FP.takeExtension file == ".cabal" -> do |
| 72 | + cabalFile <- parseRelFile file |
| 73 | + liftIO |
| 74 | + (BS.writeFile |
| 75 | + (toFilePath (tempDir </> cabalFile)) |
| 76 | + lbs) |
| 77 | + return (Just cabalFile) |
| 78 | + (_,_) -> extractCabalFile tempDir entries |
| 79 | + _ -> extractCabalFile tempDir entries |
| 80 | + extractCabalFile _ _ = return Nothing |
| 81 | + |
| 82 | +-- | Sign a haskell package with the given url to the signature |
| 83 | +-- service, a package tarball path (package tarball name) and a lazy |
| 84 | +-- bytestring of bytes that represent the tarball bytestream. The |
| 85 | +-- function will write the bytes to the path in a temp dir and sign |
| 86 | +-- the tarball with GPG. |
| 87 | +signTarBytes |
| 88 | + :: (MonadCatch m, MonadBaseControl IO m, MonadIO m, MonadMask m, MonadLogger m, MonadThrow m) |
| 89 | + => Maybe (Path Abs Dir) -> String -> Path Rel File -> L.ByteString -> m () |
| 90 | +signTarBytes Nothing _ _ _ = throwM SigNoProjectRootException |
| 91 | +signTarBytes (Just projectRoot) url tarPath bs = |
| 92 | + withStackWorkTempDir |
| 93 | + projectRoot |
| 94 | + (\tempDir -> |
| 95 | + do let tempTarBall = tempDir </> tarPath |
| 96 | + liftIO (L.writeFile (toFilePath tempTarBall) bs) |
| 97 | + sign (Just projectRoot) url tempTarBall) |
| 98 | + |
| 99 | +-- | Sign a haskell package given the url to the signature service, a |
| 100 | +-- @PackageIdentifier@ and a file path to the package on disk. |
| 101 | +signPackage |
| 102 | + :: (MonadCatch m, MonadBaseControl IO m, MonadIO m, MonadMask m, MonadLogger m, MonadThrow m) |
| 103 | + => String -> PackageIdentifier -> Path Abs File -> m () |
| 104 | +signPackage url pkg filePath = do |
| 105 | + $logInfo ("GPG signing " <> T.pack (toFilePath filePath)) |
| 106 | + sig@(Signature signature) <- GPG.signPackage filePath |
| 107 | + let (PackageIdentifier n v) = pkg |
| 108 | + name = show n |
| 109 | + version = show v |
| 110 | + verify <- GPG.verifyFile sig filePath |
| 111 | + fingerprint <- GPG.fullFingerprint verify |
| 112 | + req <- |
| 113 | + parseUrl |
| 114 | + (url <> "/upload/signature/" <> name <> "/" <> version <> "/" <> |
| 115 | + T.unpack (fingerprintSample fingerprint)) |
| 116 | + let put = |
| 117 | + req |
| 118 | + { method = methodPut |
| 119 | + , requestBody = RequestBodyBS signature |
| 120 | + } |
| 121 | + mgr <- liftIO (newManager tlsManagerSettings) |
| 122 | + res <- liftIO (httpLbs put mgr) |
| 123 | + when |
| 124 | + (responseStatus res /= status200) |
| 125 | + (throwM (GPGSignException "unable to sign & upload package")) |
| 126 | + |
| 127 | +withStackWorkTempDir |
| 128 | + :: (MonadCatch m, MonadIO m, MonadMask m, MonadLogger m) |
| 129 | + => Path Abs Dir -> (Path Abs Dir -> m ()) -> m () |
| 130 | +withStackWorkTempDir projectRoot f = do |
| 131 | + uuid <- liftIO nextRandom |
| 132 | + uuidPath <- parseRelDir (toString uuid) |
| 133 | + let tempDir = projectRoot </> workDirRel </> $(mkRelDir "tmp") </> uuidPath |
| 134 | + bracket |
| 135 | + (createTree tempDir) |
| 136 | + (const (removeTree tempDir)) |
| 137 | + (const (f tempDir)) |
0 commit comments