-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT

{- | The pure boot pass accumulates refusals and advisories for each role.
The composition root builds only from 'ValidatedPlan'. Unvetted settings remain on 'vpSettings'.
-}
module Ecluse.Composition.Validate (
    -- * The validate phase
    ValidatedPlan (vpMounts, vpPublications, vpMirrorStores, vpPrivateCaches, vpProgressFloor, vpSettings),
    vetBoot,
    vetBootWith,
    vetMirrorCapabilities,
    vetMirrorCapabilitiesWith,

    -- * What it clears
    VettedMount (vmEcosystem, vmAdapter, vmMount, vmConfig),
    VettedPublication (vpubTarget, vpubFirstParty, vpubStaticCredential, vpubStaticPresentation, vpubMaxRequestBytes),
) where

import Data.Map.Strict qualified as Map

import Ecluse.Composition.BootError (
    Advisory (DredgerQuotaOverrideUnmatched, RemediationSkipsWithoutStore),
    BootError (
        AdvisoryDenyWithoutStore,
        DredgerChunkPauseBeneathFloor,
        DredgerQuotaScopeConflict,
        FirstPartyMissing,
        FirstPartyWithoutPrivateUpstream,
        MinProgressBytesNotPositive,
        MirrorCompanionMismatch,
        MirrorStateRequired,
        MirrorTargetWithoutPublish,
        MissingAdapter,
        ProgressWindowNotBelowServeCap,
        ProgressWindowNotPositive,
        PublicationTargetWithoutPublish,
        PublishStaticCredentialNeedsEdge,
        RuleBlockedOnMount,
        RuleRevocationUnsupported,
        UploadConfigurationRequired
    ),
 )
import Ecluse.Composition.Endpoints (
    PublicationTarget,
    VettedEndpoints (vePublicationTargets),
    vetEndpoints,
 )
import Ecluse.Composition.Maintenance.Plan (ClearedBackend, ResolveMaintenanceAdapter, overrideKey, vetPrivateCaches, vetStoreBackends)
import Ecluse.Composition.Types (BootRole, MirrorRole (MirrorOnly, ServeAndMirror, ServeOnly), pipelineRoleOf)
import Ecluse.Composition.Vet (Severity (Advise, Ignore, Refuse), Vet, byStoreRole, decided, rule)
import Ecluse.Config (
    AppConfig (cfgDredger, cfgLimits, cfgMirrorState, cfgMounts, cfgServer),
    Config (configApp, configMounts),
    DredgerSettings (drgChunkPause, drgQuotaOverrides),
    FirstParty,
    LimitsSettings (limMinProgressBytes, limProgressWindow),
    MirrorEndpoint (meUsername),
    MirrorTarget (mtUrl),
    Mount,
    MountConfig (mntFirstParty, mntMaxRequestBytes, mntMirrorTarget, mntPrivateUpstream, mntPublicationTarget),
    PrivateEndpoint (preTarget),
    PublicationEndpoint (peTarget, peToken, peUsername),
    QuotaOverride (qoQuotas, qoScope, qoWeights),
    ServerSettings (srvAuthToken),
    StoreBackend,
    StoreTag,
    Target (tgtTag, tgtUrl),
    advisorySourced,
    meTarget,
    mountAdvisoryDenials,
    mountPolicy,
    mountRegistries,
    regMirrorTarget,
    taggedKeyPath,
 )
import Ecluse.Config.Target (storePresentation)
import Ecluse.Core.Credential (ClientCredential (ClientCredential), Secret)
import Ecluse.Core.Ecosystem (Ecosystem)
import Ecluse.Core.Registry.Adapter (RegistryAdapter, adapterFor, adapterPublish, adapterRuleBlocks)
import Ecluse.Core.Registry.Adapter.Capability (
    AdapterPublish (publishIdentity, publishMirrorRequirements),
    MirrorRequirements (mirrorStateRequirement),
    UploadIdentityRequirement (NamedIdentity),
    companionsAgree,
    durableMirrorDocuments,
 )
import Ecluse.Core.Registry.Request (ServicePresentation, StoreOperation (StoreWrite), nativePresentation, sendsCredentialUsername)
import Ecluse.Core.Registry.Sweep.Types (minimumChunkPause)
import Ecluse.Core.Rules.Types (
    AllowIfRemediatesCveParams (aircOnUnavailable),
    FailureAlignment (FailNoDecision),
    PrecededRule (prRule, ruleReach),
    Rule (AllowIfRemediatesCve),
    RuleReach (AdmissionAndRevocation),
    RuleType (DenyIfDeprecatedType, DenyIfYankedType),
    ruleType,
 )
import Ecluse.Core.Security (
    ProgressFloor,
    ProgressFloorError (MinBytesNotPositive, WindowNotBelowServeCap, WindowNotPositive),
    mkProgressFloor,
    serveCapSeconds,
 )
import Ecluse.Core.Security.Egress (registryUrlText)

-- | What the pure boot pass cleared: the mounts a role may serve, the endpoints it may use, and the settings no rule vets.
data ValidatedPlan = ValidatedPlan
    { ValidatedPlan -> [VettedMount]
vpMounts :: [VettedMount]
    -- ^ Every active mount, in ascending ecosystem order, with the adapter that serves it.
    , ValidatedPlan -> Map Ecosystem VettedPublication
vpPublications :: Map Ecosystem VettedPublication
    -- ^ Each mount's cleared publish path, absent where the mount declares no target.
    , ValidatedPlan -> Map Ecosystem ClearedBackend
vpMirrorStores :: Map Ecosystem ClearedBackend
    -- ^ The backend for each store a sweep may delete from. Only @ecluse dredger@'s pass clears one.
    , ValidatedPlan -> Map Ecosystem (Maybe StoreBackend, ClearedBackend)
vpPrivateCaches :: Map Ecosystem (Maybe StoreBackend, ClearedBackend)
    -- ^ Private caches cleared for this role with their own credential plans.
    , ValidatedPlan -> ProgressFloor
vpProgressFloor :: ProgressFloor
    -- ^ The upstream progress floor the configured window and byte count make under the serve-path cap.
    , ValidatedPlan -> AppConfig
vpSettings :: AppConfig
    -- ^ The settings no rule vets. Its mounts are the raw declarations, and 'vpMounts' holds the vetted ones.
    }

-- | One active mount, paired with the adapter this build ships for its ecosystem.
data VettedMount = VettedMount
    { VettedMount -> Ecosystem
vmEcosystem :: Ecosystem
    , VettedMount -> RegistryAdapter
vmAdapter :: RegistryAdapter
    , VettedMount -> Mount
vmMount :: Mount
    , VettedMount -> MountConfig
vmConfig :: MountConfig
    }

-- | A mount's cleared publish path: the endpoint, the first-party namespaces, and the static credential the edge gate covers.
data VettedPublication = VettedPublication
    { VettedPublication -> PublicationTarget
vpubTarget :: PublicationTarget
    , VettedPublication -> FirstParty
vpubFirstParty :: FirstParty
    , VettedPublication -> Maybe ClientCredential
vpubStaticCredential :: Maybe ClientCredential
    , VettedPublication -> ServicePresentation
vpubStaticPresentation :: ServicePresentation
    -- ^ How the publication target's store asks for the static credential.
    , VettedPublication -> Maybe Int
vpubMaxRequestBytes :: Maybe Int
    }

-- | Require only capabilities this process uses. Store maintenance never opens mirror state or uploads packages.
vetMirrorCapabilities :: BootRole -> AppConfig -> Vet ()
vetMirrorCapabilities :: BootRole -> AppConfig -> Vet ()
vetMirrorCapabilities = (StoreTag -> Ecosystem -> ServicePresentation)
-> BootRole -> AppConfig -> Vet ()
vetMirrorCapabilitiesWith StoreTag -> Ecosystem -> ServicePresentation
storePresentation

-- | 'vetMirrorCapabilities' with each write target's store presentation drawn from one lookup.
vetMirrorCapabilitiesWith :: (StoreTag -> Ecosystem -> ServicePresentation) -> BootRole -> AppConfig -> Vet ()
vetMirrorCapabilitiesWith :: (StoreTag -> Ecosystem -> ServicePresentation)
-> BootRole -> AppConfig -> Vet ()
vetMirrorCapabilitiesWith StoreTag -> Ecosystem -> ServicePresentation
presentationOf BootRole
role AppConfig
app = Either [BootError] () -> Vet ()
forall a. Either [BootError] a -> Vet a
decided (Either [BootError] () -> Vet ())
-> Either [BootError] () -> Vet ()
forall a b. (a -> b) -> a -> b
$ case [BootError]
missing of
    [] -> () -> Either [BootError] ()
