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

-- | Pure role-specific store vetting. Only a successful pass constructs a cleared backend.
module Ecluse.Composition.Maintenance.Plan (
    ClearedBackend (cbUrl, cbAlphabet, cbFetchManifest, cbUsername, cbControl),
    ClearedControl (..),
    ClearedProtocolStore (..),
    ResolveMaintenanceAdapter,
    vetStoreBackends,
    vetPrivateCaches,
    overrideKey,
) where

import Data.Map.Strict qualified as Map
import Data.Text qualified as T

import Ecluse.Composition.BootError (
    BootError (StoreMaintenanceUnavailable),
    StoreMaintenanceReason (DeletionNotPermitted, NoControlPlane, NoProtocolMaintenance, PrivateCacheUnavailable),
 )
import Ecluse.Composition.Types (RegistryRole (MirrorPreviewer, MirrorPruner, MirrorWriter))
import Ecluse.Composition.Vet (Severity (Ignore, Refuse), Vet, byStoreRole, rule, withRole)
import Ecluse.Config (
    ControlPlane (ControlCodeArtifact, ControlNone, ControlProtocol),
    DeletionConsent (DeletionWithheld),
    MirrorTarget (mtBackend, mtUrl, mtUsername),
    Mount (mountRegistries),
    MountConfig (mntPrivateUpstream),
    MountMap,
    PrivateEndpoint (..),
    StoreBackend (BackendVerdaccio),
    StoreTag (TagCodeArtifact, TagRegistry, TagVerdaccio),
    Target (tgtTag, tgtUrl),
    regMirrorTarget,
    sbControl,
    sbTag,
    taggedKeyPath,
 )
import Ecluse.Config.Resolve (mountKeyRef)
import Ecluse.Config.Target (resolvePrivateBackend, storePresentation)
import Ecluse.Core.Credential (Secret)
import Ecluse.Core.Ecosystem (Ecosystem)
import Ecluse.Core.Fault (TransportCause (TransportProtocol), transportFault)
import Ecluse.Core.Registry (FetchFault (FetchTransport))
import Ecluse.Core.Registry.Adapter (RegistryAdapter, adapterMaintenance, adapterMetadata, adapterPublish)
import Ecluse.Core.Registry.Adapter.Capability (
    AdapterMaintenance (maintenanceAlphabet, maintenanceListing, maintenanceVersionDelete),
    AdapterMetadata (metadataRead),
    AdapterPublish (publishCodec),
    StoreListing,
    VersionDelete,
 )
import Ecluse.Core.Registry.Maintenance.NameSpace (NameAlphabet, noNameAlphabet)
import Ecluse.Core.Registry.Metadata (MetadataError (MetadataFetch))
import Ecluse.Core.Registry.Metadata.Fetch.Preparation (PrepareRead, prepareManifest)
import Ecluse.Core.Registry.Metadata.Fetch.Types (ManifestFetch)
import Ecluse.Core.Registry.Publish (PublishCodec (pcTargetRead))
import Ecluse.Core.Registry.Request (ServicePresentation)
import Ecluse.Core.Security.Egress (RegistryUrl)
import Ecluse.Runtime.Maintenance.CodeArtifact.Decide (CodeArtifactStore)

-- | A store cleared by its role-specific pass. Its constructor stays private.
data ClearedBackend = ClearedBackend
    { ClearedBackend -> RegistryUrl
cbUrl :: RegistryUrl
    -- ^ Where the store answers, which is both what a sweep reads and what it deletes from.
    , ClearedBackend -> NameAlphabet
cbAlphabet :: NameAlphabet
    -- ^ The characters a full walk partitions this store's names by.
    , ClearedBackend -> ManifestFetch
cbFetchManifest :: ManifestFetch
    -- ^ The ecosystem's metadata read, directed at this store and presenting its declared credential mechanism.
    , ClearedBackend -> Maybe Text
cbUsername :: Maybe Text
    -- ^ The target's resolved credential username, paired with each refreshed secret.
    , ClearedBackend -> ClearedControl
cbControl :: ClearedControl
    -- ^ The backend control plane.
    }

