Skip to content

Instantly share code, notes, and snippets.

@lucasdicioccio
Last active March 30, 2023 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucasdicioccio/4523429f3292eb77fe1522b9205b4539 to your computer and use it in GitHub Desktop.
Save lucasdicioccio/4523429f3292eb77fe1522b9205b4539 to your computer and use it in GitHub Desktop.
example TypeFamilies-based roles to mitigate risk of dev backdoors in applications
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE KindSignatures #-}
-- | Simple type families for limiting things to some given roles.
module TF where
-- | Lookup for a value in a list.
type family Find x ys where
Find x '[] = 'False
Find x (x ': ys) = 'True
Find x (y ': ys) = Find x ys
-- | If a key k evaluates to True, then the value is taken, otherwise we use
-- unit.
type family If k v where
If 'True v = v
If 'False v = ()
data Role
= Dev
| CI
| Staging
| Prod
data Env (r :: Role)
= Env
{ aside :: If (Find r '[Dev, CI]) DangerousThing
, app :: Application
}
type DangerousThing = Int
type Application = String
backdoor :: DangerousThing
backdoor = 42
devEnv :: Env 'Dev
devEnv = Env backdoor "hello"
prodEnv :: Env 'Prod
prodEnv = Env () "hello"
main :: IO ()
main = putStrLn "ok"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment