module Amazonka.S3.Encryption
(
Key (..),
kmsKey,
asymmetricKey,
symmetricKey,
newSecret,
encrypt,
decrypt,
initiate,
encryptInstructions,
decryptInstructions,
initiateInstructions,
cleanupInstructions,
Ext (..),
defaultExtension,
EncryptionError (..),
AsEncryptionError (..),
)
where
import Amazonka as AWS
import Amazonka.Prelude
import Amazonka.S3
import Amazonka.S3.Encryption.Decrypt
import Amazonka.S3.Encryption.Encrypt
import Amazonka.S3.Encryption.Envelope
import Amazonka.S3.Encryption.Instructions
import Amazonka.S3.Encryption.Types
import Control.Lens
import Crypto.PubKey.RSA.Types as RSA
import Crypto.Random
kmsKey :: Text -> Key
kmsKey :: Text -> Key
kmsKey Text
k = Text -> Description -> Key
KMS Text
k Description
forall a. Monoid a => a
mempty
asymmetricKey :: PrivateKey -> Key
asymmetricKey :: PrivateKey -> Key
asymmetricKey PrivateKey
k = KeyPair -> Description -> Key
Asymmetric (PrivateKey -> KeyPair
KeyPair PrivateKey
k) Description
forall a. Monoid a => a
mempty
symmetricKey :: MonadIO m => ByteString -> m Key
symmetricKey :: ByteString -> m Key
symmetricKey = (AES256 -> Key) -> m AES256 -> m Key
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (AES256 -> Description -> Key
`Symmetric` Description
forall a. Monoid a => a
mempty) (m AES256 -> m Key)
-> (ByteString -> m AES256) -> ByteString -> m Key
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> m AES256
forall (m :: * -> *) a b.
(MonadIO m, ByteArray a, Cipher b) =>
a -> m b
createCipher
newSecret :: MonadRandom m => m ByteString
newSecret :: m ByteString
newSecret = Int -> m ByteString
forall (m :: * -> *) byteArray.
(MonadRandom m, ByteArray byteArray) =>
Int -> m byteArray
getRandomBytes Int
aesKeySize
encrypt ::
MonadResource m =>
Key ->
Env ->
PutObject ->
m PutObjectResponse
encrypt :: Key -> Env -> PutObject -> m PutObjectResponse
encrypt Key
key Env
env PutObject
x = do
(Encrypted PutObject
a, PutInstructions
_) <- Key -> Env -> PutObject -> m (Encrypted PutObject, PutInstructions)
forall (m :: * -> *) a.
(MonadResource m, ToEncrypted a) =>
Key -> Env -> a -> m (Encrypted a, PutInstructions)
encrypted Key
key Env
env PutObject
x
Env -> Encrypted PutObject -> m (AWSResponse (Encrypted PutObject))
forall (m :: * -> *) a.
(MonadResource m, AWSRequest a) =>
Env -> a -> m (AWSResponse a)
send Env
env (ASetter
(Encrypted PutObject) (Encrypted PutObject) Location Location
-> Location -> Encrypted PutObject -> Encrypted PutObject
forall s t a b. ASetter s t a b -> b -> s -> t
set ASetter
(Encrypted PutObject) (Encrypted PutObject) Location Location
forall a. Setter' (Encrypted a) Location
location Location
Metadata Encrypted PutObject
a)
encryptInstructions ::
MonadResource m =>
Key ->
Env ->
PutObject ->
m PutObjectResponse
encryptInstructions :: Key -> Env -> PutObject -> m PutObjectResponse
encryptInstructions Key
key Env
env PutObject
x = do
(Encrypted PutObject
a, PutInstructions
b) <- Key -> Env -> PutObject -> m (Encrypted PutObject, PutInstructions)
forall (m :: * -> *) a.
(MonadResource m, ToEncrypted a) =>
Key -> Env -> a -> m (Encrypted a, PutInstructions)
encrypted Key
key Env
env PutObject
x
PutObjectResponse
_ <- Env -> PutInstructions -> m (AWSResponse PutInstructions)
forall (m :: * -> *) a.
(MonadResource m, AWSRequest a) =>
Env -> a -> m (AWSResponse a)
send Env
env PutInstructions
b
Env -> Encrypted PutObject -> m (AWSResponse (Encrypted PutObject))
forall (m :: * -> *) a.
(MonadResource m, AWSRequest a) =>
Env -> a -> m (AWSResponse a)
send Env
env Encrypted PutObject
a
initiate ::
MonadResource m =>
Key ->
Env ->
CreateMultipartUpload ->
m
( CreateMultipartUploadResponse,
UploadPart -> Encrypted UploadPart
)
initiate :: Key
-> Env
-> CreateMultipartUpload
-> m (CreateMultipartUploadResponse,
UploadPart -> Encrypted UploadPart)
initiate Key
key Env
env CreateMultipartUpload
x = do
(Encrypted CreateMultipartUpload
a, PutInstructions
_) <- Key
-> Env
-> CreateMultipartUpload
-> m (Encrypted CreateMultipartUpload, PutInstructions)
forall (m :: * -> *) a.
(MonadResource m, ToEncrypted a) =>
Key -> Env -> a -> m (Encrypted a, PutInstructions)
encrypted Key
key Env
env CreateMultipartUpload
x
CreateMultipartUploadResponse
rs <- Env
-> Encrypted CreateMultipartUpload
-> m (AWSResponse (Encrypted CreateMultipartUpload))
forall (m :: * -> *) a.
(MonadResource m, AWSRequest a) =>
Env -> a -> m (AWSResponse a)
send Env
env (ASetter
(Encrypted CreateMultipartUpload)
(Encrypted CreateMultipartUpload)
Location
Location
-> Location
-> Encrypted CreateMultipartUpload
-> Encrypted CreateMultipartUpload
forall s t a b. ASetter s t a b -> b -> s -> t
set ASetter
(Encrypted CreateMultipartUpload)
(Encrypted CreateMultipartUpload)
Location
Location
forall a. Setter' (Encrypted a) Location
location Location
Metadata Encrypted CreateMultipartUpload
a)
(CreateMultipartUploadResponse, UploadPart -> Encrypted UploadPart)
-> m (CreateMultipartUploadResponse,
UploadPart -> Encrypted UploadPart)
forall (m :: * -> *) a. Monad m => a -> m a
return (CreateMultipartUploadResponse
rs, Encrypted CreateMultipartUpload
-> UploadPart -> Encrypted UploadPart
encryptPart Encrypted CreateMultipartUpload
a)
initiateInstructions ::
MonadResource m =>
Key ->
Env ->
CreateMultipartUpload ->
m
( CreateMultipartUploadResponse,
UploadPart -> Encrypted UploadPart
)
initiateInstructions :: Key
-> Env
-> CreateMultipartUpload
-> m (CreateMultipartUploadResponse,
UploadPart -> Encrypted UploadPart)
initiateInstructions Key
key Env
env CreateMultipartUpload
x = do
(Encrypted CreateMultipartUpload
a, PutInstructions
b) <- Key
-> Env
-> CreateMultipartUpload
-> m (Encrypted CreateMultipartUpload, PutInstructions)
forall (m :: * -> *) a.
(MonadResource m, ToEncrypted a) =>
Key -> Env -> a -> m (Encrypted a, PutInstructions)
encrypted Key
key Env
env CreateMultipartUpload
x
CreateMultipartUploadResponse
rs <- Env
-> Encrypted CreateMultipartUpload
-> m (AWSResponse (Encrypted CreateMultipartUpload))
forall (m :: * -> *) a.
(MonadResource m, AWSRequest a) =>
Env -> a -> m (AWSResponse a)
send Env
env Encrypted CreateMultipartUpload
a
PutObjectResponse
_ <- Env -> PutInstructions -> m (AWSResponse PutInstructions)
forall (m :: * -> *) a.
(MonadResource m, AWSRequest a) =>
Env -> a -> m (AWSResponse a)
send Env
env PutInstructions
b
(CreateMultipartUploadResponse, UploadPart -> Encrypted UploadPart)
-> m (CreateMultipartUploadResponse,
UploadPart -> Encrypted UploadPart)
forall (m :: * -> *) a. Monad m => a -> m a
return (CreateMultipartUploadResponse
rs, Encrypted CreateMultipartUpload
-> UploadPart -> Encrypted UploadPart
encryptPart Encrypted CreateMultipartUpload
a)
decrypt ::
MonadResource m =>
Key ->
Env ->
GetObject ->
m GetObjectResponse
decrypt :: Key -> Env -> GetObject -> m GetObjectResponse
decrypt Key
key Env
env GetObject
x = do
let (Decrypt GetObject
a, GetInstructions
_) = GetObject -> (Decrypt GetObject, GetInstructions)
decrypted GetObject
x
Decrypted forall (m :: * -> *).
MonadResource m =>
Key -> Env -> Maybe Envelope -> m GetObjectResponse
f <- Env -> Decrypt GetObject -> m (AWSResponse (Decrypt GetObject))
forall (m :: * -> *) a.
(MonadResource m, AWSRequest a) =>
Env -> a -> m (AWSResponse a)
send Env
env Decrypt GetObject
a
Key -> Env -> Maybe Envelope -> m GetObjectResponse
forall (m :: * -> *).
MonadResource m =>
Key -> Env -> Maybe Envelope -> m GetObjectResponse
f Key
key Env
env Maybe Envelope
forall a. Maybe a
Nothing
decryptInstructions ::
MonadResource m =>
Key ->
Env ->
GetObject ->
m GetObjectResponse
decryptInstructions :: Key -> Env -> GetObject -> m GetObjectResponse
decryptInstructions Key
key Env
env GetObject
x = do
let (Decrypt GetObject
a, GetInstructions
b) = GetObject -> (Decrypt GetObject, GetInstructions)
decrypted GetObject
x
Instructions forall (m :: * -> *). MonadResource m => Key -> Env -> m Envelope
g <- Env -> GetInstructions -> m (AWSResponse GetInstructions)
forall (m :: * -> *) a.
(MonadResource m, AWSRequest a) =>
Env -> a -> m (AWSResponse a)
send Env
env GetInstructions
b
Decrypted forall (m :: * -> *).
MonadResource m =>
Key -> Env -> Maybe Envelope -> m GetObjectResponse
f <- Env -> Decrypt GetObject -> m (AWSResponse (Decrypt GetObject))
forall (m :: * -> *) a.
(MonadResource m, AWSRequest a) =>
Env -> a -> m (AWSResponse a)
send Env
env Decrypt GetObject
a
Key -> Env -> m Envelope
forall (m :: * -> *). MonadResource m => Key -> Env -> m Envelope
g Key
key Env
env m Envelope
-> (Envelope -> m GetObjectResponse) -> m GetObjectResponse
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Key -> Env -> Maybe Envelope -> m GetObjectResponse
forall (m :: * -> *).
MonadResource m =>
Key -> Env -> Maybe Envelope -> m GetObjectResponse
f Key
key Env
env (Maybe Envelope -> m GetObjectResponse)
-> (Envelope -> Maybe Envelope) -> Envelope -> m GetObjectResponse
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Envelope -> Maybe Envelope
forall a. a -> Maybe a
Just
cleanupInstructions ::
( MonadResource m,
RemoveInstructions a
) =>
Env ->
a ->
m (AWSResponse a)
cleanupInstructions :: Env -> a -> m (AWSResponse a)
cleanupInstructions Env
env a
x = do
AWSResponse a
rs <- Env -> a -> m (AWSResponse a)
forall (m :: * -> *) a.
(MonadResource m, AWSRequest a) =>
Env -> a -> m (AWSResponse a)
send Env
env a
x
DeleteObjectResponse
_ <- Env -> DeleteInstructions -> m (AWSResponse DeleteInstructions)
forall (m :: * -> *) a.
(MonadResource m, AWSRequest a) =>
Env -> a -> m (AWSResponse a)
send Env
env (a -> DeleteInstructions
forall a. RemoveInstructions a => a -> DeleteInstructions
deleteInstructions a
x)
AWSResponse a -> m (AWSResponse a)
forall (m :: * -> *) a. Monad m => a -> m a
return AWSResponse a
rs