forall a b. b -> Either a b
Right ()
    [BootError]
errors -> [BootError] -> Either [BootError] ()
forall a b. a -> Either a b
Left [BootError]
errors
  where
    pipeline :: Maybe MirrorRole
pipeline = BootRole -> Maybe MirrorRole
pipelineRoleOf BootRole
role
    mirrorWrites :: Bool
mirrorWrites = Maybe MirrorRole
pipeline Maybe MirrorRole -> [Maybe MirrorRole] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [MirrorRole -> Maybe MirrorRole
forall a. a -> Maybe a
Just MirrorRole
ServeAndMirror, MirrorRole -> Maybe MirrorRole
forall a. a -> Maybe a
Just MirrorRole
MirrorOnly]
    clientWrites :: Bool
clientWrites = Maybe MirrorRole
pipeline Maybe MirrorRole -> [Maybe MirrorRole] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [MirrorRole -> Maybe MirrorRole
forall a. a -> Maybe a
Just MirrorRole
ServeAndMirror, MirrorRole -> Maybe MirrorRole
forall a. a -> Maybe a
Just MirrorRole
ServeOnly]
    missing :: [BootError]
missing = ((Ecosystem, MountConfig) -> [BootError])
-> [(Ecosystem, MountConfig)] -> [BootError]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Ecosystem, MountConfig) -> [BootError]
missingOnMount (Map Ecosystem MountConfig -> [(Ecosystem, MountConfig)]
forall k a. Map k a -> [(k, a)]
Map.toList (AppConfig -> Map Ecosystem MountConfig
cfgMounts AppConfig
app))
    missingOnMount :: (Ecosystem, MountConfig) -> [BootError]
missingOnMount (Ecosystem
eco, MountConfig
mount) = case Ecosystem -> Maybe RegistryAdapter
adapterFor Ecosystem
eco Maybe RegistryAdapter
-> (RegistryAdapter -> Maybe AdapterPublish)
-> Maybe AdapterPublish
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= RegistryAdapter -> Maybe AdapterPublish
adapterPublish of
        Maybe AdapterPublish
Nothing -> []
        Just AdapterPublish
publish ->
            [BootError]
-> (MirrorEndpoint -> [BootError])
-> Maybe MirrorEndpoint
-> [BootError]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (Ecosystem -> AdapterPublish -> MirrorEndpoint -> [BootError]
mirrorNeeds Ecosystem
eco AdapterPublish
publish) (MountConfig -> Maybe MirrorEndpoint
mntMirrorTarget MountConfig
mount)
                [BootError] -> [BootError] -> [BootError]
forall a. Semigroup a => a -> a -> a
<> [BootError]
-> (PublicationEndpoint -> [BootError])
-> Maybe PublicationEndpoint
-> [BootError]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (Ecosystem -> AdapterPublish -> PublicationEndpoint -> [BootError]
publicationNeeds Ecosystem
eco AdapterPublish
publish) (MountConfig -> Maybe PublicationEndpoint
mntPublicationTarget MountConfig
mount)
    mirrorNeeds :: Ecosystem -> AdapterPublish -> MirrorEndpoint -> [BootError]
mirrorNeeds Ecosystem
eco AdapterPublish
publish MirrorEndpoint
endpoint =
        [Ecosystem -> BootError
MirrorStateRequired Ecosystem
eco | Maybe MirrorDocuments -> Bool
forall a. Maybe a -> Bool
isJust (MirrorStateRequirement -> Maybe MirrorDocuments
durableMirrorDocuments (MirrorRequirements -> MirrorStateRequirement
mirrorStateRequirement (AdapterPublish -> MirrorRequirements
publishMirrorRequirements AdapterPublish
publish))), Maybe MirrorRole -> Bool
forall a. Maybe a -> Bool
isJust Maybe MirrorRole
pipeline, Maybe AdvisoryStoreUrl -> Bool
forall a. Maybe a -> Bool
isNothing (AppConfig -> Maybe AdvisoryStoreUrl
cfgMirrorState AppConfig
app)]
            [BootError] -> [BootError] -> [BootError]
forall a. Semigroup a => a -> a -> a
<> [Ecosystem -> Text -> Target -> BootError
usernameRequired Ecosystem
eco Text
"mirrorTarget" (MirrorEndpoint -> Target
meTarget MirrorEndpoint
endpoint) | AdapterPublish -> UploadIdentityRequirement
publishIdentity AdapterPublish
publish UploadIdentityRequirement -> UploadIdentityRequirement -> Bool
forall a. Eq a => a -> a -> Bool
== UploadIdentityRequirement
NamedIdentity, Bool
mirrorWrites, Ecosystem -> Target -> Bool
uploadSendsUsername Ecosystem
eco (MirrorEndpoint -> Target
meTarget MirrorEndpoint
endpoint), Maybe Text -> Bool
forall a. Maybe a -> Bool
isNothing (MirrorEndpoint -> Maybe Text
meUsername MirrorEndpoint
endpoint)]
    publicationNeeds :: Ecosystem -> AdapterPublish -> PublicationEndpoint -> [BootError]
publicationNeeds Ecosystem
eco AdapterPublish
publish PublicationEndpoint
endpoint =
        [Ecosystem -> Text -> Target -> BootError
usernameRequired Ecosystem
eco Text
"publicationTarget" (PublicationEndpoint -> Target
peTarget PublicationEndpoint
endpoint) | AdapterPublish -> UploadIdentityRequirement
publishIdentity AdapterPublish
publish UploadIdentityRequirement -> UploadIdentityRequirement -> Bool
forall a. Eq a => a -> a -> Bool
== UploadIdentityRequirement
NamedIdentity, Bool
clientWrites, Maybe Secret -> Bool
forall a. Maybe a -> Bool
isJust (PublicationEndpoint -> Maybe Secret
peToken PublicationEndpoint
endpoint), Ecosystem -> Target -> Bool
uploadSendsUsername Ecosystem
eco (PublicationEndpoint -> Target
peTarget PublicationEndpoint
endpoint), Maybe Text -> Bool
forall a. Maybe a -> Bool
isNothing (PublicationEndpoint -> Maybe Text
peUsername PublicationEndpoint
endpoint)]
    usernameRequired :: Ecosystem -> Text -> Target -> BootError
usernameRequired Ecosystem
eco Text
endpoint Target
target = Ecosystem -> Text -> BootError
UploadConfigurationRequired Ecosystem
eco (Text -> StoreTag -> Text -> Text
taggedKeyPath Text
endpoint (Target -> StoreTag
tgtTag Target
target) Text
"username")
    -- The same question the upload asks: a store that declares its write mechanism never sends a configured username.
    uploadSendsUsername :: Ecosystem -> Target -> Bool
uploadSendsUsername Ecosystem
eco Target
target = ServicePresentation -> StoreOperation -> Bool
sendsCredentialUsername (StoreTag -> Ecosystem -> ServicePresentation
presentationOf (Target -> StoreTag
tgtTag Target
target) Ecosystem
eco) StoreOperation
StoreWrite

-- | Accumulate every pure refusal and advisory for one role before constructing its plan.
vetBoot :: Config -> Vet ValidatedPlan
vetBoot :: Config -> Vet ValidatedPlan
vetBoot = (Ecosystem -> Maybe RegistryAdapter) -> Config -> Vet ValidatedPlan
vetBootWith Ecosystem -> Maybe RegistryAdapter
adapterFor

-- | Vet every adapter-dependent capability through one lookup, including absent capabilities.
vetBootWith :: ResolveMaintenanceAdapter -> Config -> Vet ValidatedPlan
vetBootWith :: (Ecosystem -> Maybe RegistryAdapter) -> Config -> Vet ValidatedPlan
vetBootWith Ecosystem -> Maybe RegistryAdapter
resolveAdapter Config
config =
    [VettedMount]
-> Map
     Ecosystem
     (FirstParty, Maybe ClientCredential, ServicePresentation,
      Maybe Int)
-> VettedEndpoints
-> Map Ecosystem ClearedBackend
-> Map Ecosystem (Maybe StoreBackend, ClearedBackend)
-> ProgressFloor
-> ValidatedPlan
assemble
        ([VettedMount]
 -> Map
      Ecosystem
      (FirstParty, Maybe ClientCredential, ServicePresentation,
       Maybe Int)
 -> VettedEndpoints
 -> Map Ecosystem ClearedBackend
 -> Map Ecosystem (Maybe StoreBackend, ClearedBackend)
 -> ProgressFloor
 -> ValidatedPlan)
