|
| 1 | +{-# LANGUAGE DataKinds #-} |
| 2 | +{-# LANGUAGE DuplicateRecordFields #-} |
| 3 | +{-# LANGUAGE TypeOperators #-} |
| 4 | + |
| 5 | +module Vault |
| 6 | + ( deleteSecret, |
| 7 | + patchSecret, |
| 8 | + putSecret, |
| 9 | + fetchSecret, |
| 10 | + SecretMount (..), |
| 11 | + VaultToken (..), |
| 12 | + SecretPath (..), |
| 13 | + SecretRequest (..), |
| 14 | + SecretResponse (..), |
| 15 | + SecretData (..), |
| 16 | + SecretPatch (..), |
| 17 | + Options (..), |
| 18 | + ) |
| 19 | +where |
| 20 | + |
| 21 | +import Data.Aeson qualified as Aeson |
| 22 | +import Data.Map (Map) |
| 23 | +import Data.Text (Text) |
| 24 | +import GHC.Generics (Generic) |
| 25 | +import Network.HTTP.Media.MediaType qualified as MT |
| 26 | +import Servant |
| 27 | +import Servant.Client qualified as SC |
| 28 | +import Share.Utils.Show (Censored (..)) |
| 29 | + |
| 30 | +type RequiredHeader = Header' '[Required, Strict] |
| 31 | + |
| 32 | +-- API definition |
| 33 | +type VaultAPI = |
| 34 | + (RequiredHeader "X-Vault-Token" VaultToken :> Capture "mount" SecretMount :> "data" :> Capture "path" SecretPath :> DeleteNoContent) |
| 35 | + :<|> (RequiredHeader "X-Vault-Token" VaultToken :> Capture "mount" SecretMount :> "data" :> Capture "path" SecretPath :> ReqBody '[JsonMergePatch] SecretPatch :> PatchNoContent) |
| 36 | + :<|> (RequiredHeader "X-Vault-Token" VaultToken :> Capture "mount" SecretMount :> "data" :> Capture "path" SecretPath :> ReqBody '[JSON] SecretRequest :> PutNoContent) |
| 37 | + :<|> (RequiredHeader "X-Vault-Token" VaultToken :> Capture "mount" SecretMount :> "data" :> Capture "path" SecretPath :> Get '[JSON] SecretResponse) |
| 38 | + |
| 39 | +vaultApi :: Proxy VaultAPI |
| 40 | +vaultApi = Proxy |
| 41 | + |
| 42 | +data JsonMergePatch |
| 43 | + |
| 44 | +instance Accept JsonMergePatch where |
| 45 | + contentType _ = "application" MT.// "merge-patch+json" |
| 46 | + |
| 47 | +newtype SecretMount = SecretMount Text |
| 48 | + deriving newtype (Show, Eq, Ord, FromHttpApiData, ToHttpApiData) |
| 49 | + |
| 50 | +newtype SecretPath = SecretPath Text |
| 51 | + deriving newtype (Show, Eq, Ord, FromHttpApiData, ToHttpApiData) |
| 52 | + |
| 53 | +newtype VaultToken = VaultToken Text |
| 54 | + deriving newtype (Eq, Ord, FromHttpApiData, ToHttpApiData) |
| 55 | + deriving (Show) via Censored VaultToken |
| 56 | + |
| 57 | +deleteSecret :: VaultToken -> SecretMount -> SecretPath -> SC.ClientM NoContent |
| 58 | +patchSecret :: VaultToken -> SecretMount -> SecretPath -> SecretPatch -> SC.ClientM NoContent |
| 59 | +putSecret :: VaultToken -> SecretMount -> SecretPath -> SecretRequest -> SC.ClientM NoContent |
| 60 | +fetchSecret :: VaultToken -> SecretMount -> SecretPath -> SC.ClientM SecretResponse |
| 61 | +deleteSecret :<|> patchSecret :<|> putSecret :<|> fetchSecret = SC.client vaultApi |
| 62 | + |
| 63 | +-- Data types |
| 64 | + |
| 65 | +data SecretRequest = SecretRequest |
| 66 | + { options :: Maybe Options, |
| 67 | + data_ :: Aeson.Value |
| 68 | + } |
| 69 | + deriving (Generic) |
| 70 | + |
| 71 | +instance Aeson.ToJSON SecretRequest where |
| 72 | + toJSON (SecretRequest options data_) = Aeson.object ["options" Aeson..= options, "data" Aeson..= data_] |
| 73 | + |
| 74 | +newtype Options = Options |
| 75 | + { cas :: Int |
| 76 | + } |
| 77 | + deriving (Generic) |
| 78 | + |
| 79 | +instance Aeson.ToJSON Options |
| 80 | + |
| 81 | +newtype SecretResponse = SecretResponse |
| 82 | + { -- yes, the data is nested under two separate "data" keys |
| 83 | + data_ :: SecretData |
| 84 | + } |
| 85 | + |
| 86 | +newtype SecretData = SecretData {data_ :: Aeson.Value} |
| 87 | + |
| 88 | +data SecretPatch = SecretPatch !(Maybe Options) !(Map Text (Maybe Text)) |
| 89 | + |
| 90 | +instance MimeRender JsonMergePatch SecretPatch where |
| 91 | + mimeRender _ (SecretPatch options data_) = Aeson.encode $ Aeson.object ["options" Aeson..= options, "data" Aeson..= data_] |
| 92 | + |
| 93 | +instance Aeson.FromJSON SecretResponse where |
| 94 | + parseJSON = Aeson.withObject "SecretResponse" $ \o -> do |
| 95 | + data_ <- o Aeson..: "data" |
| 96 | + return $ SecretResponse {data_} |
| 97 | + |
| 98 | +instance Aeson.FromJSON SecretData where |
| 99 | + parseJSON = Aeson.withObject "SecretData" $ \o -> do |
| 100 | + data_ <- o Aeson..: "data" |
| 101 | + return $ SecretData {data_} |
0 commit comments