-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT
{-# LANGUAGE TupleSections #-}

{- | Resolve named rule patches against a base policy, accumulating malformed entries.
Mount patches keep unspecified parameters and phases, or remove an entry with @enabled: false@.
-}
module Ecluse.Config.Rule (
    -- * Policies
    RulePolicy (..),
    emptyPolicy,
    resolvePolicy,

    -- * Declared patches
    RulePatch (..),
    RuleEntry (..),
    knownRuleTypes,

    -- * Refusals
    PolicyError (..),
    renderPolicyError,
) where

import Prelude hiding (universe)

import Data.Map.Strict qualified as Map
import Data.Text qualified as T
import Data.Time (NominalDiffTime)
import Data.Universe.Class (Universe (universe))
import Validation (eitherToValidation, validationToEither)

import Ecluse.Core.Package (mkScope)
import Ecluse.Core.Rules.Types (
    AllowIfRemediatesCveParams (..),
    DenyIfCveParams (..),
    DenyIfEpssParams (..),
    DifferentialLimits (..),
    FailureAlignment (..),
    PrecededRule (..),
    RemainingAdvisories (..),
    Rule (..),
    RuleReach (..),
    RuleType,
    ScoreGap (..),
    defaultDifferentialLimits,
    defaultPrecedence,
    ruleDenies,
    ruleName,
 )
import Ecluse.Core.Wire (renderWire)

-- | A resolved rule set, keyed by the rule name an operator patches it under.
newtype RulePolicy = RulePolicy
    { RulePolicy -> Map Text PrecededRule
policyRules :: Map Text PrecededRule
    }
    deriving stock (RulePolicy -> RulePolicy -> Bool
(RulePolicy -> RulePolicy -> Bool)
-> (RulePolicy -> RulePolicy -> Bool) -> Eq RulePolicy
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: RulePolicy -> RulePolicy -> Bool
== :: RulePolicy -> RulePolicy -> Bool
$c/= :: RulePolicy -> RulePolicy -> Bool
/= :: RulePolicy -> RulePolicy -> Bool
Eq, Int -> RulePolicy -> ShowS
[RulePolicy] -> ShowS
RulePolicy -> String
(Int -> RulePolicy -> ShowS)
-> (RulePolicy -> String)
-> ([RulePolicy] -> ShowS)
-> Show RulePolicy
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> RulePolicy -> ShowS
showsPrec :: Int -> RulePolicy -> ShowS
$cshow :: RulePolicy -> String
show :: RulePolicy -> String
$cshowList :: [RulePolicy] -> ShowS
showList :: [RulePolicy] -> ShowS
Show)

-- | The policy a load starts from before the shipped defaults are applied.
emptyPolicy :: RulePolicy
emptyPolicy :: RulePolicy
emptyPolicy = Map Text PrecededRule -> RulePolicy
RulePolicy Map Text PrecededRule
forall k a. Map k a
Map.empty

-- | A declared @rules@ object: one 'RuleEntry' per rule name it names.
newtype RulePatch = RulePatch (Map Text RuleEntry)
    deriving stock (RulePatch -> RulePatch -> Bool
(RulePatch -> RulePatch -> Bool)
-> (RulePatch -> RulePatch -> Bool) -> Eq RulePatch
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: RulePatch -> RulePatch -> Bool
== :: RulePatch -> RulePatch -> Bool
$c/= :: RulePatch -> RulePatch -> Bool
/= :: RulePatch -> RulePatch -> Bool
Eq, Int -> RulePatch -> ShowS
[RulePatch] -> ShowS
RulePatch -> String
(Int -> RulePatch -> ShowS)
-> (RulePatch -> String)
-> ([RulePatch] -> ShowS)
-> Show RulePatch
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> RulePatch -> ShowS
showsPrec :: Int -> RulePatch -> ShowS
$cshow :: RulePatch -> String
show :: RulePatch -> String
$cshowList :: [RulePatch] -> ShowS
showList :: [RulePatch] -> ShowS
Show)

-- | One rule's declared keys, every one optional. 'refuseStrayParameters' refuses those its type does not read.
data RuleEntry = RuleEntry
    { RuleEntry -> Maybe Text
entryType :: Maybe Text
    , RuleEntry -> Maybe Int
entryPrecedence :: Maybe Int
    , RuleEntry -> Maybe Bool
entryEnabled :: Maybe Bool
    , RuleEntry -> Maybe Integer
entryAgeSeconds :: Maybe Integer
    , RuleEntry -> Maybe Text
entryScope :: Maybe Text
    , RuleEntry -> Maybe Text
entryIdentity :: Maybe Text
    , RuleEntry -> Maybe Double
entryMinCvss :: Maybe Double
    , RuleEntry -> Maybe Double
entryMinEpss :: Maybe Double
    , RuleEntry -> Maybe Text
entryOnUnavailable :: Maybe Text
    , RuleEntry -> Maybe Text
entryRemainingAdvisories :: Maybe Text
    , RuleEntry -> Maybe Double
entryMaxRemainingCvss :: Maybe Double
    , RuleEntry -> Maybe Double
entryMaxRemainingEpss :: Maybe Double
    , RuleEntry -> Maybe Double
entryMinCvssGap :: Maybe Double
    , RuleEntry -> Maybe Double
entryMinEpssGap :: Maybe Double
    , RuleEntry -> Maybe [Text]
entryAppliesTo :: Maybe [Text]
    }
    deriving stock (RuleEntry -> RuleEntry -> Bool
(RuleEntry -> RuleEntry -> Bool)
-> (RuleEntry -> RuleEntry -> Bool) -> Eq RuleEntry
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: RuleEntry -> RuleEntry -> Bool
== :: RuleEntry -> RuleEntry -> Bool
$c/= :: RuleEntry -> RuleEntry -> Bool
/= :: RuleEntry -> RuleEntry -> Bool
Eq, Int -> RuleEntry -> ShowS
[RuleEntry] -> ShowS
RuleEntry -> String
(Int -> RuleEntry -> ShowS)
-> (RuleEntry -> String)
-> ([RuleEntry] -> ShowS)
-> Show RuleEntry
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> RuleEntry -> ShowS
showsPrec :: Int -> RuleEntry -> ShowS
$cshow :: RuleEntry -> String
show :: RuleEntry -> String
$cshowList :: [RuleEntry] -> ShowS
showList :: [RuleEntry] -> ShowS
Show)

-- | Why one declared rule was refused. A load reports every one it accumulated.
data PolicyError
    = MissingRuleType Text
    | UnknownRuleType Text Text
    | MalformedRule Text Text
    | SuppressUnknownRule Text
    deriving stock (PolicyError -> PolicyError -> Bool
(PolicyError -> PolicyError -> Bool)
-> (PolicyError -> PolicyError -> Bool) -> Eq PolicyError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PolicyError -> PolicyError -> Bool
== :: PolicyError -> PolicyError -> Bool
$c/= :: PolicyError -> PolicyError -> Bool
/= :: PolicyError -> PolicyError -> Bool
Eq, Int -> PolicyError -> ShowS
[PolicyError] -> ShowS
PolicyError -> String
(Int -> PolicyError -> ShowS)
-> (PolicyError -> String)
-> ([PolicyError] -> ShowS)
-> Show PolicyError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PolicyError -> ShowS
showsPrec :: Int -> PolicyError -> ShowS
$cshow :: PolicyError -> String
show :: PolicyError -> String
$cshowList :: [PolicyError] -> ShowS
showList :: [PolicyError] -> ShowS
Show)

-- | One refusal as the boot reports it, naming the rule it was declared under.
renderPolicyError :: PolicyError -> Text
renderPolicyError :: PolicyError -> Text
renderPolicyError = \case
    MissingRuleType Text
name ->
        Text
"rule " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
quote Text
name Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" is not a default and is missing its \"type\""
    UnknownRuleType Text
name Text
ty ->
        Text
"rule " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
quote Text
name Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" names unknown type " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
quote Text
ty
    MalformedRule Text
name Text
reason ->
        Text
"rule " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
quote Text
name Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
": " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
reason
    SuppressUnknownRule Text
name ->
        Text
"rule " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
quote Text
name Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" disables a rule that no default defines"

quote :: Text -> Text
quote :: Text -> Text
quote Text
t = Text
"\"" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
t Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\""

-- | Apply a patch to a base policy. Every entry resolves before any applies, so one load reports every refusal.
resolvePolicy :: RulePolicy -> RulePatch -> Either [PolicyError] RulePolicy
resolvePolicy :: RulePolicy -> RulePatch -> Either [PolicyError] RulePolicy
resolvePolicy (RulePolicy Map Text PrecededRule
base) (RulePatch Map Text RuleEntry
patch) =
    Validation [PolicyError] RulePolicy
-> Either [PolicyError] RulePolicy
forall e a. Validation e a -> Either e a
validationToEither (Validation [PolicyError] RulePolicy
 -> Either [PolicyError] RulePolicy)
-> Validation [PolicyError] RulePolicy
-> Either [PolicyError] RulePolicy
forall a b. (a -> b) -> a -> b
$
        Map Text PrecededRule -> RulePolicy
RulePolicy (Map Text PrecededRule -> RulePolicy)
-> ([(Text, Maybe PrecededRule)] -> Map Text PrecededRule)
-> [(Text, Maybe PrecededRule)]
-> RulePolicy
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Map Text PrecededRule
 -> (Text, Maybe PrecededRule) -> Map Text PrecededRule)