-- | The control plane a cleared store offers, one arm per backend kind.
data ClearedControl
    = -- | A CodeArtifact repository, deleted through the vendor's own control plane.
      ClearedCodeArtifact CodeArtifactStore
    | -- | The configured cache permits refill without weakening ordinary mirror classification.
      ClearedCodeArtifactCache CodeArtifactStore
    | -- | A store with no vendor control plane, deleted through the ecosystem protocol.
      ClearedProtocol ClearedProtocolStore

-- | Vetted protocol operations and target-local authentication. An absent token means anonymous reads.
data ClearedProtocolStore = ClearedProtocolStore
    { ClearedProtocolStore -> Maybe Secret
cpsToken :: Maybe Secret
    , ClearedProtocolStore -> StoreTag
cpsTag :: StoreTag
    -- ^ The tag the store was declared under, which names the backend and its consent key.
    , ClearedProtocolStore -> DeletionConsent
cpsConsent :: DeletionConsent
    -- ^ What the operator wrote under that key, which the handle's own verdict reads.
    , ClearedProtocolStore -> Text
cpsConsentKey :: Text
    , ClearedProtocolStore -> StoreListing
cpsListing :: StoreListing
    , ClearedProtocolStore -> VersionDelete
cpsDelete :: VersionDelete
    , ClearedProtocolStore -> PublishCodec
cpsCodec :: PublishCodec
    }

-- | Resolve each ecosystem to the adapter whose maintenance capabilities the pass checks.
type ResolveMaintenanceAdapter = Ecosystem -> Maybe RegistryAdapter

-- | Require a sweepable backend for both Dredger modes. Writing roles skip this check.
vetStoreBackends :: ResolveMaintenanceAdapter -> MountMap -> Vet (Map Ecosystem ClearedBackend)
vetStoreBackends :: ResolveMaintenanceAdapter
-> MountMap -> Vet (Map Ecosystem ClearedBackend)
vetStoreBackends ResolveMaintenanceAdapter
resolveAdapter MountMap
mounts = (RegistryRole -> Vet (Map Ecosystem ClearedBackend))
-> Vet (Map Ecosystem ClearedBackend)
forall a. (RegistryRole -> Vet a) -> Vet a
withRole ((RegistryRole -> Vet (Map Ecosystem ClearedBackend))
 -> Vet (Map Ecosystem ClearedBackend))
-> (RegistryRole -> Vet (Map Ecosystem ClearedBackend))
-> Vet (Map Ecosystem ClearedBackend)
forall a b. (a -> b) -> a -> b
$ \RegistryRole
role ->
    let resolved :: [(Ecosystem, Either StoreMaintenanceReason ClearedBackend)]
resolved = RegistryRole
-> [(Ecosystem, Either StoreMaintenanceReason ClearedBackend)]
resolvedFor RegistryRole
role
     in RegistryRole
-> [(Ecosystem, Either StoreMaintenanceReason ClearedBackend)]
-> Map Ecosystem ClearedBackend
forall {reason} {a}.
RegistryRole -> [(Ecosystem, Either reason a)] -> Map Ecosystem a
clearedFor RegistryRole
role [(Ecosystem, Either StoreMaintenanceReason ClearedBackend)]
resolved Map Ecosystem ClearedBackend
-> Vet () -> Vet (Map Ecosystem ClearedBackend)
forall a b. a -> Vet b -> Vet a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ((Ecosystem, Either StoreMaintenanceReason ClearedBackend)
 -> Vet ())
-> [(Ecosystem, Either StoreMaintenanceReason ClearedBackend)]
-> Vet ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ ((RegistryRole -> Severity (Ecosystem, StoreMaintenanceReason))
-> ((Ecosystem, Either StoreMaintenanceReason ClearedBackend)
    -> Maybe (Ecosystem, StoreMaintenanceReason))
-> (Ecosystem, Either StoreMaintenanceReason ClearedBackend)
-> Vet ()
forall finding input.
(RegistryRole -> Severity finding)
-> (input -> Maybe finding) -> input -> Vet ()
rule RegistryRole -> Severity (Ecosystem, StoreMaintenanceReason)
severity (Ecosystem, Either StoreMaintenanceReason ClearedBackend)
-> Maybe (Ecosystem, StoreMaintenanceReason)
forall reason a.
(Ecosystem, Either reason a) -> Maybe (Ecosystem, reason)
unmaintainedOf) [(Ecosystem, Either StoreMaintenanceReason ClearedBackend)]
resolved
  where
    resolvedFor :: RegistryRole