-> Vet [VettedMount]
-> Vet
     (Map
        Ecosystem
        (FirstParty, Maybe ClientCredential, ServicePresentation,
         Maybe Int)
      -> VettedEndpoints
      -> Map Ecosystem ClearedBackend
      -> Map Ecosystem (Maybe StoreBackend, ClearedBackend)
      -> ProgressFloor
      -> ValidatedPlan)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Ecosystem -> Maybe RegistryAdapter) -> Config -> Vet [VettedMount]
vetMounts Ecosystem -> Maybe RegistryAdapter
resolveAdapter Config
config
        Vet
  (Map
     Ecosystem
     (FirstParty, Maybe ClientCredential, ServicePresentation,
      Maybe Int)
   -> VettedEndpoints
   -> Map Ecosystem ClearedBackend
   -> Map Ecosystem (Maybe StoreBackend, ClearedBackend)
   -> ProgressFloor
   -> ValidatedPlan)
-> Vet
     (Map
        Ecosystem
        (FirstParty, Maybe ClientCredential, ServicePresentation,
         Maybe Int))
-> Vet
     (VettedEndpoints
      -> Map Ecosystem ClearedBackend
      -> Map Ecosystem (Maybe StoreBackend, ClearedBackend)
      -> ProgressFloor
      -> ValidatedPlan)
forall a b. Vet (a -> b) -> Vet a -> Vet b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> AppConfig
-> Vet
     (Map
        Ecosystem
        (FirstParty, Maybe ClientCredential, ServicePresentation,
         Maybe Int))
vetPublishPolicy AppConfig
app
        Vet
  (VettedEndpoints
   -> Map Ecosystem ClearedBackend
   -> Map Ecosystem (Maybe StoreBackend, ClearedBackend)
   -> ProgressFloor
   -> ValidatedPlan)
-> Vet VettedEndpoints
-> Vet
     (Map Ecosystem ClearedBackend
      -> Map Ecosystem (Maybe StoreBackend, ClearedBackend)
      -> ProgressFloor
      -> ValidatedPlan)
forall a b. Vet (a -> b) -> Vet a -> Vet b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Map Ecosystem MountConfig -> Vet VettedEndpoints
vetEndpoints (AppConfig -> Map Ecosystem MountConfig
cfgMounts AppConfig
app)
        Vet
  (Map Ecosystem ClearedBackend
   -> Map Ecosystem (Maybe StoreBackend, ClearedBackend)
   -> ProgressFloor
   -> ValidatedPlan)
-> Vet (Map Ecosystem ClearedBackend)
-> Vet
     (Map Ecosystem (Maybe StoreBackend, ClearedBackend)
      -> ProgressFloor -> ValidatedPlan)
forall a b. Vet (a -> b) -> Vet a -> Vet b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Ecosystem -> Maybe RegistryAdapter)
-> MountMap -> Vet (Map Ecosystem ClearedBackend)
vetStoreBackends Ecosystem -> Maybe RegistryAdapter
resolveAdapter (Config -> MountMap
configMounts Config
config)
        Vet
  (Map Ecosystem (Maybe StoreBackend, ClearedBackend)
   -> ProgressFloor -> ValidatedPlan)
-> Vet (Map Ecosystem (Maybe StoreBackend, ClearedBackend))
-> Vet (ProgressFloor -> ValidatedPlan)
forall a b. Vet (a -> b) -> Vet a -> Vet b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Ecosystem -> Maybe RegistryAdapter)
-> Map Ecosystem MountConfig
-> MountMap
-> Vet (Map Ecosystem (Maybe StoreBackend, ClearedBackend))
vetPrivateCaches Ecosystem -> Maybe RegistryAdapter
resolveAdapter (AppConfig -> Map Ecosystem MountConfig
cfgMounts AppConfig
app) (Config -> MountMap
configMounts Config
config)
        Vet (ProgressFloor -> ValidatedPlan)
-> Vet ProgressFloor -> Vet ValidatedPlan
forall a b. Vet (a -> b) -> Vet a -> Vet b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> AppConfig -> Vet ProgressFloor
vetProgressFloor AppConfig
app
        Vet ValidatedPlan -> Vet () -> Vet ValidatedPlan
forall a b. Vet a -> Vet b -> Vet a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* AppConfig -> Vet ()
vetSweepPacing AppConfig
app
        Vet ValidatedPlan -> Vet () -> Vet ValidatedPlan
forall a b. Vet a -> Vet b -> Vet a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Config -> Vet ()
vetAdvisoryStore Config
config
        Vet ValidatedPlan -> Vet () -> Vet ValidatedPlan
forall a b. Vet a -> Vet b -> Vet a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Config -> Vet ()
vetRemediationStore Config
config
        Vet ValidatedPlan -> Vet () -> Vet ValidatedPlan
forall a b. Vet a -> Vet b -> Vet a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* (Ecosystem -> Maybe RegistryAdapter) -> Config -> Vet ()
vetRuleBlocks Ecosystem -> Maybe RegistryAdapter
resolveAdapter Config
config
        Vet ValidatedPlan -> Vet () -> Vet ValidatedPlan
forall a b. Vet a -> Vet b -> Vet a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Config -> Vet ()
vetQuotaOverrides Config
config
        Vet ValidatedPlan -> Vet () -> Vet ValidatedPlan
forall a b. Vet a -> Vet b -> Vet a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* AppConfig -> Vet ()
vetQuotaScopes AppConfig
app
  where
    app :: AppConfig
app = Config -> AppConfig
configApp Config
config

    assemble :: [VettedMount]
-> Map
     Ecosystem
     (FirstParty, Maybe ClientCredential, ServicePresentation,
      Maybe Int)
-> VettedEndpoints
-> Map Ecosystem ClearedBackend
-> Map Ecosystem (Maybe StoreBackend, ClearedBackend)
-> ProgressFloor
-> ValidatedPlan
assemble [VettedMount]
mounts Map
  Ecosystem
  (FirstParty, Maybe ClientCredential, ServicePresentation,
   Maybe Int)
policies VettedEndpoints
endpoints Map Ecosystem ClearedBackend
backends Map Ecosystem (Maybe StoreBackend, ClearedBackend)
caches ProgressFloor
progress =
        ValidatedPlan
            { vpMounts :: [VettedMount]
vpMounts = [VettedMount]
mounts
            , vpPublications :: Map Ecosystem VettedPublication
vpPublications = (PublicationTarget
 -> (FirstParty, Maybe ClientCredential, ServicePresentation,
     Maybe Int)
 -> VettedPublication)
-> Map Ecosystem PublicationTarget
-> Map
     Ecosystem
     (FirstParty, Maybe ClientCredential, ServicePresentation,
      Maybe Int)
-> Map Ecosystem VettedPublication
forall k a b c.
Ord k =>
(a -> b -> c) -> Map k a -> Map k b -> Map k c
Map.intersectionWith PublicationTarget
-> (FirstParty, Maybe ClientCredential, ServicePresentation,
    Maybe Int)
-> VettedPublication
cleared (VettedEndpoints -> Map Ecosystem PublicationTarget
vePublicationTargets VettedEndpoints
endpoints) Map
  Ecosystem
  (FirstParty, Maybe ClientCredential, ServicePresentation,
   Maybe Int)
policies
            , vpMirrorStores :: Map Ecosystem ClearedBackend
vpMirrorStores = Map Ecosystem ClearedBackend
backends
            , vpPrivateCaches :: Map Ecosystem (Maybe StoreBackend, ClearedBackend)
vpPrivateCaches = Map Ecosystem (Maybe StoreBackend, ClearedBackend)
caches
            , vpProgressFloor :: ProgressFloor
vpProgressFloor = ProgressFloor
progress
            , vpSettings :: AppConfig
vpSettings = AppConfig
app
            }

    cleared :: PublicationTarget
-> (FirstParty, Maybe ClientCredential, ServicePresentation,
    Maybe Int)
-> VettedPublication
cleared PublicationTarget
target (FirstParty
firstParty, Maybe ClientCredential
credential, ServicePresentation
presentation, Maybe Int
requestBytes) = PublicationTarget
-> FirstParty
-> Maybe ClientCredential
-> ServicePresentation
-> Maybe Int
-> VettedPublication
VettedPublication PublicationTarget
target FirstParty
firstParty Maybe ClientCredential
credential ServicePresentation
presentation Maybe Int
requestBytes

