2013-04-25 21:28:25 +00:00
|
|
|
{- S3 remotes
|
2011-03-28 02:00:44 +00:00
|
|
|
-
|
2014-08-08 22:54:04 +00:00
|
|
|
- Copyright 2011-2014 Joey Hess <joey@kitenet.net>
|
2011-03-28 02:00:44 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2014-10-23 19:48:37 +00:00
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
|
2014-10-22 21:14:38 +00:00
|
|
|
module Remote.S3 (remote, iaHost, configIA, isIA, iaItemUrl) where
|
2011-03-28 02:00:44 +00:00
|
|
|
|
2014-08-08 22:54:04 +00:00
|
|
|
import qualified Aws as AWS
|
|
|
|
import qualified Aws.Core as AWS
|
|
|
|
import qualified Aws.S3 as S3
|
2012-12-01 18:11:37 +00:00
|
|
|
import qualified Data.Text as T
|
2014-05-28 00:31:25 +00:00
|
|
|
import qualified Data.Text.Encoding as T
|
|
|
|
import qualified Data.ByteString.Lazy as L
|
2014-08-09 19:58:01 +00:00
|
|
|
import qualified Data.ByteString as S
|
2011-03-29 17:49:54 +00:00
|
|
|
import qualified Data.Map as M
|
2011-05-16 15:20:30 +00:00
|
|
|
import Data.Char
|
2013-04-25 17:14:49 +00:00
|
|
|
import Network.Socket (HostName)
|
2014-08-08 22:54:04 +00:00
|
|
|
import Network.HTTP.Conduit (Manager, newManager, closeManager)
|
|
|
|
import Network.HTTP.Client (defaultManagerSettings, managerResponseTimeout, responseStatus, responseBody, RequestBody(..))
|
|
|
|
import Network.HTTP.Types
|
|
|
|
import Control.Monad.Trans.Resource
|
|
|
|
import Control.Monad.Catch
|
2014-08-09 19:58:01 +00:00
|
|
|
import Data.Conduit
|
2011-03-28 02:00:44 +00:00
|
|
|
|
2011-10-05 20:02:51 +00:00
|
|
|
import Common.Annex
|
2011-06-02 01:56:04 +00:00
|
|
|
import Types.Remote
|
|
|
|
import Types.Key
|
2011-06-30 17:16:57 +00:00
|
|
|
import qualified Git
|
2011-03-30 19:15:46 +00:00
|
|
|
import Config
|
2013-03-13 20:16:01 +00:00
|
|
|
import Config.Cost
|
2011-08-17 00:49:54 +00:00
|
|
|
import Remote.Helper.Special
|
2014-08-09 18:23:54 +00:00
|
|
|
import Remote.Helper.Http
|
2012-11-20 20:43:58 +00:00
|
|
|
import qualified Remote.Helper.AWS as AWS
|
2012-11-14 23:32:27 +00:00
|
|
|
import Creds
|
2013-09-07 22:38:00 +00:00
|
|
|
import Annex.UUID
|
2013-04-25 21:28:25 +00:00
|
|
|
import Logs.Web
|
2014-08-09 19:58:01 +00:00
|
|
|
import Utility.Metered
|
2013-04-25 21:28:25 +00:00
|
|
|
|
2014-08-09 00:29:56 +00:00
|
|
|
type BucketName = String
|
2011-03-28 02:00:44 +00:00
|
|
|
|
2012-01-06 03:14:10 +00:00
|
|
|
remote :: RemoteType
|
|
|
|
remote = RemoteType {
|
2011-03-29 18:55:59 +00:00
|
|
|
typename = "S3",
|
2011-03-30 18:00:54 +00:00
|
|
|
enumerate = findSpecialRemotes "s3",
|
|
|
|
generate = gen,
|
2011-03-29 18:55:59 +00:00
|
|
|
setup = s3Setup
|
|
|
|
}
|
2011-03-29 03:51:07 +00:00
|
|
|
|
2013-09-12 19:54:35 +00:00
|
|
|
gen :: Git.Repo -> UUID -> RemoteConfig -> RemoteGitConfig -> Annex (Maybe Remote)
|
2014-08-10 02:13:03 +00:00
|
|
|
gen r u c gc = do
|
|
|
|
cst <- remoteCost gc expensiveRemoteCost
|
|
|
|
info <- extractS3Info c
|
|
|
|
return $ new cst info
|
2012-11-30 04:55:59 +00:00
|
|
|
where
|
2014-08-10 02:13:03 +00:00
|
|
|
new cst info = Just $ specialRemote c
|
|
|
|
(prepareS3 this info $ store this)
|
|
|
|
(prepareS3 this info retrieve)
|
|
|
|
(prepareS3 this info remove)
|
|
|
|
(prepareS3 this info $ checkKey this)
|
2011-04-17 15:01:34 +00:00
|
|
|
this
|
2012-11-30 04:55:59 +00:00
|
|
|
where
|
|
|
|
this = Remote {
|
|
|
|
uuid = u,
|
|
|
|
cost = cst,
|
|
|
|
name = Git.repoDescribe r,
|
2014-08-02 19:51:58 +00:00
|
|
|
storeKey = storeKeyDummy,
|
|
|
|
retrieveKeyFile = retreiveKeyFileDummy,
|
run Preparer to get Remover and CheckPresent actions
This will allow special remotes to eg, open a http connection and reuse it,
while checking if chunks are present, or removing chunks.
S3 and WebDAV both need this to support chunks with reasonable speed.
Note that a special remote might want to cache a http connection across
multiple requests. A simple case of this is that CheckPresent is typically
called before Store or Remove. A remote using this interface can certianly
use a Preparer that eg, uses a MVar to cache a http connection.
However, it's up to the remote to then deal with things like stale or
stalled http connections when eg, doing a series of downloads from a remote
and other places. There could be long delays between calls to a remote,
which could lead to eg, http connection stalls; the machine might even
move to a new network, etc.
It might be nice to improve this interface later to allow
the simple case without needing to handle the full complex case.
One way to do it would be to have a `Transaction SpecialRemote cache`,
where SpecialRemote contains methods for Storer, Retriever, Remover, and
CheckPresent, that all expect to be passed a `cache`.
2014-08-06 18:28:36 +00:00
|
|
|
retrieveKeyFileCheap = retrieveCheap,
|
|
|
|
removeKey = removeKeyDummy,
|
|
|
|
checkPresent = checkPresentDummy,
|
2014-08-06 17:45:19 +00:00
|
|
|
checkPresentCheap = False,
|
2012-11-30 04:55:59 +00:00
|
|
|
whereisKey = Nothing,
|
2013-10-11 20:03:18 +00:00
|
|
|
remoteFsck = Nothing,
|
2013-10-27 19:38:59 +00:00
|
|
|
repairRepo = Nothing,
|
2012-11-30 04:55:59 +00:00
|
|
|
config = c,
|
|
|
|
repo = r,
|
2013-01-01 17:52:47 +00:00
|
|
|
gitconfig = gc,
|
2012-11-30 04:55:59 +00:00
|
|
|
localpath = Nothing,
|
|
|
|
readonly = False,
|
2014-01-13 18:41:10 +00:00
|
|
|
availability = GloballyAvailable,
|
2014-08-10 18:52:58 +00:00
|
|
|
remotetype = remote,
|
2014-10-21 18:36:09 +00:00
|
|
|
mkUnavailable = gen r u (M.insert "host" "!dne!" c) gc,
|
2014-10-21 19:34:32 +00:00
|
|
|
getInfo = includeCredsInfo c (AWS.creds u) $ catMaybes
|
2014-10-23 19:41:57 +00:00
|
|
|
[ Just ("bucket", fromMaybe "unknown" (getBucketName c))
|
2014-10-21 19:34:32 +00:00
|
|
|
, if isIA c
|
2014-10-23 19:41:57 +00:00
|
|
|
then Just ("internet archive item", iaItemUrl $ fromMaybe "unknown" $ getBucketName c)
|
2014-10-21 19:34:32 +00:00
|
|
|
else Nothing
|
|
|
|
]
|
2012-11-30 04:55:59 +00:00
|
|
|
}
|
2011-03-28 05:32:47 +00:00
|
|
|
|
2014-02-11 18:06:50 +00:00
|
|
|
s3Setup :: Maybe UUID -> Maybe CredPair -> RemoteConfig -> Annex (RemoteConfig, UUID)
|
|
|
|
s3Setup mu mcreds c = do
|
2013-09-07 22:38:00 +00:00
|
|
|
u <- maybe (liftIO genUUID) return mu
|
glacier, S3: Fix bug that caused embedded creds to not be encypted using the remote's key.
encryptionSetup must be called before setRemoteCredPair. Otherwise,
the RemoteConfig doesn't have the cipher in it, and so no cipher is used to
encrypt the embedded creds.
This is a security fix for non-shared encryption methods!
For encryption=shared, there's no security problem, just an
inconsistentency in whether the embedded creds are encrypted.
This is very important to get right, so used some types to help ensure that
setRemoteCredPair is only run after encryptionSetup. Note that the external
special remote bypasses the type safety, since creds can be set after the
initial remote config, if the external special remote program requests it.
Also note that IA remotes never use encryption, so encryptionSetup is not
run for them at all, and again the type safety is bypassed.
This leaves two open questions:
1. What to do about S3 and glacier remotes that were set up
using encryption=pubkey/hybrid with embedcreds?
Such a git repo has a security hole embedded in it, and this needs to be
communicated to the user. Is the changelog enough?
2. enableremote won't work in such a repo, because git-annex will
try to decrypt the embedded creds, which are not encrypted, so fails.
This needs to be dealt with, especially for ecryption=shared repos,
which are not really broken, just inconsistently configured.
Noticing that problem for encryption=shared is what led to commit
fbdeeeed5fa276d94be587c8916d725eddcaf546, which tried to
fix the problem by not decrypting the embedded creds.
This commit was sponsored by Josh Taylor.
2014-09-18 21:07:17 +00:00
|
|
|
s3Setup' u mcreds c
|
|
|
|
s3Setup' :: UUID -> Maybe CredPair -> RemoteConfig -> Annex (RemoteConfig, UUID)
|
|
|
|
s3Setup' u mcreds c = if isIA c then archiveorg else defaulthost
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
|
|
|
remotename = fromJust (M.lookup "name" c)
|
|
|
|
defbucket = remotename ++ "-" ++ fromUUID u
|
|
|
|
defaults = M.fromList
|
2012-12-01 18:11:37 +00:00
|
|
|
[ ("datacenter", T.unpack $ AWS.defaultRegion AWS.S3)
|
2012-11-11 04:51:07 +00:00
|
|
|
, ("storageclass", "STANDARD")
|
2014-08-08 22:54:04 +00:00
|
|
|
, ("host", AWS.s3DefaultHost)
|
|
|
|
, ("port", "80")
|
2012-11-11 04:51:07 +00:00
|
|
|
, ("bucket", defbucket)
|
|
|
|
]
|
2011-05-16 15:20:30 +00:00
|
|
|
|
2012-11-11 04:51:07 +00:00
|
|
|
use fullconfig = do
|
|
|
|
gitConfigSpecialRemote u fullconfig "s3" "true"
|
2014-02-24 19:14:44 +00:00
|
|
|
return (fullconfig, u)
|
2011-05-16 15:20:30 +00:00
|
|
|
|
2012-11-11 04:51:07 +00:00
|
|
|
defaulthost = do
|
glacier, S3: Fix bug that caused embedded creds to not be encypted using the remote's key.
encryptionSetup must be called before setRemoteCredPair. Otherwise,
the RemoteConfig doesn't have the cipher in it, and so no cipher is used to
encrypt the embedded creds.
This is a security fix for non-shared encryption methods!
For encryption=shared, there's no security problem, just an
inconsistentency in whether the embedded creds are encrypted.
This is very important to get right, so used some types to help ensure that
setRemoteCredPair is only run after encryptionSetup. Note that the external
special remote bypasses the type safety, since creds can be set after the
initial remote config, if the external special remote program requests it.
Also note that IA remotes never use encryption, so encryptionSetup is not
run for them at all, and again the type safety is bypassed.
This leaves two open questions:
1. What to do about S3 and glacier remotes that were set up
using encryption=pubkey/hybrid with embedcreds?
Such a git repo has a security hole embedded in it, and this needs to be
communicated to the user. Is the changelog enough?
2. enableremote won't work in such a repo, because git-annex will
try to decrypt the embedded creds, which are not encrypted, so fails.
This needs to be dealt with, especially for ecryption=shared repos,
which are not really broken, just inconsistently configured.
Noticing that problem for encryption=shared is what led to commit
fbdeeeed5fa276d94be587c8916d725eddcaf546, which tried to
fix the problem by not decrypting the embedded creds.
This commit was sponsored by Josh Taylor.
2014-09-18 21:07:17 +00:00
|
|
|
(c', encsetup) <- encryptionSetup c
|
|
|
|
c'' <- setRemoteCredPair encsetup c' (AWS.creds u) mcreds
|
|
|
|
let fullconfig = c'' `M.union` defaults
|
2012-11-11 04:51:07 +00:00
|
|
|
genBucket fullconfig u
|
|
|
|
use fullconfig
|
2011-05-16 15:20:30 +00:00
|
|
|
|
2012-11-11 04:51:07 +00:00
|
|
|
archiveorg = do
|
|
|
|
showNote "Internet Archive mode"
|
2014-10-12 17:15:52 +00:00
|
|
|
c' <- setRemoteCredPair noEncryptionUsed c (AWS.creds u) mcreds
|
2013-10-16 20:35:47 +00:00
|
|
|
-- Ensure user enters a valid bucket name, since
|
|
|
|
-- this determines the name of the archive.org item.
|
2014-08-10 02:13:03 +00:00
|
|
|
let validbucket = replace " " "-" $ map toLower $
|
2013-10-16 20:35:47 +00:00
|
|
|
fromMaybe (error "specify bucket=") $
|
2014-10-22 21:14:38 +00:00
|
|
|
getBucketName c'
|
2013-10-16 20:35:47 +00:00
|
|
|
let archiveconfig =
|
2014-08-09 01:42:46 +00:00
|
|
|
-- IA acdepts x-amz-* as an alias for x-archive-*
|
2012-11-11 04:51:07 +00:00
|
|
|
M.mapKeys (replace "x-archive-" "x-amz-") $
|
|
|
|
-- encryption does not make sense here
|
|
|
|
M.insert "encryption" "none" $
|
2014-08-10 02:13:03 +00:00
|
|
|
M.insert "bucket" validbucket $
|
2014-10-12 17:15:52 +00:00
|
|
|
M.union c' $
|
2012-11-11 04:51:07 +00:00
|
|
|
-- special constraints on key names
|
2014-08-10 02:17:40 +00:00
|
|
|
M.insert "mungekeys" "ia" defaults
|
2014-08-10 02:13:03 +00:00
|
|
|
info <- extractS3Info archiveconfig
|
|
|
|
withS3Handle archiveconfig u info $
|
2014-08-08 22:54:04 +00:00
|
|
|
writeUUIDFile archiveconfig u
|
2013-10-16 20:35:47 +00:00
|
|
|
use archiveconfig
|
2011-03-29 20:21:21 +00:00
|
|
|
|
2014-08-09 18:23:54 +00:00
|
|
|
-- Sets up a http connection manager for S3 encdpoint, which allows
|
|
|
|
-- http connections to be reused across calls to the helper.
|
2014-08-10 02:13:03 +00:00
|
|
|
prepareS3 :: Remote -> S3Info -> (S3Handle -> helper) -> Preparer helper
|
|
|
|
prepareS3 r info = resourcePrepare $ const $
|
|
|
|
withS3Handle (config r) (uuid r) info
|
2013-04-25 21:28:25 +00:00
|
|
|
|
2014-08-09 18:23:54 +00:00
|
|
|
store :: Remote -> S3Handle -> Storer
|
|
|
|
store r h = fileStorer $ \k f p -> do
|
|
|
|
rbody <- liftIO $ httpBodyStorer f p
|
2014-08-10 02:13:03 +00:00
|
|
|
void $ sendS3Handle h $ putObject h (bucketObject (hinfo h) k) rbody
|
2014-08-09 18:23:54 +00:00
|
|
|
|
|
|
|
-- Store public URL to item in Internet Archive.
|
2014-08-10 02:13:03 +00:00
|
|
|
when (isIA (hinfo h) && not (isChunkKey k)) $
|
2014-08-09 18:23:54 +00:00
|
|
|
setUrlPresent k (iaKeyUrl r k)
|
|
|
|
|
|
|
|
return True
|
2014-08-02 19:51:58 +00:00
|
|
|
|
2014-08-09 19:58:01 +00:00
|
|
|
{- Implemented as a fileRetriever, that uses conduit to stream the chunks
|
|
|
|
- out to the file. Would be better to implement a byteRetriever, but
|
|
|
|
- that is difficult. -}
|
2014-08-09 18:30:28 +00:00
|
|
|
retrieve :: S3Handle -> Retriever
|
2014-08-09 19:58:01 +00:00
|
|
|
retrieve h = fileRetriever $ \f k p -> liftIO $ runResourceT $ do
|
|
|
|
(fr, fh) <- allocate (openFile f WriteMode) hClose
|
2014-08-10 02:13:03 +00:00
|
|
|
let req = S3.getObject (bucket info) (bucketObject info k)
|
2014-08-09 19:58:01 +00:00
|
|
|
S3.GetObjectResponse { S3.gorResponse = rsp } <- sendS3Handle' h req
|
|
|
|
responseBody rsp $$+- sinkprogressfile fh p zeroBytesProcessed
|
|
|
|
release fr
|
|
|
|
where
|
2014-08-10 02:13:03 +00:00
|
|
|
info = hinfo h
|
2014-08-09 19:58:01 +00:00
|
|
|
sinkprogressfile fh meterupdate sofar = do
|
|
|
|
mbs <- await
|
|
|
|
case mbs of
|
|
|
|
Nothing -> return ()
|
|
|
|
Just bs -> do
|
2014-08-09 20:49:31 +00:00
|
|
|
let sofar' = addBytesProcessed sofar (S.length bs)
|
2014-08-09 19:58:01 +00:00
|
|
|
liftIO $ do
|
|
|
|
void $ meterupdate sofar'
|
|
|
|
S.hPut fh bs
|
|
|
|
sinkprogressfile fh meterupdate sofar'
|
2011-04-19 18:45:19 +00:00
|
|
|
|
run Preparer to get Remover and CheckPresent actions
This will allow special remotes to eg, open a http connection and reuse it,
while checking if chunks are present, or removing chunks.
S3 and WebDAV both need this to support chunks with reasonable speed.
Note that a special remote might want to cache a http connection across
multiple requests. A simple case of this is that CheckPresent is typically
called before Store or Remove. A remote using this interface can certianly
use a Preparer that eg, uses a MVar to cache a http connection.
However, it's up to the remote to then deal with things like stale or
stalled http connections when eg, doing a series of downloads from a remote
and other places. There could be long delays between calls to a remote,
which could lead to eg, http connection stalls; the machine might even
move to a new network, etc.
It might be nice to improve this interface later to allow
the simple case without needing to handle the full complex case.
One way to do it would be to have a `Transaction SpecialRemote cache`,
where SpecialRemote contains methods for Storer, Retriever, Remover, and
CheckPresent, that all expect to be passed a `cache`.
2014-08-06 18:28:36 +00:00
|
|
|
retrieveCheap :: Key -> FilePath -> Annex Bool
|
|
|
|
retrieveCheap _ _ = return False
|
2012-01-20 17:23:11 +00:00
|
|
|
|
2013-04-25 21:28:25 +00:00
|
|
|
{- Internet Archive doesn't easily allow removing content.
|
|
|
|
- While it may remove the file, there are generally other files
|
|
|
|
- derived from it that it does not remove. -}
|
2014-08-09 18:30:28 +00:00
|
|
|
remove :: S3Handle -> Remover
|
|
|
|
remove h k
|
2014-08-10 02:13:03 +00:00
|
|
|
| isIA info = do
|
2013-04-25 19:20:31 +00:00
|
|
|
warning "Cannot remove content from the Internet Archive"
|
|
|
|
return False
|
2014-08-09 18:23:54 +00:00
|
|
|
| otherwise = do
|
|
|
|
res <- tryNonAsync $ sendS3Handle h $
|
2014-08-10 02:13:03 +00:00
|
|
|
S3.DeleteObject (bucketObject info k) (bucket info)
|
2014-08-09 18:23:54 +00:00
|
|
|
return $ either (const False) (const True) res
|
2014-08-10 02:13:03 +00:00
|
|
|
where
|
|
|
|
info = hinfo h
|
2013-04-25 19:20:31 +00:00
|
|
|
|
2014-08-09 18:23:54 +00:00
|
|
|
checkKey :: Remote -> S3Handle -> CheckPresent
|
|
|
|
checkKey r h k = do
|
2011-07-19 18:07:23 +00:00
|
|
|
showAction $ "checking " ++ name r
|
2014-08-09 18:23:54 +00:00
|
|
|
catchMissingException $ do
|
|
|
|
void $ sendS3Handle h $
|
2014-08-10 02:13:03 +00:00
|
|
|
S3.headObject (bucket (hinfo h)) (bucketObject (hinfo h) k)
|
2014-08-09 18:23:54 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
{- Catch exception headObject returns when an object is not present
|
|
|
|
- in the bucket, and returns False. All other exceptions indicate a
|
|
|
|
- check error and are let through. -}
|
|
|
|
catchMissingException :: Annex Bool -> Annex Bool
|
|
|
|
catchMissingException a = catchJust missing a (const $ return False)
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
2014-08-09 18:23:54 +00:00
|
|
|
-- This is not very good; see
|
|
|
|
-- https://github.com/aristidb/aws/issues/121
|
|
|
|
missing :: AWS.HeaderException -> Maybe ()
|
|
|
|
missing e
|
|
|
|
| AWS.headerErrorMessage e == "ETag missing" = Just ()
|
|
|
|
| otherwise = Nothing
|
|
|
|
|
2014-07-11 19:21:43 +00:00
|
|
|
{- Generate the bucket if it does not already exist, including creating the
|
|
|
|
- UUID file within the bucket.
|
|
|
|
-
|
2014-08-08 22:54:04 +00:00
|
|
|
- Some ACLs can allow read/write to buckets, but not querying them,
|
|
|
|
- so first check if the UUID file already exists and we can skip doing
|
|
|
|
- anything.
|
2014-07-11 19:21:43 +00:00
|
|
|
-}
|
2012-09-26 16:06:44 +00:00
|
|
|
genBucket :: RemoteConfig -> UUID -> Annex ()
|
|
|
|
genBucket c u = do
|
2011-07-19 18:07:23 +00:00
|
|
|
showAction "checking bucket"
|
2014-08-10 02:13:03 +00:00
|
|
|
info <- extractS3Info c
|
|
|
|
withS3Handle c u info $ \h ->
|
2014-08-08 22:54:04 +00:00
|
|
|
go h =<< checkUUIDFile c u h
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
2014-08-08 22:54:04 +00:00
|
|
|
go _ (Right True) = noop
|
|
|
|
go h _ = do
|
2014-08-10 02:13:03 +00:00
|
|
|
v <- tryS3 $ sendS3Handle h (S3.getBucket $ bucket $ hinfo h)
|
2014-08-08 22:54:04 +00:00
|
|
|
case v of
|
|
|
|
Right _ -> noop
|
|
|
|
Left _ -> do
|
|
|
|
showAction $ "creating bucket in " ++ datacenter
|
2014-08-09 00:29:56 +00:00
|
|
|
void $ sendS3Handle h $
|
2014-08-10 02:13:03 +00:00
|
|
|
S3.PutBucket (bucket $ hinfo h) Nothing $
|
2014-08-08 22:54:04 +00:00
|
|
|
AWS.mkLocationConstraint $
|
|
|
|
T.pack datacenter
|
|
|
|
writeUUIDFile c u h
|
|
|
|
|
2012-11-11 04:51:07 +00:00
|
|
|
datacenter = fromJust $ M.lookup "datacenter" c
|
2011-05-16 13:42:54 +00:00
|
|
|
|
2013-04-27 21:01:24 +00:00
|
|
|
{- Writes the UUID to an annex-uuid file within the bucket.
|
|
|
|
-
|
|
|
|
- If the file already exists in the bucket, it must match.
|
|
|
|
-
|
2014-08-08 22:54:04 +00:00
|
|
|
- Note that IA buckets can only created by having a file
|
|
|
|
- stored in them. So this also takes care of that.
|
2013-04-27 21:01:24 +00:00
|
|
|
-}
|
2014-08-08 22:54:04 +00:00
|
|
|
writeUUIDFile :: RemoteConfig -> UUID -> S3Handle -> Annex ()
|
|
|
|
writeUUIDFile c u h = do
|
|
|
|
v <- checkUUIDFile c u h
|
2014-07-11 19:21:43 +00:00
|
|
|
case v of
|
2014-08-08 22:54:04 +00:00
|
|
|
Left e -> throwM e
|
|
|
|
Right True -> noop
|
2014-08-09 00:29:56 +00:00
|
|
|
Right False -> void $ sendS3Handle h mkobject
|
2013-04-27 21:01:24 +00:00
|
|
|
where
|
2014-08-08 22:54:04 +00:00
|
|
|
file = T.pack $ uuidFile c
|
2014-07-11 19:21:43 +00:00
|
|
|
uuidb = L.fromChunks [T.encodeUtf8 $ T.pack $ fromUUID u]
|
|
|
|
|
2014-08-09 18:44:53 +00:00
|
|
|
mkobject = putObject h file (RequestBodyLBS uuidb)
|
2014-07-11 19:21:43 +00:00
|
|
|
|
2014-08-08 22:54:04 +00:00
|
|
|
{- Checks if the UUID file exists in the bucket
|
|
|
|
- and has the specified UUID already. -}
|
|
|
|
checkUUIDFile :: RemoteConfig -> UUID -> S3Handle -> Annex (Either SomeException Bool)
|
|
|
|
checkUUIDFile c u h = tryNonAsync $ check <$> get
|
2014-07-11 19:21:43 +00:00
|
|
|
where
|
2014-08-08 22:54:04 +00:00
|
|
|
get = liftIO
|
|
|
|
. runResourceT
|
|
|
|
. either (pure . Left) (Right <$$> AWS.loadToMemory)
|
2014-08-10 02:13:03 +00:00
|
|
|
=<< tryS3 (sendS3Handle h (S3.getObject (bucket (hinfo h)) file))
|
2014-08-08 22:54:04 +00:00
|
|
|
check (Right (S3.GetObjectMemoryResponse _meta rsp)) =
|
|
|
|
responseStatus rsp == ok200 && responseBody rsp == uuidb
|
|
|
|
check (Left _S3Error) = False
|
|
|
|
|
|
|
|
file = T.pack $ uuidFile c
|
2014-06-06 18:04:35 +00:00
|
|
|
uuidb = L.fromChunks [T.encodeUtf8 $ T.pack $ fromUUID u]
|
2013-04-27 21:01:24 +00:00
|
|
|
|
2014-07-11 19:21:43 +00:00
|
|
|
uuidFile :: RemoteConfig -> FilePath
|
2014-08-09 00:29:56 +00:00
|
|
|
uuidFile c = getFilePrefix c ++ "annex-uuid"
|
2014-07-11 19:21:43 +00:00
|
|
|
|
2014-08-09 18:44:53 +00:00
|
|
|
putObject :: S3Handle -> T.Text -> RequestBody -> S3.PutObject
|
2014-08-10 02:17:40 +00:00
|
|
|
putObject h file rbody = (S3.putObject (bucket info) file rbody)
|
|
|
|
{ S3.poStorageClass = Just (storageClass info)
|
|
|
|
, S3.poMetadata = metaHeaders info
|
|
|
|
, S3.poAutoMakeBucket = isIA info
|
2014-08-09 18:44:53 +00:00
|
|
|
}
|
2014-08-10 02:17:40 +00:00
|
|
|
where
|
|
|
|
info = hinfo h
|
2014-08-09 18:44:53 +00:00
|
|
|
|
2014-08-09 00:51:22 +00:00
|
|
|
data S3Handle = S3Handle
|
|
|
|
{ hmanager :: Manager
|
|
|
|
, hawscfg :: AWS.Configuration
|
|
|
|
, hs3cfg :: S3.S3Configuration AWS.NormalQuery
|
2014-08-10 02:13:03 +00:00
|
|
|
, hinfo :: S3Info
|
2014-08-09 00:51:22 +00:00
|
|
|
}
|
2014-08-08 22:54:04 +00:00
|
|
|
|
|
|
|
{- Sends a request to S3 and gets back the response.
|
|
|
|
-
|
|
|
|
- Note that pureAws's use of ResourceT is bypassed here;
|
2014-08-09 00:29:56 +00:00
|
|
|
- the response should be fully processed while the S3Handle
|
|
|
|
- is still open, eg within a call to withS3Handle.
|
2014-08-08 22:54:04 +00:00
|
|
|
-}
|
|
|
|
sendS3Handle
|
|
|
|
:: (AWS.Transaction req res, AWS.ServiceConfiguration req ~ S3.S3Configuration)
|
|
|
|
=> S3Handle
|
|
|
|
-> req
|
2014-08-09 00:29:56 +00:00
|
|
|
-> Annex res
|
2014-08-09 19:58:01 +00:00
|
|
|
sendS3Handle h r = liftIO $ runResourceT $ sendS3Handle' h r
|
|
|
|
|
|
|
|
sendS3Handle'
|
|
|
|
:: (AWS.Transaction r a, AWS.ServiceConfiguration r ~ S3.S3Configuration)
|
|
|
|
=> S3Handle
|
|
|
|
-> r
|
|
|
|
-> ResourceT IO a
|
|
|
|
sendS3Handle' h = AWS.pureAws (hawscfg h) (hs3cfg h) (hmanager h)
|
2014-08-08 22:54:04 +00:00
|
|
|
|
2014-08-10 02:13:03 +00:00
|
|
|
withS3Handle :: RemoteConfig -> UUID -> S3Info -> (S3Handle -> Annex a) -> Annex a
|
|
|
|
withS3Handle c u info a = do
|
2014-08-08 22:54:04 +00:00
|
|
|
creds <- getRemoteCredPairFor "S3" c (AWS.creds u)
|
|
|
|
awscreds <- liftIO $ AWS.genCredentials $ fromMaybe nocreds creds
|
|
|
|
let awscfg = AWS.Configuration AWS.Timestamp awscreds (AWS.defaultLog AWS.Error)
|
|
|
|
bracketIO (newManager httpcfg) closeManager $ \mgr ->
|
2014-08-10 02:13:03 +00:00
|
|
|
a $ S3Handle mgr awscfg s3cfg info
|
2014-08-08 22:54:04 +00:00
|
|
|
where
|
|
|
|
s3cfg = s3Configuration c
|
|
|
|
httpcfg = defaultManagerSettings
|
|
|
|
{ managerResponseTimeout = Nothing }
|
|
|
|
nocreds = error "Cannot use S3 without credentials configured"
|
|
|
|
|
|
|
|
s3Configuration :: RemoteConfig -> S3.S3Configuration AWS.NormalQuery
|
|
|
|
s3Configuration c = (S3.s3 proto endpoint False) { S3.s3Port = port }
|
|
|
|
where
|
|
|
|
proto
|
|
|
|
| port == 443 = AWS.HTTPS
|
|
|
|
| otherwise = AWS.HTTP
|
|
|
|
host = fromJust $ M.lookup "host" c
|
|
|
|
datacenter = fromJust $ M.lookup "datacenter" c
|
|
|
|
-- When the default S3 host is configured, connect directly to
|
|
|
|
-- the S3 endpoint for the configured datacenter.
|
|
|
|
-- When another host is configured, it's used as-is.
|
|
|
|
endpoint
|
|
|
|
| host == AWS.s3DefaultHost = AWS.s3HostName $ T.pack datacenter
|
|
|
|
| otherwise = T.encodeUtf8 $ T.pack host
|
|
|
|
port = let s = fromJust $ M.lookup "port" c in
|
|
|
|
case reads s of
|
|
|
|
[(p, _)] -> p
|
|
|
|
_ -> error $ "bad S3 port value: " ++ s
|
|
|
|
|
2014-08-09 00:29:56 +00:00
|
|
|
tryS3 :: Annex a -> Annex (Either S3.S3Error a)
|
|
|
|
tryS3 a = (Right <$> a) `catch` (pure . Left)
|
|
|
|
|
2014-08-10 02:13:03 +00:00
|
|
|
data S3Info = S3Info
|
|
|
|
{ bucket :: S3.Bucket
|
|
|
|
, storageClass :: S3.StorageClass
|
|
|
|
, bucketObject :: Key -> T.Text
|
|
|
|
, metaHeaders :: [(T.Text, T.Text)]
|
|
|
|
, isIA :: Bool
|
|
|
|
}
|
|
|
|
|
|
|
|
extractS3Info :: RemoteConfig -> Annex S3Info
|
|
|
|
extractS3Info c = do
|
|
|
|
b <- maybe
|
|
|
|
(error "S3 bucket not configured")
|
|
|
|
(return . T.pack)
|
|
|
|
(getBucketName c)
|
|
|
|
return $ S3Info
|
|
|
|
{ bucket = b
|
|
|
|
, storageClass = getStorageClass c
|
|
|
|
, bucketObject = T.pack . getBucketObject c
|
|
|
|
, metaHeaders = getMetaHeaders c
|
|
|
|
, isIA = configIA c
|
|
|
|
}
|
|
|
|
|
2014-08-09 00:29:56 +00:00
|
|
|
getBucketName :: RemoteConfig -> Maybe BucketName
|
|
|
|
getBucketName = M.lookup "bucket"
|
2013-04-27 21:01:24 +00:00
|
|
|
|
2014-08-08 22:54:04 +00:00
|
|
|
getStorageClass :: RemoteConfig -> S3.StorageClass
|
2014-08-09 00:51:22 +00:00
|
|
|
getStorageClass c = case M.lookup "storageclass" c of
|
|
|
|
Just "REDUCED_REDUNDANCY" -> S3.ReducedRedundancy
|
2014-08-08 22:54:04 +00:00
|
|
|
_ -> S3.Standard
|
2013-04-27 21:01:24 +00:00
|
|
|
|
2014-08-09 18:44:53 +00:00
|
|
|
getMetaHeaders :: RemoteConfig -> [(T.Text, T.Text)]
|
|
|
|
getMetaHeaders = map munge . filter ismetaheader . M.assocs
|
2013-04-27 21:01:24 +00:00
|
|
|
where
|
2014-08-09 18:44:53 +00:00
|
|
|
ismetaheader (h, _) = metaprefix `isPrefixOf` h
|
|
|
|
metaprefix = "x-amz-meta-"
|
|
|
|
metaprefixlen = length metaprefix
|
|
|
|
munge (k, v) = (T.pack $ drop metaprefixlen k, T.pack v)
|
2013-04-27 21:01:24 +00:00
|
|
|
|
2014-08-09 00:29:56 +00:00
|
|
|
getFilePrefix :: RemoteConfig -> String
|
|
|
|
getFilePrefix = M.findWithDefault "" "fileprefix"
|
|
|
|
|
2014-08-10 02:13:03 +00:00
|
|
|
getBucketObject :: RemoteConfig -> Key -> FilePath
|
|
|
|
getBucketObject c = munge . key2file
|
2014-08-09 18:23:54 +00:00
|
|
|
where
|
|
|
|
munge s = case M.lookup "mungekeys" c of
|
|
|
|
Just "ia" -> iaMunge $ getFilePrefix c ++ s
|
|
|
|
_ -> getFilePrefix c ++ s
|
|
|
|
|
2014-08-09 00:29:56 +00:00
|
|
|
{- Internet Archive limits filenames to a subset of ascii,
|
|
|
|
- with no whitespace. Other characters are xml entity
|
|
|
|
- encoded. -}
|
|
|
|
iaMunge :: String -> String
|
|
|
|
iaMunge = (>>= munge)
|
|
|
|
where
|
|
|
|
munge c
|
|
|
|
| isAsciiUpper c || isAsciiLower c || isNumber c = [c]
|
|
|
|
| c `elem` "_-.\"" = [c]
|
|
|
|
| isSpace c = []
|
|
|
|
| otherwise = "&" ++ show (ord c) ++ ";"
|
|
|
|
|
2014-08-10 02:13:03 +00:00
|
|
|
configIA :: RemoteConfig -> Bool
|
|
|
|
configIA = maybe False isIAHost . M.lookup "host"
|
|
|
|
|
2013-04-25 17:14:49 +00:00
|
|
|
{- Hostname to use for archive.org S3. -}
|
|
|
|
iaHost :: HostName
|
|
|
|
iaHost = "s3.us.archive.org"
|
|
|
|
|
|
|
|
isIAHost :: HostName -> Bool
|
|
|
|
isIAHost h = ".archive.org" `isSuffixOf` map toLower h
|
2013-04-25 20:42:17 +00:00
|
|
|
|
2014-08-09 00:29:56 +00:00
|
|
|
iaItemUrl :: BucketName -> URLString
|
2014-08-10 02:13:03 +00:00
|
|
|
iaItemUrl b = "http://archive.org/details/" ++ b
|
2013-04-25 21:28:25 +00:00
|
|
|
|
|
|
|
iaKeyUrl :: Remote -> Key -> URLString
|
2014-08-10 02:13:03 +00:00
|
|
|
iaKeyUrl r k = "http://archive.org/download/" ++ b ++ "/" ++ getBucketObject (config r) k
|
2013-04-25 21:28:25 +00:00
|
|
|
where
|
2014-08-10 02:13:03 +00:00
|
|
|
b = fromMaybe "" $ getBucketName $ config r
|