module Ecluse.Runtime.Mirror.State.S3 (
newS3MirrorState,
mirrorStateForEnv,
) where
import Amazonka qualified as AWS
import Data.Text qualified as T
import Ecluse.Core.Deadline (Deadlines)
import Ecluse.Core.Mirror.State
import Ecluse.Core.Mirror.State.Codec (chargedDecodeProjectRecord, chargedEncodeProjectRecord)
import Ecluse.Core.Mirror.State.Format (MirrorStateFormat)
import Ecluse.Core.Mirror.State.Internal (StateRevision (..), VerifiedCompanionBytes (..), projectKey)
import Ecluse.Core.Package (PackageName)
import Ecluse.Core.Registry.Exchange (newSingleAttemptManager)
import Ecluse.Core.Security (BodyLimit (MetadataBodyLimit), LimitError (BodyTooLarge), Limits (..))
import Ecluse.Runtime.Aws.Env (AwsEndpoint)
import Ecluse.Runtime.Aws.S3 (buildS3Env)
import Ecluse.Runtime.Mirror.State.S3.Internal
newS3MirrorState :: Maybe AwsEndpoint -> Deadlines -> Limits -> MirrorStateFormat -> Text -> Text -> StateTarget -> IO MirrorState
newS3MirrorState :: Maybe AwsEndpoint
-> Deadlines
-> Limits
-> MirrorStateFormat
-> Text
-> Text
-> StateTarget
-> IO MirrorState
newS3MirrorState Maybe AwsEndpoint
endpoint Deadlines
deadlines Limits
limits MirrorStateFormat
format Text
bucket Text
prefix StateTarget
target = Maybe AwsEndpoint -> IO Env
buildS3Env Maybe AwsEndpoint
endpoint IO Env -> (Env -> IO MirrorState) -> IO MirrorState
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Deadlines
-> Limits
-> MirrorStateFormat
-> Text
-> Text
-> StateTarget
-> Env
-> IO MirrorState
mirrorStateForEnv Deadlines
deadlines Limits
limits MirrorStateFormat
format Text
bucket Text
prefix StateTarget
target
mirrorStateForEnv :: Deadlines -> Limits -> MirrorStateFormat -> Text -> Text -> StateTarget -> AWS.Env -> IO MirrorState
mirrorStateForEnv :: Deadlines
-> Limits
-> MirrorStateFormat
-> Text
-> Text
-> StateTarget
-> Env
-> IO MirrorState
mirrorStateForEnv Deadlines
deadlines Limits
limits MirrorStateFormat
format Text
bucket Text
prefix StateTarget
target Env
env =
IO Manager
newSingleAttemptManager IO Manager -> (Manager -> MirrorState) -> IO MirrorState
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \Manager
writes -> Deadlines
-> Limits
-> MirrorStateFormat
-> Text
-> Text
-> StateTarget
-> Env
-> Env
-> MirrorState
stateOver Deadlines
deadlines Limits
limits MirrorStateFormat
format Text
bucket Text
prefix StateTarget
target Env
env Env
env{AWS.manager = writes}
stateOver :: Deadlines -> Limits -> MirrorStateFormat -> Text -> Text -> StateTarget -> AWS.Env -> AWS.Env -> MirrorState
stateOver :: Deadlines
-> Limits
-> MirrorStateFormat
-> Text
-> Text
-> StateTarget
-> Env
-> Env
-> MirrorState
stateOver Deadlines
deadlines Limits
limits MirrorStateFormat
format Text
bucket Text
prefix StateTarget
target Env
readEnv Env
writeEnv =
MirrorState
{ msReadProject :: StateCharge
-> PackageName
-> Maybe StateRevision
-> IO (Either MirrorStateFault StateRead)
msReadProject = StateCharge
-> PackageName
-> Maybe StateRevision
-> IO (Either MirrorStateFault StateRead)
readProject
, msWriteProject :: StateCharge
-> PackageName
-> Maybe StateRevision
-> ProjectMirrorRecord
-> IO (Either MirrorStateFault StateWrite)
msWriteProject = StateCharge
-> PackageName
-> Maybe StateRevision
-> ProjectMirrorRecord
-> IO (Either MirrorStateFault StateWrite)
writeProject
, msRetainCompanion :: StateCharge
-> VerifiedCompanionBytes
-> IO (Either MirrorStateFault CompanionRef)
msRetainCompanion = StateCharge
-> VerifiedCompanionBytes
-> IO (Either MirrorStateFault CompanionRef)
retain
, msReadCompanion :: StateCharge
-> CompanionRef -> IO (Either MirrorStateFault ByteString)
msReadCompanion = StateCharge
-> CompanionRef -> IO (Either MirrorStateFault ByteString)
readCompanion
}
where
key :: PackageName -> Text
key PackageName
name = Text -> Text
underPrefix (StateTarget -> PackageName -> Text
projectKey StateTarget
target PackageName
name Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/project.json")
underPrefix :: Text -> Text
underPrefix Text
suffix = if Text -> Bool
T.null Text
prefix then Text
suffix else (Char -> Bool) -> Text -> Text
T.dropWhileEnd (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'/') Text
prefix Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
suffix
referenceKey :: CompanionRef -> Text
referenceKey CompanionRef
reference = Text -> Text
underPrefix (Text
"companions/sha256/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> CompanionRef -> Text
companionDigest CompanionRef
reference)
readProject :: StateCharge
-> PackageName
-> Maybe StateRevision
-> IO (Either MirrorStateFault StateRead)
readProject StateCharge
charge PackageName
name Maybe StateRevision
revision = case StateTarget
-> PackageName
-> Maybe StateRevision
-> Either MirrorStateFault (Maybe ByteString)
revisionBytes StateTarget
target PackageName
name Maybe StateRevision
revision of
Left MirrorStateFault
fault -> Either MirrorStateFault StateRead
-> IO (Either MirrorStateFault StateRead)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MirrorStateFault -> Either MirrorStateFault StateRead
forall a b. a -> Either a b
Left MirrorStateFault
fault)
Right Maybe ByteString
expected ->
StateCharge
-> Deadlines
-> Env
-> Limits
-> Text
-> Text
-> Maybe ByteString
-> IO (Either ObjectFault ObjectRead)
readObject StateCharge
charge Deadlines
deadlines Env
readEnv Limits
limits Text
bucket (PackageName -> Text
key PackageName
name) Maybe ByteString
expected IO (Either ObjectFault ObjectRead)
-> (Either ObjectFault ObjectRead
-> IO (Either MirrorStateFault StateRead))
-> IO (Either MirrorStateFault StateRead)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Left ObjectFault
fault -> Either MirrorStateFault StateRead
-> IO (Either MirrorStateFault StateRead)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MirrorStateFault -> Either MirrorStateFault StateRead
forall a b. a -> Either a b
Left (ObjectFault -> MirrorStateFault
objectFault ObjectFault
fault))
Right ObjectRead
ObjectAbsent -> Either MirrorStateFault StateRead
-> IO (Either MirrorStateFault StateRead)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StateRead -> Either MirrorStateFault StateRead
forall a b. b -> Either a b
Right StateRead
StateAbsent)
Right (ObjectUnchanged ByteString
etag) -> Either MirrorStateFault StateRead
-> IO (Either MirrorStateFault StateRead)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StateRead -> Either MirrorStateFault StateRead
forall a b. b -> Either a b
Right (StateRevision -> StateRead
StateUnchanged (StateTarget -> PackageName -> ByteString -> StateRevision
StateRevision StateTarget
target PackageName
name ByteString
etag)))
Right (ObjectLoaded ByteString
etag ByteString
bytes) -> (ProjectMirrorRecord -> StateRead)
-> Either MirrorStateFault ProjectMirrorRecord
-> Either MirrorStateFault StateRead
forall a b.
(a -> b) -> Either MirrorStateFault a -> Either MirrorStateFault b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (StateRevision -> ProjectMirrorRecord -> StateRead
StateLoaded (StateTarget -> PackageName -> ByteString -> StateRevision
StateRevision StateTarget
target PackageName
name ByteString
etag)) (Either MirrorStateFault ProjectMirrorRecord
-> Either MirrorStateFault StateRead)
-> IO (Either MirrorStateFault ProjectMirrorRecord)
-> IO (Either MirrorStateFault StateRead)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MirrorStateFormat
-> StateCharge
-> Limits
-> StateTarget
-> PackageName
-> ByteString
-> IO (Either MirrorStateFault ProjectMirrorRecord)
chargedDecodeProjectRecord MirrorStateFormat
format StateCharge
charge Limits
limits StateTarget
target PackageName
name ByteString
bytes
writeProject :: StateCharge
-> PackageName
-> Maybe StateRevision
-> ProjectMirrorRecord
-> IO (Either MirrorStateFault StateWrite)
writeProject StateCharge
charge PackageName
name Maybe StateRevision
revision ProjectMirrorRecord
record
| ProjectMirrorRecord -> StateTarget
recordTarget ProjectMirrorRecord
record StateTarget -> StateTarget -> Bool
forall a. Eq a => a -> a -> Bool
/= StateTarget
target Bool -> Bool -> Bool
|| ProjectMirrorRecord -> PackageName
recordPackage ProjectMirrorRecord
record PackageName -> PackageName -> Bool
forall a. Eq a => a -> a -> Bool
/= PackageName
name = Either MirrorStateFault StateWrite
-> IO (Either MirrorStateFault StateWrite)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MirrorStateFault -> Either MirrorStateFault StateWrite
forall a b. a -> Either a b
Left MirrorStateFault
StateIdentityMismatch)
| Bool
otherwise = case StateTarget
-> PackageName
-> Maybe StateRevision
-> Either MirrorStateFault (Maybe ByteString)
revisionBytes StateTarget
target PackageName
name Maybe StateRevision
revision of
Left MirrorStateFault
fault -> Either MirrorStateFault StateWrite
-> IO (Either MirrorStateFault StateWrite)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MirrorStateFault -> Either MirrorStateFault StateWrite
forall a b. a -> Either a b
Left MirrorStateFault
fault)
Right Maybe ByteString
expected ->
MirrorStateFormat
-> StateCharge
-> Limits
-> ProjectMirrorRecord
-> IO (Either MirrorStateFault ByteString)
chargedEncodeProjectRecord MirrorStateFormat
format StateCharge
charge Limits
limits ProjectMirrorRecord
record IO (Either MirrorStateFault ByteString)
-> (Either MirrorStateFault ByteString
-> IO (Either MirrorStateFault StateWrite))
-> IO (Either MirrorStateFault StateWrite)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Left MirrorStateFault
fault -> Either MirrorStateFault StateWrite
-> IO (Either MirrorStateFault StateWrite)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MirrorStateFault -> Either MirrorStateFault StateWrite
forall a b. a -> Either a b
Left MirrorStateFault
fault)
Right ByteString
bytes ->
(Either ObjectFault ObjectWrite
-> Either MirrorStateFault ObjectWrite)
-> IO (Either ObjectFault ObjectWrite)
-> IO (Either MirrorStateFault ObjectWrite)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((ObjectFault -> MirrorStateFault)
-> Either ObjectFault ObjectWrite
-> Either MirrorStateFault ObjectWrite
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first ObjectFault -> MirrorStateFault
objectFault) (StateCharge
-> Deadlines
-> Env
-> Limits
-> Text
-> Text
-> Maybe ByteString
-> ByteString
-> IO (Either ObjectFault ObjectWrite)
writeObject StateCharge
charge Deadlines
deadlines Env
writeEnv Limits
limits Text
bucket (PackageName -> Text
key PackageName
name) Maybe ByteString
expected ByteString
bytes)
IO (Either MirrorStateFault ObjectWrite)
-> (Either MirrorStateFault ObjectWrite
-> Either MirrorStateFault StateWrite)
-> IO (Either MirrorStateFault StateWrite)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> (ObjectWrite -> StateWrite)
-> Either MirrorStateFault ObjectWrite
-> Either MirrorStateFault StateWrite
forall a b.
(a -> b) -> Either MirrorStateFault a -> Either MirrorStateFault b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
( \case
ObjectStored ByteString
etag -> StateRevision -> StateWrite
StateStored (StateTarget -> PackageName -> ByteString -> StateRevision
StateRevision StateTarget
target PackageName
name ByteString
etag)
ObjectWrite
ObjectConflict -> StateWrite
StateConflict
)
retain :: StateCharge
-> VerifiedCompanionBytes
-> IO (Either MirrorStateFault CompanionRef)
retain StateCharge
charge VerifiedCompanionBytes
verified =
StateCharge
-> VerifiedCompanionBytes
-> IO (Either MirrorStateFault CompanionRef)
storeOrReread StateCharge
charge VerifiedCompanionBytes
verified IO (Either MirrorStateFault CompanionRef)
-> (Either MirrorStateFault CompanionRef
-> IO (Either MirrorStateFault CompanionRef))
-> IO (Either MirrorStateFault CompanionRef)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Left MirrorStateFault
StateMissingCompanion -> StateCharge
-> VerifiedCompanionBytes
-> IO (Either MirrorStateFault CompanionRef)
storeOrReread StateCharge
charge VerifiedCompanionBytes
verified
Either MirrorStateFault CompanionRef
settled -> Either MirrorStateFault CompanionRef
-> IO (Either MirrorStateFault CompanionRef)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Either MirrorStateFault CompanionRef
settled
storeOrReread :: StateCharge
-> VerifiedCompanionBytes
-> IO (Either MirrorStateFault CompanionRef)
storeOrReread StateCharge
charge verified :: VerifiedCompanionBytes
verified@(VerifiedCompanionBytes Artifact
_ CompanionRef
reference ByteString
_) = do
result <- StateCharge
-> Deadlines
-> Env
-> Limits
-> Text
-> Text
-> Maybe ByteString
-> ByteString
-> IO (Either ObjectFault ObjectWrite)
writeObject StateCharge
charge Deadlines
deadlines Env
writeEnv Limits
limits Text
bucket (CompanionRef -> Text
referenceKey CompanionRef
reference) Maybe ByteString
forall a. Maybe a
Nothing (VerifiedCompanionBytes -> ByteString
companionBody VerifiedCompanionBytes
verified)
case result of
Right (ObjectStored ByteString
_) -> Either MirrorStateFault CompanionRef
-> IO (Either MirrorStateFault CompanionRef)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CompanionRef -> Either MirrorStateFault CompanionRef
forall a b. b -> Either a b
Right CompanionRef
reference)
Either ObjectFault ObjectWrite
_ -> StateCharge
-> CompanionRef -> IO (Either MirrorStateFault ByteString)
readCompanion StateCharge
charge CompanionRef
reference IO (Either MirrorStateFault ByteString)
-> (Either MirrorStateFault ByteString
-> Either MirrorStateFault CompanionRef)
-> IO (Either MirrorStateFault CompanionRef)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> (ByteString -> CompanionRef)
-> Either MirrorStateFault ByteString
-> Either MirrorStateFault CompanionRef
forall a b.
(a -> b) -> Either MirrorStateFault a -> Either MirrorStateFault b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (CompanionRef -> ByteString -> CompanionRef
forall a b. a -> b -> a
const CompanionRef
reference)
readCompanion :: StateCharge
-> CompanionRef -> IO (Either MirrorStateFault ByteString)
readCompanion StateCharge
charge CompanionRef
reference =
(Either ObjectFault ObjectRead
-> Either MirrorStateFault ObjectRead)
-> IO (Either ObjectFault ObjectRead)
-> IO (Either MirrorStateFault ObjectRead)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((ObjectFault -> MirrorStateFault)
-> Either ObjectFault ObjectRead
-> Either MirrorStateFault ObjectRead
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first ObjectFault -> MirrorStateFault
objectFault) (StateCharge
-> Deadlines
-> Env
-> Limits
-> Text
-> Text
-> Maybe ByteString
-> IO (Either ObjectFault ObjectRead)
readObject StateCharge
charge Deadlines
deadlines Env
readEnv Limits
limits Text
bucket (CompanionRef -> Text
referenceKey CompanionRef
reference) Maybe ByteString
forall a. Maybe a
Nothing)
IO (Either MirrorStateFault ObjectRead)
-> (Either MirrorStateFault ObjectRead
-> Either MirrorStateFault ByteString)
-> IO (Either MirrorStateFault ByteString)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> ( Either MirrorStateFault ObjectRead
-> (ObjectRead -> Either MirrorStateFault ByteString)
-> Either MirrorStateFault ByteString
forall a b.
Either MirrorStateFault a
-> (a -> Either MirrorStateFault b) -> Either MirrorStateFault b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
\case
ObjectLoaded ByteString
_ ByteString
bytes -> CompanionRef -> ByteString -> Either MirrorStateFault ByteString
verifyRetainedCompanion CompanionRef
reference ByteString
bytes
ObjectRead
ObjectAbsent -> MirrorStateFault -> Either MirrorStateFault ByteString
forall a b. a -> Either a b
Left MirrorStateFault
StateMissingCompanion
ObjectUnchanged ByteString
_ -> MirrorStateFault -> Either MirrorStateFault ByteString
forall a b. a -> Either a b
Left MirrorStateFault
StateRevisionMismatch
)
revisionBytes :: StateTarget -> PackageName -> Maybe StateRevision -> Either MirrorStateFault (Maybe ByteString)
revisionBytes :: StateTarget
-> PackageName
-> Maybe StateRevision
-> Either MirrorStateFault (Maybe ByteString)
revisionBytes StateTarget
target PackageName
name = (StateRevision -> Either MirrorStateFault ByteString)
-> Maybe StateRevision
-> Either MirrorStateFault (Maybe ByteString)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Maybe a -> f (Maybe b)
traverse ((StateRevision -> Either MirrorStateFault ByteString)
-> Maybe StateRevision
-> Either MirrorStateFault (Maybe ByteString))
-> (StateRevision -> Either MirrorStateFault ByteString)
-> Maybe StateRevision
-> Either MirrorStateFault (Maybe ByteString)
forall a b. (a -> b) -> a -> b
$ \(StateRevision StateTarget
recordedTarget PackageName
recordedName ByteString
bytes) ->
if StateTarget
recordedTarget StateTarget -> StateTarget -> Bool
forall a. Eq a => a -> a -> Bool
== StateTarget
target Bool -> Bool -> Bool
&& PackageName
recordedName PackageName -> PackageName -> Bool
forall a. Eq a => a -> a -> Bool
== PackageName
name then ByteString -> Either MirrorStateFault ByteString
forall a b. b -> Either a b
Right ByteString
bytes else MirrorStateFault -> Either MirrorStateFault ByteString
forall a b. a -> Either a b
Left MirrorStateFault
StateRevisionMismatch
objectFault :: ObjectFault -> MirrorStateFault
objectFault :: ObjectFault -> MirrorStateFault
objectFault = \case
ObjectTransport TransportFault
fault -> TransportFault -> MirrorStateFault
StateTransport TransportFault
fault
ObjectTooLarge Int
cap -> LimitError -> MirrorStateFault
StateBound (BodyLimit -> LimitError
BodyTooLarge (Int -> BodyLimit
MetadataBodyLimit Int
cap))
ObjectFault
ObjectNoRevision -> Text -> MirrorStateFault
StateInvalid Text
"state response omitted its revision"
ObjectFault
ObjectUnknownAbsence -> Text -> MirrorStateFault
StateInvalid Text
"state absence could not be established"