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

-- | Configuration loading, mount resolution, and redacted operator diagnostics.
module Ecluse.Config (
    Config (..),
    AppConfig (..),
    ServerSettings (..),
    QueueSettings (..),
    LimitsSettings (..),
    CacheSettings (..),
    IntegritySettings (..),
    EgressSettings (..),
    AdvisoriesSettings (..),
    RuntimeSettings (..),
    ObservabilitySettings (..),
    DredgerSettings (..),
    QuotaOverride (..),
    MountMap,
    Mount (..),
    RuleInactivity (..),
    MountRegistries (..),
    MountMode (..),
    MirroredLegs (..),
    regPrivateUpstream,
    regMirrorTarget,
    MirrorTarget (..),
    StoreTag (..),
    storeTagName,
    taggedKeyPath,
    Target (..),
    PrivateEndpoint (..),
    DeletionConsent (..),
    MirrorWrite (..),
    MirrorEndpoint (..),
    meTarget,
    PublicationEndpoint (..),
    MintPlan (..),
    ControlPlane (..),
    StoreBackend (..),
    sbTag,
    sbMint,
    sbControl,
    FirstParty (..),
    MountIntegrity (..),
    MountConfig (..),
    Url,
    unUrl,
    QueueTarget (..),
    QueueUrl,
    queueUrlText,
    queueUrlTarget,
    AdvisoryStoreTarget (..),
    AdvisoryStoreUrl,
    advisoryStoreUrlText,
    advisoryStoreTarget,
    advisoryStoreBucket,
    advisoryObjectKey,
    RulePatch (..),
    RuleEntry (..),
    RulePolicy (..),
    PolicyError (..),
    renderPolicyError,
    emptyPolicy,
    decodeDefaultConfig,
    sourceDefaultNames,
    advisorySourced,
    ConfigError (..),
    renderConfigError,
    loadConfig,
    sameRegistry,
    mountPostureLines,
    mountAdvisoryAge,
    mountEpssRequirement,
    compileMountEpssRequirement,
    mountAdvisoryDenials,
    mountDatabaseRequirement,
    advisoryAgeLines,
    advisoryEpssLines,
    resolvedKeyProvenance,
) where