-> [(Ecosystem, Either StoreMaintenanceReason ClearedBackend)]
resolvedFor RegistryRole
role =
        [ (Ecosystem
eco, RegistryRole
-> Maybe RegistryAdapter
-> Ecosystem
-> MirrorTarget
-> Either StoreMaintenanceReason ClearedBackend
sweepableStore RegistryRole
role (ResolveMaintenanceAdapter
resolveAdapter Ecosystem
eco) Ecosystem
eco MirrorTarget
target)
        | (Ecosystem
eco, Mount
mount) <- MountMap -> [(Ecosystem, Mount)]
forall k a. Map k a -> [(k, a)]
Map.toAscList MountMap
mounts
        , Just MirrorTarget
target <- [MountRegistries -> Maybe MirrorTarget
regMirrorTarget (Mount -> MountRegistries
mountRegistries Mount
mount)]
        ]

    severity :: RegistryRole -> Severity (Ecosystem, StoreMaintenanceReason)
severity = Severity (Ecosystem, StoreMaintenanceReason)
-> Severity (Ecosystem, StoreMaintenanceReason)
-> RegistryRole
-> Severity (Ecosystem, StoreMaintenanceReason)
forall finding.
Severity finding
-> Severity finding -> RegistryRole -> Severity finding
byStoreRole (((Ecosystem, StoreMaintenanceReason) -> BootError)
-> Severity (Ecosystem, StoreMaintenanceReason)
forall finding. (finding -> BootError) -> Severity finding
Refuse ((Ecosystem -> StoreMaintenanceReason -> BootError)
-> (Ecosystem, StoreMaintenanceReason) -> BootError
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Ecosystem -> StoreMaintenanceReason -> BootError
StoreMaintenanceUnavailable)) Severity (Ecosystem, StoreMaintenanceReason)
forall finding. Severity finding
Ignore

    clearedFor :: RegistryRole -> [(Ecosystem, Either reason a)] -> Map Ecosystem a
clearedFor RegistryRole
role [(Ecosystem, Either reason a)]
resolved = case RegistryRole
role of
        RegistryRole
MirrorWriter -> Map Ecosystem a
forall k a. Map k a
Map.empty
        RegistryRole
MirrorPruner -> [(Ecosystem, Either reason a)] -> Map Ecosystem a
forall reason a. [(Ecosystem, Either reason a)] -> Map Ecosystem a
clearedOf [(Ecosystem, Either reason a)]
resolved
        RegistryRole
MirrorPreviewer -> [(Ecosystem, Either reason a)] -> Map Ecosystem a
forall reason a. [(Ecosystem, Either reason a)] -> Map Ecosystem a
clearedOf [(Ecosystem, Either reason a)]
resolved

-- | Vet each private cache under its own backend declaration and maintenance authority.
vetPrivateCaches :: ResolveMaintenanceAdapter -> Map Ecosystem MountConfig -> MountMap -> Vet (Map Ecosystem (Maybe StoreBackend, ClearedBackend))
vetPrivateCaches :: ResolveMaintenanceAdapter
-> Map Ecosystem MountConfig
-> MountMap
-> Vet (Map Ecosystem (Maybe StoreBackend, ClearedBackend))
vetPrivateCaches ResolveMaintenanceAdapter
resolveAdapter Map Ecosystem MountConfig
configured MountMap
mounts = (RegistryRole
 -> Vet (Map Ecosystem (Maybe StoreBackend, ClearedBackend)))
-> Vet (Map Ecosystem (Maybe StoreBackend, ClearedBackend))
forall a. (RegistryRole -> Vet a) -> Vet a
withRole ((RegistryRole
  -> Vet (Map Ecosystem (Maybe StoreBackend, ClearedBackend)))
 -> Vet (Map Ecosystem (Maybe StoreBackend, ClearedBackend)))
-> (RegistryRole
    -> Vet (Map Ecosystem (Maybe StoreBackend, ClearedBackend)))
-> Vet (Map Ecosystem (Maybe StoreBackend, ClearedBackend))
forall a b. (a -> b) -> a -> b
$ \case
    RegistryRole
MirrorWriter -> Map Ecosystem (Maybe StoreBackend, ClearedBackend)
-> Vet (Map Ecosystem (Maybe StoreBackend, ClearedBackend))
forall a. a -> Vet a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Map Ecosystem (Maybe StoreBackend, ClearedBackend)
forall k a. Map k a
Map.empty
    RegistryRole
