From 45d5f9e6768aba21b71d07e4d433700525421177 Mon Sep 17 00:00:00 2001 From: Krishnan Parthasarathi Date: Mon, 9 Jan 2017 14:12:10 +0530 Subject: [PATCH] Move region to RequestInfo Region is a request specific attribute, mostly applies to bucket operations. --- src/Network/Minio/API.hs | 2 +- src/Network/Minio/Data.hs | 8 ++++++-- src/Network/Minio/S3API.hs | 1 - src/Network/Minio/Sign/V4.hs | 10 +++++----- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Network/Minio/API.hs b/src/Network/Minio/API.hs index 76fd898..64d980f 100644 --- a/src/Network/Minio/API.hs +++ b/src/Network/Minio/API.hs @@ -94,4 +94,4 @@ mkStreamRequest ri = do requestInfo :: Method -> Maybe Bucket -> Maybe Object -> Query -> [Header] -> Payload -> RequestInfo -requestInfo m b o q h p = RequestInfo m b o q h p "" +requestInfo m b o q h p = RequestInfo m b o q h p "" Nothing diff --git a/src/Network/Minio/Data.hs b/src/Network/Minio/Data.hs index 5a16f40..f6f04c9 100644 --- a/src/Network/Minio/Data.hs +++ b/src/Network/Minio/Data.hs @@ -9,6 +9,7 @@ module Network.Minio.Data , Location , BucketInfo(..) , getPathFromRI + , getRegionFromRI , Minio , MinioErr(..) , runMinio @@ -38,12 +39,11 @@ data ConnectInfo = ConnectInfo { , connectAccessKey :: Text , connectSecretKey :: Text , connectIsSecure :: Bool - , connectRegion :: Text } deriving (Eq, Show) defaultConnectInfo :: ConnectInfo defaultConnectInfo = - ConnectInfo "localhost" 9000 "minio" "minio123" False "us-east-1" + ConnectInfo "localhost" 9000 "minio" "minio123" False type Bucket = Text type Object = Text @@ -68,6 +68,7 @@ data RequestInfo = RequestInfo { , headers :: [Header] , payload :: Payload , payloadHash :: ByteString + , region :: Maybe Location } @@ -77,6 +78,9 @@ getPathFromRI ri = B.concat $ parts objPart = maybe [] (\o -> ["/", encodeUtf8 o]) $ object ri parts = maybe ["/"] (\b -> "/" : encodeUtf8 b : objPart) $ bucket ri +getRegionFromRI :: RequestInfo -> Text +getRegionFromRI ri = maybe "us-east-1" identity (region ri) + data MinioErr = MErrMsg ByteString | MErrHttp HttpException | MErrXml ByteString diff --git a/src/Network/Minio/S3API.hs b/src/Network/Minio/S3API.hs index 48379fb..cdde5d0 100644 --- a/src/Network/Minio/S3API.hs +++ b/src/Network/Minio/S3API.hs @@ -49,4 +49,3 @@ putBucket bucket location = do let httpStatus = NC.responseStatus resp when (httpStatus /= HT.ok200) $ throwError $ MErrXml $ LBS.toStrict $ NC.responseBody resp - return () diff --git a/src/Network/Minio/Sign/V4.hs b/src/Network/Minio/Sign/V4.hs index d3d7a7c..f101a55 100644 --- a/src/Network/Minio/Sign/V4.hs +++ b/src/Network/Minio/Sign/V4.hs @@ -88,7 +88,7 @@ signV4AtTime ts ci ri = authHeader = (mk "Authorization", authHeaderValue) - scope = getScope ts ci + scope = getScope ts ri authHeaderValue = B.concat [ "AWS4-HMAC-SHA256 Credential=", @@ -105,7 +105,7 @@ signV4AtTime ts ci ri = signingKey = hmacSHA256RawBS "aws4_request" . hmacSHA256RawBS "s3" - . hmacSHA256RawBS (encodeUtf8 $ connectRegion ci) + . hmacSHA256RawBS (encodeUtf8 $ getRegionFromRI ri) . hmacSHA256RawBS (awsDateFormatBS ts) $ (B.concat ["AWS4", encodeUtf8 $ connectSecretKey ci]) @@ -119,10 +119,10 @@ signV4AtTime ts ci ri = canonicalRequest = getCanonicalRequest ri headersToSign -getScope :: UTCTime -> ConnectInfo -> ByteString -getScope ts ci = B.intercalate "/" $ [ +getScope :: UTCTime -> RequestInfo -> ByteString +getScope ts ri = B.intercalate "/" $ [ pack $ Time.formatTime Time.defaultTimeLocale "%Y%m%d" ts, - encodeUtf8 $ connectRegion ci, "s3", "aws4_request" + encodeUtf8 $ getRegionFromRI ri, "s3", "aws4_request" ] getHeadersToSign :: [Header] -> [(ByteString, ByteString)]