import Data.Aeson (Value (..), encode)
import Data.Aeson.Key qualified as Key
import Data.Aeson.KeyMap qualified as KeyMap
import Data.Aeson.Types (parseEither)
import Data.ByteString.Lazy qualified as LBS
import Data.Map.Strict qualified as Map
import Data.Set qualified as Set
import Data.Text qualified as T
import Data.Yaml (decodeEither')

import Ecluse.Config.AdvisoryStore (advisoryObjectKey, advisoryStoreBucket)
import Ecluse.Config.Aeson (parseAppConfig, parseRulesPatch)
import Ecluse.Config.DefaultConfig (defaultConfigBytes)
import Ecluse.Config.Mirror (validateMirrorSettings)
import Ecluse.Config.Resolve (buildEnvAst, deepMerge, secretLeafKeys)
import Ecluse.Config.Rule
import Ecluse.Config.Target (resolveStoreBackend, vetPrivateRepository, vetTargetTag)
import Ecluse.Config.Types
import Ecluse.Core.Ecosystem (Ecosystem, ecosystemName, parseEcosystem)
import Ecluse.Core.Osv.Schema (EpssRequirement (EpssOptional, EpssRequired))
import Ecluse.Core.Rules.Freshness (
    AdvisoryAgeBasis (AgeBeforeQuarantine, AgeConfigured, AgeFloor),
    MaxAdvisoryAge (maxAdvisoryAge, maxAdvisoryAgeBasis),
    maxAdvisoryAgeFor,
 )
import Ecluse.Core.Rules.Render (renderDuration)
import Ecluse.Core.Rules.Types (PrecededRule (prRule), Rule, deniesOnAdvisories, readsAdvisories, requiresEpss, ruleName)
import Ecluse.Core.Security (HostPort, hostPortAddress)
import Ecluse.Core.Security.Egress (RegistryUrl, registryUrlText)
import Ecluse.Core.Server.Readiness (DatabaseRequirement (DatabaseOptional, DatabaseRequired))
import Ecluse.Core.Text (registryPath, stripTrailingSlash)

-- | Decode the default backbone without resolving mounts that need an operator overlay.
decodeDefaultConfig :: ByteString -> Either [ConfigError] (Value, RulePolicy)
decodeDefaultConfig :: StrictByteString -> Either [ConfigError] (Value, RulePolicy)
decodeDefaultConfig StrictByteString
bytes = do
    ast <- StrictByteString -> Either [ConfigError] Value
parseDefaultAst StrictByteString
bytes
    _ <- decodeAppConfig ast
    patch <- decodeRulesPatch ast
    policy <- first (pure . PolicyErrors) (resolvePolicy emptyPolicy patch)
    pure (ast, policy)

{- Each descriptor stays patchable after a disable, until the final mount policy is selected. The
middle field is why a rule left to the source does not run, absent when a source is configured. -}
data PendingPolicy = PendingPolicy RulePolicy (Maybe RuleInactivity) (Map Text Activation)

-- How one scope decides a source-activated rule: left to the source, or enabled or disabled by a patch.
data Activation = SourceDecides | OperatorEnabled | OperatorDisabled
    deriving stock (Activation -> Activation -> Bool
(Activation -> Activation -> Bool)
-> (Activation -> Activation -> Bool) -> Eq Activation
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Activation -> Activation -> Bool
== :: Activation -> Activation -> Bool
$c/= :: Activation -> Activation -> Bool
/= :: Activation -> Activation -> Bool
Eq)

-- | The shipped rules an advisory source activates: neither can decide anything without one.
sourceDefaultNames :: Set Text
sourceDefaultNames :: Set Text
sourceDefaultNames = [Text] -> Set Text
forall a. Ord a => [a] -> Set a
Set.fromList [Text
"deny-known-malicious", Text
"remediation-fast-track"]

pendingPolicy :: RulePolicy -> Bool -> PendingPolicy
pendingPolicy :: RulePolicy -> Bool -> PendingPolicy
pendingPolicy RulePolicy
policy Bool
sourced =
    RulePolicy
-> Maybe RuleInactivity -> Map Text Activation -> PendingPolicy
PendingPolicy RulePolicy
policy (RuleInactivity
NoAdvisorySource RuleInactivity -> Maybe () -> Maybe RuleInactivity
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 (Bool -> Bool
not Bool
sourced)) ((Text -> Activation) -> Set Text -> Map Text Activation
forall k a. (k -> a) -> Set k -> Map k a
Map.fromSet (Activation -> Text -> Activation
forall a b. a -> b -> a
const Activation
SourceDecides) Set Text
sourceDefaultNames)

-- @enabled: false@ deactivates, @enabled: true@ or a restated type activates, and a partial patch keeps the state.
resolvePendingPolicy :: PendingPolicy -> RulePatch -> Either [PolicyError] PendingPolicy
resolvePendingPolicy :: PendingPolicy -> RulePatch -> Either [PolicyError] PendingPolicy
resolvePendingPolicy (PendingPolicy RulePolicy
policy Maybe RuleInactivity
unsourced Map Text Activation
activation) (RulePatch Map Text RuleEntry
patch) =
    (\RulePolicy
resolved -> RulePolicy
-> Maybe RuleInactivity -> Map Text Activation -> PendingPolicy
PendingPolicy RulePolicy
resolved Maybe RuleInactivity
unsourced ((Text -> Activation -> Activation)
-> Map Text Activation -> Map Text Activation
forall k a b. (k -> a -> b) -> Map k a -> Map k b
Map.mapWithKey Text -> Activation -> Activation
decide Map Text Activation
activation)) (RulePolicy -> PendingPolicy)
-> Either [PolicyError] RulePolicy
-> Either [PolicyError] PendingPolicy
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RulePolicy -> RulePatch -> Either [PolicyError] RulePolicy
resolvePolicy RulePolicy
policy (Map Text RuleEntry -> RulePatch
RulePatch Map Text RuleEntry
descriptorPatch)
  where
    decide :: Text -> Activation -> Activation
decide Text
name Activation
current = case Text -> Map Text RuleEntry -> Maybe RuleEntry
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
name Map Text RuleEntry
patch of
        Maybe RuleEntry
Nothing -> Activation
current
        Just RuleEntry
declared -> case RuleEntry -> Maybe Bool
entryEnabled RuleEntry
declared of
            Just Bool
True -> Activation
OperatorEnabled
            Just Bool
False -> Activation
OperatorDisabled
            Maybe Bool
Nothing -> if Maybe Text -> Bool
forall a. Maybe a -> Bool
isJust (RuleEntry -> Maybe Text
entryType RuleEntry
declared) then Activation
OperatorEnabled else Activation
current
    descriptorPatch :: Map Text RuleEntry
descriptorPatch = (Text -> Map Text RuleEntry -> Map Text RuleEntry)
-> Map Text RuleEntry -> [Text] -> Map Text RuleEntry
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ((RuleEntry -> RuleEntry)
-> Text -> Map Text RuleEntry -> Map Text RuleEntry
forall k a. Ord k => (a -> a) -> k -> Map k a -> Map k a
Map.adjust (\RuleEntry
declared -> RuleEntry
declared{entryEnabled = Just True})) Map Text RuleEntry
patch (Map Text Activation -> [Text]
forall k a. Map k a -> [k]
Map.keys Map Text Activation
activation)

-- A scope's selected rules, and the rules it leaves to a source that is not configured, with why.
data SettledPolicy = SettledPolicy RulePolicy (Map Text RuleInactivity)

settlePolicy :: PendingPolicy -> SettledPolicy
settlePolicy :: PendingPolicy -> SettledPolicy
settlePolicy (PendingPolicy RulePolicy
policy Maybe RuleInactivity
unsourced Map Text Activation
activation) =
    RulePolicy -> Map Text RuleInactivity -> SettledPolicy
SettledPolicy (Map Text PrecededRule -> RulePolicy
RulePolicy (Map Text PrecededRule -> Set Text -> Map Text PrecededRule
forall k a. Ord k => Map k a -> Set k -> Map k a
Map.withoutKeys (RulePolicy -> Map Text PrecededRule
policyRules RulePolicy
policy) Set Text
idle)) Map Text RuleInactivity
waiting
  where
    idle :: Set Text
idle = Map Text Activation -> Set Text
forall k a. Map k a -> Set k
Map.keysSet ((Activation -> Bool) -> Map Text Activation -> Map Text Activation
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter (Bool -> Bool
not (Bool -> Bool) -> (Activation -> Bool) -> Activation -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Activation -> Bool
runs) Map Text Activation
activation)
    runs :: Activation -> Bool
runs = \case
        Activation
SourceDecides -> Maybe RuleInactivity -> Bool
forall a. Maybe a -> Bool
isNothing Maybe RuleInactivity
unsourced
        Activation
OperatorEnabled -> Bool
True
        Activation
OperatorDisabled -> Bool
False
    waiting :: Map Text RuleInactivity
waiting = Map Text RuleInactivity
-> (RuleInactivity -> Map Text RuleInactivity)
-> Maybe RuleInactivity
-> Map Text RuleInactivity
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Map Text RuleInactivity
forall k a. Map k a
Map.empty (\RuleInactivity
reason -> RuleInactivity
reason RuleInactivity -> Map Text Activation -> Map Text RuleInactivity
forall a b. a -> Map Text b -> Map Text a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (Activation -> Bool) -> Map Text Activation -> Map Text Activation
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter (Activation -> Activation -> Bool
forall a. Eq a => a -> a -> Bool
== Activation
SourceDecides) Map Text Activation
activation) Maybe RuleInactivity
unsourced

-- The source-activated rules a scope runs.
runningSourceRules :: SettledPolicy -> Set Text
runningSourceRules :: SettledPolicy -> Set Text
runningSourceRules (SettledPolicy RulePolicy
policy Map Text RuleInactivity
_) = Map Text PrecededRule -> Set Text
forall k a. Map k a -> Set k
Map.keysSet (Map Text PrecededRule -> Set Text -> Map Text PrecededRule
forall k a. Ord k => Map k a -> Set k -> Map k a
Map.restrictKeys (RulePolicy -> Map Text PrecededRule
policyRules RulePolicy
policy) Set Text
sourceDefaultNames)

-- A shared rule left to the source is marked only where some mount leaves it to the source too and none runs it.
sharedInactivity :: MountMap -> Set Text -> SettledPolicy -> Map Text RuleInactivity
sharedInactivity :: MountMap -> Set Text -> SettledPolicy -> Map Text RuleInactivity
sharedInactivity MountMap
mounts Set Text
mountRuns (SettledPolicy RulePolicy
_ Map Text RuleInactivity
waiting) =
    Map Text RuleInactivity -> Set Text -> Map Text RuleInactivity
forall k a. Ord k => Map k a -> Set k -> Map k a
Map.restrictKeys Map Text RuleInactivity
waiting ((Mount -> Set Text) -> MountMap -> Set Text
forall m a. Monoid m => (a -> m) -> Map Ecosystem a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (Map Text RuleInactivity -> Set Text
forall k a. Map k a -> Set k
Map.keysSet (Map Text RuleInactivity -> Set Text)
-> (Mount -> Map Text RuleInactivity) -> Mount -> Set Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Mount -> Map Text RuleInactivity
mountInactiveRules) MountMap
mounts Set Text -> Set Text -> Set Text
forall a. Ord a => Set a -> Set a -> Set a
`Set.difference` Set Text
mountRuns)

-- | Whether @advisories.url@ is configured, the source the advisory rules read.
advisorySourced :: AppConfig -> Bool
advisorySourced :: AppConfig -> Bool
advisorySourced = Maybe AdvisoryStoreUrl -> Bool
forall a. Maybe a -> Bool
isJust (Maybe AdvisoryStoreUrl -> Bool)
-> (AppConfig -> Maybe AdvisoryStoreUrl) -> AppConfig -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AdvisoriesSettings -> Maybe AdvisoryStoreUrl
advUrl (AdvisoriesSettings -> Maybe AdvisoryStoreUrl)
-> (AppConfig -> AdvisoriesSettings)
-> AppConfig
-> Maybe AdvisoryStoreUrl
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AppConfig -> AdvisoriesSettings
cfgAdvisories

-- | Load the defaults, the operator document, then the environment overlay, strongest last. A mount is active only where the overlay declares it.
loadConfig :: [(String, String)] -> Maybe ByteString -> Either [ConfigError] Config
loadConfig :: [(String, String)]
-> Maybe StrictByteString -> Either [ConfigError] Config
loadConfig [(String, String)]
envVars Maybe StrictByteString
mBytes = do
    (defaultAst, catalogue) <- StrictByteString -> Either [ConfigError] (Value, RulePolicy)
decodeDefaultConfig StrictByteString
defaultConfigBytes
    docAst <- parseDocumentAst mBytes
    let overridesAst = Value -> Value -> Value
deepMerge Value
docAst ([(String, String)] -> Value
buildEnvAst [(String, String)]
envVars)
    let merged = Value -> Value -> Value
deepMerge Value
defaultAst Value
overridesAst
    parsed <- decodeAppConfig merged
    active <- declaredMounts overridesAst
    let appConfig = AppConfig
parsed{cfgMounts = servedMounts active (cfgMounts parsed)}
    globalPolicy <- resolveGlobalPolicy (pendingPolicy catalogue (advisorySourced appConfig)) overridesAst
    validateMirrorSettings appConfig
    (mounts, mountRuns) <- case (publicUrlErrors appConfig, resolveMounts defaultAst overridesAst globalPolicy appConfig) of
        ([], Either [ConfigError] (MountMap, Set Text)
resolved) -> Either [ConfigError] (MountMap, Set Text)
resolved
        ([ConfigError]
errs, Either [ConfigError] (MountMap, Set Text)
resolved) -> [ConfigError] -> Either [ConfigError] (MountMap, Set Text)
forall a b. a -> Either a b
Left ([ConfigError]
errs [ConfigError] -> [ConfigError] -> [ConfigError]
forall a. Semigroup a => a -> a -> a
<> [ConfigError]
-> Either [ConfigError] (MountMap, Set Text) -> [ConfigError]
forall a b. a -> Either a b -> a
fromLeft [] Either [ConfigError] (MountMap, Set Text)
resolved)
    Right (Config appConfig mounts (sharedInactivity mounts mountRuns (settlePolicy globalPolicy)))

-- enabled: false switches a declared mount off. Anything else declared serves.
servedMounts :: Set Ecosystem -> Map Ecosystem MountConfig -> Map Ecosystem MountConfig
servedMounts :: Set Ecosystem
-> Map Ecosystem MountConfig -> Map Ecosystem MountConfig
servedMounts Set Ecosystem
active Map Ecosystem MountConfig
declared =
    (MountConfig -> Bool)
-> Map Ecosystem MountConfig -> Map Ecosystem MountConfig
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter (\MountConfig
mcfg -> MountConfig -> Maybe Bool
mntEnabled MountConfig
mcfg Maybe Bool -> Maybe Bool -> Bool
forall a. Eq a => a -> a -> Bool
/= Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False) (Map Ecosystem MountConfig
-> Set Ecosystem -> Map Ecosystem MountConfig
forall k a. Ord k => Map k a -> Set k -> Map k a
Map.restrictKeys Map Ecosystem MountConfig
declared Set Ecosystem
active)

-- The proxy rewrites served tarball URLs against its own public base URL. Without one, every
-- install fails client by client instead of loudly at boot.
publicUrlErrors :: AppConfig -> [ConfigError]
publicUrlErrors :: AppConfig -> [ConfigError]
publicUrlErrors AppConfig
appConfig =
    [ ConfigError
PublicUrlRequired
    | Bool -> Bool
not (Map Ecosystem MountConfig -> Bool
forall k a. Map k a -> Bool
Map.null (AppConfig -> Map Ecosystem MountConfig
cfgMounts AppConfig
appConfig))
    , Maybe Url -> Bool
forall a. Maybe a -> Bool
isNothing (ServerSettings -> Maybe Url
srvPublicUrl (AppConfig -> ServerSettings
cfgServer AppConfig
appConfig))
    ]

declaredMounts :: Value -> Either [ConfigError] (Set Ecosystem)
declaredMounts :: Value -> Either [ConfigError] (Set Ecosystem)
declaredMounts Value
overridesAst = [Ecosystem] -> Set Ecosystem
forall a. Ord a => [a] -> Set a
Set.fromList ([Ecosystem] -> Set Ecosystem)
-> Either [ConfigError] [Ecosystem]
-> Either [ConfigError] (Set Ecosystem)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Key -> Either [ConfigError] Ecosystem)
-> [Key] -> Either [ConfigError] [Ecosystem]
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 Key -> Either [ConfigError] Ecosystem
parseKey (Value -> [Key]
mountKeysOf Value
overridesAst)
  where
    parseKey :: Key -> Either [ConfigError] Ecosystem
parseKey Key
k = case Text -> Maybe Ecosystem
parseEcosystem (Key -> Text
Key.toText Key
k) of
        Just Ecosystem
eco -> Ecosystem -> Either [ConfigError] Ecosystem
forall a b. b -> Either a b
Right Ecosystem
eco
        Maybe Ecosystem
Nothing -> [ConfigError] -> Either [ConfigError] Ecosystem
forall a b. a -> Either a b
Left [Text -> ConfigError
ParseError (Text
"Invalid ecosystem: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Key -> Text
Key.toText Key
k)]

mountKeysOf :: Value -> [Key.Key]
mountKeysOf :: Value -> [Key]
mountKeysOf (Object Object
o) = case Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"mounts" Object
o of
    Just (Object Object
mounts) -> Object -> [Key]
forall v. KeyMap v -> [Key]
KeyMap.keys Object
mounts
    Maybe Value
_ -> []
mountKeysOf Value
_ = []

parseDefaultAst :: ByteString -> Either [ConfigError] Value
parseDefaultAst :: StrictByteString -> Either [ConfigError] Value
parseDefaultAst StrictByteString
bytes = case StrictByteString -> Either ParseException Value
forall a. FromJSON a => StrictByteString -> Either ParseException a
decodeEither' StrictByteString
bytes of
    Right Value
ast -> Value -> Either [ConfigError] Value
forall a b. b -> Either a b
Right Value
ast
    Left ParseException
err -> [ConfigError] -> Either [ConfigError] Value
forall a b. a -> Either a b
Left [Text -> ConfigError
ParseError (Text
"config/default.yaml is invalid YAML: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack (ParseException -> String
forall b a. (Show a, IsString b) => a -> b
show ParseException
err))]

parseDocumentAst :: Maybe ByteString -> Either [ConfigError] Value
parseDocumentAst :: Maybe StrictByteString -> Either [ConfigError] Value
parseDocumentAst = \case
    Maybe StrictByteString
Nothing -> Value -> Either [ConfigError] Value
forall a b. b -> Either a b
Right (Object -> Value
Object Object
forall a. Monoid a => a
mempty)
    Just StrictByteString
bytes -> case StrictByteString -> Either ParseException Value
forall a. FromJSON a => StrictByteString -> Either ParseException a
decodeEither' StrictByteString
bytes of
        Right Value
ast -> Value -> Either [ConfigError] Value
forall a b. b -> Either a b
Right Value
ast
        Left ParseException
err -> [ConfigError] -> Either [ConfigError] Value
forall a b. a -> Either a b
Left [Text -> ConfigError
ParseError (Text
"the config document is invalid YAML: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack (ParseException -> String
forall b a. (Show a, IsString b) => a -> b
show ParseException
err))]

decodeAppConfig :: Value -> Either [ConfigError] AppConfig
decodeAppConfig :: Value -> Either [ConfigError] AppConfig
decodeAppConfig Value
merged = case (Value -> Parser AppConfig) -> Value -> Either String AppConfig
forall a b. (a -> Parser b) -> a -> Either String b
parseEither Value -> Parser AppConfig
parseAppConfig Value
merged of
    Right AppConfig
appConfig -> AppConfig -> Either [ConfigError] AppConfig
forall a b. b -> Either a b
Right AppConfig
appConfig
    Left String
err -> [ConfigError] -> Either [ConfigError] AppConfig
forall a b. a -> Either a b
Left [Text -> ConfigError
ParseError (Text
"Configuration parse error: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack String
err)]

decodeRulesPatch :: Value -> Either [ConfigError] RulePatch
decodeRulesPatch :: Value -> Either [ConfigError] RulePatch
decodeRulesPatch Value
ast = case (Value -> Parser RulePatch) -> Value -> Either String RulePatch
forall a b. (a -> Parser b) -> a -> Either String b
parseEither Value -> Parser RulePatch
parseRulesPatch Value
ast of
    Right RulePatch
patch -> RulePatch -> Either [ConfigError] RulePatch
forall a b. b -> Either a b
Right RulePatch
patch
    Left String
err -> [ConfigError] -> Either [ConfigError] RulePatch
forall a b. a -> Either a b
Left [Text -> ConfigError
ParseError (Text
"Rules parse error: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack String
err)]

resolveGlobalPolicy :: PendingPolicy -> Value -> Either [ConfigError] PendingPolicy
resolveGlobalPolicy :: PendingPolicy -> Value -> Either [ConfigError] PendingPolicy
resolveGlobalPolicy PendingPolicy
shared Value
overridesAst = do
    globalRulePatch <- Value -> Either [ConfigError] RulePatch
decodeRulesPatch Value
overridesAst
    first sharedPolicyErrors (resolvePendingPolicy shared globalRulePatch)

sharedPolicyErrors :: [PolicyError] -> [ConfigError]
sharedPolicyErrors :: [PolicyError] -> [ConfigError]
sharedPolicyErrors [PolicyError]
errors
    | ((Text, NonEmpty Ecosystem) -> Bool)
-> [(Text, NonEmpty Ecosystem)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ((PolicyError -> [PolicyError] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [PolicyError]
errors) (PolicyError -> Bool)
-> ((Text, NonEmpty Ecosystem) -> PolicyError)
-> (Text, NonEmpty Ecosystem)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> PolicyError
SuppressUnknownRule (Text -> PolicyError)
-> ((Text, NonEmpty Ecosystem) -> Text)
-> (Text, NonEmpty Ecosystem)
-> PolicyError
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, NonEmpty Ecosystem) -> Text
forall a b. (a, b) -> a
fst) [(Text, NonEmpty Ecosystem)]
shippedMountRules = [[PolicyError] -> ConfigError
SharedPolicyErrors [PolicyError]
errors]
    | Bool
otherwise = [[PolicyError] -> ConfigError
PolicyErrors [PolicyError]
errors]

-- The resolved mounts, and the source-activated rules any of them runs.
resolveMounts :: Value -> Value -> PendingPolicy -> AppConfig -> Either [ConfigError] (MountMap, Set Text)
resolveMounts :: Value
-> Value
-> PendingPolicy
-> AppConfig
-> Either [ConfigError] (MountMap, Set Text)
resolveMounts Value
defaultAst Value
overridesAst PendingPolicy
globalPolicy AppConfig
appConfig =
    case [Either [ConfigError] ((Ecosystem, Mount), Set Text)]
-> ([[ConfigError]], [((Ecosystem, Mount), Set Text)])
forall a b. [Either a b] -> ([a], [b])
partitionEithers (((Ecosystem, MountConfig)
 -> Either [ConfigError] ((Ecosystem, Mount), Set Text))
-> [(Ecosystem, MountConfig)]
-> [Either [ConfigError] ((Ecosystem, Mount), Set Text)]
forall a b. (a -> b) -> [a] -> [b]
map (Ecosystem, MountConfig)
-> Either [ConfigError] ((Ecosystem, Mount), Set Text)
resolveOne (Map Ecosystem MountConfig -> [(Ecosystem, MountConfig)]
forall k a. Map k a -> [(k, a)]
Map.toAscList (AppConfig -> Map Ecosystem MountConfig
cfgMounts AppConfig
appConfig))) of
        ([], [((Ecosystem, Mount), Set Text)]
resolved) -> (MountMap, Set Text) -> Either [ConfigError] (MountMap, Set Text)
forall a b. b -> Either a b
Right ([(Ecosystem, Mount)] -> MountMap
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ((((Ecosystem, Mount), Set Text) -> (Ecosystem, Mount))
-> [((Ecosystem, Mount), Set Text)] -> [(Ecosystem, Mount)]
forall a b. (a -> b) -> [a] -> [b]
map ((Ecosystem, Mount), Set Text) -> (Ecosystem, Mount)
forall a b. (a, b) -> a
fst [((Ecosystem, Mount), Set Text)]
resolved), (((Ecosystem, Mount), Set Text) -> Set Text)
-> [((Ecosystem, Mount), Set Text)] -> Set Text
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ((Ecosystem, Mount), Set Text) -> Set Text
forall a b. (a, b) -> b
snd [((Ecosystem, Mount), Set Text)]
resolved)
        ([[ConfigError]]
errs, [((Ecosystem, Mount), Set Text)]
_) -> [ConfigError] -> Either [ConfigError] (MountMap, Set Text)
forall a b. a -> Either a b
Left ([[ConfigError]] -> [ConfigError]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[ConfigError]]
errs)
  where
    resolveOne :: (Ecosystem, MountConfig)
-> Either [ConfigError] ((Ecosystem, Mount), Set Text)
resolveOne (Ecosystem
eco, MountConfig
mcfg) = case [Either ConfigError ()] -> [ConfigError]
forall a b. [Either a b] -> [a]
lefts (Ecosystem -> MountConfig -> [Either ConfigError ()]
declarationErrors Ecosystem
eco MountConfig
mcfg) of
        [] -> do
            shipped <- Value -> Ecosystem -> Either [ConfigError] RulePatch
mountRulePatch Value
defaultAst Ecosystem
eco
            declared <- mountRulePatch overridesAst eco
            settled <- settlePolicy <$> resolveMountPolicy globalPolicy shipped declared
            mount <- resolveMode settled eco mcfg
            pure ((eco, mount), runningSourceRules settled)
        [ConfigError]
tagErrs -> [ConfigError]
-> Either [ConfigError] ((Ecosystem, Mount), Set Text)
forall a b. a -> Either a b
Left [ConfigError]
tagErrs

{- Every endpoint checked against the tag it was declared under, and the private upstream checked
once more for the repository the boot addresses its aggregation question to. -}
declarationErrors :: Ecosystem -> MountConfig -> [Either ConfigError ()]
declarationErrors :: Ecosystem -> MountConfig -> [Either ConfigError ()]
declarationErrors Ecosystem
eco MountConfig
mcfg =
    ((Text, Target) -> Either ConfigError ())
-> [(Text, Target)] -> [Either ConfigError ()]
forall a b. (a -> b) -> [a] -> [b]
map ((Text -> Target -> Either ConfigError ())
-> (Text, Target) -> Either ConfigError ()
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (Ecosystem -> Text -> Target -> Either ConfigError ()
vetTargetTag Ecosystem
eco)) (MountConfig -> [(Text, Target)]
readAndPublishTargets MountConfig
mcfg)
        [Either ConfigError ()]
-> [Either ConfigError ()] -> [Either ConfigError ()]
forall a. Semigroup a => a -> a -> a
<> [Ecosystem -> Target -> Either ConfigError ()
vetPrivateRepository Ecosystem
eco (PrivateEndpoint -> Target
preTarget PrivateEndpoint
endpoint) | Just PrivateEndpoint
endpoint <- [MountConfig -> Maybe PrivateEndpoint
mntPrivateUpstream MountConfig
mcfg]]

{- Each read or publish endpoint a mount declares, with the key it is written under. The mirror
target is absent because 'resolveStoreBackend' vets it while resolving its backend. -}
readAndPublishTargets :: MountConfig -> [(Text, Target)]
readAndPublishTargets :: MountConfig -> [(Text, Target)]
readAndPublishTargets MountConfig
mcfg =
    [(Text
"privateUpstream", PrivateEndpoint -> Target
preTarget PrivateEndpoint
target) | Just PrivateEndpoint
target <- [MountConfig -> Maybe PrivateEndpoint
mntPrivateUpstream MountConfig
mcfg]]
        [(Text, Target)] -> [(Text, Target)] -> [(Text, Target)]
forall a. Semigroup a => a -> a -> a
<> [(Text
"publicationTarget", PublicationEndpoint -> Target
peTarget PublicationEndpoint
endpoint) | Just PublicationEndpoint
endpoint <- [MountConfig -> Maybe PublicationEndpoint
mntPublicationTarget MountConfig
mcfg]]