MirrorPruner -> RegistryRole
-> Vet (Map Ecosystem (Maybe StoreBackend, ClearedBackend))
clearedFor RegistryRole
MirrorPruner
    RegistryRole
MirrorPreviewer -> RegistryRole
-> Vet (Map Ecosystem (Maybe StoreBackend, ClearedBackend))
clearedFor RegistryRole
MirrorPreviewer
  where
    clearedFor :: RegistryRole
-> Vet (Map Ecosystem (Maybe StoreBackend, ClearedBackend))
clearedFor RegistryRole
role =
        let resolved :: [(Ecosystem,
  Either
    StoreMaintenanceReason (Maybe StoreBackend, ClearedBackend))]
resolved = RegistryRole
-> [(Ecosystem,
     Either
       StoreMaintenanceReason (Maybe StoreBackend, ClearedBackend))]
resolvedFor RegistryRole
role
         in [(Ecosystem,
  Either
    StoreMaintenanceReason (Maybe StoreBackend, ClearedBackend))]
-> Map Ecosystem (Maybe StoreBackend, ClearedBackend)
forall reason a. [(Ecosystem, Either reason a)] -> Map Ecosystem a
clearedOf [(Ecosystem,
  Either
    StoreMaintenanceReason (Maybe StoreBackend, ClearedBackend))]
resolved
                Map Ecosystem (Maybe StoreBackend, ClearedBackend)
-> Vet ()
-> Vet (Map Ecosystem (Maybe StoreBackend, ClearedBackend))
forall a b. a -> Vet b -> Vet a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ((Ecosystem,
  Either StoreMaintenanceReason (Maybe StoreBackend, ClearedBackend))
 -> Vet ())
-> [(Ecosystem,
     Either
       StoreMaintenanceReason (Maybe StoreBackend, ClearedBackend))]
-> Vet ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ ((RegistryRole -> Severity (Ecosystem, StoreMaintenanceReason))
-> ((Ecosystem,
     Either StoreMaintenanceReason (Maybe StoreBackend, ClearedBackend))
    -> Maybe (Ecosystem, StoreMaintenanceReason))
-> (Ecosystem,
    Either StoreMaintenanceReason (Maybe StoreBackend, ClearedBackend))
-> Vet ()
forall finding input.
(RegistryRole -> Severity finding)
-> (input -> Maybe finding) -> input -> Vet ()
rule (Severity (Ecosystem, StoreMaintenanceReason)
-> RegistryRole -> Severity (Ecosystem, StoreMaintenanceReason)
forall a b. a -> b -> a
const (((Ecosystem, StoreMaintenanceReason) -> BootError)
-> Severity (Ecosystem, StoreMaintenanceReason)
forall finding. (finding -> BootError) -> Severity finding
Refuse ((Ecosystem -> StoreMaintenanceReason -> BootError)
-> (Ecosystem, StoreMaintenanceReason) -> BootError
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Ecosystem -> StoreMaintenanceReason -> BootError
StoreMaintenanceUnavailable))) (Ecosystem,
 Either StoreMaintenanceReason (Maybe StoreBackend, ClearedBackend))
-> Maybe (Ecosystem, StoreMaintenanceReason)
forall reason a.
(Ecosystem, Either reason a) -> Maybe (Ecosystem, reason)
unmaintainedOf) [(Ecosystem,
  Either
    StoreMaintenanceReason (Maybe StoreBackend, ClearedBackend))]
resolved
    resolvedFor :: RegistryRole
-> [(Ecosystem,
     Either
       StoreMaintenanceReason (Maybe StoreBackend, ClearedBackend))]
