Skip to content

Commit

Permalink
Added AppRole types and functions to allow connecting to vault via Ap…
Browse files Browse the repository at this point in the history
…pRole credentials.
  • Loading branch information
mdunnio committed Feb 15, 2018
1 parent 3bddc89 commit 08c879f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
63 changes: 62 additions & 1 deletion vault-tool/src/Network/VaultTool.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module Network.VaultTool
( VaultAddress(..)
, VaultUnsealKey(..)
, VaultAuthToken(..)
, VaultAppRoleId(..)
, VaultAppRoleSecretId(..)
, VaultException(..)

, VaultHealth(..)
Expand All @@ -17,6 +19,8 @@ module Network.VaultTool
, VaultConnection
, connectToVault

, connectToVaultAppRole

, vaultInit
, VaultSealStatus(..)
, vaultSealStatus
Expand Down Expand Up @@ -56,6 +60,7 @@ import Network.HTTP.Client (Manager, newManager)
import Network.HTTP.Client.TLS (tlsManagerSettings)
import qualified Data.HashMap.Strict as H
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE

import Network.VaultTool.Internal
import Network.VaultTool.Types
Expand Down Expand Up @@ -111,6 +116,14 @@ connectToVault addr authToken = do
, _VaultConnection_Manager = manager
}

-- | Initializes the 'VaultConnection' objects using approle credentials to retrieve an authtoken,
-- and then calls `connectToVault`
connectToVaultAppRole :: VaultAddress -> VaultAppRoleId -> VaultAppRoleSecretId -> IO VaultConnection
connectToVaultAppRole addr roleId secretId = do
manager <- newManager tlsManagerSettings
authToken <- vaultAppRoleLogin addr manager roleId secretId
connectToVault addr authToken

-- | <https://www.vaultproject.io/docs/http/sys-init.html>
--
-- See 'vaultInit'
Expand Down Expand Up @@ -171,6 +184,55 @@ vaultSealStatus addr = do
manager <- newManager tlsManagerSettings
vaultRequestJSON manager "GET" (vaultUrl addr "/sys/seal-status") [] (Nothing :: Maybe ()) [200]

-- | <https://www.vaultproject.io/api/auth/approle/index.html>
--
-- See 'sample-response-7'
data VaultAuth = VaultAuth
{ _VaultAuth_Renewable :: Bool
, _VaultAuth_LeaseDuration :: Int
, _VaultAuth_Policies :: [Text]
, _VaultAuth_ClientToken :: VaultAuthToken
}
deriving (Show, Eq, Ord)

instance FromJSON VaultAuth where
parseJSON (Object v) =
VaultAuth <$>
v .: "renewable" <*>
v .: "lease_duration" <*>
v .: "policies" <*>
v .: "client_token"
parseJSON _ = fail "Not an Object"

-- | <https://www.vaultproject.io/api/auth/approle/index.html>
--
-- See 'sample-response-7'
data VaultAppRoleAuthResponse = VaultAppRoleAuthResponse
{ _VaultAppRoleAuthResponse_Auth :: VaultAuth
, _VaultAppRoleAuthResponse_LeaseDuration :: Int
, _VaultAppRoleAuthResponse_Renewable :: Bool
}
deriving (Show, Eq, Ord)

instance FromJSON VaultAppRoleAuthResponse where
parseJSON (Object v) =
VaultAppRoleAuthResponse <$>
v .: "auth" <*>
v .: "lease_duration" <*>
v .: "renewable"
parseJSON _ = fail "Not an Object"

-- | <https://www.vaultproject.io/docs/auth/approle.html>
vaultAppRoleLogin :: VaultAddress -> Manager -> VaultAppRoleId -> VaultAppRoleSecretId -> IO VaultAuthToken
vaultAppRoleLogin addr manager roleId secretId = do
response <- vaultRequestJSON manager "POST" (vaultUrl addr "/auth/approle/login") [] (Just reqBody) [200]
return . _VaultAuth_ClientToken $ _VaultAppRoleAuthResponse_Auth response
where
reqBody = object
[ "role_id" .= TE.decodeUtf8 (unVaultAppRoleId roleId),
"secret_id" .= TE.decodeUtf8 (unVaultAppRoleSecretId secretId)
]

vaultSeal :: VaultConnection -> IO ()
vaultSeal VaultConnection{_VaultConnection_VaultAddress, _VaultConnection_Manager, _VaultConnection_AuthToken} = do
_ <- vaultRequest _VaultConnection_Manager "PUT" (vaultUrl _VaultConnection_VaultAddress "/sys/seal") headers (Nothing :: Maybe ()) [204]
Expand Down Expand Up @@ -199,7 +261,6 @@ vaultUnseal addr unseal = do
manager <- newManager tlsManagerSettings
vaultRequestJSON manager "PUT" (vaultUrl addr "/sys/unseal") [] (Just reqBody) [200]


type VaultMountRead = VaultMount Text VaultMountConfigRead
type VaultMountWrite = VaultMount (Maybe Text) (Maybe VaultMountConfigWrite)
type VaultMountConfigRead = VaultMountConfig Int
Expand Down
6 changes: 6 additions & 0 deletions vault-tool/src/Network/VaultTool/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ instance FromJSON VaultAuthToken where
newtype VaultSecretPath = VaultSecretPath { unVaultSecretPath :: Text }
deriving (Show, Eq, Ord)

newtype VaultAppRoleId = VaultAppRoleId { unVaultAppRoleId :: ByteString }
deriving (Show, Eq, Ord)

newtype VaultAppRoleSecretId = VaultAppRoleSecretId { unVaultAppRoleSecretId :: ByteString }
deriving (Show, Eq, Ord)

data VaultException
= VaultException
| VaultException_InvalidAddress ByteString String
Expand Down

0 comments on commit 08c879f

Please sign in to comment.