-- Every role dials upstreams, so every role refuses a floor the window and byte count cannot make.
vetProgressFloor :: AppConfig -> Vet ProgressFloor
vetProgressFloor :: AppConfig -> Vet ProgressFloor
vetProgressFloor AppConfig
app =
    Either [BootError] ProgressFloor -> Vet ProgressFloor
forall a. Either [BootError] a -> Vet a
decided ((NonEmpty ProgressFloorError -> [BootError])
-> Either (NonEmpty ProgressFloorError) ProgressFloor
-> Either [BootError] ProgressFloor
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 ((ProgressFloorError -> BootError)
-> [ProgressFloorError] -> [BootError]
forall a b. (a -> b) -> [a] -> [b]
map ProgressFloorError -> BootError
refusal ([ProgressFloorError] -> [BootError])
-> (NonEmpty ProgressFloorError -> [ProgressFloorError])
-> NonEmpty ProgressFloorError
-> [BootError]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty ProgressFloorError -> [ProgressFloorError]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList) (NominalDiffTime
-> NominalDiffTime
-> Int
-> Either (NonEmpty ProgressFloorError) ProgressFloor
mkProgressFloor (Int -> NominalDiffTime
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
serveCapSeconds) (Int -> NominalDiffTime
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
window) Int
minBytes))
  where
    window :: Int
window = LimitsSettings -> Int
limProgressWindow (AppConfig -> LimitsSettings
cfgLimits AppConfig
app)
    minBytes :: Int
minBytes = LimitsSettings -> Int
limMinProgressBytes (AppConfig -> LimitsSettings
cfgLimits AppConfig
app)
    refusal :: ProgressFloorError -> BootError
refusal = \case
        ProgressFloorError
WindowNotPositive -> Int -> BootError
ProgressWindowNotPositive Int
window
        ProgressFloorError
WindowNotBelowServeCap -> Int -> Int -> BootError
ProgressWindowNotBelowServeCap Int
window Int
serveCapSeconds
        ProgressFloorError
MinBytesNotPositive -> Int -> BootError
MinProgressBytesNotPositive Int
minBytes

{- The floor under the sweep's own pace. Deletion is permanent, so both store roles refuse a
pause that would sweep faster than an operator can stop it, and every other role reads none. -}
vetSweepPacing :: AppConfig -> Vet ()
vetSweepPacing :: AppConfig -> Vet ()
vetSweepPacing AppConfig
app = (RegistryRole -> Severity NominalDiffTime)
-> (NominalDiffTime -> Maybe NominalDiffTime)
-> NominalDiffTime
-> Vet ()
forall finding input.
(RegistryRole -> Severity finding)
-> (input -> Maybe finding) -> input -> Vet ()
rule RegistryRole -> Severity NominalDiffTime
severity NominalDiffTime -> Maybe NominalDiffTime
forall {f :: * -> *}.
Alternative f =>
NominalDiffTime -> f NominalDiffTime
beneathFloor (DredgerSettings -> NominalDiffTime
drgChunkPause (AppConfig -> DredgerSettings
cfgDredger AppConfig
app))
  where
    severity :: RegistryRole -> Severity NominalDiffTime
severity = Severity NominalDiffTime
-> Severity NominalDiffTime
-> RegistryRole
-> Severity NominalDiffTime
forall finding.
Severity finding
-> Severity finding -> RegistryRole -> Severity finding
byStoreRole ((NominalDiffTime -> BootError) -> Severity NominalDiffTime
forall finding. (finding -> BootError) -> Severity finding
Refuse (NominalDiffTime -> NominalDiffTime -> BootError
`DredgerChunkPauseBeneathFloor` NominalDiffTime
minimumChunkPause)) Severity NominalDiffTime
forall finding. Severity finding
Ignore

    beneathFloor :: NominalDiffTime -> f NominalDiffTime
beneathFloor NominalDiffTime
configured = NominalDiffTime
configured NominalDiffTime -> f () -> f NominalDiffTime
forall a b. a -> f b -> f a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Bool -> f ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (NominalDiffTime
configured NominalDiffTime -> NominalDiffTime -> Bool
forall a. Ord a => a -> a -> Bool
< NominalDiffTime
minimumChunkPause)

-- A rule that refuses without advisory evidence can never decide without a store, so every role
-- refuses the pairing rather than serve a mount that refuses everything.
vetAdvisoryStore :: Config -> Vet ()
vetAdvisoryStore :: Config -> Vet ()
vetAdvisoryStore Config
config =
    ((Ecosystem, Mount) -> Vet ()) -> [(Ecosystem, Mount)] -> Vet ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ ((RegistryRole -> Severity (Ecosystem, NonEmpty Text))
-> ((Ecosystem, Mount) -> Maybe (Ecosystem, NonEmpty Text))
-> (Ecosystem, Mount)
-> Vet ()
forall finding input.
(RegistryRole -> Severity finding)
-> (input -> Maybe finding) -> input -> Vet ()
rule (Severity (Ecosystem, NonEmpty Text)
-> RegistryRole -> Severity (Ecosystem, NonEmpty Text)
forall a b. a -> b -> a
const (((Ecosystem, NonEmpty Text) -> BootError)
-> Severity (Ecosystem, NonEmpty Text)
forall finding. (finding -> BootError) -> Severity finding
Refuse ((Ecosystem -> NonEmpty Text -> BootError)
-> (Ecosystem, NonEmpty Text) -> BootError
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Ecosystem -> NonEmpty Text -> BootError
AdvisoryDenyWithoutStore))) (Ecosystem, Mount) -> Maybe (Ecosystem, NonEmpty Text)
forall {a}. (a, Mount) -> Maybe (a, NonEmpty Text)
denyingWithoutStore) [(Ecosystem, Mount)]
mounts
  where
    stored :: Bool
stored = AppConfig -> Bool
advisorySourced (Config -> AppConfig
configApp Config
config)
    mounts :: [(Ecosystem, Mount)]
mounts = MountMap -> [(Ecosystem, Mount)]
forall k a. Map k a -> [(k, a)]
Map.toAscList (Config -> MountMap
configMounts Config
config)

    denyingWithoutStore :: (a, Mount) -> Maybe (a, NonEmpty Text)
denyingWithoutStore (a
eco, Mount
mount)
        | Bool
stored = Maybe (a, NonEmpty Text)
forall a. Maybe a
Nothing
        | Bool
otherwise = (,) a
eco (NonEmpty Text -> (a, NonEmpty Text))
-> Maybe (NonEmpty Text) -> Maybe (a, NonEmpty Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Text] -> Maybe (NonEmpty Text)
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty (Mount -> [Text]
mountAdvisoryDenials Mount
mount)

-- A skipping remediation rule boots without a store, but admits no fix and logs every admission's skip.
vetRemediationStore :: Config -> Vet ()
vetRemediationStore :: Config -> Vet ()
vetRemediationStore Config
config = ((Ecosystem, Mount) -> Vet ()) -> [(Ecosystem, Mount)] -> Vet ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ ((RegistryRole -> Severity Ecosystem)
-> ((Ecosystem, Mount) -> Maybe Ecosystem)
-> (Ecosystem, Mount)
-> Vet ()
forall finding input.
(RegistryRole -> Severity finding)
-> (input -> Maybe finding) -> input -> Vet ()
rule (Severity Ecosystem -> RegistryRole -> Severity Ecosystem
forall a b. a -> b -> a
const ((Ecosystem -> Advisory) -> Severity Ecosystem
forall finding. (finding -> Advisory) -> Severity finding
Advise Ecosystem -> Advisory
RemediationSkipsWithoutStore)) (Ecosystem, Mount) -> Maybe Ecosystem
forall {f :: * -> *} {a}. Alternative f => (a, Mount) -> f a
skippingWithoutStore) [(Ecosystem, Mount)]
mounts
  where
    stored :: Bool
stored = AppConfig -> Bool
advisorySourced (Config -> AppConfig
configApp Config
config)
    mounts :: [(Ecosystem, Mount)]
mounts = MountMap -> [(Ecosystem, Mount)]
forall k a. Map k a -> [(k, a)]
Map.toAscList (Config -> MountMap
configMounts Config
config)

    skippingWithoutStore :: (a, Mount) -> f a
skippingWithoutStore (a
eco, Mount
mount) = a
eco a -> f () -> f a
forall a b. a -> f b -> f a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Bool -> f ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Bool
not Bool
stored Bool -> Bool -> Bool
&& (PrecededRule -> Bool) -> [PrecededRule] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Rule -> Bool
skips (Rule -> Bool) -> (PrecededRule -> Rule) -> PrecededRule -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PrecededRule -> Rule
prRule) (Mount -> [PrecededRule]
mountPolicy Mount
mount))
    skips :: Rule -> Bool
skips = \case
        AllowIfRemediatesCve AllowIfRemediatesCveParams
params -> AllowIfRemediatesCveParams -> FailureAlignment
aircOnUnavailable AllowIfRemediatesCveParams
params FailureAlignment -> FailureAlignment -> Bool
forall a. Eq a => a -> a -> Bool
== FailureAlignment
FailNoDecision
        Rule
_ -> Bool
False

-- A blocked declaration refuses every role, even when another rule outranks it.
vetRuleBlocks :: ResolveMaintenanceAdapter -> Config -> Vet ()
vetRuleBlocks :: (Ecosystem -> Maybe RegistryAdapter) -> Config -> Vet ()
vetRuleBlocks Ecosystem -> Maybe RegistryAdapter
resolveAdapter Config
config = ((Ecosystem, (Mount, MountConfig)) -> Vet ())
-> [(Ecosystem, (Mount, MountConfig))] -> Vet ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ (Ecosystem, (Mount, MountConfig)) -> Vet ()
forall {b}. (Ecosystem, (Mount, b)) -> Vet ()
vetMountRules (Config -> [(Ecosystem, (Mount, MountConfig))]
activeMounts Config
config)
  where
    vetMountRules :: (Ecosystem, (Mount, b)) -> Vet ()
vetMountRules (Ecosystem
eco, (Mount
mount, b
_)) =
        (PrecededRule -> Vet ()) -> [PrecededRule] -> Vet ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ ((RegistryRole -> Severity BootError)
-> (PrecededRule -> Maybe BootError) -> PrecededRule -> Vet ()
forall finding input.
(RegistryRole -> Severity finding)
-> (input -> Maybe finding) -> input -> Vet ()
rule (Severity BootError -> RegistryRole -> Severity BootError
forall a b. a -> b -> a
const ((BootError -> BootError) -> Severity BootError
forall finding. (finding -> BootError) -> Severity finding
Refuse BootError -> BootError
forall a. a -> a
id)) PrecededRule -> Maybe BootError
blocked) (Mount -> [PrecededRule]
mountPolicy Mount
mount)
      where
        blocked :: PrecededRule -> Maybe BootError
blocked PrecededRule
preceded = do
            adapter <- Ecosystem -> Maybe RegistryAdapter
resolveAdapter Ecosystem
eco
            let ty = Rule -> RuleType
ruleType (PrecededRule -> Rule
prRule PrecededRule
preceded)
            case Map.lookup ty (adapterRuleBlocks adapter) of
                Just Text
reason -> BootError -> Maybe BootError
forall a. a -> Maybe a
Just (Ecosystem -> RuleType -> Text -> BootError
RuleBlockedOnMount Ecosystem
eco RuleType
ty Text
reason)
                Maybe Text
Nothing ->
                    Ecosystem -> RuleType -> BootError
RuleRevocationUnsupported Ecosystem
eco RuleType
ty
                        BootError -> Maybe () -> Maybe BootError
forall a b. a -> Maybe b -> Maybe a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (PrecededRule -> RuleReach
ruleReach PrecededRule
preceded RuleReach -> RuleReach -> Bool
forall a. Eq a => a -> a -> Bool
== RuleReach
AdmissionAndRevocation Bool -> Bool -> Bool
&& RuleType
ty RuleType -> [RuleType] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [RuleType
DenyIfDeprecatedType, RuleType
DenyIfYankedType])

{- A declared capacity that matches no store paces nothing. It advises rather than refuses,
because an endpoint renamed under a running Dredger would otherwise stop the role outright. -}
vetQuotaOverrides :: Config -> Vet ()
vetQuotaOverrides :: Config -> Vet ()
vetQuotaOverrides Config
config = (Text -> Vet ()) -> [Text] -> Vet ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ ((RegistryRole -> Severity Text)
-> (Text -> Maybe Text) -> Text -> Vet ()
forall finding input.
(RegistryRole -> Severity finding)
-> (input -> Maybe finding) -> input -> Vet ()
rule RegistryRole -> Severity Text
severity Text -> Maybe Text
forall {f :: * -> *}. Alternative f => Text -> f Text
unmatched) [Text]
declaredKeys
  where
    severity :: RegistryRole -> Severity Text
severity = Severity Text -> Severity Text -> RegistryRole -> Severity Text
forall finding.
Severity finding
-> Severity finding -> RegistryRole -> Severity finding
byStoreRole ((Text -> Advisory) -> Severity Text
forall finding. (finding -> Advisory) -> Severity finding
Advise Text -> Advisory
DredgerQuotaOverrideUnmatched) Severity Text
forall finding. Severity finding
Ignore
    declaredKeys :: [Text]
declaredKeys = Map Text QuotaOverride -> [Text]
forall k a. Map k a -> [k]
Map.keys (DredgerSettings -> Map Text QuotaOverride
drgQuotaOverrides (AppConfig -> DredgerSettings
cfgDredger (Config -> AppConfig
configApp Config
config)))
    unmatched :: Text -> f Text
unmatched Text
key = Text
key Text -> f () -> f Text
forall a b. a -> f b -> f a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Bool -> f ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Text -> Text
overrideKey Text
key Text -> [Text] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`notElem` [Text]
storeKeys)
    storeKeys :: [Text]
storeKeys = (Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Text
overrideKey (Config -> [Text]
declaredStoreUrls Config
config)

{- One capacity pool takes one definition. Two entries that name the same scope and describe it
differently refuse, because whichever the boot read last would silently set the other's rate. -}
vetQuotaScopes :: AppConfig -> Vet ()
vetQuotaScopes :: AppConfig -> Vet ()
vetQuotaScopes AppConfig
app = ((Text, (Text, QuotaOverride), (Text, QuotaOverride)) -> Vet ())
-> [(Text, (Text, QuotaOverride), (Text, QuotaOverride))] -> Vet ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ ((RegistryRole -> Severity (Text, Text, Text))
-> ((Text, (Text, QuotaOverride), (Text, QuotaOverride))
    -> Maybe (Text, Text, Text))
-> (Text, (Text, QuotaOverride), (Text, QuotaOverride))
-> Vet ()
forall finding input.
(RegistryRole -> Severity finding)
-> (input -> Maybe finding) -> input -> Vet ()
rule RegistryRole -> Severity (Text, Text, Text)
severity (Text, (Text, QuotaOverride), (Text, QuotaOverride))
-> Maybe (Text, Text, Text)
forall {a} {b} {c}.
(a, (b, QuotaOverride), (c, QuotaOverride)) -> Maybe (a, b, c)
conflicting) (((Text, QuotaOverride) -> Maybe Text)
-> [(Text, QuotaOverride)]
-> [(Text, (Text, QuotaOverride), (Text, QuotaOverride))]
forall key entry.
Ord key =>
(entry -> Maybe key) -> [entry] -> [(key, entry, entry)]
pairsBy (Text, QuotaOverride) -> Maybe Text
forall {a}. (a, QuotaOverride) -> Maybe Text
declaredScope [(Text, QuotaOverride)]
entries)
  where
    severity :: RegistryRole -> Severity (Text, Text, Text)
severity = Severity (Text, Text, Text)
-> Severity (Text, Text, Text)
-> RegistryRole
-> Severity (Text, Text, Text)
forall finding.
Severity finding
-> Severity finding -> RegistryRole -> Severity finding
byStoreRole (((Text, Text, Text) -> BootError) -> Severity (Text, Text, Text)
forall finding. (finding -> BootError) -> Severity finding
Refuse (\(Text
scope, Text
first', Text
second') -> Text -> Text -> Text -> BootError
DredgerQuotaScopeConflict Text
scope Text
first' Text
second')) Severity (Text, Text, Text)
forall finding. Severity finding
Ignore
    entries :: [(Text, QuotaOverride)]
entries = Map Text QuotaOverride -> [(Text, QuotaOverride)]
forall k a. Map k a -> [(k, a)]
Map.toAscList (DredgerSettings -> Map Text QuotaOverride
drgQuotaOverrides (AppConfig -> DredgerSettings
cfgDredger AppConfig
app))
    declaredScope :: (a, QuotaOverride) -> Maybe Text
declaredScope (a
_, QuotaOverride
override) = QuotaOverride -> Maybe Text
qoScope QuotaOverride
override
    conflicting :: (a, (b, QuotaOverride), (c, QuotaOverride)) -> Maybe (a, b, c)
conflicting (a
scope, (b
leftKey, QuotaOverride
left), (c
rightKey, QuotaOverride
right))
        | QuotaOverride
-> (Map QuotaDimension Rational, Map RequestKind Rational)
describes QuotaOverride
left (Map QuotaDimension Rational, Map RequestKind Rational)
-> (Map QuotaDimension Rational, Map RequestKind Rational) -> Bool
forall a. Eq a => a -> a -> Bool
== QuotaOverride
-> (Map QuotaDimension Rational, Map RequestKind Rational)
describes QuotaOverride
right = Maybe (a, b, c)
forall a. Maybe a
Nothing
        | Bool
otherwise = (a, b, c) -> Maybe (a, b, c)
forall a. a -> Maybe a
Just (a
scope, b
leftKey, c
rightKey)
    describes :: QuotaOverride
-> (Map QuotaDimension Rational, Map RequestKind Rational)
describes QuotaOverride
override = (QuotaOverride -> Map QuotaDimension Rational
qoQuotas QuotaOverride
override, QuotaOverride -> Map RequestKind Rational
qoWeights QuotaOverride
override)

-- Every pair of entries sharing one declared key, so a rule reads two definitions at once.
pairsBy :: (Ord key) => (entry -> Maybe key) -> [entry] -> [(key, entry, entry)]
pairsBy :: forall key entry.
Ord key =>
(entry -> Maybe key) -> [entry] -> [(key, entry, entry)]
pairsBy entry -> Maybe key
keyOf [entry]
entries =
    [ (key
key, entry
left, entry
right)
    | (key
key, [entry]
grouped) <- Map key [entry] -> [(key, [entry])]
forall k a. Map k a -> [(k, a)]
Map.toAscList (([entry] -> [entry] -> [entry])
-> [(key, [entry])] -> Map key [entry]
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
Map.fromListWith [entry] -> [entry] -> [entry]
forall a. Semigroup a => a -> a -> a
(<>) [(key
key, [entry
entry]) | entry
entry <- [entry]
entries, Just key
key <- [entry -> Maybe key
keyOf entry
entry]])
    , (entry
left, entry
right) <- [entry] -> [(entry, entry)]
forall {b}. [b] -> [(b, b)]
adjacent ([entry] -> [entry]
forall a. [a] -> [a]
reverse [entry]
grouped)
    ]
  where
    adjacent :: [b] -> [(b, b)]
adjacent [b]
values = [b] -> [b] -> [(b, b)]
forall a b. [a] -> [b] -> [(a, b)]
zip [b]
values (Int -> [b] -> [b]
forall a. Int -> [a] -> [a]
drop Int
1 [b]
values)

-- Every store URL a mount declares as a sweep target: its mirror target and its private cache.
declaredStoreUrls :: Config -> [Text]
declaredStoreUrls :: Config -> [Text]
declaredStoreUrls Config
config =
    [RegistryUrl -> Text
registryUrlText (MirrorTarget -> RegistryUrl
mtUrl MirrorTarget
target) | Mount
mount <- MountMap -> [Mount]
forall k a. Map k a -> [a]
Map.elems (Config -> MountMap
configMounts Config
config), Just MirrorTarget
target <- [MountRegistries -> Maybe MirrorTarget
regMirrorTarget (Mount -> MountRegistries
mountRegistries Mount
mount)]]
        [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> [RegistryUrl -> Text
registryUrlText (Target -> RegistryUrl
tgtUrl (PrivateEndpoint -> Target
preTarget PrivateEndpoint
endpoint)) | MountConfig
mcfg <- Map Ecosystem MountConfig -> [MountConfig]
forall k a. Map k a -> [a]
Map.elems (AppConfig -> Map Ecosystem MountConfig
cfgMounts (Config -> AppConfig
configApp Config
config)), Just PrivateEndpoint
endpoint <- [MountConfig -> Maybe PrivateEndpoint
mntPrivateUpstream MountConfig
mcfg]]

vetMounts :: ResolveMaintenanceAdapter -> Config -> Vet [VettedMount]
vetMounts :: (Ecosystem -> Maybe RegistryAdapter) -> Config -> Vet [VettedMount]
vetMounts Ecosystem -> Maybe RegistryAdapter
resolveAdapter Config
config = [Maybe VettedMount] -> [VettedMount]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe VettedMount] -> [VettedMount])
-> Vet [Maybe VettedMount] -> Vet [VettedMount]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Ecosystem, (Mount, MountConfig)) -> Vet (Maybe VettedMount))
-> [(Ecosystem, (Mount, MountConfig))] -> Vet [Maybe VettedMount]
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) -> [a] -> f [b]
traverse ((Ecosystem -> Maybe RegistryAdapter)
-> (Ecosystem, (Mount, MountConfig)) -> Vet (Maybe VettedMount)
vetMount Ecosystem -> Maybe RegistryAdapter
resolveAdapter) (Config -> [(Ecosystem, (Mount, MountConfig))]
activeMounts Config
config)

{- 'Ecluse.Config.loadConfig' derives 'configMounts' from 'cfgMounts' entry for entry, so the two
maps share a keyset and this pairing is total. -}
activeMounts :: Config -> [(Ecosystem, (Mount, MountConfig))]
activeMounts :: Config -> [(Ecosystem, (Mount, MountConfig))]
activeMounts Config
config =
    Map Ecosystem (Mount, MountConfig)
-> [(Ecosystem, (Mount, MountConfig))]
forall k a. Map k a -> [(k, a)]
Map.toAscList ((Mount -> MountConfig -> (Mount, MountConfig))
-> MountMap
-> Map Ecosystem MountConfig
-> Map Ecosystem (Mount, MountConfig)
forall k a b c.
Ord k =>
(a -> b -> c) -> Map k a -> Map k b -> Map k c
Map.intersectionWith (,) (Config -> MountMap
configMounts Config
config) (AppConfig -> Map Ecosystem MountConfig
cfgMounts (Config -> AppConfig
configApp Config
config)))

-- 'Nothing' only where the rule refused, and a refused pass yields no plan to carry it into.
vetMount :: ResolveMaintenanceAdapter -> (Ecosystem, (Mount, MountConfig)) -> Vet (Maybe VettedMount)
vetMount :: (Ecosystem -> Maybe RegistryAdapter)
-> (Ecosystem, (Mount, MountConfig)) -> Vet (Maybe VettedMount)
vetMount Ecosystem -> Maybe RegistryAdapter
resolveAdapter (Ecosystem
eco, (Mount
mount, MountConfig
mcfg)) =
    Maybe VettedMount
vetted
        Maybe VettedMount -> Vet () -> Vet (Maybe VettedMount)
forall a b. a -> Vet b -> Vet a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (RegistryRole -> Severity Ecosystem)
-> (Ecosystem -> Maybe Ecosystem) -> Ecosystem -> Vet ()
forall finding input.
(RegistryRole -> Severity finding)
-> (input -> Maybe finding) -> input -> Vet ()
rule (Severity Ecosystem -> RegistryRole -> Severity Ecosystem
forall a b. a -> b -> a
const ((Ecosystem -> BootError) -> Severity Ecosystem
forall finding. (finding -> BootError) -> Severity finding
Refuse Ecosystem -> BootError
MissingAdapter)) Ecosystem -> Maybe Ecosystem
unservedEcosystem Ecosystem
eco
        Vet (Maybe VettedMount) -> Vet () -> Vet (Maybe VettedMount)
forall a b. Vet a -> Vet b -> Vet a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* (RegistryRole -> Severity Ecosystem)
-> (Ecosystem -> Maybe Ecosystem) -> Ecosystem -> Vet ()
forall finding input.
(RegistryRole -> Severity finding)
-> (input -> Maybe finding) -> input -> Vet ()
rule (Severity Ecosystem -> RegistryRole -> Severity Ecosystem
forall a b. a -> b -> a
const ((Ecosystem -> BootError) -> Severity Ecosystem
forall finding. (finding -> BootError) -> Severity finding
Refuse Ecosystem -> BootError
MirrorTargetWithoutPublish)) (Bool -> Ecosystem -> Maybe Ecosystem
declaredWithoutPublish Bool
mirrors) Ecosystem
eco
        Vet (Maybe VettedMount) -> Vet () -> Vet (Maybe VettedMount)
forall a b. Vet a -> Vet b -> Vet a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* (RegistryRole -> Severity Ecosystem)
-> (Ecosystem -> Maybe Ecosystem) -> Ecosystem -> Vet ()
forall finding input.
(RegistryRole -> Severity finding)
-> (input -> Maybe finding) -> input -> Vet ()
rule (Severity Ecosystem -> RegistryRole -> Severity Ecosystem
forall a b. a -> b -> a
const ((Ecosystem -> BootError) -> Severity Ecosystem
forall finding. (finding -> BootError) -> Severity finding
Refuse Ecosystem -> BootError
MirrorCompanionMismatch)) (Bool -> Ecosystem -> Maybe Ecosystem
mismatchedCompanions Bool
mirrors) Ecosystem
eco
        Vet (Maybe VettedMount) -> Vet () -> Vet (Maybe VettedMount)
forall a b. Vet a -> Vet b -> Vet a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* (RegistryRole -> Severity Ecosystem)
-> (Ecosystem -> Maybe Ecosystem) -> Ecosystem -> Vet ()
forall finding input.
(RegistryRole -> Severity finding)
-> (input -> Maybe finding) -> input -> Vet ()
rule (Severity Ecosystem -> RegistryRole -> Severity Ecosystem
forall a b. a -> b -> a
const ((Ecosystem -> BootError) -> Severity Ecosystem
forall finding. (finding -> BootError) -> Severity finding
Refuse Ecosystem -> BootError
PublicationTargetWithoutPublish)) (Bool -> Ecosystem -> Maybe Ecosystem
declaredWithoutPublish Bool
publishes) Ecosystem
eco
        Vet (Maybe VettedMount) -> Vet () -> Vet (Maybe VettedMount)
forall a b. Vet a -> Vet b -> Vet a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* (RegistryRole -> Severity Ecosystem)
-> ((Ecosystem, MountConfig) -> Maybe Ecosystem)
-> (Ecosystem, MountConfig)
-> Vet ()
forall finding input.
(RegistryRole -> Severity finding)
-> (input -> Maybe finding) -> input -> Vet ()
rule (Severity Ecosystem -> RegistryRole -> Severity Ecosystem
forall a b. a -> b -> a
const ((Ecosystem -> BootError) -> Severity Ecosystem
forall finding. (finding -> BootError) -> Severity finding
Refuse Ecosystem -> BootError
FirstPartyWithoutPrivateUpstream)) (Ecosystem, MountConfig) -> Maybe Ecosystem
firstPartyWithoutPrivateUpstream (Ecosystem
eco, MountConfig
mcfg)
  where
    vetted :: Maybe VettedMount
vetted = Ecosystem -> Maybe RegistryAdapter
resolveAdapter Ecosystem
eco Maybe RegistryAdapter
-> (RegistryAdapter -> VettedMount) -> Maybe VettedMount
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \RegistryAdapter
adapter -> Ecosystem -> RegistryAdapter -> Mount -> MountConfig -> VettedMount
VettedMount Ecosystem
eco RegistryAdapter
adapter Mount
mount MountConfig
mcfg

    unservedEcosystem :: Ecosystem -> Maybe Ecosystem
unservedEcosystem Ecosystem
e
        | Maybe RegistryAdapter -> Bool
forall a. Maybe a -> Bool
isNothing (Ecosystem -> Maybe RegistryAdapter
resolveAdapter Ecosystem
e) = Ecosystem -> Maybe Ecosystem
forall a. a -> Maybe a
Just Ecosystem
e
        | Bool
otherwise = Maybe Ecosystem
forall a. Maybe a
Nothing

    mirrors :: Bool
mirrors = Maybe MirrorTarget -> Bool
forall a. Maybe a -> Bool
isJust (MountRegistries -> Maybe MirrorTarget
regMirrorTarget (Mount -> MountRegistries
mountRegistries Mount
mount))
    publishes :: Bool
publishes = Maybe PublicationEndpoint -> Bool
forall a. Maybe a -> Bool
isJust (MountConfig -> Maybe PublicationEndpoint
mntPublicationTarget MountConfig
mcfg)

    declaredWithoutPublish :: Bool -> Ecosystem -> Maybe Ecosystem
declaredWithoutPublish Bool
declared Ecosystem
e = do
        Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
declared
        adapter <- Ecosystem -> Maybe RegistryAdapter
resolveAdapter Ecosystem
e
        guard (isNothing (adapterPublish adapter))
        pure e

    mismatchedCompanions :: Bool -> Ecosystem -> Maybe Ecosystem
mismatchedCompanions Bool
declared Ecosystem
e = do
        Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
declared
        publish <- Ecosystem -> Maybe RegistryAdapter
resolveAdapter Ecosystem
e Maybe RegistryAdapter
-> (RegistryAdapter -> Maybe AdapterPublish)
-> Maybe AdapterPublish
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= RegistryAdapter -> Maybe AdapterPublish
adapterPublish
        guard (not (companionsAgree publish))
        pure e

firstPartyWithoutPrivateUpstream :: (Ecosystem, MountConfig) -> Maybe Ecosystem
firstPartyWithoutPrivateUpstream :: (Ecosystem, MountConfig) -> Maybe Ecosystem
firstPartyWithoutPrivateUpstream (Ecosystem
eco, MountConfig
mcfg) =
    Ecosystem
eco Ecosystem -> Maybe () -> Maybe Ecosystem
forall a b. a -> Maybe b -> Maybe a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Maybe FirstParty -> Bool
forall a. Maybe a -> Bool
isJust (MountConfig -> Maybe FirstParty
mntFirstParty MountConfig
mcfg) Bool -> Bool -> Bool
&& Maybe PrivateEndpoint -> Bool
forall a. Maybe a -> Bool
isNothing (MountConfig -> Maybe PrivateEndpoint
mntPrivateUpstream MountConfig
mcfg))