resolvedFor RegistryRole
role =
        [ (Ecosystem
eco, RegistryRole
-> Ecosystem
-> PrivateEndpoint
-> Either
     StoreMaintenanceReason (Maybe StoreBackend, ClearedBackend)
resolve RegistryRole
role Ecosystem
eco PrivateEndpoint
endpoint)
        | (Ecosystem
eco, Mount
mount) <- MountMap -> [(Ecosystem, Mount)]
forall k a. Map k a -> [(k, a)]
Map.toAscList MountMap
mounts
        , Maybe MirrorTarget -> Bool
forall a. Maybe a -> Bool
isJust (MountRegistries -> Maybe MirrorTarget
regMirrorTarget (Mount -> MountRegistries
mountRegistries Mount
mount))
        , Just MountConfig
config <- [Ecosystem -> Map Ecosystem MountConfig -> Maybe MountConfig
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Ecosystem
eco Map Ecosystem MountConfig
configured]
        , Just PrivateEndpoint
endpoint <- [MountConfig -> Maybe PrivateEndpoint
mntPrivateUpstream MountConfig
config]
        ]
    resolve :: RegistryRole
-> Ecosystem
-> PrivateEndpoint
-> Either
     StoreMaintenanceReason (Maybe StoreBackend, ClearedBackend)
resolve RegistryRole
role Ecosystem
eco PrivateEndpoint
endpoint = case Target -> StoreTag
tgtTag Target
target of
        StoreTag
TagCodeArtifact -> do
            (backend, store) <- (ConfigError -> StoreMaintenanceReason)
-> Either ConfigError (StoreBackend, CodeArtifactStore)
-> Either StoreMaintenanceReason (StoreBackend, CodeArtifactStore)
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 -> StoreMaintenanceReason
PrivateCacheUnavailable (Text -> StoreMaintenanceReason)
-> (ConfigError -> Text) -> ConfigError -> StoreMaintenanceReason
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConfigError -> Text
forall b a. (Show a, IsString b) => a -> b
show) (Ecosystem
-> Target -> Either ConfigError (StoreBackend, CodeArtifactStore)
resolvePrivateBackend Ecosystem
eco Target
target)
            pure (Just backend, clearedBackend (tgtUrl target) Nothing adapter presentation (ClearedCodeArtifactCache store))
        StoreTag
TagRegistry -> StoreMaintenanceReason
-> Either
     StoreMaintenanceReason (Maybe StoreBackend, ClearedBackend)
forall a b. a -> Either a b
Left (Text -> StoreMaintenanceReason
PrivateCacheUnavailable Text
"registry has no inventory control plane")
        StoreTag
TagVerdaccio -> do
            Bool
-> Either StoreMaintenanceReason ()
-> Either StoreMaintenanceReason ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (RegistryRole -> Bool
refusesWithoutConsent RegistryRole
role Bool -> Bool -> Bool
&& PrivateEndpoint -> DeletionConsent
preConsent PrivateEndpoint
endpoint DeletionConsent -> DeletionConsent -> Bool
forall a. Eq a => a -> a -> Bool
== DeletionConsent
DeletionWithheld) (Either StoreMaintenanceReason ()
 -> Either StoreMaintenanceReason ())
-> Either StoreMaintenanceReason ()
-> Either StoreMaintenanceReason ()
forall a b. (a -> b) -> a -> b
$
                StoreMaintenanceReason -> Either StoreMaintenanceReason ()
forall a b. a -> Either a b
Left (Text -> StoreMaintenanceReason
PrivateCacheUnavailable (Text
descriptor Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" is not set"))
            Bool
-> Either StoreMaintenanceReason ()
-> Either StoreMaintenanceReason ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (RegistryRole
role RegistryRole -> RegistryRole -> Bool
forall a. Eq a => a -> a -> Bool
== RegistryRole
MirrorPruner Bool -> Bool -> Bool
&& Maybe Secret -> Bool
forall a. Maybe a -> Bool
isNothing (PrivateEndpoint -> Maybe Secret
preToken PrivateEndpoint
endpoint)) (Either StoreMaintenanceReason ()
 -> Either StoreMaintenanceReason ())
-> Either StoreMaintenanceReason ()
-> Either StoreMaintenanceReason ()
forall a b. (a -> b) -> a -> b
$
                StoreMaintenanceReason -> Either StoreMaintenanceReason ()
forall a b. a -> Either a b
Left (Text -> StoreMaintenanceReason
PrivateCacheUnavailable (Ecosystem -> Text -> Text
mountKeyRef Ecosystem
eco (Text -> StoreTag -> Text -> Text
taggedKeyPath Text
"privateUpstream" StoreTag
TagVerdaccio Text
"token") Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" is not set"))
            store <- Maybe RegistryAdapter