-> Map Text PrecededRule
-> [(Text, Maybe PrecededRule)]
-> Map Text PrecededRule
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Map Text PrecededRule
-> (Text, Maybe PrecededRule) -> Map Text PrecededRule
applyResolvedEntry Map Text PrecededRule
base
            ([(Text, Maybe PrecededRule)] -> RulePolicy)
-> Validation [PolicyError] [(Text, Maybe PrecededRule)]
-> Validation [PolicyError] RulePolicy
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Text, RuleEntry)
 -> Validation [PolicyError] (Text, Maybe PrecededRule))
-> [(Text, RuleEntry)]
-> Validation [PolicyError] [(Text, Maybe PrecededRule)]
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 (Either [PolicyError] (Text, Maybe PrecededRule)
-> Validation [PolicyError] (Text, Maybe PrecededRule)
forall e a. Either e a -> Validation e a
eitherToValidation (Either [PolicyError] (Text, Maybe PrecededRule)
 -> Validation [PolicyError] (Text, Maybe PrecededRule))
-> ((Text, RuleEntry)
    -> Either [PolicyError] (Text, Maybe PrecededRule))
-> (Text, RuleEntry)
-> Validation [PolicyError] (Text, Maybe PrecededRule)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map Text PrecededRule
-> (Text, RuleEntry)
-> Either [PolicyError] (Text, Maybe PrecededRule)
resolveEntry Map Text PrecededRule
base) (Map Text RuleEntry -> [(Text, RuleEntry)]
forall k a. Map k a -> [(k, a)]
Map.toList Map Text RuleEntry
patch)

applyResolvedEntry :: Map Text PrecededRule -> (Text, Maybe PrecededRule) -> Map Text PrecededRule
applyResolvedEntry :: Map Text PrecededRule
-> (Text, Maybe PrecededRule) -> Map Text PrecededRule
applyResolvedEntry Map Text PrecededRule
acc (Text
name, Maybe PrecededRule
Nothing) = Text -> Map Text PrecededRule -> Map Text PrecededRule
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete Text
name Map Text PrecededRule
acc
applyResolvedEntry Map Text PrecededRule
acc (Text
name, Just PrecededRule
pr) = Text
-> PrecededRule -> Map Text PrecededRule -> Map Text PrecededRule
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert Text
name PrecededRule
pr Map Text PrecededRule
acc

resolveEntry :: Map Text PrecededRule -> (Text, RuleEntry) -> Either [PolicyError] (Text, Maybe PrecededRule)
resolveEntry :: Map Text PrecededRule
-> (Text, RuleEntry)
-> Either [PolicyError] (Text, Maybe PrecededRule)
resolveEntry Map Text PrecededRule
base (Text
name, RuleEntry
entry)
    | RuleEntry -> Maybe Bool
entryEnabled RuleEntry
entry Maybe Bool -> Maybe Bool -> Bool
forall a. Eq a => a -> a -> Bool
== Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False =
        if Text -> Map Text PrecededRule -> Bool
forall k a. Ord k => k -> Map k a -> Bool
Map.member Text
name Map Text PrecededRule
base
            then (Text, Maybe PrecededRule)
-> Either [PolicyError] (Text, Maybe PrecededRule)
forall a b. b -> Either a b
Right (Text
name, Maybe PrecededRule
forall a. Maybe a
Nothing)
            else [PolicyError] -> Either [PolicyError] (Text, Maybe PrecededRule)
forall a b. a -> Either a b
Left [Text -> PolicyError
SuppressUnknownRule Text
name]
    | Bool
otherwise =
        case Text -> Map Text PrecededRule -> Maybe PrecededRule
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
name Map Text PrecededRule
base of
            Just PrecededRule
existing -> (Text
name,) (Maybe PrecededRule -> (Text, Maybe PrecededRule))
-> (PrecededRule -> Maybe PrecededRule)
-> PrecededRule
-> (Text, Maybe PrecededRule)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PrecededRule -> Maybe PrecededRule
forall a. a -> Maybe a
Just (PrecededRule -> (Text, Maybe PrecededRule))
-> Either [PolicyError] PrecededRule
-> Either [PolicyError] (Text, Maybe PrecededRule)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text
-> RuleEntry -> PrecededRule -> Either [PolicyError] PrecededRule
patchExistingRule Text
name RuleEntry
entry PrecededRule
existing
            Maybe PrecededRule
Nothing -> (Text
name,) (Maybe PrecededRule -> (Text, Maybe PrecededRule))
-> (PrecededRule -> Maybe PrecededRule)
-> PrecededRule
-> (Text, Maybe PrecededRule)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PrecededRule -> Maybe PrecededRule
forall a. a -> Maybe a
Just (PrecededRule -> (Text, Maybe PrecededRule))
-> Either [PolicyError] PrecededRule
-> Either [PolicyError] (Text, Maybe PrecededRule)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> RuleEntry -> Either [PolicyError] PrecededRule
addNewRule Text
name RuleEntry
entry

patchExistingRule :: Text -> RuleEntry -> PrecededRule -> Either [PolicyError] PrecededRule
patchExistingRule :: Text
-> RuleEntry -> PrecededRule -> Either [PolicyError] PrecededRule
patchExistingRule Text
name RuleEntry
entry (PrecededRule Int
prec RuleReach
reach Rule
rule) = do
    Text -> RuleEntry -> Rule -> Either [PolicyError] ()
checkRestatedType Text
name RuleEntry
entry Rule
rule
    Text -> Text -> RuleEntry -> Either [PolicyError] ()
refuseStrayParameters Text
name (Rule -> Text
ruleName Rule
rule) RuleEntry
entry
    rule' <- Text -> RuleEntry -> Rule -> Either [PolicyError] Rule
patchRuleValue Text
name RuleEntry
entry Rule
rule
    reach' <- resolveReach name entry reach rule'
    pure (PrecededRule (fromMaybe prec (entryPrecedence entry)) reach' rule')

-- The type is gated first, so a stray parameter is never reported against a type that does
-- not exist, and the stray before 'buildRule', so it is reported ahead of a missing required key.
addNewRule :: Text -> RuleEntry -> Either [PolicyError] PrecededRule
addNewRule :: Text -> RuleEntry -> Either [PolicyError] PrecededRule
addNewRule Text
name RuleEntry
entry = case RuleEntry -> Maybe Text
entryType RuleEntry
entry of
    Maybe Text
Nothing -> [PolicyError] -> Either [PolicyError] PrecededRule
forall a b. a -> Either a b
Left [Text -> PolicyError
MissingRuleType Text
name]
    Just Text
ty
        | Text
ty Text -> [Text] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`notElem` [Text]
knownRuleTypes -> [PolicyError] -> Either [PolicyError] PrecededRule
forall a b. a -> Either a b
Left [Text -> Text -> PolicyError
UnknownRuleType Text
name Text
ty]
        | Bool
otherwise -> do
            Text -> Text -> RuleEntry -> Either [PolicyError] ()