{- The two couplings a declared publication target carries: the first-party namespaces the
guard enforces, and the inbound edge a static publish credential needs. -}
vetPublishPolicy :: AppConfig -> Vet (Map Ecosystem (FirstParty, Maybe ClientCredential, ServicePresentation, Maybe Int))
vetPublishPolicy :: AppConfig
-> Vet
     (Map
        Ecosystem
        (FirstParty, Maybe ClientCredential, ServicePresentation,
         Maybe Int))
vetPublishPolicy AppConfig
app =
    [(Ecosystem,
  (FirstParty, Maybe ClientCredential, ServicePresentation,
   Maybe Int))]
-> Map
     Ecosystem
     (FirstParty, Maybe ClientCredential, ServicePresentation,
      Maybe Int)
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(Ecosystem,
   (FirstParty, Maybe ClientCredential, ServicePresentation,
    Maybe Int))]
 -> Map
      Ecosystem
      (FirstParty, Maybe ClientCredential, ServicePresentation,
       Maybe Int))
-> ([Maybe
       (Ecosystem,
        (FirstParty, Maybe ClientCredential, ServicePresentation,
         Maybe Int))]
    -> [(Ecosystem,
         (FirstParty, Maybe ClientCredential, ServicePresentation,
          Maybe Int))])
-> [Maybe
      (Ecosystem,
       (FirstParty, Maybe ClientCredential, ServicePresentation,
        Maybe Int))]
-> Map
     Ecosystem
     (FirstParty, Maybe ClientCredential, ServicePresentation,
      Maybe Int)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Maybe
   (Ecosystem,
    (FirstParty, Maybe ClientCredential, ServicePresentation,
     Maybe Int))]
-> [(Ecosystem,
     (FirstParty, Maybe ClientCredential, ServicePresentation,
      Maybe Int))]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe
    (Ecosystem,
     (FirstParty, Maybe ClientCredential, ServicePresentation,
      Maybe Int))]
 -> Map
      Ecosystem
      (FirstParty, Maybe ClientCredential, ServicePresentation,
       Maybe Int))
-> Vet
     [Maybe
        (Ecosystem,
         (FirstParty, Maybe ClientCredential, ServicePresentation,
          Maybe Int))]
-> Vet
     (Map
        Ecosystem
        (FirstParty, Maybe ClientCredential, ServicePresentation,
         Maybe Int))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Ecosystem, MountConfig)
 -> Vet
      (Maybe
         (Ecosystem,
          (FirstParty, Maybe ClientCredential, ServicePresentation,
           Maybe Int))))
