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)
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)
data PendingPolicy = PendingPolicy RulePolicy (Maybe RuleInactivity) (Map Text Activation)
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)
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)
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)
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
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)
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)
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
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)))
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)
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]
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
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]]
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]]
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
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
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
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)
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
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))
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)
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."
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
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
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
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
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
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
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)
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"
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
""