-- A declared mirror target makes the mount mirrored, which then needs its private upstream.
resolveMode :: SettledPolicy -> Ecosystem -> MountConfig -> Either [ConfigError] Mount
resolveMode :: SettledPolicy
-> Ecosystem -> MountConfig -> Either [ConfigError] Mount
resolveMode SettledPolicy
policy Ecosystem
eco MountConfig
mcfg = case (MountConfig -> Maybe MirrorEndpoint
mntMirrorTarget MountConfig
mcfg, MountConfig -> Maybe PrivateEndpoint
mntPrivateUpstream MountConfig
mcfg) of
    (Just MirrorEndpoint
mirrorTarget, Just PrivateEndpoint
privateUpstream) ->
        SettledPolicy
-> Ecosystem
-> Target
-> MirrorEndpoint
-> MountConfig
-> Either [ConfigError] Mount
resolveMirrored SettledPolicy
policy Ecosystem
eco (PrivateEndpoint -> Target
preTarget PrivateEndpoint
privateUpstream) MirrorEndpoint
mirrorTarget MountConfig
mcfg
    (Just MirrorEndpoint
_, Maybe PrivateEndpoint
Nothing) -> [ConfigError] -> Either [ConfigError] Mount
forall a b. a -> Either a b
Left [Ecosystem -> ConfigError
MountMissingPrivateUpstream Ecosystem
eco]
    (Maybe MirrorEndpoint
Nothing, Maybe PrivateEndpoint
mPrivate) -> SettledPolicy
-> Ecosystem
-> Maybe Target
-> MountConfig
-> Either [ConfigError] Mount
resolveServeOnly SettledPolicy
policy Ecosystem
eco (PrivateEndpoint -> Target
preTarget (PrivateEndpoint -> Target)
-> Maybe PrivateEndpoint -> Maybe Target
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe PrivateEndpoint
mPrivate) MountConfig
mcfg

resolveMirrored :: SettledPolicy -> Ecosystem -> Target -> MirrorEndpoint -> MountConfig -> Either [ConfigError] Mount
resolveMirrored :: SettledPolicy
-> Ecosystem
-> Target
-> MirrorEndpoint
-> MountConfig
-> Either [ConfigError] Mount
resolveMirrored SettledPolicy
policy Ecosystem
eco Target
privateUpstream MirrorEndpoint
mirrorTarget MountConfig
mcfg = do
    backend <- (ConfigError -> [ConfigError])
-> Either ConfigError StoreBackend
-> Either [ConfigError] StoreBackend
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 (ConfigError -> [ConfigError] -> [ConfigError]
forall a. a -> [a] -> [a]
: []) (Ecosystem -> MirrorEndpoint -> Either ConfigError StoreBackend
resolveStoreBackend Ecosystem
eco MirrorEndpoint
mirrorTarget)
    Right $
        mountOf eco mcfg policy $
            Mirrored
                MirroredLegs
                    { mlPrivateUpstream = tgtUrl privateUpstream
                    , mlMirrorTarget =
                        MirrorTarget
                            { mtUrl = meUrl mirrorTarget
                            , mtBackend = backend
                            , mtUploadUrl = fromMaybe (meUrl mirrorTarget) (meUploadUrl mirrorTarget)
                            , mtUsername = meUsername mirrorTarget
                            }
                    }

resolveServeOnly :: SettledPolicy -> Ecosystem -> Maybe Target -> MountConfig -> Either [ConfigError] Mount
resolveServeOnly :: SettledPolicy
-> Ecosystem
-> Maybe Target
-> MountConfig
-> Either [ConfigError] Mount
resolveServeOnly SettledPolicy
policy Ecosystem
eco Maybe Target
mPrivate MountConfig
mcfg =
    Mount -> Either [ConfigError] Mount
forall a b. b -> Either a b
Right (Ecosystem -> MountConfig -> SettledPolicy -> MountMode -> Mount
mountOf Ecosystem
eco MountConfig
mcfg SettledPolicy
policy (Maybe RegistryUrl -> MountMode
ServeOnly (Target -> RegistryUrl
tgtUrl (Target -> RegistryUrl) -> Maybe Target -> Maybe RegistryUrl
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Target
mPrivate)))

mountRulePatch :: Value -> Ecosystem -> Either [ConfigError] RulePatch
mountRulePatch :: Value -> Ecosystem -> Either [ConfigError] RulePatch
mountRulePatch Value
ast Ecosystem
eco =
    (String -> [ConfigError])
-> Either String RulePatch -> Either [ConfigError] RulePatch
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 (ConfigError -> [ConfigError]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ConfigError -> [ConfigError])
-> (String -> ConfigError) -> String -> [ConfigError]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ConfigError
ParseError (Text -> ConfigError) -> (String -> Text) -> String -> ConfigError
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text
prefix Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>) (Text -> Text) -> (String -> Text) -> String -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack) (Either String RulePatch -> Either [ConfigError] RulePatch)
-> Either String RulePatch -> Either [ConfigError] RulePatch
forall a b. (a -> b) -> a -> b
$
        (Value -> Parser RulePatch) -> Value -> Either String RulePatch
forall a b. (a -> Parser b) -> a -> Either String b
parseEither Value -> Parser RulePatch
parseRulesPatch (Value -> Maybe Value -> Value
forall a. a -> Maybe a -> a
fromMaybe (Object -> Value
Object Object
forall a. Monoid a => a
mempty) ([Text] -> Value -> Maybe Value
lookupPath [Text
"mounts", Ecosystem -> Text
ecosystemName Ecosystem
eco] Value
ast))
  where
    prefix :: Text
prefix = Text
"mounts." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Ecosystem -> Text
ecosystemName Ecosystem
eco Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
".rules parse error: "

resolveMountPolicy :: PendingPolicy -> RulePatch -> RulePatch -> Either [ConfigError] PendingPolicy
resolveMountPolicy :: PendingPolicy
-> RulePatch -> RulePatch -> Either [ConfigError] PendingPolicy
resolveMountPolicy PendingPolicy
shared RulePatch
shipped RulePatch
declared =
    ([PolicyError] -> [ConfigError])
-> Either [PolicyError] PendingPolicy
-> Either [ConfigError] PendingPolicy
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 (ConfigError -> [ConfigError]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ConfigError -> [ConfigError])
-> ([PolicyError] -> ConfigError) -> [PolicyError] -> [ConfigError]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [PolicyError] -> ConfigError
PolicyErrors) (PendingPolicy -> RulePatch -> Either [PolicyError] PendingPolicy
resolvePendingPolicy PendingPolicy
shared RulePatch
shipped Either [PolicyError] PendingPolicy
-> (PendingPolicy -> Either [PolicyError] PendingPolicy)
-> Either [PolicyError] PendingPolicy
forall a b.
Either [PolicyError] a
-> (a -> Either [PolicyError] b) -> Either [PolicyError] b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (PendingPolicy -> RulePatch -> Either [PolicyError] PendingPolicy
`resolvePendingPolicy` RulePatch
declared))

mountOf :: Ecosystem -> MountConfig -> SettledPolicy -> MountMode -> Mount
mountOf :: Ecosystem -> MountConfig -> SettledPolicy -> MountMode -> Mount
mountOf Ecosystem
eco MountConfig
mcfg (SettledPolicy RulePolicy
policy Map Text RuleInactivity
inactive) MountMode
mode =
    Mount
        { mountEcosystem :: Ecosystem
mountEcosystem = Ecosystem
eco
        , mountRegistries :: MountRegistries
mountRegistries =
            MountRegistries
                { regPublicUpstream :: RegistryUrl
regPublicUpstream = MountConfig -> RegistryUrl
mntPublicUpstream MountConfig
mcfg
                , regMode :: MountMode
regMode = MountMode
mode
                }
        , mountPolicy :: [PrecededRule]
mountPolicy = RulePolicy -> [PrecededRule]
rulesOf RulePolicy
policy
        , mountInactiveRules :: Map Text RuleInactivity
mountInactiveRules = Map Text RuleInactivity
inactive
        }

