module Ecluse.Core.Registry.MirrorUpload.Internal (
MirrorReadData (..),
mirrorReadDocument,
MirrorInventory,
ExpectedMirrorFile,
VerifiedArtifactBytes,
MirrorUploadData,
checkedMirrorInventory,
restoreMirrorInventory,
restoreMirrorFragment,
splitMirrorInventory,
expectedIdentityMatches,
expectedTargetMatchesWith,
targetIdentityConflict,
inventoryDetails,
inventoryFiles,
inventoryReadData,
expectedArtifact,
expectedDescriptor,
verifyMirrorBytes,
verifiedFile,
verifiedBytes,
verifiedTargetHash,
checkedMirrorUploadData,
uploadInventory,
uploadFile,
uploadBytes,
uploadTargetHash,
uploadMetadata,
) where
import Control.Monad (foldM)
import Data.Aeson (FromJSON (parseJSON), Object, Value (Object, String))
import Data.Aeson.KeyMap qualified as KeyMap
import Data.Aeson.Types (parseEither)
import Data.Map.Strict qualified as Map
import Data.Set qualified as Set
import Ecluse.Core.Ecosystem (Ecosystem (Cargo, Npm, PyPI, RubyGems))
import Ecluse.Core.Package (Artifact (..), Hash, HashAlg (SHA256), PackageDetails (..), PackageInfo (infoVersions), PackageName, mkHash, pkgEcosystem)
import Ecluse.Core.Package.Entry (EntryKey, validEntryKey)
import Ecluse.Core.Package.Hash (canonicalHashValue, computeDigest, hexDigestText)
import Ecluse.Core.Package.Integrity (IntegrityResult (..), assertedAlg, authoritativeDigest, verifyIntegrity)
import Ecluse.Core.Registry (MirrorArtifact (..), PublishFault (PublishIntegrityMismatch, PublishSourceUnavailable))
import Ecluse.Core.Registry.CachedDocument (CachedDoc, cargoCached, npmCached, pypiSimpleCached, rubyGemsCached)
import Ecluse.Core.Registry.Cargo.Document (CargoDocument)
import Ecluse.Core.Registry.Cargo.SourceState (restrictCargoDocument, validateCargoSource)
import Ecluse.Core.Registry.Metadata (VersionDoc (..))
import Ecluse.Core.Registry.PyPI.Document (SimpleDocument, restrictSimpleFiles, simpleEnvelope, simpleFiles)
import Ecluse.Core.Registry.PyPI.Project (fileCoordinate, projectFile, projectName, projectSimpleIndex)
import Ecluse.Core.Registry.PyPI.Wire (IndexFile (ifEntryKey, ifFilename))
import Ecluse.Core.Registry.RubyGems.Document (GemDocument, restrictGemDocument)
import Ecluse.Core.Registry.RubyGems.SourceState (validateGemSource)
import Ecluse.Core.Registry.ServedDocument (uniqueEntries)
import Ecluse.Core.Server.Path (mkFilename, unFilename)
import Ecluse.Core.Version (Version, renderVersion)
data MirrorReadData
= NpmMirrorRead Object
| PyPIMirrorRead SimpleDocument
|
CargoMirrorRead CargoDocument
| RubyGemsMirrorRead GemDocument
deriving stock (MirrorReadData -> MirrorReadData -> Bool
(MirrorReadData -> MirrorReadData -> Bool)
-> (MirrorReadData -> MirrorReadData -> Bool) -> Eq MirrorReadData
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MirrorReadData -> MirrorReadData -> Bool
== :: MirrorReadData -> MirrorReadData -> Bool
$c/= :: MirrorReadData -> MirrorReadData -> Bool
/= :: MirrorReadData -> MirrorReadData -> Bool
Eq, Int -> MirrorReadData -> ShowS
[MirrorReadData] -> ShowS
MirrorReadData -> String
(Int -> MirrorReadData -> ShowS)
-> (MirrorReadData -> String)
-> ([MirrorReadData] -> ShowS)
-> Show MirrorReadData
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MirrorReadData -> ShowS
showsPrec :: Int -> MirrorReadData -> ShowS
$cshow :: MirrorReadData -> String
show :: MirrorReadData -> String
$cshowList :: [MirrorReadData] -> ShowS
showList :: [MirrorReadData] -> ShowS
Show)
mirrorReadDocument :: MirrorReadData -> CachedDoc
mirrorReadDocument :: MirrorReadData -> CachedDoc
mirrorReadDocument = \case
NpmMirrorRead Object
fields -> (Value -> CachedDoc, CachedDoc -> Maybe Value)
-> Value -> CachedDoc
forall a b. (a, b) -> a
fst (Value -> CachedDoc, CachedDoc -> Maybe Value)
npmCached (Object -> Value
Object Object
fields)
PyPIMirrorRead SimpleDocument
document -> (SimpleDocument -> CachedDoc, CachedDoc -> Maybe SimpleDocument)
-> SimpleDocument -> CachedDoc
forall a b. (a, b) -> a
fst (SimpleDocument -> CachedDoc, CachedDoc -> Maybe SimpleDocument)
pypiSimpleCached SimpleDocument
document
CargoMirrorRead CargoDocument
document -> (CargoDocument -> CachedDoc, CachedDoc -> Maybe CargoDocument)
-> CargoDocument -> CachedDoc
forall a b. (a, b) -> a
fst (CargoDocument -> CachedDoc, CachedDoc -> Maybe CargoDocument)
cargoCached CargoDocument
document
RubyGemsMirrorRead GemDocument
document -> (GemDocument -> CachedDoc, CachedDoc -> Maybe GemDocument)
-> GemDocument -> CachedDoc
forall a b. (a, b) -> a
fst (GemDocument -> CachedDoc, CachedDoc -> Maybe GemDocument)
rubyGemsCached GemDocument
document
data MirrorInventory = MirrorInventory
{ MirrorInventory -> PackageDetails
miDetails :: PackageDetails
, MirrorInventory -> NonEmpty ExpectedMirrorFile
miFiles :: NonEmpty ExpectedMirrorFile
, MirrorInventory -> MirrorReadData
miReadData :: MirrorReadData
}
deriving stock (MirrorInventory -> MirrorInventory -> Bool
(MirrorInventory -> MirrorInventory -> Bool)
-> (MirrorInventory -> MirrorInventory -> Bool)
-> Eq MirrorInventory
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MirrorInventory -> MirrorInventory -> Bool
== :: MirrorInventory -> MirrorInventory -> Bool
$c/= :: MirrorInventory -> MirrorInventory -> Bool
/= :: MirrorInventory -> MirrorInventory -> Bool
Eq, Int -> MirrorInventory -> ShowS
[MirrorInventory] -> ShowS
MirrorInventory -> String
(Int -> MirrorInventory -> ShowS)
-> (MirrorInventory -> String)
-> ([MirrorInventory] -> ShowS)
-> Show MirrorInventory
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MirrorInventory -> ShowS
showsPrec :: Int -> MirrorInventory -> ShowS
$cshow :: MirrorInventory -> String
show :: MirrorInventory -> String
$cshowList :: [MirrorInventory] -> ShowS
showList :: [MirrorInventory] -> ShowS
Show)
data ExpectedMirrorFile = ExpectedMirrorFile
{ ExpectedMirrorFile -> Artifact
emfArtifact :: Artifact
, ExpectedMirrorFile -> MirrorArtifact
emfDescriptor :: MirrorArtifact
}
deriving stock (ExpectedMirrorFile -> ExpectedMirrorFile -> Bool
(ExpectedMirrorFile -> ExpectedMirrorFile -> Bool)
-> (ExpectedMirrorFile -> ExpectedMirrorFile -> Bool)
-> Eq ExpectedMirrorFile
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ExpectedMirrorFile -> ExpectedMirrorFile -> Bool
== :: ExpectedMirrorFile -> ExpectedMirrorFile -> Bool
$c/= :: ExpectedMirrorFile -> ExpectedMirrorFile -> Bool
/= :: ExpectedMirrorFile -> ExpectedMirrorFile -> Bool
Eq, Int -> ExpectedMirrorFile -> ShowS
[ExpectedMirrorFile] -> ShowS
ExpectedMirrorFile -> String
(Int -> ExpectedMirrorFile -> ShowS)
-> (ExpectedMirrorFile -> String)
-> ([ExpectedMirrorFile] -> ShowS)
-> Show ExpectedMirrorFile
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ExpectedMirrorFile -> ShowS
showsPrec :: Int -> ExpectedMirrorFile -> ShowS
$cshow :: ExpectedMirrorFile -> String
show :: ExpectedMirrorFile -> String
$cshowList :: [ExpectedMirrorFile] -> ShowS
showList :: [ExpectedMirrorFile] -> ShowS
Show)
data VerifiedArtifactBytes = VerifiedArtifactBytes
{ VerifiedArtifactBytes -> ExpectedMirrorFile
vabFile :: ExpectedMirrorFile
, VerifiedArtifactBytes -> ByteString
vabBytes :: ByteString
, VerifiedArtifactBytes -> Hash
vabTargetHash :: Hash
}
deriving stock (VerifiedArtifactBytes -> VerifiedArtifactBytes -> Bool
(VerifiedArtifactBytes -> VerifiedArtifactBytes -> Bool)
-> (VerifiedArtifactBytes -> VerifiedArtifactBytes -> Bool)
-> Eq VerifiedArtifactBytes
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: VerifiedArtifactBytes -> VerifiedArtifactBytes -> Bool
== :: VerifiedArtifactBytes -> VerifiedArtifactBytes -> Bool
$c/= :: VerifiedArtifactBytes -> VerifiedArtifactBytes -> Bool
/= :: VerifiedArtifactBytes -> VerifiedArtifactBytes -> Bool
Eq, Int -> VerifiedArtifactBytes -> ShowS
[VerifiedArtifactBytes] -> ShowS
VerifiedArtifactBytes -> String
(Int -> VerifiedArtifactBytes -> ShowS)
-> (VerifiedArtifactBytes -> String)
-> ([VerifiedArtifactBytes] -> ShowS)
-> Show VerifiedArtifactBytes
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> VerifiedArtifactBytes -> ShowS
showsPrec :: Int -> VerifiedArtifactBytes -> ShowS
$cshow :: VerifiedArtifactBytes -> String
show :: VerifiedArtifactBytes -> String
$cshowList :: [VerifiedArtifactBytes] -> ShowS
showList :: [VerifiedArtifactBytes] -> ShowS
Show)
data MirrorUploadData metadata = MirrorUploadData
{ forall metadata. MirrorUploadData metadata -> MirrorInventory
mudInventory :: MirrorInventory
, forall metadata. MirrorUploadData metadata -> ExpectedMirrorFile
mudFile :: ExpectedMirrorFile
, forall metadata. MirrorUploadData metadata -> VerifiedArtifactBytes
mudVerified :: VerifiedArtifactBytes
, forall metadata. MirrorUploadData metadata -> metadata
mudMetadata :: metadata
}
deriving stock (MirrorUploadData metadata -> MirrorUploadData metadata -> Bool
(MirrorUploadData metadata -> MirrorUploadData metadata -> Bool)
-> (MirrorUploadData metadata -> MirrorUploadData metadata -> Bool)
-> Eq (MirrorUploadData metadata)
forall metadata.
Eq metadata =>
MirrorUploadData metadata -> MirrorUploadData metadata -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall metadata.
Eq metadata =>
MirrorUploadData metadata -> MirrorUploadData metadata -> Bool
== :: MirrorUploadData metadata -> MirrorUploadData metadata -> Bool
$c/= :: forall metadata.
Eq metadata =>
MirrorUploadData metadata -> MirrorUploadData metadata -> Bool
/= :: MirrorUploadData metadata -> MirrorUploadData metadata -> Bool
Eq, Int -> MirrorUploadData metadata -> ShowS
[MirrorUploadData metadata] -> ShowS
MirrorUploadData metadata -> String
(Int -> MirrorUploadData metadata -> ShowS)
-> (MirrorUploadData metadata -> String)
-> ([MirrorUploadData metadata] -> ShowS)
-> Show (MirrorUploadData metadata)
forall metadata.
Show metadata =>
Int -> MirrorUploadData metadata -> ShowS
forall metadata.
Show metadata =>
[MirrorUploadData metadata] -> ShowS
forall metadata.
Show metadata =>
MirrorUploadData metadata -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall metadata.
Show metadata =>
Int -> MirrorUploadData metadata -> ShowS
showsPrec :: Int -> MirrorUploadData metadata -> ShowS
$cshow :: forall metadata.
Show metadata =>
MirrorUploadData metadata -> String
show :: MirrorUploadData metadata -> String
$cshowList :: forall metadata.
Show metadata =>
[MirrorUploadData metadata] -> ShowS
showList :: [MirrorUploadData metadata] -> ShowS
Show)
inventoryDetails :: MirrorInventory -> PackageDetails
inventoryDetails :: MirrorInventory -> PackageDetails
inventoryDetails = MirrorInventory -> PackageDetails
miDetails
inventoryFiles :: MirrorInventory -> NonEmpty ExpectedMirrorFile
inventoryFiles :: MirrorInventory -> NonEmpty ExpectedMirrorFile
inventoryFiles = MirrorInventory -> NonEmpty ExpectedMirrorFile
miFiles
inventoryReadData :: MirrorInventory -> MirrorReadData
inventoryReadData :: MirrorInventory -> MirrorReadData
inventoryReadData = MirrorInventory -> MirrorReadData
miReadData
expectedArtifact :: ExpectedMirrorFile -> Artifact
expectedArtifact :: ExpectedMirrorFile -> Artifact
expectedArtifact = ExpectedMirrorFile -> Artifact
emfArtifact
expectedDescriptor :: ExpectedMirrorFile -> MirrorArtifact
expectedDescriptor :: ExpectedMirrorFile -> MirrorArtifact
expectedDescriptor = ExpectedMirrorFile -> MirrorArtifact
emfDescriptor
verifiedFile :: VerifiedArtifactBytes -> ExpectedMirrorFile
verifiedFile :: VerifiedArtifactBytes -> ExpectedMirrorFile
verifiedFile = VerifiedArtifactBytes -> ExpectedMirrorFile
vabFile
verifiedBytes :: VerifiedArtifactBytes -> ByteString
verifiedBytes :: VerifiedArtifactBytes -> ByteString
verifiedBytes = VerifiedArtifactBytes -> ByteString
vabBytes
verifiedTargetHash :: VerifiedArtifactBytes -> Hash
verifiedTargetHash :: VerifiedArtifactBytes -> Hash
verifiedTargetHash = VerifiedArtifactBytes -> Hash
vabTargetHash
uploadInventory :: MirrorUploadData metadata -> MirrorInventory
uploadInventory :: forall metadata. MirrorUploadData metadata -> MirrorInventory
uploadInventory = MirrorUploadData metadata -> MirrorInventory
forall metadata. MirrorUploadData metadata -> MirrorInventory
mudInventory
uploadFile :: MirrorUploadData metadata -> ExpectedMirrorFile
uploadFile :: forall metadata. MirrorUploadData metadata -> ExpectedMirrorFile
uploadFile = MirrorUploadData metadata -> ExpectedMirrorFile
forall metadata. MirrorUploadData metadata -> ExpectedMirrorFile
mudFile
uploadMetadata :: MirrorUploadData metadata -> metadata
uploadMetadata :: forall metadata. MirrorUploadData metadata -> metadata
uploadMetadata = MirrorUploadData metadata -> metadata
forall metadata. MirrorUploadData metadata -> metadata
mudMetadata
checkedMirrorInventory :: PackageName -> Version -> VersionDoc -> MirrorReadData -> Either PublishFault MirrorInventory
checkedMirrorInventory :: PackageName
-> Version
-> VersionDoc
-> MirrorReadData
-> Either PublishFault MirrorInventory
checkedMirrorInventory PackageName
name Version
version VersionDoc
doc MirrorReadData
readData = do
Bool -> Either PublishFault () -> Either PublishFault ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (PackageDetails -> PackageName
pkgName PackageDetails
details PackageName -> PackageName -> Bool
forall a. Eq a => a -> a -> Bool
== PackageName
name Bool -> Bool -> Bool
&& PackageDetails -> Version
pkgVersion PackageDetails
details Version -> Version -> Bool
forall a. Eq a => a -> a -> Bool
== Version
version) (Either PublishFault () -> Either PublishFault ())
-> Either PublishFault () -> Either PublishFault ()
forall a b. (a -> b) -> a -> b
$
Text -> Either PublishFault ()
forall a. Text -> Either PublishFault a
refused Text
"the source snapshot belongs to another package or version"
raw <- PublishFault -> Maybe CachedDoc -> Either PublishFault CachedDoc
forall l r. l -> Maybe r -> Either l r
maybeToRight (Text -> PublishFault
PublishSourceUnavailable Text
"the source snapshot has no read metadata") (VersionDoc -> Maybe CachedDoc
vdRaw VersionDoc
doc)
unless
( case readData of
NpmMirrorRead Object
fields -> (Value -> CachedDoc, CachedDoc -> Maybe Value)
-> CachedDoc -> Maybe Value
forall a b. (a, b) -> b
snd (Value -> CachedDoc, CachedDoc -> Maybe Value)
npmCached CachedDoc
raw Maybe Value -> Maybe Value -> Bool
forall a. Eq a => a -> a -> Bool
== Value -> Maybe Value
forall a. a -> Maybe a
Just (Object -> Value
Object Object
fields)
PyPIMirrorRead SimpleDocument
document -> (SimpleDocument -> CachedDoc, CachedDoc -> Maybe SimpleDocument)
-> CachedDoc -> Maybe SimpleDocument
forall a b. (a, b) -> b
snd (SimpleDocument -> CachedDoc, CachedDoc -> Maybe SimpleDocument)
pypiSimpleCached CachedDoc
raw Maybe SimpleDocument -> Maybe SimpleDocument -> Bool
forall a. Eq a => a -> a -> Bool
== SimpleDocument -> Maybe SimpleDocument
forall a. a -> Maybe a
Just SimpleDocument
document
CargoMirrorRead CargoDocument
document -> (CargoDocument -> CachedDoc, CachedDoc -> Maybe CargoDocument)
-> CachedDoc -> Maybe CargoDocument
forall a b. (a, b) -> b
snd (CargoDocument -> CachedDoc, CachedDoc -> Maybe CargoDocument)
cargoCached CachedDoc
raw Maybe CargoDocument -> Maybe CargoDocument -> Bool
forall a. Eq a => a -> a -> Bool
== CargoDocument -> Maybe CargoDocument
forall a. a -> Maybe a
Just CargoDocument
document
RubyGemsMirrorRead GemDocument
document -> (GemDocument -> CachedDoc, CachedDoc -> Maybe GemDocument)
-> CachedDoc -> Maybe GemDocument
forall a b. (a, b) -> b
snd (GemDocument -> CachedDoc, CachedDoc -> Maybe GemDocument)
rubyGemsCached CachedDoc
raw Maybe GemDocument -> Maybe GemDocument -> Bool
forall a. Eq a => a -> a -> Bool
== GemDocument -> Maybe GemDocument
forall a. a -> Maybe a
Just GemDocument
document
)
$ refused "the read metadata is not paired with the admitted snapshot"
restoreMirrorInventory details (restrictReadData (Set.fromList (map artEntryKey (toList (pkgArtifacts details)))) readData)
where
details :: PackageDetails
details = VersionDoc -> PackageDetails
vdDetails VersionDoc
doc
restoreMirrorInventory :: PackageDetails -> MirrorReadData -> Either PublishFault MirrorInventory
restoreMirrorInventory :: PackageDetails
-> MirrorReadData -> Either PublishFault MirrorInventory
restoreMirrorInventory PackageDetails
details MirrorReadData
readData = do
files <- PackageDetails
-> MirrorReadData
-> Either PublishFault (NonEmpty ExpectedMirrorFile)
restoreMirrorFragment PackageDetails
details MirrorReadData
readData
pure (MirrorInventory details files readData)
restoreMirrorFragment :: PackageDetails -> MirrorReadData -> Either PublishFault (NonEmpty ExpectedMirrorFile)
restoreMirrorFragment :: PackageDetails
-> MirrorReadData
-> Either PublishFault (NonEmpty ExpectedMirrorFile)
restoreMirrorFragment PackageDetails
details MirrorReadData
readData = do
PackageDetails -> MirrorReadData -> Either PublishFault ()
validateReadData PackageDetails
details MirrorReadData
readData
(Artifact -> Either PublishFault ExpectedMirrorFile)
-> NonEmpty Artifact
-> Either PublishFault (NonEmpty ExpectedMirrorFile)
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) -> NonEmpty a -> f (NonEmpty b)
traverse Artifact -> Either PublishFault ExpectedMirrorFile
expectedFile (PackageDetails -> NonEmpty Artifact
pkgArtifacts PackageDetails
details) Either PublishFault (NonEmpty ExpectedMirrorFile)
-> (NonEmpty ExpectedMirrorFile
-> Either PublishFault (NonEmpty ExpectedMirrorFile))
-> Either PublishFault (NonEmpty ExpectedMirrorFile)
forall a b.
Either PublishFault a
-> (a -> Either PublishFault b) -> Either PublishFault b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= NonEmpty ExpectedMirrorFile
-> Either PublishFault (NonEmpty ExpectedMirrorFile)
deduplicate
splitMirrorInventory :: Set Text -> MirrorInventory -> Maybe (PackageDetails, MirrorReadData)
splitMirrorInventory :: Set Text
-> MirrorInventory -> Maybe (PackageDetails, MirrorReadData)
splitMirrorInventory Set Text
filenames MirrorInventory
inventory = do
selected <- [Artifact] -> Maybe (NonEmpty Artifact)
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty ((Artifact -> Bool) -> [Artifact] -> [Artifact]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Text -> Set Text -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set Text
filenames) (Text -> Bool) -> (Artifact -> Text) -> Artifact -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Artifact -> Text
artFilename) (NonEmpty Artifact -> [Artifact]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (PackageDetails -> NonEmpty Artifact
pkgArtifacts PackageDetails
details)))
let keys = [EntryKey] -> Set EntryKey
forall a. Ord a => [a] -> Set a
Set.fromList ((Artifact -> EntryKey) -> [Artifact] -> [EntryKey]
forall a b. (a -> b) -> [a] -> [b]
map Artifact -> EntryKey
artEntryKey (NonEmpty Artifact -> [Artifact]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList NonEmpty Artifact
selected))
readData = Set EntryKey -> MirrorReadData -> MirrorReadData
restrictReadData Set EntryKey
keys (MirrorInventory -> MirrorReadData
inventoryReadData MirrorInventory
inventory)
pure (details{pkgArtifacts = selected}, readData)
where
details :: PackageDetails
details = MirrorInventory -> PackageDetails
inventoryDetails MirrorInventory
inventory
restrictReadData :: Set EntryKey -> MirrorReadData -> MirrorReadData
restrictReadData :: Set EntryKey -> MirrorReadData -> MirrorReadData
restrictReadData Set EntryKey
keys = \case
NpmMirrorRead Object
fields -> Object -> MirrorReadData
NpmMirrorRead Object
fields
PyPIMirrorRead SimpleDocument
document -> SimpleDocument -> MirrorReadData
PyPIMirrorRead (Set EntryKey -> SimpleDocument -> SimpleDocument
restrictSimpleFiles Set EntryKey
keys SimpleDocument
document)
CargoMirrorRead CargoDocument
document -> CargoDocument -> MirrorReadData
CargoMirrorRead (Set EntryKey -> CargoDocument -> CargoDocument
restrictCargoDocument Set EntryKey
keys CargoDocument
document)
RubyGemsMirrorRead GemDocument
document -> GemDocument -> MirrorReadData
RubyGemsMirrorRead (Set EntryKey -> GemDocument -> GemDocument
restrictGemDocument Set EntryKey
keys GemDocument
document)
validateReadData :: PackageDetails -> MirrorReadData -> Either PublishFault ()
validateReadData :: PackageDetails -> MirrorReadData -> Either PublishFault ()
validateReadData PackageDetails
details = \case
NpmMirrorRead Object
_ -> Bool -> Either PublishFault () -> Either PublishFault ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (PackageName -> Ecosystem
pkgEcosystem (PackageDetails -> PackageName
pkgName PackageDetails
details) Ecosystem -> Ecosystem -> Bool
forall a. Eq a => a -> a -> Bool
== Ecosystem
Npm) (Text -> Either PublishFault ()
forall a. Text -> Either PublishFault a
refused Text
"the source fields belong to another ecosystem")
PyPIMirrorRead SimpleDocument
document -> do
Bool -> Either PublishFault () -> Either PublishFault ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (PackageName -> Ecosystem
pkgEcosystem (PackageDetails -> PackageName
pkgName PackageDetails
details) Ecosystem -> Ecosystem -> Bool
forall a. Eq a => a -> a -> Bool
== Ecosystem
PyPI) (Text -> Either PublishFault ()
forall a. Text -> Either PublishFault a
refused Text
"the source fields belong to another ecosystem")
let entries :: Map EntryKey Value
entries = [(EntryKey, Value)] -> Map EntryKey Value
forall key value. Ord key => [(key, value)] -> Map key value
uniqueEntries (SimpleDocument -> [(EntryKey, Value)]
simpleFiles SimpleDocument
document)
expectedKeys :: Set EntryKey
expectedKeys = [EntryKey] -> Set EntryKey
forall a. Ord a => [a] -> Set a
Set.fromList ((Artifact -> EntryKey) -> [Artifact] -> [EntryKey]
forall a b. (a -> b) -> [a] -> [b]
map Artifact -> EntryKey
artEntryKey (NonEmpty Artifact -> [Artifact]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (PackageDetails -> NonEmpty Artifact
pkgArtifacts PackageDetails
details)))
Bool -> Either PublishFault () -> Either PublishFault ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Map EntryKey Value -> Set EntryKey
forall k a. Map k a -> Set k
Map.keysSet Map EntryKey Value
entries Set EntryKey -> Set EntryKey -> Bool
forall a. Eq a => a -> a -> Bool
== Set EntryKey
expectedKeys Bool -> Bool -> Bool
&& Map EntryKey Value -> Int
forall k a. Map k a -> Int
Map.size Map EntryKey Value
entries Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== [(EntryKey, Value)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (SimpleDocument -> [(EntryKey, Value)]
simpleFiles SimpleDocument
document) Bool -> Bool -> Bool
&& Set EntryKey -> Int
forall a. Set a -> Int
Set.size Set EntryKey
expectedKeys Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== NonEmpty Artifact -> Int
forall a. NonEmpty a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (PackageDetails -> NonEmpty Artifact
pkgArtifacts PackageDetails
details)) (Either PublishFault () -> Either PublishFault ())
-> Either PublishFault () -> Either PublishFault ()
forall a b. (a -> b) -> a -> b
$
Text -> Either PublishFault ()
forall a. Text -> Either PublishFault a
refused Text
"the source coordinates do not match the complete admitted entry set"
case Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"name" (SimpleDocument -> Object
simpleEnvelope SimpleDocument
document) of
Maybe Value
Nothing -> () -> Either PublishFault ()
forall a b. b -> Either a b
Right ()
Just (String Text
name) | Text -> Either ParseError PackageName
projectName Text
name Either ParseError PackageName
-> Either ParseError PackageName -> Bool
forall a. Eq a => a -> a -> Bool
== PackageName -> Either ParseError PackageName
forall a b. b -> Either a b
Right (PackageDetails -> PackageName
pkgName PackageDetails
details) -> () -> Either PublishFault ()
forall a b. b -> Either a b
Right ()
Maybe Value
_ -> Text -> Either PublishFault ()
forall a. Text -> Either PublishFault a
refused Text
"the source envelope belongs to another package"
(Artifact -> Either PublishFault ())
-> NonEmpty Artifact -> Either PublishFault ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ (PackageDetails
-> Map EntryKey Value -> Artifact -> Either PublishFault ()
validatePyPIEntry PackageDetails
details Map EntryKey Value
entries) (PackageDetails -> NonEmpty Artifact
pkgArtifacts PackageDetails
details)
RubyGemsMirrorRead GemDocument
document -> do
Bool -> Either PublishFault () -> Either PublishFault ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (PackageName -> Ecosystem
pkgEcosystem (PackageDetails -> PackageName
pkgName PackageDetails
details) Ecosystem -> Ecosystem -> Bool
forall a. Eq a => a -> a -> Bool
== Ecosystem
RubyGems) (Text -> Either PublishFault ()
forall a. Text -> Either PublishFault a
refused Text
"the source fields belong to another ecosystem")
(Text -> PublishFault) -> Either Text () -> Either PublishFault ()
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 -> PublishFault
PublishSourceUnavailable (PackageDetails -> GemDocument -> Either Text ()
validateGemSource PackageDetails
details GemDocument
document)
CargoMirrorRead CargoDocument
document -> do
Bool -> Either PublishFault () -> Either PublishFault ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (PackageName -> Ecosystem
pkgEcosystem (PackageDetails -> PackageName
pkgName PackageDetails
details) Ecosystem -> Ecosystem -> Bool
forall a. Eq a => a -> a -> Bool
== Ecosystem
Cargo) (Text -> Either PublishFault ()
forall a. Text -> Either PublishFault a
refused Text
"the source fields belong to another ecosystem")
(Text -> PublishFault) -> Either Text () -> Either PublishFault ()
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 -> PublishFault
PublishSourceUnavailable (PackageDetails -> CargoDocument -> Either Text ()
validateCargoSource PackageDetails
details CargoDocument
document)
validatePyPIEntry :: PackageDetails -> Map EntryKey Value -> Artifact -> Either PublishFault ()
validatePyPIEntry :: PackageDetails
-> Map EntryKey Value -> Artifact -> Either PublishFault ()
validatePyPIEntry PackageDetails
details Map EntryKey Value
entries Artifact
artifact = do
raw <- PublishFault -> Maybe Value -> Either PublishFault Value
forall l r. l -> Maybe r -> Either l r
maybeToRight (Text -> PublishFault
PublishSourceUnavailable Text
"a source coordinate is missing or ambiguous") (EntryKey -> Map EntryKey Value -> Maybe Value
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup (Artifact -> EntryKey
artEntryKey Artifact
artifact) Map EntryKey Value
entries)
decoded <- first (const (PublishSourceUnavailable "an admitted source file is malformed")) (parseEither parseJSON raw)
let file = IndexFile
decoded{ifEntryKey = artEntryKey artifact}
projected = PackageName -> [InvalidEntry] -> [ProjectedFile] -> PackageInfo
projectSimpleIndex (PackageDetails -> PackageName
pkgName PackageDetails
details) [] [IndexFile -> Maybe FileCoordinate -> ProjectedFile
projectFile IndexFile
file (PackageName -> Text -> Maybe FileCoordinate
fileCoordinate (PackageDetails -> PackageName
pkgName PackageDetails
details) (IndexFile -> Text
ifFilename IndexFile
file))]
version <- maybeToRight (PublishSourceUnavailable "a source filename belongs to another package or version") (Map.lookup (renderVersion (pkgVersion details)) (infoVersions projected))
let actual :| _ = pkgArtifacts version
expected <- expectedFile artifact
actualExpected <- expectedFile actual
unless (expectedIdentityMatches expected actualExpected && actual{artUrl = artUrl artifact, artHashes = artHashes artifact, artSize = artSize artifact} == artifact) $
refused "the source file facts do not match the admitted artifact"
expectedFile :: Artifact -> Either PublishFault ExpectedMirrorFile
expectedFile :: Artifact -> Either PublishFault ExpectedMirrorFile
expectedFile Artifact
artifact = do
Bool -> Either PublishFault () -> Either PublishFault ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (EntryKey -> Bool
validEntryKey (Artifact -> EntryKey
artEntryKey Artifact
artifact)) (Text -> Either PublishFault ()
forall a. Text -> Either PublishFault a
refused Text
"an expected artifact has an invalid source coordinate")
filename <- PublishFault -> Maybe Filename -> Either PublishFault Filename
forall l r. l -> Maybe r -> Either l r
maybeToRight (Text -> PublishFault
PublishSourceUnavailable Text
"an expected artifact has an invalid filename") (Text -> Maybe Filename
mkFilename (Artifact -> Text
artFilename Artifact
artifact))
unless (all (isJust . canonicalHashValue) (artHashes artifact)) (refused "an expected artifact has a malformed digest")
hashes <- maybeToRight (PublishSourceUnavailable "an expected artifact has no integrity digest") (nonEmpty (artHashes artifact))
pure (ExpectedMirrorFile artifact (MirrorArtifact filename hashes (artSize artifact)))
deduplicate :: NonEmpty ExpectedMirrorFile -> Either PublishFault (NonEmpty ExpectedMirrorFile)
deduplicate :: NonEmpty ExpectedMirrorFile
-> Either PublishFault (NonEmpty ExpectedMirrorFile)
deduplicate (ExpectedMirrorFile
firstFile :| [ExpectedMirrorFile]
rest) = do
(_, files) <- ((Map Text ExpectedMirrorFile, [ExpectedMirrorFile])
-> ExpectedMirrorFile
-> Either
PublishFault (Map Text ExpectedMirrorFile, [ExpectedMirrorFile]))
-> (Map Text ExpectedMirrorFile, [ExpectedMirrorFile])
-> [ExpectedMirrorFile]
-> Either
PublishFault (Map Text ExpectedMirrorFile, [ExpectedMirrorFile])
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM (Map Text ExpectedMirrorFile, [ExpectedMirrorFile])
-> ExpectedMirrorFile
-> Either
PublishFault (Map Text ExpectedMirrorFile, [ExpectedMirrorFile])
add (Text -> ExpectedMirrorFile -> Map Text ExpectedMirrorFile
forall k a. k -> a -> Map k a
Map.singleton (ExpectedMirrorFile -> Text
fileName ExpectedMirrorFile
firstFile) ExpectedMirrorFile
firstFile, []) [ExpectedMirrorFile]
rest
pure (firstFile :| reverse files)
where
add :: (Map Text ExpectedMirrorFile, [ExpectedMirrorFile])
-> ExpectedMirrorFile
-> Either
PublishFault (Map Text ExpectedMirrorFile, [ExpectedMirrorFile])
add (Map Text ExpectedMirrorFile
seen, [ExpectedMirrorFile]
files) ExpectedMirrorFile
candidate = case Text -> Map Text ExpectedMirrorFile -> Maybe ExpectedMirrorFile
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup (ExpectedMirrorFile -> Text
fileName ExpectedMirrorFile
candidate) Map Text ExpectedMirrorFile
seen of
Maybe ExpectedMirrorFile
Nothing -> (Map Text ExpectedMirrorFile, [ExpectedMirrorFile])
-> Either
PublishFault (Map Text ExpectedMirrorFile, [ExpectedMirrorFile])
forall a b. b -> Either a b
Right (Text
-> ExpectedMirrorFile
-> Map Text ExpectedMirrorFile
-> Map Text ExpectedMirrorFile
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert (ExpectedMirrorFile -> Text
fileName ExpectedMirrorFile
candidate) ExpectedMirrorFile
candidate Map Text ExpectedMirrorFile
seen, ExpectedMirrorFile
candidate ExpectedMirrorFile -> [ExpectedMirrorFile] -> [ExpectedMirrorFile]
forall a. a -> [a] -> [a]
: [ExpectedMirrorFile]
files)
Just ExpectedMirrorFile
previous
| ExpectedMirrorFile -> ExpectedMirrorFile -> Bool
expectedIdentityMatches ExpectedMirrorFile
previous ExpectedMirrorFile
candidate Bool -> Bool -> Bool
&& ExpectedMirrorFile -> ExpectedMirrorFile -> Bool
sameDeclarations ExpectedMirrorFile
previous ExpectedMirrorFile
candidate -> (Map Text ExpectedMirrorFile, [ExpectedMirrorFile])
-> Either
PublishFault (Map Text ExpectedMirrorFile, [ExpectedMirrorFile])
forall a b. b -> Either a b
Right (Map Text ExpectedMirrorFile
seen, [ExpectedMirrorFile]
files)
| Bool
otherwise -> Text
-> Either
PublishFault (Map Text ExpectedMirrorFile, [ExpectedMirrorFile])
forall a. Text -> Either PublishFault a
refused (Text
"conflicting source entries for " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ExpectedMirrorFile -> Text
fileName ExpectedMirrorFile
candidate)
sameDeclarations :: ExpectedMirrorFile -> ExpectedMirrorFile -> Bool
sameDeclarations :: ExpectedMirrorFile -> ExpectedMirrorFile -> Bool
sameDeclarations ExpectedMirrorFile
left ExpectedMirrorFile
right =
Artifact
a{artEntryKey = artEntryKey b, artUrl = artUrl b, artHashes = artHashes b, artSize = artSize b} Artifact -> Artifact -> Bool
forall a. Eq a => a -> a -> Bool
== Artifact
b
where
a :: Artifact
a = ExpectedMirrorFile -> Artifact
expectedArtifact ExpectedMirrorFile
left
b :: Artifact
b = ExpectedMirrorFile -> Artifact
expectedArtifact ExpectedMirrorFile
right
fileName :: ExpectedMirrorFile -> Text
fileName :: ExpectedMirrorFile -> Text
fileName = Filename -> Text
unFilename (Filename -> Text)
-> (ExpectedMirrorFile -> Filename) -> ExpectedMirrorFile -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MirrorArtifact -> Filename
maFilename (MirrorArtifact -> Filename)
-> (ExpectedMirrorFile -> MirrorArtifact)
-> ExpectedMirrorFile
-> Filename
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ExpectedMirrorFile -> MirrorArtifact
expectedDescriptor
expectedIdentityMatches :: ExpectedMirrorFile -> ExpectedMirrorFile -> Bool
expectedIdentityMatches :: ExpectedMirrorFile -> ExpectedMirrorFile -> Bool
expectedIdentityMatches ExpectedMirrorFile
left ExpectedMirrorFile
right =
MirrorArtifact -> Filename
maFilename MirrorArtifact
a Filename -> Filename -> Bool
forall a. Eq a => a -> a -> Bool
== MirrorArtifact -> Filename
maFilename MirrorArtifact
b
Bool -> Bool -> Bool
&& Maybe Int -> Maybe Int -> Bool
compatibleSize (MirrorArtifact -> Maybe Int
maSize MirrorArtifact
a) (MirrorArtifact -> Maybe Int
maSize MirrorArtifact
b)
Bool -> Bool -> Bool
&& Map HashAlg (Set Text) -> Map HashAlg (Set Text) -> Bool
sharedDigestsAgree Map HashAlg (Set Text)
aHashes Map HashAlg (Set Text)
bHashes
Bool -> Bool -> Bool
&& case (MirrorArtifact -> Maybe HashAlg
strongestAlgorithm MirrorArtifact
a, MirrorArtifact -> Maybe HashAlg
strongestAlgorithm MirrorArtifact
b) of
(Just HashAlg
leftAlgorithm, Just HashAlg
rightAlgorithm) -> HashAlg -> Map HashAlg (Set Text) -> Bool
forall k a. Ord k => k -> Map k a -> Bool
Map.member (HashAlg -> HashAlg -> HashAlg
forall a. Ord a => a -> a -> a
min HashAlg
leftAlgorithm HashAlg
rightAlgorithm) (Map HashAlg (Set Text)
-> Map HashAlg (Set Text) -> Map HashAlg (Set Text)
forall k a b. Ord k => Map k a -> Map k b -> Map k a
Map.intersection Map HashAlg (Set Text)
aHashes Map HashAlg (Set Text)
bHashes)
(Maybe HashAlg, Maybe HashAlg)
_ -> Bool
False
where
a :: MirrorArtifact
a = ExpectedMirrorFile -> MirrorArtifact
expectedDescriptor ExpectedMirrorFile
left
b :: MirrorArtifact
b = ExpectedMirrorFile -> MirrorArtifact
expectedDescriptor ExpectedMirrorFile
right
aHashes :: Map HashAlg (Set Text)
aHashes = [Hash] -> Map HashAlg (Set Text)
digestIdentities (NonEmpty Hash -> [Hash]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (MirrorArtifact -> NonEmpty Hash
maHashes MirrorArtifact
a))
bHashes :: Map HashAlg (Set Text)
bHashes = [Hash] -> Map HashAlg (Set Text)
digestIdentities (NonEmpty Hash -> [Hash]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (MirrorArtifact -> NonEmpty Hash
maHashes MirrorArtifact
b))
expectedTargetMatchesWith :: ExpectedMirrorFile -> Maybe Hash -> Artifact -> Bool
expectedTargetMatchesWith :: ExpectedMirrorFile -> Maybe Hash -> Artifact -> Bool
expectedTargetMatchesWith ExpectedMirrorFile
expected Maybe Hash
stored Artifact
target =
Bool -> Bool
not (ExpectedMirrorFile -> Maybe Hash -> Artifact -> Bool
targetIdentityConflict ExpectedMirrorFile
expected Maybe Hash
stored Artifact
target)
Bool -> Bool -> Bool
&& Map HashAlg Bool -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or ((Set Text -> Set Text -> Bool)
-> Map HashAlg (Set Text)
-> Map HashAlg (Set Text)
-> Map HashAlg Bool
forall k a b c.
Ord k =>
(a -> b -> c) -> Map k a -> Map k b -> Map k c
Map.intersectionWith (\Set Text
allowed Set Text
actual -> Bool -> Bool
not (Set Text -> Bool
forall a. Set a -> Bool
Set.null Set Text
actual) Bool -> Bool -> Bool
&& Set Text
actual Set Text -> Set Text -> Bool
forall a. Ord a => Set a -> Set a -> Bool
`Set.isSubsetOf` Set Text
allowed) (ExpectedMirrorFile -> Maybe Hash -> Map HashAlg (Set Text)
targetProofs ExpectedMirrorFile
expected Maybe Hash
stored) ([Hash] -> Map HashAlg (Set Text)
digestIdentities (Artifact -> [Hash]
artHashes Artifact
target)))
targetIdentityConflict :: ExpectedMirrorFile -> Maybe Hash -> Artifact -> Bool
targetIdentityConflict :: ExpectedMirrorFile -> Maybe Hash -> Artifact -> Bool
targetIdentityConflict ExpectedMirrorFile
expected Maybe Hash
stored Artifact
target =
Artifact -> Text
artFilename Artifact
source Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Artifact -> Text
artFilename Artifact
target
Bool -> Bool -> Bool
|| Bool -> Bool
not (Maybe Int -> Maybe Int -> Bool
compatibleSize (Artifact -> Maybe Int
artSize Artifact
source) (Artifact -> Maybe Int
artSize Artifact
target))
Bool -> Bool -> Bool
|| Map HashAlg Bool -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or ((Set Text -> Set Text -> Bool)
-> Map HashAlg (Set Text)
-> Map HashAlg (Set Text)
-> Map HashAlg Bool
forall k a b c.
Ord k =>
(a -> b -> c) -> Map k a -> Map k b -> Map k c
Map.intersectionWith (\Set Text
allowed Set Text
actual -> Bool -> Bool
not (Set Text
actual Set Text -> Set Text -> Bool
forall a. Ord a => Set a -> Set a -> Bool
`Set.isSubsetOf` Set Text
allowed)) (ExpectedMirrorFile -> Maybe Hash -> Map HashAlg (Set Text)
targetProofs ExpectedMirrorFile
expected Maybe Hash
stored) ([Hash] -> Map HashAlg (Set Text)
digestIdentities (Artifact -> [Hash]
artHashes Artifact
target)))
where
source :: Artifact
source = ExpectedMirrorFile -> Artifact
expectedArtifact ExpectedMirrorFile
expected
targetProofs :: ExpectedMirrorFile -> Maybe Hash -> Map HashAlg (Set Text)
targetProofs :: ExpectedMirrorFile -> Maybe Hash -> Map HashAlg (Set Text)
targetProofs ExpectedMirrorFile
expected Maybe Hash
stored = case Maybe Hash
stored of
Just Hash
verifiedHash
| Hash -> Maybe HashAlg
assertedAlg Hash
verifiedHash Maybe HashAlg -> Maybe HashAlg -> Bool
forall a. Eq a => a -> a -> Bool
== HashAlg -> Maybe HashAlg
forall a. a -> Maybe a
Just HashAlg
SHA256
, Just Text
digest <- Hash -> Maybe Text
canonicalHashValue Hash
verifiedHash ->
(Set Text -> Set Text -> Set Text)
-> HashAlg
-> Set Text
-> Map HashAlg (Set Text)
-> Map HashAlg (Set Text)
forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
Map.insertWith Set Text -> Set Text -> Set Text
forall a. Ord a => Set a -> Set a -> Set a
Set.intersection HashAlg
SHA256 (Text -> Set Text
forall a. a -> Set a
Set.singleton Text
digest) Map HashAlg (Set Text)
strongest
Maybe Hash
_ -> Map HashAlg (Set Text)
strongest
where
strongest :: Map HashAlg (Set Text)
strongest = case MirrorArtifact -> Maybe HashAlg
strongestAlgorithm (ExpectedMirrorFile -> MirrorArtifact
expectedDescriptor ExpectedMirrorFile
expected) of
Maybe HashAlg
Nothing -> Map HashAlg (Set Text)
forall a. Monoid a => a
mempty
Just HashAlg
algorithm -> Map HashAlg (Set Text) -> Set HashAlg -> Map HashAlg (Set Text)
forall k a. Ord k => Map k a -> Set k -> Map k a
Map.restrictKeys ([Hash] -> Map HashAlg (Set Text)
digestIdentities (Artifact -> [Hash]
artHashes (ExpectedMirrorFile -> Artifact
expectedArtifact ExpectedMirrorFile
expected))) (HashAlg -> Set HashAlg
forall a. a -> Set a
Set.singleton HashAlg
algorithm)
strongestAlgorithm :: MirrorArtifact -> Maybe HashAlg
strongestAlgorithm :: MirrorArtifact -> Maybe HashAlg
strongestAlgorithm = Hash -> Maybe HashAlg
assertedAlg (Hash -> Maybe HashAlg)
-> (MirrorArtifact -> Hash) -> MirrorArtifact -> Maybe HashAlg
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty Hash -> Hash
authoritativeDigest (NonEmpty Hash -> Hash)
-> (MirrorArtifact -> NonEmpty Hash) -> MirrorArtifact -> Hash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MirrorArtifact -> NonEmpty Hash
maHashes
digestIdentities :: [Hash] -> Map HashAlg (Set Text)
digestIdentities :: [Hash] -> Map HashAlg (Set Text)
digestIdentities [Hash]
hashes = (Set Text -> Set Text -> Set Text)
-> [(HashAlg, Set Text)] -> Map HashAlg (Set Text)
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
Map.fromListWith Set Text -> Set Text -> Set Text
forall a. Ord a => Set a -> Set a -> Set a
Set.union [(HashAlg
algorithm, Text -> Set Text
forall a. a -> Set a
Set.singleton Text
digest) | Hash
hash <- [Hash]
hashes, Just HashAlg
algorithm <- [Hash -> Maybe HashAlg
assertedAlg Hash
hash], Just Text
digest <- [Hash -> Maybe Text
canonicalHashValue Hash
hash]]
sharedDigestsAgree :: Map HashAlg (Set Text) -> Map HashAlg (Set Text) -> Bool
sharedDigestsAgree :: Map HashAlg (Set Text) -> Map HashAlg (Set Text) -> Bool
sharedDigestsAgree Map HashAlg (Set Text)
left Map HashAlg (Set Text)
right = Map HashAlg Bool -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and ((Set Text -> Set Text -> Bool)
-> Map HashAlg (Set Text)
-> Map HashAlg (Set Text)
-> Map HashAlg Bool
forall k a b c.
Ord k =>
(a -> b -> c) -> Map k a -> Map k b -> Map k c
Map.intersectionWith Set Text -> Set Text -> Bool
forall a. Eq a => a -> a -> Bool
(==) Map HashAlg (Set Text)
left Map HashAlg (Set Text)
right)
compatibleSize :: Maybe Int -> Maybe Int -> Bool
compatibleSize :: Maybe Int -> Maybe Int -> Bool
compatibleSize (Just Int
left) (Just Int
right) = Int
left Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
right
compatibleSize Maybe Int
_ Maybe Int
_ = Bool
True
verifyMirrorBytes :: ExpectedMirrorFile -> ByteString -> Either PublishFault VerifiedArtifactBytes
verifyMirrorBytes :: ExpectedMirrorFile
-> ByteString -> Either PublishFault VerifiedArtifactBytes
verifyMirrorBytes ExpectedMirrorFile
expected ByteString
bytes = case NonEmpty Hash -> ByteString -> IntegrityResult
verifyIntegrity (MirrorArtifact -> NonEmpty Hash
maHashes (ExpectedMirrorFile -> MirrorArtifact
expectedDescriptor ExpectedMirrorFile
expected)) ByteString
bytes of
IntegrityMismatch Text
detail -> PublishFault -> Either PublishFault VerifiedArtifactBytes
forall a b. a -> Either a b
Left (Text -> PublishFault
PublishIntegrityMismatch Text
detail)
IntegrityResult
IntegrityVerified -> do
compute <- PublishFault
-> Maybe (LByteString -> ByteString)
-> Either PublishFault (LByteString -> ByteString)
forall l r. l -> Maybe r -> Either l r
maybeToRight (Text -> PublishFault
PublishIntegrityMismatch Text
"SHA-256 computation is unavailable") (HashAlg -> Maybe (LByteString -> ByteString)
computeDigest HashAlg
SHA256)
targetHash <- first PublishIntegrityMismatch (mkHash SHA256 (hexDigestText (compute (toLazy bytes))))
pure (VerifiedArtifactBytes expected bytes targetHash)
checkedMirrorUploadData :: MirrorInventory -> ExpectedMirrorFile -> VerifiedArtifactBytes -> metadata -> Either PublishFault (MirrorUploadData metadata)
checkedMirrorUploadData :: forall metadata.
MirrorInventory
-> ExpectedMirrorFile
-> VerifiedArtifactBytes
-> metadata
-> Either PublishFault (MirrorUploadData metadata)
checkedMirrorUploadData MirrorInventory
inventory ExpectedMirrorFile
file VerifiedArtifactBytes
verified metadata
metadata = do
Bool -> Either PublishFault () -> Either PublishFault ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ExpectedMirrorFile
file ExpectedMirrorFile -> NonEmpty ExpectedMirrorFile -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` MirrorInventory -> NonEmpty ExpectedMirrorFile
inventoryFiles MirrorInventory
inventory Bool -> Bool -> Bool
&& VerifiedArtifactBytes -> ExpectedMirrorFile
verifiedFile VerifiedArtifactBytes
verified ExpectedMirrorFile -> ExpectedMirrorFile -> Bool
forall a. Eq a => a -> a -> Bool
== ExpectedMirrorFile
file) (Either PublishFault () -> Either PublishFault ())
-> Either PublishFault () -> Either PublishFault ()
forall a b. (a -> b) -> a -> b
$
Text -> Either PublishFault ()
forall a. Text -> Either PublishFault a
refused Text
"the verified artifact does not belong to the complete mirror inventory"
MirrorUploadData metadata
-> Either PublishFault (MirrorUploadData metadata)
forall a. a -> Either PublishFault a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MirrorInventory
-> ExpectedMirrorFile
-> VerifiedArtifactBytes
-> metadata
-> MirrorUploadData metadata
forall metadata.
MirrorInventory
-> ExpectedMirrorFile
-> VerifiedArtifactBytes
-> metadata
-> MirrorUploadData metadata
MirrorUploadData MirrorInventory
inventory ExpectedMirrorFile
file VerifiedArtifactBytes
verified metadata
metadata)
uploadBytes :: MirrorUploadData metadata -> ByteString
uploadBytes :: forall metadata. MirrorUploadData metadata -> ByteString
uploadBytes = VerifiedArtifactBytes -> ByteString
verifiedBytes (VerifiedArtifactBytes -> ByteString)
-> (MirrorUploadData metadata -> VerifiedArtifactBytes)
-> MirrorUploadData metadata
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MirrorUploadData metadata -> VerifiedArtifactBytes
forall metadata. MirrorUploadData metadata -> VerifiedArtifactBytes
mudVerified
uploadTargetHash :: MirrorUploadData metadata -> Hash
uploadTargetHash :: forall metadata. MirrorUploadData metadata -> Hash
uploadTargetHash = VerifiedArtifactBytes -> Hash
verifiedTargetHash (VerifiedArtifactBytes -> Hash)
-> (MirrorUploadData metadata -> VerifiedArtifactBytes)
-> MirrorUploadData metadata
-> Hash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MirrorUploadData metadata -> VerifiedArtifactBytes
forall metadata. MirrorUploadData metadata -> VerifiedArtifactBytes
mudVerified
refused :: Text -> Either PublishFault a
refused :: forall a. Text -> Either PublishFault a
refused = PublishFault -> Either PublishFault a
forall a b. a -> Either a b
Left (PublishFault -> Either PublishFault a)
-> (Text -> PublishFault) -> Text -> Either PublishFault a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> PublishFault
PublishSourceUnavailable