-> [(Ecosystem, MountConfig)]
-> Vet
     [Maybe
        (Ecosystem,
         (FirstParty, Maybe ClientCredential, ServicePresentation,
          Maybe Int))]
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) -> [a] -> f [b]
traverse (Maybe Secret
-> (Ecosystem, MountConfig)
-> Vet
     (Maybe
        (Ecosystem,
         (FirstParty, Maybe ClientCredential, ServicePresentation,
          Maybe Int)))
vetPublication (ServerSettings -> Maybe Secret
srvAuthToken (AppConfig -> ServerSettings
cfgServer AppConfig
app))) [(Ecosystem, MountConfig)]
publishingMounts
  where
    publishingMounts :: [(Ecosystem, MountConfig)]
publishingMounts =
        [ (Ecosystem
eco, MountConfig
mcfg)
        | (Ecosystem
eco, MountConfig
mcfg) <- Map Ecosystem MountConfig -> [(Ecosystem, MountConfig)]
forall k a. Map k a -> [(k, a)]
Map.toAscList (AppConfig -> Map Ecosystem MountConfig
cfgMounts AppConfig
app)
        , Maybe PublicationEndpoint -> Bool
forall a. Maybe a -> Bool
isJust (MountConfig -> Maybe PublicationEndpoint
mntPublicationTarget MountConfig
mcfg)
        ]

vetPublication :: Maybe Secret -> (Ecosystem, MountConfig) -> Vet (Maybe (Ecosystem, (FirstParty, Maybe ClientCredential, ServicePresentation, Maybe Int)))
vetPublication :: Maybe Secret
-> (Ecosystem, MountConfig)
-> Vet
     (Maybe
        (Ecosystem,
         (FirstParty, Maybe ClientCredential, ServicePresentation,
          Maybe Int)))
vetPublication Maybe Secret
inboundToken subject :: (Ecosystem, MountConfig)
subject@(Ecosystem
eco, MountConfig
mcfg) =
    Maybe
  (Ecosystem,
   (FirstParty, Maybe ClientCredential, ServicePresentation,
    Maybe Int))
cleared
        Maybe
  (Ecosystem,
   (FirstParty, Maybe ClientCredential, ServicePresentation,
    Maybe Int))
-> Vet ()
-> Vet
     (Maybe
        (Ecosystem,
         (FirstParty, Maybe ClientCredential, ServicePresentation,
          Maybe Int)))
forall a b. a -> Vet b -> Vet a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (RegistryRole -> Severity Ecosystem)
-> ((Ecosystem, MountConfig) -> Maybe Ecosystem)
-> (Ecosystem, MountConfig)
-> Vet ()
forall finding input.
(RegistryRole -> Severity finding)
-> (input -> Maybe finding) -> input -> Vet ()
rule (Severity Ecosystem -> RegistryRole -> Severity Ecosystem
forall a b. a -> b -> a
const ((Ecosystem -> BootError) -> Severity Ecosystem
forall finding. (finding -> BootError) -> Severity finding
Refuse Ecosystem -> BootError
FirstPartyMissing)) (Ecosystem, MountConfig) -> Maybe Ecosystem
firstPartyMissing (Ecosystem, MountConfig)
subject
        Vet
  (Maybe
     (Ecosystem,
      (FirstParty, Maybe ClientCredential, ServicePresentation,
       Maybe Int)))
-> Vet ()
-> Vet
     (Maybe
        (Ecosystem,
         (FirstParty, Maybe ClientCredential, ServicePresentation,
          Maybe Int)))
forall a b. Vet a -> Vet b -> Vet a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* (RegistryRole -> Severity (Ecosystem, StoreTag))
-> ((Ecosystem, MountConfig) -> Maybe (Ecosystem, StoreTag))
-> (Ecosystem, MountConfig)
-> Vet ()
forall finding input.
(RegistryRole -> Severity finding)
-> (input -> Maybe finding) -> input -> Vet ()
rule (Severity (Ecosystem, StoreTag)
-> RegistryRole -> Severity (Ecosystem, StoreTag)
forall a b. a -> b -> a
const (((Ecosystem, StoreTag) -> BootError)
-> Severity (Ecosystem, StoreTag)
forall finding. (finding -> BootError) -> Severity finding
Refuse ((Ecosystem -> StoreTag -> BootError)
-> (Ecosystem, StoreTag) -> BootError
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Ecosystem -> StoreTag -> BootError
PublishStaticCredentialNeedsEdge))) (Maybe Secret
-> (Ecosystem, MountConfig) -> Maybe (Ecosystem, StoreTag)
staticWithoutEdge Maybe Secret
inboundToken) (Ecosystem, MountConfig)
subject
  where
    cleared :: Maybe
  (Ecosystem,
   (FirstParty, Maybe ClientCredential, ServicePresentation,
    Maybe Int))
cleared = MountConfig -> Maybe FirstParty
mntFirstParty MountConfig
mcfg Maybe FirstParty
-> (FirstParty
    -> (Ecosystem,
        (FirstParty, Maybe ClientCredential, ServicePresentation,
         Maybe Int)))
-> Maybe
     (Ecosystem,
      (FirstParty, Maybe ClientCredential, ServicePresentation,
       Maybe Int))
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \FirstParty
firstParty -> (Ecosystem
eco, (FirstParty
firstParty, MountConfig -> Maybe ClientCredential
publicationCredential MountConfig
mcfg, Ecosystem -> MountConfig -> ServicePresentation
publicationPresentation Ecosystem
eco MountConfig
mcfg, MountConfig -> Maybe Int
mntMaxRequestBytes MountConfig
mcfg))

publicationCredential :: MountConfig -> Maybe ClientCredential
publicationCredential :: MountConfig -> Maybe ClientCredential
publicationCredential MountConfig
mount = do
    endpoint <- MountConfig -> Maybe PublicationEndpoint
mntPublicationTarget MountConfig
mount
    token <- peToken endpoint
    pure (ClientCredential (peUsername endpoint) token)

-- The publication target's store decides how a static credential travels, as it does for the mirror worker's token.
publicationPresentation :: Ecosystem -> MountConfig -> ServicePresentation
publicationPresentation :: Ecosystem -> MountConfig -> ServicePresentation
publicationPresentation Ecosystem
eco MountConfig
mcfg = ServicePresentation
-> (PublicationEndpoint -> ServicePresentation)
-> Maybe PublicationEndpoint
-> ServicePresentation
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ServicePresentation
nativePresentation (\PublicationEndpoint
endpoint -> StoreTag -> Ecosystem -> ServicePresentation
storePresentation (Target -> StoreTag
tgtTag (PublicationEndpoint -> Target
peTarget PublicationEndpoint
endpoint)) Ecosystem
eco) (MountConfig -> Maybe PublicationEndpoint
mntPublicationTarget MountConfig
mcfg)

publicationToken :: MountConfig -> Maybe Secret
publicationToken :: MountConfig -> Maybe Secret
publicationToken MountConfig
mcfg = PublicationEndpoint -> Maybe Secret
peToken (PublicationEndpoint -> Maybe Secret)
-> Maybe PublicationEndpoint -> Maybe Secret
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< MountConfig -> Maybe PublicationEndpoint
mntPublicationTarget MountConfig
mcfg

firstPartyMissing :: (Ecosystem, MountConfig) -> Maybe Ecosystem
firstPartyMissing :: (Ecosystem, MountConfig) -> Maybe Ecosystem
firstPartyMissing (Ecosystem
eco, MountConfig
mcfg)
    | Maybe FirstParty -> Bool
forall a. Maybe a -> Bool
isNothing (MountConfig -> Maybe FirstParty
mntFirstParty MountConfig
mcfg) = Ecosystem -> Maybe Ecosystem
forall a. a -> Maybe a
Just Ecosystem
eco
    | Bool
otherwise = Maybe Ecosystem
forall a. Maybe a
Nothing

{- Any unauthenticated client could otherwise publish within scope under Écluse's own credential.
The tag rides along, because the credential's key path nests below it. -}
staticWithoutEdge :: Maybe Secret -> (Ecosystem, MountConfig) -> Maybe (Ecosystem, StoreTag)
staticWithoutEdge :: Maybe Secret
-> (Ecosystem, MountConfig) -> Maybe (Ecosystem, StoreTag)
staticWithoutEdge Maybe Secret
inboundToken (Ecosystem
eco, MountConfig
mcfg) = do
    Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Maybe Secret -> Bool
forall a. Maybe a -> Bool
isJust (MountConfig -> Maybe Secret
publicationToken MountConfig
mcfg) Bool -> Bool -> Bool
&& Maybe Secret -> Bool
forall a. Maybe a -> Bool
isNothing Maybe Secret
inboundToken)
    endpoint <- MountConfig -> Maybe PublicationEndpoint
mntPublicationTarget MountConfig
mcfg
    pure (eco, tgtTag (peTarget endpoint))