refuseStrayParameters Text
name Text
ty RuleEntry
entry
            rule <- Text -> Text -> RuleEntry -> Either [PolicyError] Rule
buildRule Text
name Text
ty RuleEntry
entry
            reach <- resolveReach name entry AdmissionAndRevocation rule
            pure (PrecededRule (fromMaybe (defaultPrecedence rule) (entryPrecedence entry)) reach rule)

checkRestatedType :: Text -> RuleEntry -> Rule -> Either [PolicyError] ()
checkRestatedType :: Text -> RuleEntry -> Rule -> Either [PolicyError] ()
checkRestatedType Text
name RuleEntry
entry Rule
rule = case RuleEntry -> Maybe Text
entryType RuleEntry
entry of
    Maybe Text
Nothing -> () -> Either [PolicyError] ()
forall a b. b -> Either a b
Right ()
    Just Text
ty
        | Text
ty Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Rule -> Text
ruleName Rule
rule -> () -> Either [PolicyError] ()
forall a b. b -> Either a b
Right ()
        | Text
ty Text -> [Text] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Text]
knownRuleTypes -> [PolicyError] -> Either [PolicyError] ()
forall a b. a -> Either a b
Left [Text -> Text -> PolicyError
MalformedRule Text
name (Text
"\"type\" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
quote Text
ty Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" does not match the default rule it patches")]
        | Bool
otherwise -> [PolicyError] -> Either [PolicyError] ()
forall a b. a -> Either a b
Left [Text -> Text -> PolicyError
UnknownRuleType Text
name Text
ty]

-- | The rule type names the diagnostics recognise. Any other name is an 'UnknownRuleType'.
knownRuleTypes :: [Text]
knownRuleTypes :: [Text]
knownRuleTypes = (RuleType -> Text) -> [RuleType] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map RuleType -> Text
forall a. (WireVocab a, Eq a) => a -> Text
renderWire ([RuleType]
forall a. Universe a => [a]
universe :: [RuleType])

-- Refuse a parameter the rule type does not read. Ignored, a threshold or an @onUnavailable@
-- under the wrong type reads on a deny gate as a setting the operator made and Écluse did not apply.
refuseStrayParameters :: Text -> Text -> RuleEntry -> Either [PolicyError] ()
refuseStrayParameters :: Text -> Text -> RuleEntry -> Either [PolicyError] ()
refuseStrayParameters Text
name Text
ty RuleEntry
entry =
    case (Text -> Bool) -> [Text] -> [Text]
forall a. (a -> Bool) -> [a] -> [a]
filter (Text -> [Text] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`notElem` Text -> [Text]
ruleTypeParameters Text
ty) (RuleEntry -> [Text]
setParameters RuleEntry
entry) of
        [] -> () -> Either [PolicyError] ()
forall a b. b -> Either a b
Right ()
        [Text]
strays ->
            [PolicyError] -> Either [PolicyError] ()
forall a b. a -> Either a b
Left
                [ Text -> Text -> PolicyError
MalformedRule
                    Text
name
                    (Text -> Text
quote Text
ty Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" does not read " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> [Text] -> Text
T.intercalate Text
", " ((Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Text
quote [Text]
strays))
                ]

-- The parameter keys one rule type reads, which 'buildRule' names the same keys for.
ruleTypeParameters :: Text -> [Text]
ruleTypeParameters :: Text -> [Text]
ruleTypeParameters = \case
    Text
"AllowIfOlderThan" -> [Text
"ageSeconds"]
    Text
"AllowScope" -> [Text
"scope"]
    Text
"AllowByIdentity" -> [Text
"identity"]
    Text
"DenyByIdentity" -> [Text
"identity"]
    Text
"AllowIfRemediatesCve" -> Text
"onUnavailable" Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: Text
"remainingAdvisories" Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: [Text]
limitKeys
    Text
"DenyIfKnownMalicious" -> [Text
"onUnavailable"]
    Text
"DenyIfCve" -> [Text
"minCvss", Text
"onUnavailable"]
    Text
"DenyIfEpss" -> [Text
"minEpss", Text
"onUnavailable"]
    Text
"DenyIfYanked" -> []
    Text
"DenyIfDeprecated" -> []
    Text
"DenyInstallTimeExecution" -> []
    Text
"DenyExternalDependencies" -> []
    Text
_ -> []

-- The parameter keys an entry actually sets, in a fixed order so a refusal reads the same twice.
setParameters :: RuleEntry -> [Text]
setParameters :: RuleEntry -> [Text]
setParameters RuleEntry
entry =
    [ Text
key
    | (Text
key, Bool
isSet) <-
        [ (Text
"ageSeconds", Maybe Integer -> Bool
forall a. Maybe a -> Bool
isJust (RuleEntry -> Maybe Integer
entryAgeSeconds RuleEntry
entry))
        , (Text
"scope", Maybe Text -> Bool
forall a. Maybe a -> Bool
isJust (RuleEntry -> Maybe Text
entryScope RuleEntry
entry))
        , (Text
"identity", Maybe Text -> Bool
forall a. Maybe a -> Bool
isJust (RuleEntry -> Maybe Text
entryIdentity RuleEntry
entry))
        , (Text
"minCvss", Maybe Double -> Bool
forall a. Maybe a -> Bool
isJust (RuleEntry -> Maybe Double
entryMinCvss RuleEntry
entry))
        , (Text
"minEpss", Maybe Double -> Bool
forall a. Maybe a -> Bool
isJust (RuleEntry -> Maybe Double
entryMinEpss RuleEntry
entry))
        , (Text
"onUnavailable", Maybe Text -> Bool
forall a. Maybe a -> Bool
isJust (RuleEntry -> Maybe Text
entryOnUnavailable RuleEntry
entry))
        , (Text
"remainingAdvisories", Maybe Text -> Bool
forall a. Maybe a -> Bool
isJust (RuleEntry -> Maybe Text
entryRemainingAdvisories RuleEntry
entry))
        ]
            [(Text, Bool)] -> [(Text, Bool)] -> [(Text, Bool)]
forall a. Semigroup a => a -> a -> a
<> ((Text, Maybe Double) -> (Text, Bool))
-> [(Text, Maybe Double)] -> [(Text, Bool)]
forall a b. (a -> b) -> [a] -> [b]
map ((Maybe Double -> Bool) -> (Text, Maybe Double) -> (Text, Bool)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second Maybe Double -> Bool
forall a. Maybe a -> Bool
isJust) (RuleEntry -> [(Text, Maybe Double)]
limitEntries RuleEntry
entry)
    , Bool
isSet
    ]

-- The differential's limit keys, which only @remainingAdvisories: differential@ reads.
limitKeys :: [Text]
limitKeys :: [Text]
limitKeys = [Text
"maxRemainingCvss", Text
"maxRemainingEpss", Text
"minCvssGap", Text
"minEpssGap"]

limitEntries :: RuleEntry -> [(Text, Maybe Double)]
limitEntries :: RuleEntry -> [(Text, Maybe Double)]
limitEntries RuleEntry
entry = [Text] -> [Maybe Double] -> [(Text, Maybe Double)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Text]
limitKeys [RuleEntry -> Maybe Double
entryMaxRemainingCvss RuleEntry
entry, RuleEntry -> Maybe Double
entryMaxRemainingEpss RuleEntry
entry, RuleEntry -> Maybe Double
entryMinCvssGap RuleEntry
entry, RuleEntry -> Maybe Double
entryMinEpssGap RuleEntry
entry]

buildRule :: Text -> Text -> RuleEntry -> Either [PolicyError] Rule
buildRule :: Text -> Text -> RuleEntry -> Either [PolicyError] Rule
buildRule Text
name Text
ty RuleEntry
entry = case Text
ty of
    Text
"AllowIfOlderThan" ->
        NominalDiffTime -> Rule
AllowIfOlderThan
            (NominalDiffTime -> Rule)
-> Either [PolicyError] NominalDiffTime
-> Either [PolicyError] Rule
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text
-> Text
-> Text
-> (Integer -> Either [PolicyError] NominalDiffTime)
-> Maybe Integer
-> Either [PolicyError] NominalDiffTime
forall a b.
Text
-> Text
-> Text
-> (a -> Either [PolicyError] b)
-> Maybe a
-> Either [PolicyError] b
requireField Text
name Text
"AllowIfOlderThan" Text
"ageSeconds" (Text -> Integer -> Either [PolicyError] NominalDiffTime
validateAgeSeconds Text
name) (RuleEntry -> Maybe Integer
entryAgeSeconds RuleEntry
entry)
    Text
"AllowScope" ->
        Scope -> Rule
AllowScope (Scope -> Rule) -> (Text -> Scope) -> Text -> Rule
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Scope
mkScope (Text -> Rule)
-> Either [PolicyError] Text -> Either [PolicyError] Rule
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text
-> Text
-> Text
-> (Text -> Either [PolicyError] Text)
-> Maybe Text
-> Either [PolicyError] Text
forall a b.
Text
-> Text
-> Text
-> (a -> Either [PolicyError] b)
-> Maybe a
-> Either [PolicyError] b
requireField Text
name Text
"AllowScope" Text
"scope" Text -> Either [PolicyError] Text
forall a b. b -> Either a b
Right (RuleEntry -> Maybe Text
entryScope RuleEntry
entry)
    Text
"DenyByIdentity" ->
        Text -> Rule
DenyByIdentity (Text -> Rule)
-> Either [PolicyError] Text -> Either [PolicyError] Rule
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text
-> Text
-> Text
-> (Text -> Either [PolicyError] Text)
-> Maybe Text
-> Either [PolicyError] Text
forall a b.
Text
-> Text
-> Text
-> (a -> Either [PolicyError] b)
-> Maybe a
-> Either [PolicyError] b
requireField Text
name Text
"DenyByIdentity" Text
"identity" Text -> Either [PolicyError] Text
forall a b. b -> Either a b
Right (RuleEntry -> Maybe Text
entryIdentity RuleEntry
entry)
    Text
"AllowByIdentity" ->
        Text -> Rule
AllowByIdentity (Text -> Rule)
-> Either [PolicyError] Text -> Either [PolicyError] Rule
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text
-> Text
-> Text
-> (Text -> Either [PolicyError] Text)
-> Maybe Text
-> Either [PolicyError] Text
forall a b.
Text
-> Text
-> Text
-> (a -> Either [PolicyError] b)
-> Maybe a
-> Either [PolicyError] b
requireField Text
name Text
"AllowByIdentity" Text
"identity" Text -> Either [PolicyError] Text
forall a b. b -> Either a b
Right (RuleEntry -> Maybe Text
entryIdentity RuleEntry
entry)
    Text
"AllowIfRemediatesCve" -> AllowIfRemediatesCveParams -> Rule
AllowIfRemediatesCve (AllowIfRemediatesCveParams -> Rule)
-> Either [PolicyError] AllowIfRemediatesCveParams
-> Either [PolicyError] Rule
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text
-> RuleEntry
-> Maybe AllowIfRemediatesCveParams
-> Either [PolicyError] AllowIfRemediatesCveParams
remediationParams Text
name RuleEntry
entry Maybe AllowIfRemediatesCveParams
forall a. Maybe a
Nothing
    Text
"DenyIfKnownMalicious" -> FailureAlignment -> Rule
DenyIfKnownMalicious (FailureAlignment -> Rule)
-> Either [PolicyError] FailureAlignment
-> Either [PolicyError] Rule
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Maybe Text -> Either [PolicyError] FailureAlignment
parseOnUnavailable Text
name (RuleEntry -> Maybe Text
entryOnUnavailable RuleEntry
entry)
    Text
"DenyIfCve" -> DenyIfCveParams -> Rule
DenyIfCve (DenyIfCveParams -> Rule)
-> Either [PolicyError] DenyIfCveParams
-> Either [PolicyError] Rule
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> RuleEntry -> Either [PolicyError] DenyIfCveParams
buildDenyIfCveParams Text
name RuleEntry
entry
    Text
"DenyIfEpss" -> DenyIfEpssParams -> Rule
DenyIfEpss (DenyIfEpssParams -> Rule)
-> Either [PolicyError] DenyIfEpssParams
-> Either [PolicyError] Rule
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> RuleEntry -> Either [PolicyError] DenyIfEpssParams
buildDenyIfEpssParams Text
name RuleEntry
entry
    Text
"DenyIfYanked" -> Rule -> Either [PolicyError] Rule
forall a b. b -> Either a b
Right Rule
DenyIfYanked
    Text
"DenyIfDeprecated" -> Rule -> Either [PolicyError] Rule
forall a b. b -> Either a b
Right Rule
DenyIfDeprecated
    Text
"DenyInstallTimeExecution" -> Rule -> Either [PolicyError] Rule
forall a b. b -> Either a b
Right Rule
DenyInstallTimeExecution
    Text
"DenyExternalDependencies" -> Rule -> Either [PolicyError] Rule
forall a b. b -> Either a b
Right Rule
DenyExternalDependencies
    Text
_ -> [PolicyError] -> Either [PolicyError] Rule
forall a b. a -> Either a b
Left [Text -> Text -> PolicyError
UnknownRuleType Text
name Text
ty]

requireField :: Text -> Text -> Text -> (a -> Either [PolicyError] b) -> Maybe a -> Either [PolicyError] b
requireField :: forall a b.
Text
-> Text
-> Text
-> (a -> Either [PolicyError] b)
-> Maybe a
-> Either [PolicyError] b
requireField Text
name Text
ruleType Text
field = Either [PolicyError] b
-> (a -> Either [PolicyError] b)
-> Maybe a
-> Either [PolicyError] b
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Text -> Text -> Text -> Either [PolicyError] b
forall a. Text -> Text -> Text -> Either [PolicyError] a
missingField Text
name Text
ruleType Text
field)

missingField :: Text -> Text -> Text -> Either [PolicyError] a
missingField :: forall a. Text -> Text -> Text -> Either [PolicyError] a
missingField Text
name Text
ruleType Text
field = [PolicyError] -> Either [PolicyError] a
forall a b. a -> Either a b
Left [Text -> Text -> PolicyError
MalformedRule Text
name (Text -> Text
quote Text
ruleType Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" requires " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
quote Text
field)]

-- @minCvss@ is required, so an operator states the CVSS threshold consciously. @onUnavailable@
-- defaults to @deny@, which fails closed.
buildDenyIfCveParams :: Text -> RuleEntry -> Either [PolicyError] DenyIfCveParams
buildDenyIfCveParams :: Text -> RuleEntry -> Either [PolicyError] DenyIfCveParams
buildDenyIfCveParams Text
name RuleEntry
entry =
    Double -> FailureAlignment -> DenyIfCveParams
DenyIfCveParams
        (Double -> FailureAlignment -> DenyIfCveParams)
-> Either [PolicyError] Double
-> Either [PolicyError] (FailureAlignment -> DenyIfCveParams)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text
-> Text
-> Text
-> (Double -> Either [PolicyError] Double)
-> Maybe Double
-> Either [PolicyError] Double
forall a b.
Text
-> Text
-> Text
-> (a -> Either [PolicyError] b)
-> Maybe a
-> Either [PolicyError] b
requireField Text
name Text
"DenyIfCve" Text
"minCvss" (Text -> Text -> Double -> Either [PolicyError] Double
validateCvss Text
name Text
"minCvss") (RuleEntry -> Maybe Double
entryMinCvss RuleEntry
entry)
        Either [PolicyError] (FailureAlignment -> DenyIfCveParams)
-> Either [PolicyError] FailureAlignment
-> Either [PolicyError] DenyIfCveParams
forall a b.
Either [PolicyError] (a -> b)
-> Either [PolicyError] a -> Either [PolicyError] b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Maybe Text -> Either [PolicyError] FailureAlignment
parseOnUnavailable Text
name (RuleEntry -> Maybe Text
entryOnUnavailable RuleEntry
entry)

-- On the same terms as 'buildDenyIfCveParams'.
buildDenyIfEpssParams :: Text -> RuleEntry -> Either [PolicyError] DenyIfEpssParams
buildDenyIfEpssParams :: Text -> RuleEntry -> Either [PolicyError] DenyIfEpssParams
buildDenyIfEpssParams Text
name RuleEntry
entry =
    Double -> FailureAlignment -> DenyIfEpssParams
DenyIfEpssParams
        (Double -> FailureAlignment -> DenyIfEpssParams)
-> Either [PolicyError] Double
-> Either [PolicyError] (FailureAlignment -> DenyIfEpssParams)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text
-> Text
-> Text
-> (Double -> Either [PolicyError] Double)
-> Maybe Double
-> Either [PolicyError] Double
forall a b.
Text
-> Text
-> Text
-> (a -> Either [PolicyError] b)
-> Maybe a
-> Either [PolicyError] b
requireField Text
name Text
"DenyIfEpss" Text
"minEpss" (Text -> Text -> Double -> Either [PolicyError] Double
validateEpss Text
name Text
"minEpss") (RuleEntry -> Maybe Double
entryMinEpss RuleEntry
entry)
        Either [PolicyError] (FailureAlignment -> DenyIfEpssParams)
-> Either [PolicyError] FailureAlignment
-> Either [PolicyError] DenyIfEpssParams
forall a b.
Either [PolicyError] (a -> b)
-> Either [PolicyError] a -> Either [PolicyError] b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Maybe Text -> Either [PolicyError] FailureAlignment
parseOnUnavailable Text
name (RuleEntry -> Maybe Text
entryOnUnavailable RuleEntry
entry)

patchRuleValue :: Text -> RuleEntry -> Rule -> Either [PolicyError] Rule
patchRuleValue :: Text -> RuleEntry -> Rule -> Either [PolicyError] Rule
patchRuleValue Text
name RuleEntry
entry Rule
rule =
    case Rule
rule of
        AllowIfOlderThan NominalDiffTime
d ->
            NominalDiffTime -> Rule
AllowIfOlderThan (NominalDiffTime -> Rule)
-> Either [PolicyError] NominalDiffTime
-> Either [PolicyError] Rule
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Either [PolicyError] NominalDiffTime
-> (Integer -> Either [PolicyError] NominalDiffTime)
-> Maybe Integer
-> Either [PolicyError] NominalDiffTime
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (NominalDiffTime -> Either [PolicyError] NominalDiffTime
forall a b. b -> Either a b
Right NominalDiffTime
d) (Text -> Integer -> Either [PolicyError] NominalDiffTime
validateAgeSeconds Text
name) (RuleEntry -> Maybe Integer
entryAgeSeconds RuleEntry
entry)
        AllowScope Scope
s -> Rule -> Either [PolicyError] Rule
forall a b. b -> Either a b
Right (Scope -> Rule
AllowScope (Scope -> (Text -> Scope) -> Maybe Text -> Scope
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Scope
s Text -> Scope
mkScope (RuleEntry -> Maybe Text
entryScope RuleEntry
entry)))
        DenyByIdentity Text
i -> Rule -> Either [PolicyError] Rule
forall a b. b -> Either a b
Right (Text -> Rule
DenyByIdentity (Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
i (RuleEntry -> Maybe Text
entryIdentity RuleEntry
entry)))
        AllowByIdentity Text
i -> Rule -> Either [PolicyError] Rule
forall a b. b -> Either a b
Right (Text -> Rule
AllowByIdentity (Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
i (RuleEntry -> Maybe Text
entryIdentity RuleEntry
entry)))
        AllowIfRemediatesCve AllowIfRemediatesCveParams
params -> AllowIfRemediatesCveParams -> Rule
AllowIfRemediatesCve (AllowIfRemediatesCveParams -> Rule)
-> Either [PolicyError] AllowIfRemediatesCveParams
-> Either [PolicyError] Rule
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text
-> RuleEntry
-> Maybe AllowIfRemediatesCveParams
-> Either [PolicyError] AllowIfRemediatesCveParams
remediationParams Text
name RuleEntry
entry (AllowIfRemediatesCveParams -> Maybe AllowIfRemediatesCveParams
forall a. a -> Maybe a
Just AllowIfRemediatesCveParams
params)
        DenyIfKnownMalicious FailureAlignment
alignment ->
            FailureAlignment -> Rule
DenyIfKnownMalicious (FailureAlignment -> Rule)
-> Either [PolicyError] FailureAlignment
-> Either [PolicyError] Rule
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Either [PolicyError] FailureAlignment
-> (Text -> Either [PolicyError] FailureAlignment)
-> Maybe Text
-> Either [PolicyError] FailureAlignment
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (FailureAlignment -> Either [PolicyError] FailureAlignment
forall a b. b -> Either a b
Right FailureAlignment
alignment) (Text -> Text -> Either [PolicyError] FailureAlignment
parseAlignment Text
name) (RuleEntry -> Maybe Text
entryOnUnavailable RuleEntry
entry)
        DenyIfCve DenyIfCveParams
params ->
            (DenyIfCveParams -> Rule)
-> Either [PolicyError] DenyIfCveParams
-> Either [PolicyError] Rule
forall a b.
(a -> b) -> Either [PolicyError] a -> Either [PolicyError] b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap DenyIfCveParams -> Rule
DenyIfCve (Either [PolicyError] DenyIfCveParams -> Either [PolicyError] Rule)
-> Either [PolicyError] DenyIfCveParams
-> Either [PolicyError] Rule
forall a b. (a -> b) -> a -> b
$
                Double -> FailureAlignment -> DenyIfCveParams
DenyIfCveParams
                    (Double -> FailureAlignment -> DenyIfCveParams)
-> Either [PolicyError] Double
-> Either [PolicyError] (FailureAlignment -> DenyIfCveParams)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Either [PolicyError] Double
-> (Double -> Either [PolicyError] Double)
-> Maybe Double
-> Either [PolicyError] Double
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Double -> Either [PolicyError] Double
forall a b. b -> Either a b
Right (DenyIfCveParams -> Double
dicMinCvss DenyIfCveParams
params)) (Text -> Text -> Double -> Either [PolicyError] Double
validateCvss Text
name Text
"minCvss") (RuleEntry -> Maybe Double
entryMinCvss RuleEntry
entry)
                    Either [PolicyError] (FailureAlignment -> DenyIfCveParams)
-> Either [PolicyError] FailureAlignment
-> Either [PolicyError] DenyIfCveParams
forall a b.
Either [PolicyError] (a -> b)
-> Either [PolicyError] a -> Either [PolicyError] b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Either [PolicyError] FailureAlignment
-> (Text -> Either [PolicyError] FailureAlignment)
-> Maybe Text
-> Either [PolicyError] FailureAlignment
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (FailureAlignment -> Either [PolicyError] FailureAlignment
forall a b. b -> Either a b
Right (DenyIfCveParams -> FailureAlignment
dicOnUnavailable DenyIfCveParams
params)) (Text -> Text -> Either [PolicyError] FailureAlignment
parseAlignment Text
name) (RuleEntry -> Maybe Text
entryOnUnavailable RuleEntry
entry)
        DenyIfEpss DenyIfEpssParams
params ->
            (DenyIfEpssParams -> Rule)
-> Either [PolicyError] DenyIfEpssParams
-> Either [PolicyError] Rule
forall a b.
(a -> b) -> Either [PolicyError] a -> Either [PolicyError] b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap DenyIfEpssParams -> Rule
DenyIfEpss (Either [PolicyError] DenyIfEpssParams
 -> Either [PolicyError] Rule)
-> Either [PolicyError] DenyIfEpssParams
-> Either [PolicyError] Rule
forall a b. (a -> b) -> a -> b
$
                Double -> FailureAlignment -> DenyIfEpssParams
DenyIfEpssParams
                    (Double -> FailureAlignment -> DenyIfEpssParams)
-> Either [PolicyError] Double
-> Either [PolicyError] (FailureAlignment -> DenyIfEpssParams)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Either [PolicyError] Double
-> (Double -> Either [PolicyError] Double)
-> Maybe Double
-> Either [PolicyError] Double
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Double -> Either [PolicyError] Double
forall a b. b -> Either a b
Right (DenyIfEpssParams -> Double
dieMinEpss DenyIfEpssParams
params)) (Text -> Text -> Double -> Either [PolicyError] Double
validateEpss Text
name Text
"minEpss") (RuleEntry -> Maybe Double
entryMinEpss RuleEntry
entry)
                    Either [PolicyError] (FailureAlignment -> DenyIfEpssParams)