-> ServicePresentation
-> StoreTag
-> Maybe Secret
-> DeletionConsent
-> Text
-> Either StoreMaintenanceReason ClearedProtocolStore
protocolStoreFor Maybe RegistryAdapter
adapter ServicePresentation
presentation StoreTag
TagVerdaccio (PrivateEndpoint -> Maybe Secret
preToken PrivateEndpoint
endpoint) (PrivateEndpoint -> DeletionConsent
preConsent PrivateEndpoint
endpoint) Text
descriptor
            pure (fmap (`BackendVerdaccio` preConsent endpoint) (preToken endpoint), clearedBackend (tgtUrl target) Nothing adapter presentation (ClearedProtocol store))
      where
        target :: Target
target = PrivateEndpoint -> Target
preTarget PrivateEndpoint
endpoint
        presentation :: ServicePresentation
presentation = StoreTag -> Ecosystem -> ServicePresentation
storePresentation (Target -> StoreTag
tgtTag Target
target) Ecosystem
eco
        adapter :: Maybe RegistryAdapter
adapter = ResolveMaintenanceAdapter
resolveAdapter Ecosystem
eco
        descriptor :: Text
descriptor = Ecosystem -> Text -> Text
mountKeyRef Ecosystem
eco (Text -> StoreTag -> Text -> Text
taggedKeyPath Text
"privateUpstream" StoreTag
TagVerdaccio Text
"permitDeletion")

-- A resolved entry that failed, named by the ecosystem that declared it.
unmaintainedOf :: (Ecosystem, Either reason a) -> Maybe (Ecosystem, reason)
unmaintainedOf :: forall reason a.
(Ecosystem, Either reason a) -> Maybe (Ecosystem, reason)
unmaintainedOf (Ecosystem
eco, Either reason a
outcome) = (Ecosystem
eco,) (reason -> (Ecosystem, reason))
-> Maybe reason -> Maybe (Ecosystem, reason)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Either reason a -> Maybe reason
forall l r. Either l r -> Maybe l
leftToMaybe Either reason a
outcome

-- A refused pass yields no plan, so a target the rule refused never reaches this map.
clearedOf :: [(Ecosystem, Either reason a)] -> Map Ecosystem a
clearedOf :: forall reason a. [(Ecosystem, Either reason a)] -> Map Ecosystem a
clearedOf [(Ecosystem, Either reason a)]
resolved = [(Ecosystem, a)] -> Map Ecosystem a
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(Ecosystem
eco, a
backend) | (Ecosystem
eco, Right a
backend) <- [(Ecosystem, Either reason a)]
resolved]

-- A preview reports missing consent instead of refusing boot.
refusesWithoutConsent :: RegistryRole -> Bool
refusesWithoutConsent :: RegistryRole -> Bool
refusesWithoutConsent = \case
    RegistryRole
MirrorWriter -> Bool
False
    RegistryRole
MirrorPruner -> Bool
True
    RegistryRole
MirrorPreviewer -> Bool
False

-- The store a resolved backend lets the Dredger reach, or why this build reaches none.
sweepableStore :: RegistryRole -> Maybe RegistryAdapter -> Ecosystem -> MirrorTarget -> Either StoreMaintenanceReason ClearedBackend
sweepableStore :: RegistryRole
-> Maybe RegistryAdapter
-> Ecosystem
-> MirrorTarget
-> Either StoreMaintenanceReason ClearedBackend
sweepableStore RegistryRole
role Maybe RegistryAdapter
mAdapter Ecosystem
eco MirrorTarget
target = RegistryUrl
-> Maybe Text
-> Maybe RegistryAdapter
-> ServicePresentation
-> ClearedControl
-> ClearedBackend
clearedBackend (MirrorTarget -> RegistryUrl
mtUrl MirrorTarget
target) (MirrorTarget -> Maybe Text
mtUsername MirrorTarget
target) Maybe RegistryAdapter
mAdapter ServicePresentation
presentation (ClearedControl -> ClearedBackend)
-> Either StoreMaintenanceReason ClearedControl
-> Either StoreMaintenanceReason ClearedBackend
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Either StoreMaintenanceReason ClearedControl
control
  where
    backend :: StoreBackend
backend = MirrorTarget -> StoreBackend
mtBackend MirrorTarget
target
    presentation :: ServicePresentation
