module Ecluse.Core.Mirror.State.Internal (
StateTarget (..),
stateTarget,
StateRevision (..),
ProjectMirrorRecord (..),
StoredMirrorFragment (..),
MirrorVersion (..),
CompanionState (..),
CompanionRef (..),
VerifiedCompanionBytes (..),
MirrorStateFault (..),
StateRead (..),
StateWrite (..),
MirrorState (..),
StateCharge,
extendProjectRecord,
retainCompanionReference,
retainVerifiedIdentity,
verifyCompanionBytes,
verifyRetainedCompanion,
versionFiles,
recordVersion,
recordTarget,
recordPackage,
recordVersions,
recordEnvelope,
versionFragments,
versionCompanions,
versionVerifiedIdentities,
companionDigest,
companionSize,
companionBody,
targetKey,
projectKey,
checkMirrorCompletion,
MirrorCompletion (..),
MirrorCompletionFault (..),
declaredCompanion,
declaredSourceCompanion,
verifySourceCompanionBytes,
checkStateFiles,
) where
import Data.Aeson (Object)
import Data.ByteString qualified as BS
import Data.Map.Strict qualified as Map
import Data.Text.Short qualified as TS
import Data.Traversable (for)
import Ecluse.Core.Ecosystem (ecosystemName)
import Ecluse.Core.Fault (TransportFault)
import Ecluse.Core.Mirror.State.Format (MirrorStateFormat (msfCompanions, msfEnvelope), ProjectEnvelope (envelopeSplit))
import Ecluse.Core.Package
import Ecluse.Core.Package.Hash (sha256Hex)
import Ecluse.Core.Package.Integrity (IntegrityResult (..), verifyIntegrity)
import Ecluse.Core.Registry (CompanionRequirement (DeclaredCompanions, NativeCompanions))
import Ecluse.Core.Registry.MirrorUpload
import Ecluse.Core.Registry.MirrorUpload.Internal (restoreMirrorFragment)
import Ecluse.Core.Security (LimitError (TooManyArtifacts), Limits (..), checkVersionCountOf, withinCeiling)
import Ecluse.Core.Version (Version, renderVersion)
newtype StateTarget = StateTarget Text
deriving stock (StateTarget -> StateTarget -> Bool
(StateTarget -> StateTarget -> Bool)
-> (StateTarget -> StateTarget -> Bool) -> Eq StateTarget
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: StateTarget -> StateTarget -> Bool
== :: StateTarget -> StateTarget -> Bool
$c/= :: StateTarget -> StateTarget -> Bool
/= :: StateTarget -> StateTarget -> Bool
Eq, Eq StateTarget
Eq StateTarget =>
(StateTarget -> StateTarget -> Ordering)
-> (StateTarget -> StateTarget -> Bool)
-> (StateTarget -> StateTarget -> Bool)
-> (StateTarget -> StateTarget -> Bool)
-> (StateTarget -> StateTarget -> Bool)
-> (StateTarget -> StateTarget -> StateTarget)
-> (StateTarget -> StateTarget -> StateTarget)
-> Ord StateTarget
StateTarget -> StateTarget -> Bool
StateTarget -> StateTarget -> Ordering
StateTarget -> StateTarget -> StateTarget
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: StateTarget -> StateTarget -> Ordering
compare :: StateTarget -> StateTarget -> Ordering
$c< :: StateTarget -> StateTarget -> Bool
< :: StateTarget -> StateTarget -> Bool
$c<= :: StateTarget -> StateTarget -> Bool
<= :: StateTarget -> StateTarget -> Bool
$c> :: StateTarget -> StateTarget -> Bool
> :: StateTarget -> StateTarget -> Bool
$c>= :: StateTarget -> StateTarget -> Bool
>= :: StateTarget -> StateTarget -> Bool
$cmax :: StateTarget -> StateTarget -> StateTarget
max :: StateTarget -> StateTarget -> StateTarget
$cmin :: StateTarget -> StateTarget -> StateTarget
min :: StateTarget -> StateTarget -> StateTarget
Ord, Int -> StateTarget -> ShowS
[StateTarget] -> ShowS
StateTarget -> String
(Int -> StateTarget -> ShowS)
-> (StateTarget -> String)
-> ([StateTarget] -> ShowS)
-> Show StateTarget
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StateTarget -> ShowS
showsPrec :: Int -> StateTarget -> ShowS
$cshow :: StateTarget -> String
show :: StateTarget -> String
$cshowList :: [StateTarget] -> ShowS
showList :: [StateTarget] -> ShowS
Show)
stateTarget :: Text -> StateTarget
stateTarget :: Text -> StateTarget
stateTarget = Text -> StateTarget
StateTarget (Text -> StateTarget) -> (Text -> Text) -> Text -> StateTarget
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
sha256Hex (ByteString -> Text) -> (Text -> ByteString) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
forall a b. ConvertUtf8 a b => a -> b
encodeUtf8
data StateRevision = StateRevision StateTarget PackageName ByteString
deriving stock (StateRevision -> StateRevision -> Bool
(StateRevision -> StateRevision -> Bool)
-> (StateRevision -> StateRevision -> Bool) -> Eq StateRevision
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: StateRevision -> StateRevision -> Bool
== :: StateRevision -> StateRevision -> Bool
$c/= :: StateRevision -> StateRevision -> Bool
/= :: StateRevision -> StateRevision -> Bool
Eq, Int -> StateRevision -> ShowS
[StateRevision] -> ShowS
StateRevision -> String
(Int -> StateRevision -> ShowS)
-> (StateRevision -> String)
-> ([StateRevision] -> ShowS)
-> Show StateRevision
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StateRevision -> ShowS
showsPrec :: Int -> StateRevision -> ShowS
$cshow :: StateRevision -> String
show :: StateRevision -> String
$cshowList :: [StateRevision] -> ShowS
showList :: [StateRevision] -> ShowS
Show)
data StoredMirrorFragment = StoredMirrorFragment PackageDetails MirrorReadData (NonEmpty ExpectedMirrorFile)
deriving stock (StoredMirrorFragment -> StoredMirrorFragment -> Bool
(StoredMirrorFragment -> StoredMirrorFragment -> Bool)
-> (StoredMirrorFragment -> StoredMirrorFragment -> Bool)
-> Eq StoredMirrorFragment
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: StoredMirrorFragment -> StoredMirrorFragment -> Bool
== :: StoredMirrorFragment -> StoredMirrorFragment -> Bool
$c/= :: StoredMirrorFragment -> StoredMirrorFragment -> Bool
/= :: StoredMirrorFragment -> StoredMirrorFragment -> Bool
Eq, Int -> StoredMirrorFragment -> ShowS
[StoredMirrorFragment] -> ShowS
StoredMirrorFragment -> String
(Int -> StoredMirrorFragment -> ShowS)
-> (StoredMirrorFragment -> String)
-> ([StoredMirrorFragment] -> ShowS)
-> Show StoredMirrorFragment
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StoredMirrorFragment -> ShowS
showsPrec :: Int -> StoredMirrorFragment -> ShowS
$cshow :: StoredMirrorFragment -> String
show :: StoredMirrorFragment -> String
$cshowList :: [StoredMirrorFragment] -> ShowS
showList :: [StoredMirrorFragment] -> ShowS
Show)
data MirrorVersion = MirrorVersion
{ MirrorVersion -> NonEmpty StoredMirrorFragment
storedFragments :: NonEmpty StoredMirrorFragment
, MirrorVersion -> Map Text Hash
storedIdentities :: Map Text Hash
, MirrorVersion -> Map Text CompanionState
storedCompanions :: Map Text CompanionState
}
deriving stock (MirrorVersion -> MirrorVersion -> Bool
(MirrorVersion -> MirrorVersion -> Bool)
-> (MirrorVersion -> MirrorVersion -> Bool) -> Eq MirrorVersion
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MirrorVersion -> MirrorVersion -> Bool
== :: MirrorVersion -> MirrorVersion -> Bool
$c/= :: MirrorVersion -> MirrorVersion -> Bool
/= :: MirrorVersion -> MirrorVersion -> Bool
Eq, Int -> MirrorVersion -> ShowS
[MirrorVersion] -> ShowS
MirrorVersion -> String
(Int -> MirrorVersion -> ShowS)
-> (MirrorVersion -> String)
-> ([MirrorVersion] -> ShowS)
-> Show MirrorVersion
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MirrorVersion -> ShowS
showsPrec :: Int -> MirrorVersion -> ShowS
$cshow :: MirrorVersion -> String
show :: MirrorVersion -> String
$cshowList :: [MirrorVersion] -> ShowS
showList :: [MirrorVersion] -> ShowS
Show)
data ProjectMirrorRecord = ProjectMirrorRecord
{ ProjectMirrorRecord -> StateTarget
projectTarget :: StateTarget
, ProjectMirrorRecord -> PackageName
projectPackage :: PackageName
, ProjectMirrorRecord -> Object
projectEnvelope :: Object
, ProjectMirrorRecord -> Map Text MirrorVersion
projectVersions :: Map Text MirrorVersion
}
deriving stock (ProjectMirrorRecord -> ProjectMirrorRecord -> Bool
(ProjectMirrorRecord -> ProjectMirrorRecord -> Bool)
-> (ProjectMirrorRecord -> ProjectMirrorRecord -> Bool)
-> Eq ProjectMirrorRecord
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ProjectMirrorRecord -> ProjectMirrorRecord -> Bool
== :: ProjectMirrorRecord -> ProjectMirrorRecord -> Bool
$c/= :: ProjectMirrorRecord -> ProjectMirrorRecord -> Bool
/= :: ProjectMirrorRecord -> ProjectMirrorRecord -> Bool
Eq, Int -> ProjectMirrorRecord -> ShowS
[ProjectMirrorRecord] -> ShowS
ProjectMirrorRecord -> String
(Int -> ProjectMirrorRecord -> ShowS)
-> (ProjectMirrorRecord -> String)
-> ([ProjectMirrorRecord] -> ShowS)
-> Show ProjectMirrorRecord
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ProjectMirrorRecord -> ShowS
showsPrec :: Int -> ProjectMirrorRecord -> ShowS
$cshow :: ProjectMirrorRecord -> String
show :: ProjectMirrorRecord -> String
$cshowList :: [ProjectMirrorRecord] -> ShowS
showList :: [ProjectMirrorRecord] -> ShowS
Show)
data CompanionState = CompanionPending | CompanionRetained CompanionRef
deriving stock (CompanionState -> CompanionState -> Bool
(CompanionState -> CompanionState -> Bool)
-> (CompanionState -> CompanionState -> Bool) -> Eq CompanionState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CompanionState -> CompanionState -> Bool
== :: CompanionState -> CompanionState -> Bool
$c/= :: CompanionState -> CompanionState -> Bool
/= :: CompanionState -> CompanionState -> Bool
Eq, Int -> CompanionState -> ShowS
[CompanionState] -> ShowS
CompanionState -> String
(Int -> CompanionState -> ShowS)
-> (CompanionState -> String)
-> ([CompanionState] -> ShowS)
-> Show CompanionState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CompanionState -> ShowS
showsPrec :: Int -> CompanionState -> ShowS
$cshow :: CompanionState -> String
show :: CompanionState -> String
$cshowList :: [CompanionState] -> ShowS
showList :: [CompanionState] -> ShowS
Show)
data CompanionRef = CompanionRef Hash Int
deriving stock (CompanionRef -> CompanionRef -> Bool
(CompanionRef -> CompanionRef -> Bool)
-> (CompanionRef -> CompanionRef -> Bool) -> Eq CompanionRef
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CompanionRef -> CompanionRef -> Bool
== :: CompanionRef -> CompanionRef -> Bool
$c/= :: CompanionRef -> CompanionRef -> Bool
/= :: CompanionRef -> CompanionRef -> Bool
Eq, Int -> CompanionRef -> ShowS
[CompanionRef] -> ShowS
CompanionRef -> String
(Int -> CompanionRef -> ShowS)
-> (CompanionRef -> String)
-> ([CompanionRef] -> ShowS)
-> Show CompanionRef
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CompanionRef -> ShowS
showsPrec :: Int -> CompanionRef -> ShowS
$cshow :: CompanionRef -> String
show :: CompanionRef -> String
$cshowList :: [CompanionRef] -> ShowS
showList :: [CompanionRef] -> ShowS
Show)
data VerifiedCompanionBytes = VerifiedCompanionBytes Artifact CompanionRef ByteString
deriving stock (VerifiedCompanionBytes -> VerifiedCompanionBytes -> Bool
(VerifiedCompanionBytes -> VerifiedCompanionBytes -> Bool)
-> (VerifiedCompanionBytes -> VerifiedCompanionBytes -> Bool)
-> Eq VerifiedCompanionBytes
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: VerifiedCompanionBytes -> VerifiedCompanionBytes -> Bool
== :: VerifiedCompanionBytes -> VerifiedCompanionBytes -> Bool
$c/= :: VerifiedCompanionBytes -> VerifiedCompanionBytes -> Bool
/= :: VerifiedCompanionBytes -> VerifiedCompanionBytes -> Bool
Eq, Int -> VerifiedCompanionBytes -> ShowS
[VerifiedCompanionBytes] -> ShowS
VerifiedCompanionBytes -> String
(Int -> VerifiedCompanionBytes -> ShowS)
-> (VerifiedCompanionBytes -> String)
-> ([VerifiedCompanionBytes] -> ShowS)
-> Show VerifiedCompanionBytes
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> VerifiedCompanionBytes -> ShowS
showsPrec :: Int -> VerifiedCompanionBytes -> ShowS
$cshow :: VerifiedCompanionBytes -> String
show :: VerifiedCompanionBytes -> String
$cshowList :: [VerifiedCompanionBytes] -> ShowS
showList :: [VerifiedCompanionBytes] -> ShowS
Show)
data MirrorStateFault
= StateTransport TransportFault
| StateBound LimitError
| StateInvalid Text
| StateIdentityMismatch
| StateFileConflict Text
| StateCompanionConflict Text
| StateRevisionMismatch
| StateMissingCompanion
| StateRetryExhausted
deriving stock (MirrorStateFault -> MirrorStateFault -> Bool
(MirrorStateFault -> MirrorStateFault -> Bool)
-> (MirrorStateFault -> MirrorStateFault -> Bool)
-> Eq MirrorStateFault
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MirrorStateFault -> MirrorStateFault -> Bool
== :: MirrorStateFault -> MirrorStateFault -> Bool
$c/= :: MirrorStateFault -> MirrorStateFault -> Bool
/= :: MirrorStateFault -> MirrorStateFault -> Bool
Eq, Int -> MirrorStateFault -> ShowS
[MirrorStateFault] -> ShowS
MirrorStateFault -> String
(Int -> MirrorStateFault -> ShowS)
-> (MirrorStateFault -> String)
-> ([MirrorStateFault] -> ShowS)
-> Show MirrorStateFault
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MirrorStateFault -> ShowS
showsPrec :: Int -> MirrorStateFault -> ShowS
$cshow :: MirrorStateFault -> String
show :: MirrorStateFault -> String
$cshowList :: [MirrorStateFault] -> ShowS
showList :: [MirrorStateFault] -> ShowS
Show)
data StateRead = StateAbsent | StateUnchanged StateRevision | StateLoaded StateRevision ProjectMirrorRecord
deriving stock (StateRead -> StateRead -> Bool
(StateRead -> StateRead -> Bool)
-> (StateRead -> StateRead -> Bool) -> Eq StateRead
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: StateRead -> StateRead -> Bool
== :: StateRead -> StateRead -> Bool
$c/= :: StateRead -> StateRead -> Bool
/= :: StateRead -> StateRead -> Bool
Eq, Int -> StateRead -> ShowS
[StateRead] -> ShowS
StateRead -> String
(Int -> StateRead -> ShowS)
-> (StateRead -> String)
-> ([StateRead] -> ShowS)
-> Show StateRead
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StateRead -> ShowS
showsPrec :: Int -> StateRead -> ShowS
$cshow :: StateRead -> String
show :: StateRead -> String
$cshowList :: [StateRead] -> ShowS
showList :: [StateRead] -> ShowS
Show)
data StateWrite = StateStored StateRevision | StateConflict
deriving stock (StateWrite -> StateWrite -> Bool
(StateWrite -> StateWrite -> Bool)
-> (StateWrite -> StateWrite -> Bool) -> Eq StateWrite
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: StateWrite -> StateWrite -> Bool
== :: StateWrite -> StateWrite -> Bool
$c/= :: StateWrite -> StateWrite -> Bool
/= :: StateWrite -> StateWrite -> Bool
Eq, Int -> StateWrite -> ShowS
[StateWrite] -> ShowS
StateWrite -> String
(Int -> StateWrite -> ShowS)
-> (StateWrite -> String)
-> ([StateWrite] -> ShowS)
-> Show StateWrite
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StateWrite -> ShowS
showsPrec :: Int -> StateWrite -> ShowS
$cshow :: StateWrite -> String
show :: StateWrite -> String
$cshowList :: [StateWrite] -> ShowS
showList :: [StateWrite] -> ShowS
Show)
type StateCharge = Int -> IO ()
data MirrorState = MirrorState
{ MirrorState
-> StateCharge
-> PackageName
-> Maybe StateRevision
-> IO (Either MirrorStateFault StateRead)
msReadProject :: StateCharge -> PackageName -> Maybe StateRevision -> IO (Either MirrorStateFault StateRead)
, MirrorState
-> StateCharge
-> PackageName
-> Maybe StateRevision
-> ProjectMirrorRecord
-> IO (Either MirrorStateFault StateWrite)
msWriteProject :: StateCharge -> PackageName -> Maybe StateRevision -> ProjectMirrorRecord -> IO (Either MirrorStateFault StateWrite)
, MirrorState
-> StateCharge
-> VerifiedCompanionBytes
-> IO (Either MirrorStateFault CompanionRef)
msRetainCompanion :: StateCharge -> VerifiedCompanionBytes -> IO (Either MirrorStateFault CompanionRef)
, MirrorState
-> StateCharge
-> CompanionRef
-> IO (Either MirrorStateFault ByteString)
msReadCompanion :: StateCharge -> CompanionRef -> IO (Either MirrorStateFault ByteString)
}
recordTarget :: ProjectMirrorRecord -> StateTarget
recordTarget :: ProjectMirrorRecord -> StateTarget
recordTarget = ProjectMirrorRecord -> StateTarget
projectTarget
recordPackage :: ProjectMirrorRecord -> PackageName
recordPackage :: ProjectMirrorRecord -> PackageName
recordPackage = ProjectMirrorRecord -> PackageName
projectPackage
recordEnvelope :: ProjectMirrorRecord -> Object
recordEnvelope :: ProjectMirrorRecord -> Object
recordEnvelope = ProjectMirrorRecord -> Object
projectEnvelope
recordVersions :: ProjectMirrorRecord -> Map Text MirrorVersion
recordVersions :: ProjectMirrorRecord -> Map Text MirrorVersion
recordVersions = ProjectMirrorRecord -> Map Text MirrorVersion
projectVersions
versionFragments :: MirrorVersion -> NonEmpty StoredMirrorFragment
versionFragments :: MirrorVersion -> NonEmpty StoredMirrorFragment
versionFragments = MirrorVersion -> NonEmpty StoredMirrorFragment
storedFragments
versionCompanions :: MirrorVersion -> Map Text CompanionState
versionCompanions :: MirrorVersion -> Map Text CompanionState
versionCompanions = MirrorVersion -> Map Text CompanionState
storedCompanions
versionVerifiedIdentities :: MirrorVersion -> Map Text Hash
versionVerifiedIdentities :: MirrorVersion -> Map Text Hash
versionVerifiedIdentities = MirrorVersion -> Map Text Hash
storedIdentities
extendProjectRecord :: MirrorStateFormat -> Limits -> StateTarget -> MirrorInventory -> Maybe ProjectMirrorRecord -> Either MirrorStateFault ProjectMirrorRecord
extendProjectRecord :: MirrorStateFormat
-> Limits
-> StateTarget
-> MirrorInventory
-> Maybe ProjectMirrorRecord
-> Either MirrorStateFault ProjectMirrorRecord
extendProjectRecord MirrorStateFormat
format Limits
limits StateTarget
target MirrorInventory
inventory Maybe ProjectMirrorRecord
previous = do
let name :: PackageName
name = MirrorInventory -> PackageName
inventoryPackage MirrorInventory
inventory
initial :: ProjectMirrorRecord
initial = StateTarget
-> PackageName
-> Object
-> Map Text MirrorVersion
-> ProjectMirrorRecord
ProjectMirrorRecord StateTarget
target PackageName
name Object
forall a. Monoid a => a
mempty Map Text MirrorVersion
forall k a. Map k a
Map.empty
record :: ProjectMirrorRecord
record = ProjectMirrorRecord
-> Maybe ProjectMirrorRecord -> ProjectMirrorRecord
forall a. a -> Maybe a -> a
fromMaybe ProjectMirrorRecord
initial Maybe ProjectMirrorRecord
previous
key :: Text
key = Version -> Text
renderVersion (MirrorInventory -> Version
inventoryVersion MirrorInventory
inventory)
Bool -> Either MirrorStateFault () -> Either MirrorStateFault ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (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) (MirrorStateFault -> Either MirrorStateFault ()
forall a b. a -> Either a b
Left MirrorStateFault
StateIdentityMismatch)
updated <- MirrorStateFormat
-> MirrorInventory
-> Maybe MirrorVersion
-> Either MirrorStateFault MirrorVersion
extendVersion MirrorStateFormat
format MirrorInventory
inventory (Text -> Map Text MirrorVersion -> Maybe MirrorVersion
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
key (ProjectMirrorRecord -> Map Text MirrorVersion
recordVersions ProjectMirrorRecord
record))
let (envelope, _) = envelopeSplit (msfEnvelope format) (inventoryReadData inventory)
result = ProjectMirrorRecord
record{projectEnvelope = envelope, projectVersions = Map.insert key updated (recordVersions record)}
first StateBound (checkVersionCountOf limits (Map.size (recordVersions result)))
first StateBound (checkStateFiles limits (sum (map (Map.size . versionFiles) (Map.elems (recordVersions result)))))
pure result
checkStateFiles :: Limits -> Int -> Either LimitError ()
checkStateFiles :: Limits -> Int -> Either LimitError ()
checkStateFiles Limits
limits = (Int -> Int -> LimitError) -> Int -> Int -> Either LimitError ()
withinCeiling Int -> Int -> LimitError
TooManyArtifacts (Limits -> Int
mirrorStateFileCeiling Limits
limits)
extendVersion :: MirrorStateFormat -> MirrorInventory -> Maybe MirrorVersion -> Either MirrorStateFault MirrorVersion
extendVersion :: MirrorStateFormat
-> MirrorInventory
-> Maybe MirrorVersion
-> Either MirrorStateFault MirrorVersion
extendVersion MirrorStateFormat
format MirrorInventory
inventory Maybe MirrorVersion
previous = do
let oldFiles :: Map Text ExpectedMirrorFile
oldFiles = Map Text ExpectedMirrorFile
-> (MirrorVersion -> Map Text ExpectedMirrorFile)
-> Maybe MirrorVersion
-> Map Text ExpectedMirrorFile
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Map Text ExpectedMirrorFile
forall k a. Map k a
Map.empty MirrorVersion -> Map Text ExpectedMirrorFile
versionFiles Maybe MirrorVersion
previous
incoming :: Map Text ExpectedMirrorFile
incoming = [(Text, ExpectedMirrorFile)] -> Map Text ExpectedMirrorFile
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(Artifact -> Text
artFilename (ExpectedMirrorFile -> Artifact
expectedArtifact ExpectedMirrorFile
file), ExpectedMirrorFile
file) | ExpectedMirrorFile
file <- NonEmpty ExpectedMirrorFile -> [ExpectedMirrorFile]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (MirrorInventory -> NonEmpty ExpectedMirrorFile
inventoryFiles MirrorInventory
inventory)]
[(Text, ExpectedMirrorFile)]
-> ((Text, ExpectedMirrorFile) -> Either MirrorStateFault ())
-> Either MirrorStateFault ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ (Map Text ExpectedMirrorFile -> [(Text, ExpectedMirrorFile)]
forall k a. Map k a -> [(k, a)]
Map.toList Map Text ExpectedMirrorFile
incoming) (((Text, ExpectedMirrorFile) -> Either MirrorStateFault ())
-> Either MirrorStateFault ())
-> ((Text, ExpectedMirrorFile) -> Either MirrorStateFault ())
-> Either MirrorStateFault ()
forall a b. (a -> b) -> a -> b
$ \(Text
name, ExpectedMirrorFile
file) -> Maybe ExpectedMirrorFile
-> (ExpectedMirrorFile -> Either MirrorStateFault ())
-> Either MirrorStateFault ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ (Text -> Map Text ExpectedMirrorFile -> Maybe ExpectedMirrorFile
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
name Map Text ExpectedMirrorFile
oldFiles) ((ExpectedMirrorFile -> Either MirrorStateFault ())
-> Either MirrorStateFault ())
-> (ExpectedMirrorFile -> Either MirrorStateFault ())
-> Either MirrorStateFault ()
forall a b. (a -> b) -> a -> b
$ \ExpectedMirrorFile
old ->
Bool -> Either MirrorStateFault () -> Either MirrorStateFault ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ExpectedMirrorFile -> ExpectedMirrorFile -> Bool
expectedIdentityMatches ExpectedMirrorFile
old ExpectedMirrorFile
file) (MirrorStateFault -> Either MirrorStateFault ()
forall a b. a -> Either a b
Left (Text -> MirrorStateFault
StateFileConflict Text
name))
let additions :: Map Text ExpectedMirrorFile
additions = Map Text ExpectedMirrorFile
-> Map Text ExpectedMirrorFile -> Map Text ExpectedMirrorFile
forall k a b. Ord k => Map k a -> Map k b -> Map k a
Map.difference Map Text ExpectedMirrorFile
incoming Map Text ExpectedMirrorFile
oldFiles
case [ExpectedMirrorFile] -> Maybe (NonEmpty ExpectedMirrorFile)
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty (Map Text ExpectedMirrorFile -> [ExpectedMirrorFile]
forall k a. Map k a -> [a]
Map.elems Map Text ExpectedMirrorFile
additions) of
Maybe (NonEmpty ExpectedMirrorFile)
Nothing -> Either MirrorStateFault MirrorVersion
-> (MirrorVersion -> Either MirrorStateFault MirrorVersion)
-> Maybe MirrorVersion
-> Either MirrorStateFault MirrorVersion
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (MirrorStateFault -> Either MirrorStateFault MirrorVersion
forall a b. a -> Either a b
Left (Text -> MirrorStateFault
StateInvalid Text
"empty expected inventory")) MirrorVersion -> Either MirrorStateFault MirrorVersion
forall a b. b -> Either a b
Right Maybe MirrorVersion
previous
Just NonEmpty ExpectedMirrorFile
_ -> do
(details, source) <-
MirrorStateFault
-> Maybe (PackageDetails, MirrorReadData)
-> Either MirrorStateFault (PackageDetails, MirrorReadData)
forall l r. l -> Maybe r -> Either l r
maybeToRight (Text -> MirrorStateFault
StateInvalid Text
"cannot retain paired source fragment") (Maybe (PackageDetails, MirrorReadData)
-> Either MirrorStateFault (PackageDetails, MirrorReadData))
-> Maybe (PackageDetails, MirrorReadData)
-> Either MirrorStateFault (PackageDetails, MirrorReadData)
forall a b. (a -> b) -> a -> b
$
Set Text
-> MirrorInventory -> Maybe (PackageDetails, MirrorReadData)
splitMirrorInventory (Map Text ExpectedMirrorFile -> Set Text
forall k a. Map k a -> Set k
Map.keysSet Map Text ExpectedMirrorFile
additions) MirrorInventory
inventory
let (_, ownedSource) = envelopeSplit (msfEnvelope format) source
files <- first (const (StateInvalid "invalid source fragment")) (restoreMirrorFragment details ownedSource)
let fragment = PackageDetails
-> MirrorReadData
-> NonEmpty ExpectedMirrorFile
-> StoredMirrorFragment
StoredMirrorFragment PackageDetails
details MirrorReadData
ownedSource NonEmpty ExpectedMirrorFile
files
companions = [(Text, CompanionState)] -> Map Text CompanionState
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(Text
name, CompanionState
CompanionPending) | (Text
name, ExpectedMirrorFile
file) <- Map Text ExpectedMirrorFile -> [(Text, ExpectedMirrorFile)]
forall k a. Map k a -> [(k, a)]
Map.toList Map Text ExpectedMirrorFile
additions, CompanionRequirement -> Artifact -> Bool
declaredSourceCompanion (MirrorStateFormat -> CompanionRequirement
msfCompanions MirrorStateFormat
format) (ExpectedMirrorFile -> Artifact
expectedArtifact ExpectedMirrorFile
file)]
pure $ case previous of
Maybe MirrorVersion
Nothing -> NonEmpty StoredMirrorFragment
-> Map Text Hash -> Map Text CompanionState -> MirrorVersion
MirrorVersion (StoredMirrorFragment
fragment StoredMirrorFragment
-> [StoredMirrorFragment] -> NonEmpty StoredMirrorFragment
forall a. a -> [a] -> NonEmpty a
:| []) Map Text Hash
forall k a. Map k a
Map.empty Map Text CompanionState
companions
Just MirrorVersion
old -> MirrorVersion
old{storedFragments = versionFragments old <> (fragment :| []), storedCompanions = versionCompanions old <> companions}
versionFiles :: MirrorVersion -> Map Text ExpectedMirrorFile
versionFiles :: MirrorVersion -> Map Text ExpectedMirrorFile
versionFiles MirrorVersion
version =
[(Text, ExpectedMirrorFile)] -> Map Text ExpectedMirrorFile
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList
[(Artifact -> Text
artFilename (ExpectedMirrorFile -> Artifact
expectedArtifact ExpectedMirrorFile
file), ExpectedMirrorFile
file) | StoredMirrorFragment PackageDetails
_ MirrorReadData
_ NonEmpty ExpectedMirrorFile
files <- NonEmpty StoredMirrorFragment -> [StoredMirrorFragment]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (MirrorVersion -> NonEmpty StoredMirrorFragment
versionFragments MirrorVersion
version), ExpectedMirrorFile
file <- NonEmpty ExpectedMirrorFile -> [ExpectedMirrorFile]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList NonEmpty ExpectedMirrorFile
files]
recordVersion :: Version -> ProjectMirrorRecord -> Maybe MirrorVersion
recordVersion :: Version -> ProjectMirrorRecord -> Maybe MirrorVersion
recordVersion Version
version = Text -> Map Text MirrorVersion -> Maybe MirrorVersion
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup (Version -> Text
renderVersion Version
version) (Map Text MirrorVersion -> Maybe MirrorVersion)
-> (ProjectMirrorRecord -> Map Text MirrorVersion)
-> ProjectMirrorRecord
-> Maybe MirrorVersion
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ProjectMirrorRecord -> Map Text MirrorVersion
recordVersions
verifyCompanionBytes :: Limits -> ExpectedMirrorFile -> ByteString -> Either MirrorStateFault VerifiedCompanionBytes
verifyCompanionBytes :: Limits
-> ExpectedMirrorFile
-> ByteString
-> Either MirrorStateFault VerifiedCompanionBytes
verifyCompanionBytes Limits
limits ExpectedMirrorFile
file ByteString
bytes = do
let artifact :: Artifact
artifact = ExpectedMirrorFile -> Artifact
expectedArtifact ExpectedMirrorFile
file
Bool -> Either MirrorStateFault () -> Either MirrorStateFault ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Artifact -> Bool
declaredCompanion Artifact
artifact) (MirrorStateFault -> Either MirrorStateFault ()
forall a b. a -> Either a b
Left (Text -> MirrorStateFault
StateInvalid Text
"companion was not declared"))
Bool -> Either MirrorStateFault () -> Either MirrorStateFault ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ByteString -> Int
BS.length ByteString
bytes Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Limits -> Int
maxMetadataBytes Limits
limits) (MirrorStateFault -> Either MirrorStateFault ()
forall a b. a -> Either a b
Left (Text -> MirrorStateFault
StateInvalid Text
"companion exceeds metadata byte bound"))
digests <- case Artifact -> Maybe CoreMetadata
artCoreMetadata Artifact
artifact of
Just (MetadataDigests Map Text Text
values) -> ([Maybe Hash] -> [Hash])
-> Either MirrorStateFault [Maybe Hash]
-> Either MirrorStateFault [Hash]
forall a b.
(a -> b) -> Either MirrorStateFault a -> Either MirrorStateFault b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Maybe Hash] -> [Hash]
forall a. [Maybe a] -> [a]
catMaybes (Either MirrorStateFault [Maybe Hash]
-> Either MirrorStateFault [Hash])
-> (((Text, Text) -> Either MirrorStateFault (Maybe Hash))
-> Either MirrorStateFault [Maybe Hash])
-> ((Text, Text) -> Either MirrorStateFault (Maybe Hash))
-> Either MirrorStateFault [Hash]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Text, Text)]
-> ((Text, Text) -> Either MirrorStateFault (Maybe Hash))
-> Either MirrorStateFault [Maybe Hash]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for (Map Text Text -> [(Text, Text)]
forall k a. Map k a -> [(k, a)]
Map.toList Map Text Text
values) (((Text, Text) -> Either MirrorStateFault (Maybe Hash))
-> Either MirrorStateFault [Hash])
-> ((Text, Text) -> Either MirrorStateFault (Maybe Hash))
-> Either MirrorStateFault [Hash]
forall a b. (a -> b) -> a -> b
$ \(Text
algorithm, Text
value) ->
case Text -> Either Text HashAlg
parseHashAlg Text
algorithm of
Left Text
_ -> Maybe Hash -> Either MirrorStateFault (Maybe Hash)
forall a b. b -> Either a b
Right Maybe Hash
forall a. Maybe a
Nothing
Right HashAlg
alg -> Hash -> Maybe Hash
forall a. a -> Maybe a
Just (Hash -> Maybe Hash)
-> Either MirrorStateFault Hash
-> Either MirrorStateFault (Maybe Hash)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Text -> MirrorStateFault)
-> Either Text Hash -> Either MirrorStateFault Hash
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 Text -> MirrorStateFault
StateInvalid (HashAlg -> Text -> Either Text Hash
mkHash HashAlg
alg Text
value)
Maybe CoreMetadata
_ -> [Hash] -> Either MirrorStateFault [Hash]
forall a. a -> Either MirrorStateFault a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
for_ (nonEmpty digests) $ \NonEmpty Hash
expected -> case NonEmpty Hash -> ByteString -> IntegrityResult
verifyIntegrity NonEmpty Hash
expected ByteString
bytes of
IntegrityResult
IntegrityVerified -> () -> Either MirrorStateFault ()
forall a b. b -> Either a b
Right ()
IntegrityMismatch Text
detail -> MirrorStateFault -> Either MirrorStateFault ()
forall a b. a -> Either a b
Left (Text -> MirrorStateFault
StateInvalid Text
detail)
digest <- first StateInvalid (mkHash SHA256 (sha256Hex bytes))
pure (VerifiedCompanionBytes artifact (CompanionRef digest (BS.length bytes)) bytes)
verifyRetainedCompanion :: CompanionRef -> ByteString -> Either MirrorStateFault ByteString
verifyRetainedCompanion :: CompanionRef -> ByteString -> Either MirrorStateFault ByteString
verifyRetainedCompanion (CompanionRef Hash
digest Int
size) ByteString
bytes = do
Bool -> Either MirrorStateFault () -> Either MirrorStateFault ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int
size Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString -> Int
BS.length ByteString
bytes Bool -> Bool -> Bool
&& Hash -> Text
hashValue Hash
digest Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString -> Text
sha256Hex ByteString
bytes) (MirrorStateFault -> Either MirrorStateFault ()
forall a b. a -> Either a b
Left (Text -> MirrorStateFault
StateInvalid Text
"retained companion identity mismatch"))
ByteString -> Either MirrorStateFault ByteString
forall a. a -> Either MirrorStateFault a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ByteString
bytes
retainCompanionReference :: Version -> VerifiedCompanionBytes -> CompanionRef -> ProjectMirrorRecord -> Either MirrorStateFault ProjectMirrorRecord
retainCompanionReference :: Version
-> VerifiedCompanionBytes
-> CompanionRef
-> ProjectMirrorRecord
-> Either MirrorStateFault ProjectMirrorRecord
retainCompanionReference Version
version (VerifiedCompanionBytes Artifact
parent CompanionRef
verified ByteString
_) CompanionRef
reference ProjectMirrorRecord
record = do
Bool -> Either MirrorStateFault () -> Either MirrorStateFault ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (CompanionRef
verified CompanionRef -> CompanionRef -> Bool
forall a. Eq a => a -> a -> Bool
== CompanionRef
reference) (MirrorStateFault -> Either MirrorStateFault ()
forall a b. a -> Either a b
Left (Text -> MirrorStateFault
StateCompanionConflict (Artifact -> Text
artFilename Artifact
parent)))
old <- MirrorStateFault
-> Maybe MirrorVersion -> Either MirrorStateFault MirrorVersion
forall l r. l -> Maybe r -> Either l r
maybeToRight MirrorStateFault
StateMissingCompanion (Version -> ProjectMirrorRecord -> Maybe MirrorVersion
recordVersion Version
version ProjectMirrorRecord
record)
file <- maybeToRight StateMissingCompanion (Map.lookup (artFilename parent) (versionFiles old))
unless (expectedArtifact file == parent) (Left (StateCompanionConflict (artFilename parent)))
retained <- maybeToRight StateMissingCompanion (Map.lookup (artFilename parent) (versionCompanions old))
case retained of
CompanionRetained CompanionRef
existing | CompanionRef
existing CompanionRef -> CompanionRef -> Bool
forall a. Eq a => a -> a -> Bool
/= CompanionRef
reference -> MirrorStateFault -> Either MirrorStateFault ProjectMirrorRecord
forall a b. a -> Either a b
Left (Text -> MirrorStateFault
StateCompanionConflict (Artifact -> Text
artFilename Artifact
parent))
CompanionState
_ ->
let updated :: MirrorVersion
updated = MirrorVersion
old{storedCompanions = Map.insert (artFilename parent) (CompanionRetained reference) (versionCompanions old)}
in ProjectMirrorRecord -> Either MirrorStateFault ProjectMirrorRecord
forall a b. b -> Either a b
Right ProjectMirrorRecord
record{projectVersions = Map.insert (renderVersion version) updated (recordVersions record)}
retainVerifiedIdentity :: Version -> VerifiedArtifactBytes -> ProjectMirrorRecord -> Either MirrorStateFault ProjectMirrorRecord
retainVerifiedIdentity :: Version
-> VerifiedArtifactBytes
-> ProjectMirrorRecord
-> Either MirrorStateFault ProjectMirrorRecord
retainVerifiedIdentity Version
version VerifiedArtifactBytes
verified ProjectMirrorRecord
record = do
old <- MirrorStateFault
-> Maybe MirrorVersion -> Either MirrorStateFault MirrorVersion
forall l r. l -> Maybe r -> Either l r
maybeToRight (Text -> MirrorStateFault
StateInvalid Text
"missing expected version") (Version -> ProjectMirrorRecord -> Maybe MirrorVersion
recordVersion Version
version ProjectMirrorRecord
record)
let file = VerifiedArtifactBytes -> ExpectedMirrorFile
verifiedFile VerifiedArtifactBytes
verified
name = Artifact -> Text
artFilename (ExpectedMirrorFile -> Artifact
expectedArtifact ExpectedMirrorFile
file)
expected <- maybeToRight (StateFileConflict name) (Map.lookup name (versionFiles old))
unless (expectedIdentityMatches expected file) (Left (StateFileConflict name))
let digest = VerifiedArtifactBytes -> Hash
verifiedTargetHash VerifiedArtifactBytes
verified
for_ (Map.lookup name (versionVerifiedIdentities old)) $ \Hash
previous ->
Bool -> Either MirrorStateFault () -> Either MirrorStateFault ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Hash
previous Hash -> Hash -> Bool
forall a. Eq a => a -> a -> Bool
== Hash
digest) (MirrorStateFault -> Either MirrorStateFault ()
forall a b. a -> Either a b
Left (Text -> MirrorStateFault
StateFileConflict Text
name))
let updated = MirrorVersion
old{storedIdentities = Map.insert name digest (versionVerifiedIdentities old)}
pure record{projectVersions = Map.insert (renderVersion version) updated (recordVersions record)}
data MirrorCompletion = MirrorComplete | MirrorIncomplete (NonEmpty MirrorCompletionFault)
deriving stock (MirrorCompletion -> MirrorCompletion -> Bool
(MirrorCompletion -> MirrorCompletion -> Bool)
-> (MirrorCompletion -> MirrorCompletion -> Bool)
-> Eq MirrorCompletion
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MirrorCompletion -> MirrorCompletion -> Bool
== :: MirrorCompletion -> MirrorCompletion -> Bool
$c/= :: MirrorCompletion -> MirrorCompletion -> Bool
/= :: MirrorCompletion -> MirrorCompletion -> Bool
Eq, Int -> MirrorCompletion -> ShowS
[MirrorCompletion] -> ShowS
MirrorCompletion -> String
(Int -> MirrorCompletion -> ShowS)
-> (MirrorCompletion -> String)
-> ([MirrorCompletion] -> ShowS)
-> Show MirrorCompletion
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MirrorCompletion -> ShowS
showsPrec :: Int -> MirrorCompletion -> ShowS
$cshow :: MirrorCompletion -> String
show :: MirrorCompletion -> String
$cshowList :: [MirrorCompletion] -> ShowS
showList :: [MirrorCompletion] -> ShowS
Show)
data MirrorCompletionFault
= MissingMirrorFile Text
| ConflictingMirrorFile Text
| MissingMirrorProof Text
| PendingMirrorCompanion Text
deriving stock (MirrorCompletionFault -> MirrorCompletionFault -> Bool
(MirrorCompletionFault -> MirrorCompletionFault -> Bool)
-> (MirrorCompletionFault -> MirrorCompletionFault -> Bool)
-> Eq MirrorCompletionFault
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MirrorCompletionFault -> MirrorCompletionFault -> Bool
== :: MirrorCompletionFault -> MirrorCompletionFault -> Bool
$c/= :: MirrorCompletionFault -> MirrorCompletionFault -> Bool
/= :: MirrorCompletionFault -> MirrorCompletionFault -> Bool
Eq, Int -> MirrorCompletionFault -> ShowS
[MirrorCompletionFault] -> ShowS
MirrorCompletionFault -> String
(Int -> MirrorCompletionFault -> ShowS)
-> (MirrorCompletionFault -> String)
-> ([MirrorCompletionFault] -> ShowS)
-> Show MirrorCompletionFault
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MirrorCompletionFault -> ShowS
showsPrec :: Int -> MirrorCompletionFault -> ShowS
$cshow :: MirrorCompletionFault -> String
show :: MirrorCompletionFault -> String
$cshowList :: [MirrorCompletionFault] -> ShowS
showList :: [MirrorCompletionFault] -> ShowS
Show)
checkMirrorCompletion :: MirrorVersion -> [Artifact] -> MirrorCompletion
checkMirrorCompletion :: MirrorVersion -> [Artifact] -> MirrorCompletion
checkMirrorCompletion MirrorVersion
version [Artifact]
targetFiles =
MirrorCompletion
-> (NonEmpty MirrorCompletionFault -> MirrorCompletion)
-> Maybe (NonEmpty MirrorCompletionFault)
-> MirrorCompletion
forall b a. b -> (a -> b) -> Maybe a -> b
maybe MirrorCompletion
MirrorComplete NonEmpty MirrorCompletionFault -> MirrorCompletion
MirrorIncomplete ([MirrorCompletionFault] -> Maybe (NonEmpty MirrorCompletionFault)
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty ([MirrorCompletionFault]
fileFaults [MirrorCompletionFault]
-> [MirrorCompletionFault] -> [MirrorCompletionFault]
forall a. Semigroup a => a -> a -> a
<> [MirrorCompletionFault]
companionFaults))
where
current :: Map Text [Artifact]
current = ([Artifact] -> [Artifact] -> [Artifact])
-> [(Text, [Artifact])] -> Map Text [Artifact]
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
Map.fromListWith [Artifact] -> [Artifact] -> [Artifact]
forall a. Semigroup a => a -> a -> a
(<>) [(Artifact -> Text
artFilename Artifact
file, [Artifact
file]) | Artifact
file <- [Artifact]
targetFiles]
fileFaults :: [MirrorCompletionFault]
fileFaults = ((Text, ExpectedMirrorFile) -> Maybe MirrorCompletionFault)
-> [(Text, ExpectedMirrorFile)] -> [MirrorCompletionFault]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Text, ExpectedMirrorFile) -> Maybe MirrorCompletionFault
checkFile (Map Text ExpectedMirrorFile -> [(Text, ExpectedMirrorFile)]
forall k a. Map k a -> [(k, a)]
Map.toList (MirrorVersion -> Map Text ExpectedMirrorFile
versionFiles MirrorVersion
version))
checkFile :: (Text, ExpectedMirrorFile) -> Maybe MirrorCompletionFault
checkFile (Text
name, ExpectedMirrorFile
expected) = case Text -> Map Text [Artifact] -> Maybe [Artifact]
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
name Map Text [Artifact]
current of
Maybe [Artifact]
Nothing -> MirrorCompletionFault -> Maybe MirrorCompletionFault
forall a. a -> Maybe a
Just (Text -> MirrorCompletionFault
MissingMirrorFile Text
name)
Just [Artifact]
files
| (Artifact -> Bool) -> [Artifact] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (ExpectedMirrorFile -> Maybe Hash -> Artifact -> Bool
expectedTargetMatchesWith ExpectedMirrorFile
expected Maybe Hash
verified) [Artifact]
files -> Maybe MirrorCompletionFault
forall a. Maybe a
Nothing
| (Artifact -> Bool) -> [Artifact] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (ExpectedMirrorFile -> Maybe Hash -> Artifact -> Bool
targetIdentityConflict ExpectedMirrorFile
expected Maybe Hash
verified) [Artifact]
files -> MirrorCompletionFault -> Maybe MirrorCompletionFault
forall a. a -> Maybe a
Just (Text -> MirrorCompletionFault
ConflictingMirrorFile Text
name)
| Bool
otherwise -> MirrorCompletionFault -> Maybe MirrorCompletionFault
forall a. a -> Maybe a
Just (Text -> MirrorCompletionFault
MissingMirrorProof Text
name)
where
verified :: Maybe Hash
verified = Text -> Map Text Hash -> Maybe Hash
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
name (MirrorVersion -> Map Text Hash
versionVerifiedIdentities MirrorVersion
version)
companionFaults :: [MirrorCompletionFault]
companionFaults = [Text -> MirrorCompletionFault
PendingMirrorCompanion Text
name | (Text
name, CompanionState
CompanionPending) <- Map Text CompanionState -> [(Text, CompanionState)]
forall k a. Map k a -> [(k, a)]
Map.toList (MirrorVersion -> Map Text CompanionState
versionCompanions MirrorVersion
version)]
companionDigest :: CompanionRef -> Text
companionDigest :: CompanionRef -> Text
companionDigest (CompanionRef Hash
digest Int
_) = Hash -> Text
hashValue Hash
digest
companionSize :: CompanionRef -> Int
companionSize :: CompanionRef -> Int
companionSize (CompanionRef Hash
_ Int
size) = Int
size
companionBody :: VerifiedCompanionBytes -> ByteString
companionBody :: VerifiedCompanionBytes -> ByteString
companionBody (VerifiedCompanionBytes Artifact
_ CompanionRef
_ ByteString
bytes) = ByteString
bytes
targetKey :: StateTarget -> Text
targetKey :: StateTarget -> Text
targetKey (StateTarget Text
digest) = Text
digest
projectKey :: StateTarget -> PackageName -> Text
projectKey :: StateTarget -> PackageName -> Text
projectKey StateTarget
target PackageName
name = Text
"v1/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> StateTarget -> Text
targetKey StateTarget
target Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Ecosystem -> Text
ecosystemName (PackageName -> Ecosystem
pkgEcosystem PackageName
name) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ByteString -> Text
sha256Hex (Text -> ByteString
forall a b. ConvertUtf8 a b => a -> b
encodeUtf8 (ShortText -> Text
TS.toText (PackageName -> ShortText
pkgCanonical PackageName
name)))
declaredCompanion :: Artifact -> Bool
declaredCompanion :: Artifact -> Bool
declaredCompanion Artifact
artifact = case Artifact -> Maybe CoreMetadata
artCoreMetadata Artifact
artifact of
Just (MetadataAvailable Bool
available) -> Bool
available
Just (MetadataDigests Map Text Text
values) -> Bool -> Bool
not (Map Text Text -> Bool
forall k a. Map k a -> Bool
Map.null Map Text Text
values)
Maybe CoreMetadata
Nothing -> Bool
False
declaredSourceCompanion :: CompanionRequirement -> Artifact -> Bool
declaredSourceCompanion :: CompanionRequirement -> Artifact -> Bool
declaredSourceCompanion CompanionRequirement
requirement Artifact
artifact = case CompanionRequirement
requirement of
CompanionRequirement
NativeCompanions -> Bool
True
CompanionRequirement
DeclaredCompanions -> Artifact -> Bool
declaredCompanion Artifact
artifact
verifySourceCompanionBytes :: Limits -> CompanionRequirement -> ExpectedMirrorFile -> ByteString -> Either MirrorStateFault VerifiedCompanionBytes
verifySourceCompanionBytes :: Limits
-> CompanionRequirement
-> ExpectedMirrorFile
-> ByteString
-> Either MirrorStateFault VerifiedCompanionBytes
verifySourceCompanionBytes Limits
limits CompanionRequirement
requirement ExpectedMirrorFile
file ByteString
bytes = case CompanionRequirement
requirement of
CompanionRequirement
NativeCompanions -> do
Bool -> Either MirrorStateFault () -> Either MirrorStateFault ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ByteString -> Int
BS.length ByteString
bytes Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Limits -> Int
maxMetadataBytes Limits
limits) (MirrorStateFault -> Either MirrorStateFault ()
forall a b. a -> Either a b
Left (Text -> MirrorStateFault
StateInvalid Text
"companion exceeds metadata byte bound"))
digest <- (Text -> MirrorStateFault)
-> Either Text Hash -> Either MirrorStateFault Hash
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 Text -> MirrorStateFault
StateInvalid (HashAlg -> Text -> Either Text Hash
mkHash HashAlg
SHA256 (ByteString -> Text
sha256Hex ByteString
bytes))
pure (VerifiedCompanionBytes (expectedArtifact file) (CompanionRef digest (BS.length bytes)) bytes)
CompanionRequirement
DeclaredCompanions -> Limits
-> ExpectedMirrorFile
-> ByteString
-> Either MirrorStateFault VerifiedCompanionBytes
verifyCompanionBytes Limits
limits ExpectedMirrorFile
file ByteString
bytes