-> Either [PolicyError] FailureAlignment
-> Either [PolicyError] DenyIfEpssParams
forall a b.
Either [PolicyError] (a -> b)
-> Either [PolicyError] a -> Either [PolicyError] b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Either [PolicyError] FailureAlignment
-> (Text -> Either [PolicyError] FailureAlignment)
-> Maybe Text
-> Either [PolicyError] FailureAlignment
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (FailureAlignment -> Either [PolicyError] FailureAlignment
forall a b. b -> Either a b
Right (DenyIfEpssParams -> FailureAlignment
dieOnUnavailable DenyIfEpssParams
params)) (Text -> Text -> Either [PolicyError] FailureAlignment
parseAlignment Text
name) (RuleEntry -> Maybe Text
entryOnUnavailable RuleEntry
entry)
        Rule
DenyIfYanked -> Rule -> Either [PolicyError] Rule
forall a b. b -> Either a b
Right Rule
DenyIfYanked
        Rule
DenyIfDeprecated -> Rule -> Either [PolicyError] Rule
forall a b. b -> Either a b
Right Rule
DenyIfDeprecated
        Rule
DenyInstallTimeExecution -> Rule -> Either [PolicyError] Rule
forall a b. b -> Either a b
Right Rule
DenyInstallTimeExecution
        Rule
DenyExternalDependencies -> Rule -> Either [PolicyError] Rule
forall a b. b -> Either a b
Right Rule
DenyExternalDependencies

-- A publish-age threshold: a non-negative number of seconds.
validateAgeSeconds :: Text -> Integer -> Either [PolicyError] NominalDiffTime
validateAgeSeconds :: Text -> Integer -> Either [PolicyError] NominalDiffTime
validateAgeSeconds Text
name Integer
secs
    | Integer
secs Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
0 = NominalDiffTime -> Either [PolicyError] NominalDiffTime
forall a b. b -> Either a b
Right (Integer -> NominalDiffTime
forall a. Num a => Integer -> a
fromInteger Integer
secs)
    | Bool
otherwise = [PolicyError] -> Either [PolicyError] NominalDiffTime
forall a b. a -> Either a b
Left [Text -> Text -> PolicyError
MalformedRule Text
name Text
"\"ageSeconds\" must be non-negative"]

-- A CVSS score in the range [0, 10]. Every comparison with NaN is false, so the range refuses it.
validateCvss :: Text -> Text -> Double -> Either [PolicyError] Double
validateCvss :: Text -> Text -> Double -> Either [PolicyError] Double
validateCvss Text
name Text
key Double
s
    | Double
s Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
>= Double
0 Bool -> Bool -> Bool
&& Double
s Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
10 = Double -> Either [PolicyError] Double
forall a b. b -> Either a b
Right Double
s
    | Bool
otherwise = [PolicyError] -> Either [PolicyError] Double
forall a b. a -> Either a b
Left [Text -> Text -> PolicyError
MalformedRule Text
name (Text -> Text
quote Text
key Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" must be a CVSS score between 0 and 10")]

-- An EPSS probability in the range [0, 1], refusing NaN on the same terms.
validateEpss :: Text -> Text -> Double -> Either [PolicyError] Double
validateEpss :: Text -> Text -> Double -> Either [PolicyError] Double
validateEpss Text
name Text
key Double
s
    | Double
s Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
>= Double
0 Bool -> Bool -> Bool
&& Double
s Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
1 = Double -> Either [PolicyError] Double
forall a b. b -> Either a b
Right Double
s
    | Bool
otherwise = [PolicyError] -> Either [PolicyError] Double
forall a b. a -> Either a b
Left [Text -> Text -> PolicyError
MalformedRule Text
name (Text -> Text
quote Text
key Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" must be an EPSS probability between 0 and 1")]

-- How an advisory deny resolves when the database cannot answer. Absent fails closed.
parseOnUnavailable :: Text -> Maybe Text -> Either [PolicyError] FailureAlignment
parseOnUnavailable :: Text -> Maybe Text -> Either [PolicyError] FailureAlignment
parseOnUnavailable Text
name = Either [PolicyError] FailureAlignment
-> (Text -> Either [PolicyError] FailureAlignment)
-> Maybe Text
-> Either [PolicyError] FailureAlignment
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (FailureAlignment -> Either [PolicyError] FailureAlignment
forall a b. b -> Either a b
Right FailureAlignment
FailDeny) (Text -> Text -> Either [PolicyError] FailureAlignment
parseAlignment Text
name)

parseAlignment :: Text -> Text -> Either [PolicyError] FailureAlignment
parseAlignment :: Text -> Text -> Either [PolicyError] FailureAlignment
parseAlignment Text
name = \case
    Text
"deny" -> FailureAlignment -> Either [PolicyError] FailureAlignment
forall a b. b -> Either a b
Right FailureAlignment
FailDeny
    Text
"skip" -> FailureAlignment -> Either [PolicyError] FailureAlignment
forall a b. b -> Either a b
Right FailureAlignment
FailNoDecision
    Text
other -> [PolicyError] -> Either [PolicyError] FailureAlignment
forall a b. a -> Either a b
Left [Text -> Text -> PolicyError
MalformedRule Text
name (Text
"\"onUnavailable\" must be \"deny\" or \"skip\", not " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
quote Text
other)]

-- A new rule states both choices, and a patch inherits the ones it leaves out. Nothing defaults them.
remediationParams :: Text -> RuleEntry -> Maybe AllowIfRemediatesCveParams -> Either [PolicyError] AllowIfRemediatesCveParams
remediationParams :: Text
-> RuleEntry
-> Maybe AllowIfRemediatesCveParams
-> Either [PolicyError] AllowIfRemediatesCveParams
remediationParams Text
name RuleEntry
entry Maybe AllowIfRemediatesCveParams
inherited =
    Validation [PolicyError] AllowIfRemediatesCveParams
-> Either [PolicyError] AllowIfRemediatesCveParams
forall e a. Validation e a -> Either e a
validationToEither (Validation [PolicyError] AllowIfRemediatesCveParams
 -> Either [PolicyError] AllowIfRemediatesCveParams)
-> Validation [PolicyError] AllowIfRemediatesCveParams
-> Either [PolicyError] AllowIfRemediatesCveParams
forall a b. (a -> b) -> a -> b
$
        FailureAlignment
-> RemainingAdvisories -> AllowIfRemediatesCveParams
AllowIfRemediatesCveParams
            (FailureAlignment
 -> RemainingAdvisories -> AllowIfRemediatesCveParams)
-> Validation [PolicyError] FailureAlignment
-> Validation
     [PolicyError] (RemainingAdvisories -> AllowIfRemediatesCveParams)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Either [PolicyError] FailureAlignment
-> Validation [PolicyError] FailureAlignment
forall e a. Either e a -> Validation e a
eitherToValidation (Text
-> (Text -> Either [PolicyError] FailureAlignment)
-> Maybe Text
-> Maybe FailureAlignment
-> Either [PolicyError] FailureAlignment
forall {t} {b}.
Text
-> (t -> Either [PolicyError] b)
-> Maybe t
-> Maybe b
-> Either [PolicyError] b
stated Text
"onUnavailable" (Text -> Text -> Either [PolicyError] FailureAlignment
parseAlignment Text
name) (RuleEntry -> Maybe Text
entryOnUnavailable RuleEntry
entry) (AllowIfRemediatesCveParams -> FailureAlignment
aircOnUnavailable (AllowIfRemediatesCveParams -> FailureAlignment)
-> Maybe AllowIfRemediatesCveParams -> Maybe FailureAlignment
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe AllowIfRemediatesCveParams
inherited))
            Validation
  [PolicyError] (RemainingAdvisories -> AllowIfRemediatesCveParams)
-> Validation [PolicyError] RemainingAdvisories
-> Validation [PolicyError] AllowIfRemediatesCveParams
forall a b.
Validation [PolicyError] (a -> b)
-> Validation [PolicyError] a -> Validation [PolicyError] b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Either [PolicyError] RemainingAdvisories
-> Validation [PolicyError] RemainingAdvisories
forall e a. Either e a -> Validation e a
eitherToValidation (Text
-> (Text -> Either [PolicyError] RemainingMode)
-> Maybe Text
-> Maybe RemainingMode
-> Either [PolicyError] RemainingMode
forall {t} {b}.
Text
-> (t -> Either [PolicyError] b)
-> Maybe t
-> Maybe b
-> Either [PolicyError] b
stated Text
"remainingAdvisories" (Text -> Text -> Either [PolicyError] RemainingMode
parseMode Text
name) (RuleEntry -> Maybe Text
entryRemainingAdvisories RuleEntry
entry) (RemainingAdvisories -> RemainingMode
modeOf (RemainingAdvisories -> RemainingMode)
-> (AllowIfRemediatesCveParams -> RemainingAdvisories)
-> AllowIfRemediatesCveParams
-> RemainingMode
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AllowIfRemediatesCveParams -> RemainingAdvisories
aircRemaining (AllowIfRemediatesCveParams -> RemainingMode)
-> Maybe AllowIfRemediatesCveParams -> Maybe RemainingMode
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe AllowIfRemediatesCveParams
inherited) Either [PolicyError] RemainingMode
-> (RemainingMode -> Either [PolicyError] RemainingAdvisories)
-> Either [PolicyError] RemainingAdvisories
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
>>= Text
-> RuleEntry
-> Maybe RemainingAdvisories
-> RemainingMode
-> Either [PolicyError] RemainingAdvisories
remainingIn Text
name RuleEntry
entry (AllowIfRemediatesCveParams -> RemainingAdvisories
aircRemaining (AllowIfRemediatesCveParams -> RemainingAdvisories)
-> Maybe AllowIfRemediatesCveParams -> Maybe RemainingAdvisories
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe AllowIfRemediatesCveParams
inherited))
  where
    stated :: Text
-> (t -> Either [PolicyError] b)
-> Maybe t
-> Maybe b
-> Either [PolicyError] b
stated Text
key t -> Either [PolicyError] b
parse Maybe t
declared Maybe b
current = case (Maybe t
declared, Maybe b
current) of
        (Just t
word, Maybe b
_) -> t -> Either [PolicyError] b
parse t
word
        (Maybe t
Nothing, Just b
value) -> b -> Either [PolicyError] b
forall a b. b -> Either a b
Right b
value
        (Maybe t
Nothing, Maybe b
Nothing) -> Text -> Text -> Text -> Either [PolicyError] b
forall a. Text -> Text -> Text -> Either [PolicyError] a
missingField Text
name Text
"AllowIfRemediatesCve" Text
key

data RemainingMode = AbstainMode | DifferentialMode

modeOf :: RemainingAdvisories -> RemainingMode
modeOf :: RemainingAdvisories -> RemainingMode
modeOf = \case
    RemainingAdvisories
AbstainOnRemaining -> RemainingMode
AbstainMode
    Differential{} -> RemainingMode
DifferentialMode

parseMode :: Text -> Text -> Either [PolicyError] RemainingMode
parseMode :: Text -> Text -> Either [PolicyError] RemainingMode
parseMode Text
name = \case
    Text
"abstain" -> RemainingMode -> Either [PolicyError] RemainingMode
forall a b. b -> Either a b
Right RemainingMode
AbstainMode
    Text
"differential" -> RemainingMode -> Either [PolicyError] RemainingMode
forall a b. b -> Either a b
Right RemainingMode
DifferentialMode
    Text
other -> [PolicyError] -> Either [PolicyError] RemainingMode
forall a b. a -> Either a b
Left [Text -> Text -> PolicyError
MalformedRule Text
name (Text
"\"remainingAdvisories\" must be \"abstain\" or \"differential\", not " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
quote Text
other)]

-- Abstaining drops inherited limits and refuses supplied ones. Differential starts from the inherited
-- limits, or the shipped ones when it switches from abstaining.
remainingIn :: Text -> RuleEntry -> Maybe RemainingAdvisories -> RemainingMode -> Either [PolicyError] RemainingAdvisories
remainingIn :: Text
-> RuleEntry
-> Maybe RemainingAdvisories
-> RemainingMode
-> Either [PolicyError] RemainingAdvisories
remainingIn Text
name RuleEntry
entry Maybe RemainingAdvisories
inherited = \case
    RemainingMode
AbstainMode -> case [Text
key | (Text
key, Just Double
_) <- RuleEntry -> [(Text, Maybe Double)]
limitEntries RuleEntry
entry] of
        [] -> RemainingAdvisories -> Either [PolicyError] RemainingAdvisories
forall a b. b -> Either a b
Right RemainingAdvisories
AbstainOnRemaining
        [Text]
supplied -> [PolicyError] -> Either [PolicyError] RemainingAdvisories
forall a b. a -> Either a b
Left [Text -> Text -> PolicyError
MalformedRule Text
name (Text -> Text
quote Text
key Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" applies only when \"remainingAdvisories\" is \"differential\"") | Text
key <- [Text]
supplied]
    RemainingMode
DifferentialMode -> DifferentialLimits -> RemainingAdvisories
Differential (DifferentialLimits -> RemainingAdvisories)
-> Either [PolicyError] DifferentialLimits
-> Either [PolicyError] RemainingAdvisories
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text
-> RuleEntry
-> DifferentialLimits
-> Either [PolicyError] DifferentialLimits
overlayLimits Text
name RuleEntry
entry (DifferentialLimits
-> Maybe DifferentialLimits -> DifferentialLimits
forall a. a -> Maybe a -> a
fromMaybe DifferentialLimits
defaultDifferentialLimits (RemainingAdvisories -> Maybe DifferentialLimits
inheritedLimits (RemainingAdvisories -> Maybe DifferentialLimits)
-> Maybe RemainingAdvisories -> Maybe DifferentialLimits
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Maybe RemainingAdvisories
inherited))
  where
    inheritedLimits :: RemainingAdvisories -> Maybe DifferentialLimits
inheritedLimits = \case
        Differential DifferentialLimits
limits -> DifferentialLimits -> Maybe DifferentialLimits
forall a. a -> Maybe a
Just DifferentialLimits
limits
        RemainingAdvisories
AbstainOnRemaining -> Maybe DifferentialLimits
forall a. Maybe a
Nothing

-- Each supplied limit replaces its base value. A zero gap is no gap condition.
overlayLimits :: Text -> RuleEntry -> DifferentialLimits -> Either [PolicyError] DifferentialLimits
overlayLimits :: Text
-> RuleEntry
-> DifferentialLimits
-> Either [PolicyError] DifferentialLimits
overlayLimits Text
name RuleEntry
entry DifferentialLimits
base =
    Validation [PolicyError] DifferentialLimits
-> Either [PolicyError] DifferentialLimits
forall e a. Validation e a -> Either e a
validationToEither (Validation [PolicyError] DifferentialLimits
 -> Either [PolicyError] DifferentialLimits)
-> Validation [PolicyError] DifferentialLimits
-> Either [PolicyError] DifferentialLimits
forall a b. (a -> b) -> a -> b
$
        Double -> Double -> ScoreGap -> ScoreGap -> DifferentialLimits
DifferentialLimits
            (Double -> Double -> ScoreGap -> ScoreGap -> DifferentialLimits)
-> Validation [PolicyError] Double
-> Validation
     [PolicyError]
     (Double -> ScoreGap -> ScoreGap -> DifferentialLimits)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Double -> Either [PolicyError] Double)
-> Maybe Double -> Double -> Validation [PolicyError] Double
forall {a} {e} {a}.
(a -> Either e a) -> Maybe a -> a -> Validation e a
overlay (Text -> Text -> Double -> Either [PolicyError] Double
validateCvss Text
name Text
"maxRemainingCvss") (RuleEntry -> Maybe Double
entryMaxRemainingCvss RuleEntry
entry) (DifferentialLimits -> Double
dlMaxRemainingCvss DifferentialLimits
base)
            Validation
  [PolicyError]
  (Double -> ScoreGap -> ScoreGap -> DifferentialLimits)
-> Validation [PolicyError] Double
-> Validation
     [PolicyError] (ScoreGap -> ScoreGap -> DifferentialLimits)