presentation = StoreTag -> Ecosystem -> ServicePresentation
storePresentation (StoreBackend -> StoreTag
sbTag StoreBackend
backend) Ecosystem
eco
    control :: Either StoreMaintenanceReason ClearedControl
control = case StoreBackend -> ControlPlane
sbControl StoreBackend
backend of
        ControlCodeArtifact CodeArtifactStore
store -> ClearedControl -> Either StoreMaintenanceReason ClearedControl
forall a b. b -> Either a b
Right (CodeArtifactStore -> ClearedControl
ClearedCodeArtifact CodeArtifactStore
store)
        ControlPlane
ControlNone -> StoreMaintenanceReason
-> Either StoreMaintenanceReason ClearedControl
forall a b. a -> Either a b
Left (StoreTag -> StoreMaintenanceReason
NoControlPlane (StoreBackend -> StoreTag
sbTag StoreBackend
backend))
        ControlProtocol Secret
token DeletionConsent
consent -> do
            Bool
-> Either StoreMaintenanceReason ()
-> Either StoreMaintenanceReason ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (DeletionConsent
consent DeletionConsent -> DeletionConsent -> Bool
forall a. Eq a => a -> a -> Bool
== DeletionConsent
DeletionWithheld Bool -> Bool -> Bool
&& RegistryRole -> Bool
refusesWithoutConsent RegistryRole
role) (StoreMaintenanceReason -> Either StoreMaintenanceReason ()
forall a b. a -> Either a b
Left (StoreTag -> StoreMaintenanceReason
DeletionNotPermitted (StoreBackend -> StoreTag
sbTag StoreBackend
backend)))
            ClearedProtocolStore -> ClearedControl
ClearedProtocol (ClearedProtocolStore -> ClearedControl)
-> Either StoreMaintenanceReason ClearedProtocolStore
-> Either StoreMaintenanceReason ClearedControl
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe RegistryAdapter
-> ServicePresentation
-> StoreTag
-> Maybe Secret
-> DeletionConsent
-> Text
-> Either StoreMaintenanceReason ClearedProtocolStore
protocolStoreFor Maybe RegistryAdapter
mAdapter ServicePresentation
presentation (StoreBackend -> StoreTag
sbTag StoreBackend
backend) (Secret -> Maybe Secret
forall a. a -> Maybe a
Just Secret
token) DeletionConsent
consent (Ecosystem -> StoreTag -> Text
consentDescriptor Ecosystem
eco (StoreBackend -> StoreTag
sbTag StoreBackend
backend))

clearedBackend :: RegistryUrl -> Maybe Text -> Maybe RegistryAdapter -> ServicePresentation -> ClearedControl -> ClearedBackend
clearedBackend :: RegistryUrl
-> Maybe Text
-> Maybe RegistryAdapter
-> ServicePresentation
-> ClearedControl
-> ClearedBackend
clearedBackend RegistryUrl
url Maybe Text
username Maybe RegistryAdapter
adapter ServicePresentation
presentation ClearedControl
control =
    ClearedBackend
        { cbUrl :: RegistryUrl
cbUrl = RegistryUrl
url
        , cbAlphabet :: NameAlphabet
cbAlphabet = NameAlphabet
-> (RegistryAdapter -> NameAlphabet)
-> Maybe RegistryAdapter
-> NameAlphabet
forall b a. b -> (a -> b) -> Maybe a -> b
maybe NameAlphabet
noNameAlphabet (AdapterMaintenance -> NameAlphabet
maintenanceAlphabet (AdapterMaintenance -> NameAlphabet)
-> (RegistryAdapter -> AdapterMaintenance)
-> RegistryAdapter
-> NameAlphabet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RegistryAdapter -> AdapterMaintenance
adapterMaintenance) Maybe RegistryAdapter
adapter
        , cbFetchManifest :: ManifestFetch
cbFetchManifest = ManifestFetch
-> (RegistryAdapter -> ManifestFetch)
-> Maybe RegistryAdapter
-> ManifestFetch
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ManifestFetch
absentManifestRead (PrepareRead -> ManifestFetch
prepareManifest (PrepareRead -> ManifestFetch)
-> (RegistryAdapter -> PrepareRead)
-> RegistryAdapter
-> ManifestFetch
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ServicePresentation -> RegistryAdapter -> PrepareRead
storeRead ServicePresentation
presentation) Maybe RegistryAdapter
adapter
        , cbUsername :: Maybe Text
cbUsername = Maybe Text
username
        , cbControl :: ClearedControl
cbControl = ClearedControl
control
        }

-- The adapter's metadata read, presenting the store's credential as the store declares. An adapter with no publish codec reads natively.
storeRead :: ServicePresentation -> RegistryAdapter -> PrepareRead
storeRead :: ServicePresentation -> RegistryAdapter -> PrepareRead
storeRead ServicePresentation
presentation RegistryAdapter
adapter = PrepareRead
-> (AdapterPublish -> PrepareRead)
-> Maybe AdapterPublish
-> PrepareRead
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (AdapterMetadata -> PrepareRead
metadataRead (RegistryAdapter -> AdapterMetadata
adapterMetadata RegistryAdapter
adapter)) (\AdapterPublish
publish -> PublishCodec -> PrepareRead
pcTargetRead (AdapterPublish -> ServicePresentation -> PublishCodec
publishCodec AdapterPublish
publish ServicePresentation
presentation)) (RegistryAdapter -> Maybe AdapterPublish
adapterPublish RegistryAdapter
adapter)

protocolStoreFor :: Maybe RegistryAdapter -> ServicePresentation -> StoreTag -> Maybe Secret -> DeletionConsent -> Text -> Either StoreMaintenanceReason ClearedProtocolStore
protocolStoreFor :: Maybe RegistryAdapter
-> ServicePresentation
-> StoreTag
-> Maybe Secret
-> DeletionConsent
-> Text
-> Either StoreMaintenanceReason ClearedProtocolStore
protocolStoreFor Maybe RegistryAdapter
mAdapter ServicePresentation
presentation StoreTag
tag Maybe Secret
token DeletionConsent
consent Text
descriptor = do
    adapter <- StoreMaintenanceReason
-> Maybe RegistryAdapter
-> Either StoreMaintenanceReason RegistryAdapter
forall l r. l -> Maybe r -> Either l r
maybeToRight StoreMaintenanceReason
NoProtocolMaintenance Maybe RegistryAdapter
mAdapter
    listing <- maybeToRight NoProtocolMaintenance (maintenanceListing (adapterMaintenance adapter))
    delete <- maybeToRight NoProtocolMaintenance (maintenanceVersionDelete (adapterMaintenance adapter))
    publish <- maybeToRight NoProtocolMaintenance (adapterPublish adapter)
    pure
        ClearedProtocolStore
            { cpsToken = token
            , cpsTag = tag
            , cpsConsent = consent
            , cpsConsentKey = descriptor
            , cpsListing = listing
            , cpsDelete = delete
            , cpsCodec = publishCodec publish presentation
            }

-- The read a store cleared without an adapter would make, which no boot reaches.
absentManifestRead :: ManifestFetch
absentManifestRead :: ManifestFetch
absentManifestRead TracingPort
_ OriginClient
_ PackageName
_ =
    Either MetadataError Manifest -> IO (Either MetadataError Manifest)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MetadataError -> Either MetadataError Manifest
forall a b. a -> Either a b
Left (FetchFault -> MetadataError
MetadataFetch (TransportFault -> FetchFault
FetchTransport (TransportCause -> Text -> TransportFault
transportFault TransportCause
TransportProtocol Text
absentAdapterDetail))))

absentAdapterDetail :: Text
absentAdapterDetail :: Text
absentAdapterDetail = Text
"this build serves the mount's ecosystem no metadata read"

-- | Describe the mirror target consent required for deletion. Preview reports withheld consent instead.
consentDescriptor :: Ecosystem -> StoreTag -> Text
consentDescriptor :: Ecosystem -> StoreTag -> Text
consentDescriptor Ecosystem
eco StoreTag
tag =
    Text
"set "
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Ecosystem -> Text -> Text
mountKeyRef Ecosystem
eco (Text -> StoreTag -> Text -> Text
taggedKeyPath Text
"mirrorTarget" StoreTag
tag Text
"permitDeletion")
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" to true: the Dredger deletes nothing from a store that does not carry it"

-- | Match capacity declarations without case or trailing-slash differences.
overrideKey :: Text -> Text
overrideKey :: Text -> Text
overrideKey = (Char -> Bool) -> Text -> Text
T.dropWhileEnd (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'/') (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
T.toLower (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
T.strip