rulesOf :: RulePolicy -> [PrecededRule]
rulesOf :: RulePolicy -> [PrecededRule]
rulesOf = Map Text PrecededRule -> [PrecededRule]
forall k a. Map k a -> [a]
Map.elems (Map Text PrecededRule -> [PrecededRule])
-> (RulePolicy -> Map Text PrecededRule)
-> RulePolicy
-> [PrecededRule]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RulePolicy -> Map Text PrecededRule
policyRules

-- | Whether two endpoints name one registry: the authority folds to lower case with its default port, and the path compares past a trailing slash.
sameRegistry :: RegistryUrl -> RegistryUrl -> Bool
sameRegistry :: RegistryUrl -> RegistryUrl -> Bool
sameRegistry RegistryUrl
a RegistryUrl
b = RegistryUrl -> (Maybe HostPort, Text)
registryKey RegistryUrl
a (Maybe HostPort, Text) -> (Maybe HostPort, Text) -> Bool
forall a. Eq a => a -> a -> Bool
== RegistryUrl -> (Maybe HostPort, Text)
registryKey RegistryUrl
b

{- The key two endpoints are equal on. A boot refusal gates permanent deletion on it, so the
authority folds the way DNS and TLS resolve it, and two unreadable ones count as one store. -}
registryKey :: RegistryUrl -> (Maybe HostPort, Text)
registryKey :: RegistryUrl -> (Maybe HostPort, Text)
registryKey RegistryUrl
url = (Text -> Maybe HostPort
hostPortAddress Text
raw, Text -> Text
stripTrailingSlash (Text -> Text
registryPath Text
raw))
  where
    raw :: Text
raw = RegistryUrl -> Text
registryUrlText RegistryUrl
url

-- | One line per resolved leaf of the layers 'Config' loaded from: path, redacted value, layer, and any rule inactivity.
resolvedKeyProvenance :: Config -> [(String, String)] -> Maybe ByteString -> [Text]
resolvedKeyProvenance :: Config -> [(String, String)] -> Maybe StrictByteString -> [Text]
resolvedKeyProvenance Config
config [(String, String)]
envVars Maybe StrictByteString
mBytes = [Text] -> Either [ConfigError] [Text] -> [Text]
forall b a. b -> Either a b -> b
fromRight [] (Either [ConfigError] [Text] -> [Text])
-> Either [ConfigError] [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ do
    defaultAst <- StrictByteString -> Either [ConfigError] Value
parseDefaultAst StrictByteString
defaultConfigBytes
    docAst <- parseDocumentAst mBytes
    let envAst = [(String, String)] -> Value
buildEnvAst [(String, String)]
envVars
        merged = Value -> Value -> Value
deepMerge Value
defaultAst (Value -> Value -> Value
deepMerge Value
docAst Value
envAst)
    pure (map (renderResolvedLeaf config envAst docAst) (sortOn fst (leafPaths [] merged)))

leafPaths :: [Text] -> Value -> [(Text, Value)]
leafPaths :: [Text] -> Value -> [(Text, Value)]
leafPaths [Text]
path (Object Object
o) =
    ((Key, Value) -> [(Text, Value)])
-> [(Key, Value)] -> [(Text, Value)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\(Key
k, Value
v) -> [Text] -> Value -> [(Text, Value)]
leafPaths ([Text]
path [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> [Key -> Text
Key.toText Key
k]) Value
v) (Object -> [(Key, Value)]
forall v. KeyMap v -> [(Key, v)]
KeyMap.toList Object
o)
leafPaths [Text]
path Value
v = [(Text -> [Text] -> Text
T.intercalate Text
"." [Text]
path, Value
v)]

renderResolvedLeaf :: Config -> Value -> Value -> (Text, Value) -> Text
renderResolvedLeaf :: Config -> Value -> Value -> (Text, Value) -> Text
renderResolvedLeaf Config
config Value
envAst Value
docAst (Text
path, Value
v) =
    Text
"config: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
path Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" = " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Value -> Text
renderLeafValue Text
path Value
v Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" (" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
source Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> (RuleInactivity -> Text) -> Maybe RuleInactivity -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" RuleInactivity -> Text
inactiveMarker (Config -> [Text] -> Maybe RuleInactivity
leafInactivity Config
config [Text]
segments) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"
  where
    segments :: [Text]
segments = HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
T.splitOn Text
"." Text
path
    source :: Text
source
        | Value -> Bool
pathPresentIn Value
envAst = Text
"environment"
        | Value -> Bool
pathPresentIn Value
docAst = Text
"document"
        | Bool
otherwise = Text
"default"
    pathPresentIn :: Value -> Bool
pathPresentIn Value
ast = Maybe Value -> Bool
forall a. Maybe a -> Bool
isJust ([Text] -> Value -> Maybe Value
lookupPath [Text]
segments Value
ast)

-- A shared rule's key reads the shared inactivity, and a mount rule's key that mount's.
leafInactivity :: Config -> [Text] -> Maybe RuleInactivity
leafInactivity :: Config -> [Text] -> Maybe RuleInactivity
leafInactivity Config
config = \case
    Text
"rules" : Text
name : [Text]
_ -> Text -> Map Text RuleInactivity -> Maybe RuleInactivity
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
name (Config -> Map Text RuleInactivity
configInactiveRules Config
config)
    Text
"mounts" : Text
eco : Text
"rules" : Text
name : [Text]
_ -> Text -> Map Text RuleInactivity -> Maybe RuleInactivity
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
name (Map Text RuleInactivity -> Maybe RuleInactivity)
-> (Mount -> Map Text RuleInactivity)
-> Mount
-> Maybe RuleInactivity
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Mount -> Map Text RuleInactivity
mountInactiveRules (Mount -> Maybe RuleInactivity)
-> Maybe Mount -> Maybe RuleInactivity
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (Ecosystem -> MountMap -> Maybe Mount
forall k a. Ord k => k -> Map k a -> Maybe a
`Map.lookup` Config -> MountMap
configMounts Config
config) (Ecosystem -> Maybe Mount) -> Maybe Ecosystem -> Maybe Mount
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Text -> Maybe Ecosystem
parseEcosystem Text
eco
    [Text]
_ -> Maybe RuleInactivity
forall a. Maybe a
Nothing

inactiveMarker :: RuleInactivity -> Text
inactiveMarker :: RuleInactivity -> Text
inactiveMarker = \case
    RuleInactivity
NoAdvisorySource -> Text
", inactive: no advisories.url"

lookupPath :: [Text] -> Value -> Maybe Value
lookupPath :: [Text] -> Value -> Maybe Value
lookupPath [] Value
ast = Value -> Maybe Value
forall a. a -> Maybe a
Just Value
ast
lookupPath (Text
k : [Text]
ks) (Object Object
o) = [Text] -> Value -> Maybe Value
lookupPath [Text]
ks (Value -> Maybe Value) -> Maybe Value -> Maybe Value
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup (Text -> Key
Key.fromText Text
k) Object
o
lookupPath [Text]
_ Value
_ = Maybe Value
forall a. Maybe a
Nothing

-- Secret-typed keys are redacted: the provenance dump must never widen a secret's exposure
-- beyond the layer it arrived on.
renderLeafValue :: Text -> Value -> Text
renderLeafValue :: Text -> Value -> Text
renderLeafValue Text
path Value
v
    | (Text -> Bool) -> [Text] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Text -> Text -> Bool
`T.isSuffixOf` Text
path) [Text]
secretLeafKeys = Text
"<redacted>"
    | Bool
otherwise = case Value
v of
        String Text
t -> Text
t
        Value
other -> StrictByteString -> Text
forall a b. ConvertUtf8 a b => b -> a
decodeUtf8 (LazyByteString -> StrictByteString
LBS.toStrict (Value -> LazyByteString
forall a. ToJSON a => a -> LazyByteString
encode Value
other))

-- | Mount modes followed by the live-environment limits of @check-config@, shared with boot.
mountPostureLines :: Config -> [Text]
mountPostureLines :: Config -> [Text]
mountPostureLines Config
config =
    ((Ecosystem, Mount) -> Text) -> [(Ecosystem, Mount)] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Ecosystem, Mount) -> Text