forall a b.
Validation [PolicyError] (a -> b)
-> Validation [PolicyError] a -> Validation [PolicyError] b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Double -> Either [PolicyError] Double)
-> Maybe Double -> Double -> Validation [PolicyError] Double
forall {a} {e} {a}.
(a -> Either e a) -> Maybe a -> a -> Validation e a
overlay (Text -> Text -> Double -> Either [PolicyError] Double
validateEpss Text
name Text
"maxRemainingEpss") (RuleEntry -> Maybe Double
entryMaxRemainingEpss RuleEntry
entry) (DifferentialLimits -> Double
dlMaxRemainingEpss DifferentialLimits
base)
            Validation
  [PolicyError] (ScoreGap -> ScoreGap -> DifferentialLimits)
-> Validation [PolicyError] ScoreGap
-> Validation [PolicyError] (ScoreGap -> DifferentialLimits)
forall a b.
Validation [PolicyError] (a -> b)
-> Validation [PolicyError] a -> Validation [PolicyError] b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Double -> Either [PolicyError] ScoreGap)
-> Maybe Double -> ScoreGap -> Validation [PolicyError] ScoreGap
forall {a} {e} {a}.
(a -> Either e a) -> Maybe a -> a -> Validation e a
overlay ((Double -> ScoreGap)
-> Either [PolicyError] Double -> Either [PolicyError] ScoreGap
forall a b.
(a -> b) -> Either [PolicyError] a -> Either [PolicyError] b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Double -> ScoreGap
scoreGap (Either [PolicyError] Double -> Either [PolicyError] ScoreGap)
-> (Double -> Either [PolicyError] Double)
-> Double
-> Either [PolicyError] ScoreGap
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text -> Double -> Either [PolicyError] Double
validateCvss Text
name Text
"minCvssGap") (RuleEntry -> Maybe Double
entryMinCvssGap RuleEntry
entry) (DifferentialLimits -> ScoreGap
dlCvssGap DifferentialLimits
base)
            Validation [PolicyError] (ScoreGap -> DifferentialLimits)
-> Validation [PolicyError] ScoreGap
-> Validation [PolicyError] DifferentialLimits
forall a b.
Validation [PolicyError] (a -> b)
-> Validation [PolicyError] a -> Validation [PolicyError] b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Double -> Either [PolicyError] ScoreGap)
-> Maybe Double -> ScoreGap -> Validation [PolicyError] ScoreGap
forall {a} {e} {a}.
(a -> Either e a) -> Maybe a -> a -> Validation e a
overlay ((Double -> ScoreGap)
-> Either [PolicyError] Double -> Either [PolicyError] ScoreGap
forall a b.
(a -> b) -> Either [PolicyError] a -> Either [PolicyError] b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Double -> ScoreGap
scoreGap (Either [PolicyError] Double -> Either [PolicyError] ScoreGap)
-> (Double -> Either [PolicyError] Double)
-> Double
-> Either [PolicyError] ScoreGap
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text -> Double -> Either [PolicyError] Double
validateEpss Text
name Text
"minEpssGap") (RuleEntry -> Maybe Double
entryMinEpssGap RuleEntry
entry) (DifferentialLimits -> ScoreGap
dlEpssGap DifferentialLimits
base)
  where
    overlay :: (a -> Either e a) -> Maybe a -> a -> Validation e a
overlay a -> Either e a
validate Maybe a
supplied a
current = Either e a -> Validation e a
forall e a. Either e a -> Validation e a
eitherToValidation (Either e a -> (a -> Either e a) -> Maybe a -> Either e a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (a -> Either e a
forall a b. b -> Either a b
Right a
current) a -> Either e a
validate Maybe a
supplied)
    scoreGap :: Double -> ScoreGap
scoreGap Double
least
        | Double
least Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
== Double
0 = ScoreGap
NoGapRequired
        | Bool
otherwise = Double -> ScoreGap
GapOfAtLeast Double
least

-- The phases the entry states, or @inherited@ where it states none. An allow the Dredger did not
-- read would leave a lower deny free to delete a version the gate admits, so only a deny narrows.
resolveReach :: Text -> RuleEntry -> RuleReach -> Rule -> Either [PolicyError] RuleReach
resolveReach :: Text
-> RuleEntry -> RuleReach -> Rule -> Either [PolicyError] RuleReach
resolveReach Text
name RuleEntry
entry RuleReach
inherited Rule
rule = case RuleEntry -> Maybe [Text]
entryAppliesTo RuleEntry
entry of
    Maybe [Text]
Nothing -> RuleReach -> Either [PolicyError] RuleReach
forall a b. b -> Either a b
Right RuleReach
inherited
    Just [Text]
phases ->
        Text -> [Text] -> Either [PolicyError] RuleReach
parseAppliesTo Text
name [Text]
phases Either [PolicyError] RuleReach
-> (RuleReach -> Either [PolicyError] RuleReach)
-> Either [PolicyError] RuleReach
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
>>= \case
            RuleReach
AdmissionOnly | Bool -> Bool
not (Rule -> Bool
ruleDenies Rule
rule) -> [PolicyError] -> Either [PolicyError] RuleReach
forall a b. a -> Either a b
Left [Text -> Text -> PolicyError
MalformedRule Text
name (Text -> Text
admissionOnlyAllow (Rule -> Text
ruleName Rule
rule))]
            RuleReach
reach -> RuleReach -> Either [PolicyError] RuleReach
forall a b. b -> Either a b
Right RuleReach
reach
  where
    admissionOnlyAllow :: Text -> Text
admissionOnlyAllow Text
ty =
        Text -> Text
quote Text
ty
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" is an allow, and an allow cannot be limited to admission. If the Dredger ignored it, a lower deny could delete a version this rule admits. Limit the deny instead"

-- A repeated phase counts once, and every unknown word is reported.
parseAppliesTo :: Text -> [Text] -> Either [PolicyError] RuleReach
parseAppliesTo :: Text -> [Text] -> Either [PolicyError] RuleReach
parseAppliesTo Text
name [Text]
phases = case ([Text]
unknown, Text
"admission" Text -> [Text] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Text]
phases, Text
"revocation" Text -> [Text] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Text]
phases) of
    (Text
_ : [Text]
_, Bool
_, Bool
_) -> [PolicyError] -> Either [PolicyError] RuleReach
forall a b. a -> Either a b
Left ((Text -> PolicyError) -> [Text] -> [PolicyError]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> Text -> PolicyError
MalformedRule Text
name (Text -> PolicyError) -> (Text -> Text) -> Text -> PolicyError
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
unknownPhase) [Text]
unknown)
    ([], Bool
True, Bool
True) -> RuleReach -> Either [PolicyError] RuleReach
forall a b. b -> Either a b
Right RuleReach
AdmissionAndRevocation
    ([], Bool
True, Bool
False) -> RuleReach -> Either [PolicyError] RuleReach
forall a b. b -> Either a b
Right RuleReach
AdmissionOnly
    ([], Bool
False, Bool
True) ->
        [PolicyError] -> Either [PolicyError] RuleReach
forall a b. a -> Either a b
Left [Text -> Text -> PolicyError
MalformedRule Text
name Text
"\"appliesTo\" must include \"admission\". A rule that applied at revocation alone would delete copies of versions the gate still admits"]
    ([], Bool
False, Bool
False) ->
        [PolicyError] -> Either [PolicyError] RuleReach
forall a b. a -> Either a b
Left [Text -> Text -> PolicyError
MalformedRule Text
name Text
"\"appliesTo\" names no phase. Write [admission] or [admission, revocation], or switch the rule off with \"enabled\": false"]
  where
    unknown :: [Text]
unknown = [Text] -> [Text]
forall a. Ord a => [a] -> [a]
ordNub ((Text -> Bool) -> [Text] -> [Text]
forall a. (a -> Bool) -> [a] -> [a]
filter (Text -> [Text] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`notElem` [Text
"admission", Text
"revocation"]) [Text]
phases)
    unknownPhase :: Text -> Text
unknownPhase Text
phase = Text
"\"appliesTo\" names unknown phase " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
quote Text
phase Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
". The phases are \"admission\" and \"revocation\""