module Ecluse.Core.Registry.RubyGems.SourceState (
gemSourceValue,
parseGemSource,
validateGemSource,
gemSourcePublishedAt,
gemSourceDecodeCharge,
gemSourceCompanions,
) where
import Data.Aeson (Value (..), object, (.:), (.=))
import Data.Aeson.KeyMap qualified as KeyMap
import Data.Aeson.Types (Parser, withObject)
import Data.ByteArray.Encoding (Base (Base64), convertFromBase, convertToBase)
import Data.Map.Strict qualified as Map
import Data.Set qualified as Set
import Data.Time (UTCTime)
import Ecluse.Core.Package
import Ecluse.Core.Package.Entry (EntryKey (RecordEntry), distinctKeys, validRecordKey)
import Ecluse.Core.Registry (CompanionRequirement (NativeCompanions), ParseError (..))
import Ecluse.Core.Registry.RubyGems.Document
import Ecluse.Core.Registry.RubyGems.Project
import Ecluse.Core.Registry.RubyGems.Wire (decodeInfoRow)
import Ecluse.Core.Registry.WireSupport (newestUpload)
import Ecluse.Core.Server.MemoryModel (decodedDocumentCharge)
import Ecluse.Core.Server.Path (unFilename)
import Ecluse.Core.Version (renderVersion)
gemSourceCompanions :: CompanionRequirement
gemSourceCompanions :: CompanionRequirement
gemSourceCompanions = CompanionRequirement
NativeCompanions
gemSourceValue :: GemDocument -> Value
gemSourceValue :: GemDocument -> Value
gemSourceValue GemDocument
document =
[Pair] -> Value
object
[ Key
"kind" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= (Text
"rubygems" :: Text)
, Key
"format" Key -> Int -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= (Int
1 :: Int)
, Key
"name" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= PackageName -> Text
renderPackageName (GemDocument -> PackageName
gemName GemDocument
document)
, Key
"header" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ByteString -> Text
encodeBytes (GemDocument -> ByteString
gemHeader GemDocument
document)
, Key
"entries" Key -> [Value] -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= [[Pair] -> Value
object [Key
"position" Key -> Int -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= GemRow -> Int
position GemRow
row, Key
"source" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ByteString -> Text
encodeBytes (GemRow -> ByteString
grSource GemRow
row)] | GemRow
row <- GemDocument -> [GemRow]
gemRows GemDocument
document]
]
where
position :: GemRow -> Int
position GemRow
row = case GemRow -> EntryKey
grEntryKey GemRow
row of
RecordEntry Int
value -> Int
value
EntryKey
_ -> -Int
1
parseGemSource :: Value -> Parser GemDocument
parseGemSource :: Value -> Parser GemDocument
parseGemSource = String
-> (Object -> Parser GemDocument) -> Value -> Parser GemDocument
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"RubyGems source" ((Object -> Parser GemDocument) -> Value -> Parser GemDocument)
-> (Object -> Parser GemDocument) -> Value -> Parser GemDocument
forall a b. (a -> b) -> a -> b
$ \Object
fields -> do
format <- Object
fields Object -> Key -> Parser Int
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"format"
unless (format == (1 :: Int)) (fail "unsupported RubyGems source format")
name <- fields .: "name" >>= either (fail . toString . parseErrorMessage) pure . projectName
header <- fields .: "header" >>= decodeBytes
rows <-
fields .: "entries"
>>= traverse
( withObject "RubyGems source row" $ \Object
entry -> do
position <- Object
entry Object -> Key -> Parser Int
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"position"
unless (validRecordKey (RecordEntry position)) (fail "invalid RubyGems source position")
bytes <- entry .: "source" >>= decodeBytes
either (fail . show) pure (decodeInfoRow name (RecordEntry position) bytes)
)
let keys = (GemRow -> EntryKey) -> [GemRow] -> [EntryKey]
forall a b. (a -> b) -> [a] -> [b]
map GemRow -> EntryKey
grEntryKey [GemRow]
rows
unless (distinctKeys keys) (fail "duplicate RubyGems source position")
pure (GemDocument name header rows)
validateGemSource :: PackageDetails -> GemDocument -> Either Text ()
validateGemSource :: PackageDetails -> GemDocument -> Either Text ()
validateGemSource PackageDetails
details GemDocument
document = do
Bool -> Either Text () -> Either Text ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (GemDocument -> PackageName
gemName GemDocument
document PackageName -> PackageName -> Bool
forall a. Eq a => a -> a -> Bool
== PackageDetails -> PackageName
pkgName PackageDetails
details) (Text -> Either Text ()
forall a b. a -> Either a b
Left Text
"RubyGems source names another package")
let entries :: Map EntryKey GemRow
entries = [(EntryKey, GemRow)] -> Map EntryKey GemRow
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(GemRow -> EntryKey
grEntryKey GemRow
row, GemRow
row) | GemRow
row <- GemDocument -> [GemRow]
gemRows GemDocument
document]
expected :: Set EntryKey
expected = [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 Text () -> Either Text ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ((EntryKey -> Bool) -> [EntryKey] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all EntryKey -> Bool
validRecordKey (Map EntryKey GemRow -> [EntryKey]
forall k a. Map k a -> [k]
Map.keys Map EntryKey GemRow
entries)) (Text -> Either Text ()
forall a b. a -> Either a b
Left Text
"RubyGems source position is not a physical record")
Bool -> Either Text () -> Either Text ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Map EntryKey GemRow -> Set EntryKey
forall k a. Map k a -> Set k
Map.keysSet Map EntryKey GemRow
entries Set EntryKey -> Set EntryKey -> Bool
forall a. Eq a => a -> a -> Bool
== Set EntryKey
expected Bool -> Bool -> Bool
&& Map EntryKey GemRow -> Int
forall k a. Map k a -> Int
Map.size Map EntryKey GemRow
entries Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== [GemRow] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (GemDocument -> [GemRow]
gemRows GemDocument
document) Bool -> Bool -> Bool
&& Set EntryKey -> Int
forall a. Set a -> Int
Set.size Set EntryKey
expected 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)) (Text -> Either Text ()
forall a b. a -> Either a b
Left Text
"RubyGems source membership disagrees with its artifacts")
Bool -> Either Text () -> Either Text ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (PackageDetails -> CodeExecSignal
pkgInstallCode PackageDetails
details CodeExecSignal -> CodeExecSignal -> Bool
forall a. Eq a => a -> a -> Bool
== CodeExecSignal
CodeExecUnknown Bool -> Bool -> Bool
&& PackageDetails -> Availability
pkgAvailability PackageDetails
details Availability -> Availability -> Bool
forall a. Eq a => a -> a -> Bool
== Availability
Available) (Text -> Either Text ()
forall a b. a -> Either a b
Left Text
"RubyGems source has inconsistent policy facts")
NonEmpty Artifact -> (Artifact -> Either Text ()) -> Either Text ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ (PackageDetails -> NonEmpty Artifact
pkgArtifacts PackageDetails
details) ((Artifact -> Either Text ()) -> Either Text ())
-> (Artifact -> Either Text ()) -> Either Text ()
forall a b. (a -> b) -> a -> b
$ \Artifact
artifact -> do
row <- Text -> Maybe GemRow -> Either Text GemRow
forall l r. l -> Maybe r -> Either l r
maybeToRight Text
"missing RubyGems source row" (EntryKey -> Map EntryKey GemRow -> Maybe GemRow
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup (Artifact -> EntryKey
artEntryKey Artifact
artifact) Map EntryKey GemRow
entries)
reparsed <- first show (decodeInfoRow (gemName document) (grEntryKey row) (grSource row))
unless (reparsed == row) (Left "RubyGems retained row differs from its source bytes")
case (grPublishedAt row, pkgPublishedAt details) of
(Maybe UTCTime
Nothing, Just UTCTime
_) -> Text -> Either Text ()
forall a b. a -> Either a b
Left Text
"RubyGems unknown source time became a known admission time"
(Just UTCTime
published, Just UTCTime
admitted) | UTCTime
published UTCTime -> UTCTime -> Bool
forall a. Ord a => a -> a -> Bool
> UTCTime
admitted -> Text -> Either Text ()
forall a b. a -> Either a b
Left Text
"RubyGems source upload is newer than its admitted version"
(Maybe UTCTime, Maybe UTCTime)
_ -> () -> Either Text ()
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
let coordinate = GemRow -> GemCoordinate
grCoordinate GemRow
row
unless (coordinateName coordinate == pkgName details && renderVersion (coordinateVersion coordinate) == renderVersion (pkgVersion details)) (Left "RubyGems source identity disagrees with its admitted version")
unless (unFilename (coordinateFilename coordinate) == artFilename artifact && artHashes artifact == [grChecksum row] && isNothing (artCoreMetadata artifact) && isNothing (artSize artifact)) (Left "RubyGems artifact disagrees with its native source")
encodeBytes :: ByteString -> Text
encodeBytes :: ByteString -> Text
encodeBytes ByteString
bytes = ByteString -> Text
forall a b. ConvertUtf8 a b => b -> a
decodeUtf8 (Base -> ByteString -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
Base -> bin -> bout
convertToBase Base
Base64 ByteString
bytes :: ByteString)
decodeBytes :: Text -> Parser ByteString
decodeBytes :: Text -> Parser ByteString
decodeBytes = (String -> Parser ByteString)
-> (ByteString -> Parser ByteString)
-> Either String ByteString
-> Parser ByteString
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either String -> Parser ByteString
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail ByteString -> Parser ByteString
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either String ByteString -> Parser ByteString)
-> (Text -> Either String ByteString) -> Text -> Parser ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either String ByteString
sourceBytes
sourceBytes :: Text -> Either String ByteString
sourceBytes :: Text -> Either String ByteString
sourceBytes Text
encoded = Base -> ByteString -> Either String ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
Base -> bin -> Either String bout
convertFromBase Base
Base64 (Text -> ByteString
forall a b. ConvertUtf8 a b => a -> b
encodeUtf8 Text
encoded :: ByteString)
gemSourcePublishedAt :: GemDocument -> Maybe UTCTime
gemSourcePublishedAt :: GemDocument -> Maybe UTCTime
gemSourcePublishedAt GemDocument
document = case GemDocument -> [GemRow]
gemRows GemDocument
document of
[] -> Maybe UTCTime
forall a. Maybe a
Nothing
GemRow
firstRow : [GemRow]
rest -> (Maybe UTCTime -> GemRow -> Maybe UTCTime)
-> Maybe UTCTime -> [GemRow] -> Maybe UTCTime
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\Maybe UTCTime
previous GemRow
row -> Maybe UTCTime -> Maybe UTCTime -> Maybe UTCTime
newestUpload Maybe UTCTime
previous (GemRow -> Maybe UTCTime
grPublishedAt GemRow
row)) (GemRow -> Maybe UTCTime
grPublishedAt GemRow
firstRow) [GemRow]
rest
gemSourceDecodeCharge :: Value -> Integer
gemSourceDecodeCharge :: Value -> Integer
gemSourceDecodeCharge (Object Object
fields) = case Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"entries" Object
fields of
Just (Array Array
entries) -> [Integer] -> Integer
forall a (f :: * -> *). (Foldable f, Num a) => f a -> a
sum ((Value -> Integer) -> [Value] -> [Integer]
forall a b. (a -> b) -> [a] -> [b]
map Value -> Integer
entryCharge (Array -> [Value]
forall a. Vector a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Array
entries))
Maybe Value
_ -> Integer
0
where
entryCharge :: Value -> Integer
entryCharge (Object Object
entry) = case Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"source" Object
entry of
Just (String Text
encoded) -> (String -> Integer)
-> (ByteString -> Integer) -> Either String ByteString -> Integer
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Integer -> String -> Integer
forall a b. a -> b -> a
const Integer
0) (Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> (ByteString -> Int) -> ByteString -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int
decodedDocumentCharge) (Text -> Either String ByteString
sourceBytes Text
encoded)
Maybe Value
_ -> Integer
0
entryCharge Value
_ = Integer
0
gemSourceDecodeCharge Value
_ = Integer
0