postureLine [(Ecosystem, Mount)]
mounts [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> ((Ecosystem, Mount) -> Maybe Text)
-> [(Ecosystem, Mount)] -> [Text]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Ecosystem, Mount) -> Maybe Text
maintenanceClientLine [(Ecosystem, Mount)]
mounts [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> ((Ecosystem, Mount) -> Maybe Text)
-> [(Ecosystem, Mount)] -> [Text]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Ecosystem, Mount) -> Maybe Text
upstreamProbeLine [(Ecosystem, Mount)]
mounts
  where
    mounts :: [(Ecosystem, Mount)]
mounts = MountMap -> [(Ecosystem, Mount)]
forall k a. Map k a -> [(k, a)]
Map.toAscList (Config -> MountMap
configMounts Config
config)

{- Asking a backend what its private upstream aggregates is a call against the live control plane,
which a checker makes none of. -}
upstreamProbeLine :: (Ecosystem, Mount) -> Maybe Text
upstreamProbeLine :: (Ecosystem, Mount) -> Maybe Text
upstreamProbeLine (Ecosystem
eco, Mount
mount) =
    Text
notice Text -> Maybe RegistryUrl -> Maybe Text
forall a b. a -> Maybe b -> Maybe a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ MountRegistries -> Maybe RegistryUrl
regPrivateUpstream (Mount -> MountRegistries
mountRegistries Mount
mount)
  where
    notice :: Text
notice =
        Text
"mount \""
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Ecosystem -> Text
ecosystemName Ecosystem
eco
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\": the private upstream is asked at boot whether it, or a repository in its upstream chain, connects to a public registry. check-config does not make that call."

maintenanceClientLine :: (Ecosystem, Mount) -> Maybe Text
maintenanceClientLine :: (Ecosystem, Mount) -> Maybe Text
maintenanceClientLine (Ecosystem
eco, Mount
mount) = do
    target <- MountRegistries -> Maybe MirrorTarget
regMirrorTarget (Mount -> MountRegistries
mountRegistries Mount
mount)
    case sbControl (mtBackend target) of
        ControlPlane
ControlNone -> Maybe Text
forall a. Maybe a
Nothing
        ControlCodeArtifact{} -> Text -> Maybe Text
forall a. a -> Maybe a
Just Text
notice
        ControlProtocol{} -> Text -> Maybe Text
forall a. a -> Maybe a
Just Text
notice
  where
    notice :: Text
notice =
        Text
"mount \""
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Ecosystem -> Text
ecosystemName Ecosystem
eco
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\": the store maintenance client is built at boot against the live environment. check-config does not attempt this build."

-- | One mount's maximum advisory push age, derived from its own rules unless @advisories.maxAgeSeconds@ overrides it.
mountAdvisoryAge :: AdvisoriesSettings -> Mount -> MaxAdvisoryAge
mountAdvisoryAge :: AdvisoriesSettings -> Mount -> MaxAdvisoryAge
mountAdvisoryAge AdvisoriesSettings
advisories = Maybe NominalDiffTime -> [Rule] -> MaxAdvisoryAge
maxAdvisoryAgeFor (AdvisoriesSettings -> Maybe NominalDiffTime
advMaxAgeSeconds AdvisoriesSettings
advisories) ([Rule] -> MaxAdvisoryAge)
-> (Mount -> [Rule]) -> Mount -> MaxAdvisoryAge
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Mount -> [Rule]
mountRulesOf

-- | Require enrichment when this mount's resolved policy holds a rule that reads EPSS scores.
mountEpssRequirement :: Mount -> EpssRequirement
mountEpssRequirement :: Mount -> EpssRequirement
mountEpssRequirement = EpssRequirement -> EpssRequirement -> Bool -> EpssRequirement
forall a. a -> a -> Bool -> a
bool EpssRequirement
EpssRequired EpssRequirement
EpssOptional (Bool -> EpssRequirement)
-> (Mount -> Bool) -> Mount -> EpssRequirement
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([Text] -> Bool) -> (Mount -> [Text]) -> Mount -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Mount -> [Text]
mountEpssRules

mountEpssRules :: Mount -> [Text]
mountEpssRules :: Mount -> [Text]
mountEpssRules = [Text] -> [Text]
forall a. Ord a => [a] -> [a]
ordNub ([Text] -> [Text]) -> (Mount -> [Text]) -> Mount -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Rule -> Text) -> [Rule] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Rule -> Text
ruleName ([Rule] -> [Text]) -> (Mount -> [Rule]) -> Mount -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Rule -> Bool) -> [Rule] -> [Rule]
forall a. (a -> Bool) -> [a] -> [a]
filter Rule -> Bool
requiresEpss ([Rule] -> [Rule]) -> (Mount -> [Rule]) -> Mount -> [Rule]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Mount -> [Rule]
mountRulesOf

-- | A compile's EPSS requirement for this mount. Without a source it cannot tell which rules its consumers run.
compileMountEpssRequirement :: AdvisoriesSettings -> Mount -> EpssRequirement
compileMountEpssRequirement :: AdvisoriesSettings -> Mount -> EpssRequirement
compileMountEpssRequirement AdvisoriesSettings
advisories Mount
mount = case AdvisoriesSettings -> Mount -> EpssBasis
compileEpssBasis AdvisoriesSettings
advisories Mount
mount of
    EpssBasis
EpssUnread -> EpssRequirement
EpssOptional
    EpssReadBy NonEmpty Text
_ -> EpssRequirement
EpssRequired
    EpssBasis
EpssUnsourced -> EpssRequirement
EpssRequired

-- Why a compile for one mount needs EPSS enrichment, or why it does not.
data EpssBasis = EpssReadBy (NonEmpty Text) | EpssUnsourced | EpssUnread

compileEpssBasis :: AdvisoriesSettings -> Mount -> EpssBasis
compileEpssBasis :: AdvisoriesSettings -> Mount -> EpssBasis
compileEpssBasis AdvisoriesSettings
advisories Mount
mount = case ([Text] -> Maybe (NonEmpty Text)
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty (Mount -> [Text]
mountEpssRules Mount
mount), AdvisoriesSettings -> Maybe AdvisoryStoreUrl
advUrl AdvisoriesSettings
advisories) of
    (Just NonEmpty Text
rules, Maybe AdvisoryStoreUrl
_) -> NonEmpty Text -> EpssBasis
EpssReadBy NonEmpty Text
rules
    (Maybe (NonEmpty Text)
Nothing, Maybe AdvisoryStoreUrl
Nothing) -> EpssBasis
EpssUnsourced
    (Maybe (NonEmpty Text)
Nothing, Just AdvisoryStoreUrl
_) -> EpssBasis
EpssUnread

mountRulesOf :: Mount -> [Rule]
mountRulesOf :: Mount -> [Rule]
mountRulesOf = (PrecededRule -> Rule) -> [PrecededRule] -> [Rule]
forall a b. (a -> b) -> [a] -> [b]
map PrecededRule -> Rule
prRule ([PrecededRule] -> [Rule])
-> (Mount -> [PrecededRule]) -> Mount -> [Rule]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Mount -> [PrecededRule]
mountPolicy

-- | This mount's rules that refuse without advisory evidence. Any one makes a store mandatory and readiness wait for it.
mountAdvisoryDenials :: Mount -> [Text]
mountAdvisoryDenials :: Mount -> [Text]
mountAdvisoryDenials = [Text] -> [Text]
forall a. Ord a => [a] -> [a]
ordNub ([Text] -> [Text]) -> (Mount -> [Text]) -> Mount -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Rule -> Text) -> [Rule] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Rule -> Text
ruleName ([Rule] -> [Text]) -> (Mount -> [Rule]) -> Mount -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Rule -> Bool) -> [Rule] -> [Rule]
forall a. (a -> Bool) -> [a] -> [a]
filter Rule -> Bool
deniesOnAdvisories ([Rule] -> [Rule]) -> (Mount -> [Rule]) -> Mount -> [Rule]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Mount -> [Rule]
mountRulesOf

-- | Whether this mount must hold an advisory database before it can serve anything.
mountDatabaseRequirement :: Mount -> DatabaseRequirement
mountDatabaseRequirement :: Mount -> DatabaseRequirement
mountDatabaseRequirement = DatabaseRequirement
-> DatabaseRequirement -> Bool -> DatabaseRequirement
forall a. a -> a -> Bool -> a
bool DatabaseRequirement
DatabaseRequired DatabaseRequirement
DatabaseOptional (Bool -> DatabaseRequirement)
-> (Mount -> Bool) -> Mount -> DatabaseRequirement
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([Text] -> Bool) -> (Mount -> [Text]) -> Mount -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Mount -> [Text]
mountAdvisoryDenials

-- | The maximum push age of every mount whose rules read advisories, with its basis. Without a store nothing has an age.
advisoryAgeLines :: Config -> [Text]
advisoryAgeLines :: Config -> [Text]
advisoryAgeLines Config
config =
    [ Ecosystem -> MaxAdvisoryAge -> Text
ageLine Ecosystem
eco (AdvisoriesSettings -> Mount -> MaxAdvisoryAge
mountAdvisoryAge AdvisoriesSettings
advisories Mount
mount)
    | AppConfig -> Bool
advisorySourced (Config -> AppConfig
configApp Config
config)
    , (Ecosystem
eco, Mount
mount) <- MountMap -> [(Ecosystem, Mount)]
forall k a. Map k a -> [(k, a)]
Map.toAscList (Config -> MountMap
configMounts Config
config)
    , (Rule -> Bool) -> [Rule] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Rule -> Bool
readsAdvisories (Mount -> [Rule]
mountRulesOf Mount
mount)
    ]
  where
    advisories :: AdvisoriesSettings
advisories = AppConfig -> AdvisoriesSettings
cfgAdvisories (Config -> AppConfig
configApp Config
config)

ageLine :: Ecosystem -> MaxAdvisoryAge -> Text
ageLine :: Ecosystem -> MaxAdvisoryAge -> Text
ageLine Ecosystem
eco MaxAdvisoryAge
limit =
    Text
"mount \""
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Ecosystem -> Text
ecosystemName Ecosystem
eco
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\": advisory denial refuses on an advisory push older than "
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> NominalDiffTime -> Text
renderDuration (MaxAdvisoryAge -> NominalDiffTime
maxAdvisoryAge MaxAdvisoryAge
limit)
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", "
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> AdvisoryAgeBasis -> Text
renderBasis (MaxAdvisoryAge -> AdvisoryAgeBasis
maxAdvisoryAgeBasis MaxAdvisoryAge
limit)

-- | Each mount's EPSS requirement, as 'compileMountEpssRequirement' decides it, with or without a store.
advisoryEpssLines :: Config -> [Text]
advisoryEpssLines :: Config -> [Text]
advisoryEpssLines Config
config =
    [Ecosystem -> EpssBasis -> Text
epssLine Ecosystem
eco (AdvisoriesSettings -> Mount -> EpssBasis
compileEpssBasis (AppConfig -> AdvisoriesSettings
cfgAdvisories (Config -> AppConfig
configApp Config
config)) Mount
mount) | (Ecosystem
eco, Mount
mount) <- MountMap -> [(Ecosystem, Mount)]
forall k a. Map k a -> [(k, a)]
Map.toAscList (Config -> MountMap
configMounts Config
config)]

epssLine :: Ecosystem -> EpssBasis -> Text
epssLine :: Ecosystem -> EpssBasis -> Text
epssLine Ecosystem
eco = \case
    EpssBasis
EpssUnread -> Text
prefix Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"EPSS enrichment is optional, because no active rule reads EPSS scores. A failed EPSS feed publishes the OSV data with epss_status=unavailable"
    EpssReadBy NonEmpty Text
rules -> Text
prefix Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"EPSS enrichment is required, because these active rules read EPSS scores: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> [Text] -> Text
T.intercalate Text
", " (NonEmpty Text -> [Text]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList NonEmpty Text
rules) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
". A failed EPSS feed publishes no artifact, and consumers refuse one without enrichment"
    EpssBasis
EpssUnsourced -> Text
prefix Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"EPSS enrichment is required for a compile. With no advisories.url, no source activates the shipped remediation rule, so a compile cannot tell which rules its consumers run. A failed EPSS feed publishes no artifact"
  where
    prefix :: Text
prefix = Text
"mount \"" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Ecosystem -> Text
ecosystemName Ecosystem
eco Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\": "

renderBasis :: AdvisoryAgeBasis -> Text
renderBasis :: AdvisoryAgeBasis -> Text
renderBasis = \case
    AdvisoryAgeBasis
AgeConfigured -> Text
"set by advisories.maxAgeSeconds"
    AgeBeforeQuarantine NominalDiffTime
quarantine ->
        Text
"derived a day ahead of this mount's earliest AllowIfOlderThan quarantine of " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> NominalDiffTime -> Text
renderDuration NominalDiffTime
quarantine
    AdvisoryAgeBasis
AgeFloor -> Text
"the shipped floor, which no derivation goes below"

postureLine :: (Ecosystem, Mount) -> Text
postureLine :: (Ecosystem, Mount) -> Text
postureLine (Ecosystem
eco, Mount
mount) = case MountRegistries -> MountMode
regMode (Mount -> MountRegistries
mountRegistries Mount
mount) of
    Mirrored MirroredLegs
legs ->
        Text
"mount \""
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Ecosystem -> Text
ecosystemName Ecosystem
eco
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\": mirrored; admitted public artifacts back-fill the "
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> StoreTag -> Text
storeTagName (StoreBackend -> StoreTag
sbTag (MirrorTarget -> StoreBackend
mtBackend (MirroredLegs -> MirrorTarget
mlMirrorTarget MirroredLegs
legs)))
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" store "
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> RegistryUrl -> Text
registryUrlText (MirrorTarget -> RegistryUrl
mtUrl (MirroredLegs -> MirrorTarget
mlMirrorTarget MirroredLegs
legs))
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> StoreBackend -> Text
consentClause (MirrorTarget -> StoreBackend
mtBackend (MirroredLegs -> MirrorTarget
mlMirrorTarget MirroredLegs
legs))
    ServeOnly (Just RegistryUrl
private) ->
        Text
"mount \""
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Ecosystem -> Text
ecosystemName Ecosystem
eco
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\": serve-only (no mirrorTarget declared): merges the private upstream "
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> RegistryUrl -> Text
registryUrlText RegistryUrl
private
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" and never mirrors; admitted public artifacts stay on the gated public leg"
    ServeOnly Maybe RegistryUrl
Nothing ->
        Text
"mount \""
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Ecosystem -> Text
ecosystemName Ecosystem
eco
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\": serve-only pure public gate (no private upstream, no mirrorTarget): every artifact streams from the gated public leg and is never mirrored"

{- The deletion consent a store carries, which the operator declares on the store itself. Only a
Verdaccio store carries one, so the clause is empty everywhere else. -}
consentClause :: StoreBackend -> Text
consentClause :: StoreBackend -> Text
consentClause = \case
    BackendVerdaccio Secret
_ DeletionConsent
DeletionPermitted -> Text
", which permits deletion"
    BackendVerdaccio Secret
_ DeletionConsent
DeletionWithheld -> Text
", which withholds deletion"
    BackendRegistry{} -> Text
""
    BackendCodeArtifact{} -> Text
""