Skip to content

Commit d24cd5b

Browse files
committed
TLS with each node in cluster mode
1 parent 1155945 commit d24cd5b

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

hedis.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,10 @@ test-suite hedis-test-cluster
191191
hedis,
192192
HUnit,
193193
async,
194+
crypton-x509-store,
194195
stm,
195196
text,
197+
tls,
196198
mtl == 2.*,
197199
test-framework,
198200
test-framework-hunit,

src/Database/Redis/Cluster.hs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import System.IO.Unsafe(unsafeInterleaveIO)
3636

3737
import Database.Redis.Protocol(Reply(Error), renderRequest, reply)
3838
import qualified Database.Redis.Cluster.Command as CMD
39+
import Network.TLS (ClientParams (..))
3940

4041
-- This module implements a clustered connection whilst maintaining
4142
-- compatibility with the original Hedis codebase. In particular it still
@@ -100,8 +101,8 @@ instance Exception UnsupportedClusterCommandException
100101
newtype CrossSlotException = CrossSlotException [[B.ByteString]] deriving (Show, Typeable)
101102
instance Exception CrossSlotException
102103

103-
connect :: [CMD.CommandInfo] -> MVar ShardMap -> Maybe Int -> IO Connection
104-
connect commandInfos shardMapVar timeoutOpt = do
104+
connect :: Maybe ClientParams -> [CMD.CommandInfo] -> MVar ShardMap -> Maybe Int -> IO Connection
105+
connect mTlsParams commandInfos shardMapVar timeoutOpt = do
105106
shardMap <- readMVar shardMapVar
106107
stateVar <- newMVar $ Pending []
107108
pipelineVar <- newMVar $ Pipeline stateVar
@@ -111,7 +112,18 @@ connect commandInfos shardMapVar timeoutOpt = do
111112
nodeConnections shardMap = HM.fromList <$> mapM connectNode (nub $ nodes shardMap)
112113
connectNode :: Node -> IO (NodeID, NodeConnection)
113114
connectNode (Node n _ host port) = do
114-
ctx <- CC.connect host (CC.PortNumber $ toEnum port) timeoutOpt
115+
ctx0 <- CC.connect host (CC.PortNumber $ toEnum port) timeoutOpt
116+
ctx <- case mTlsParams of
117+
Nothing -> pure ctx0
118+
Just defaultTlsParams -> do
119+
-- The defaultTlsParams are used to connect to the first
120+
-- host in the cluster, other hosts have different
121+
-- hostnames and so require a different server
122+
-- identification params
123+
let tlsParams = defaultTlsParams {
124+
clientServerIdentification = (host, Char8.pack $ show port)
125+
}
126+
CC.enableTLS tlsParams ctx0
115127
ref <- IOR.newIORef Nothing
116128
return (n, NodeConnection ctx ref n)
117129

src/Database/Redis/Connection.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@ connectCluster bootstrapConnInfo = do
231231
Left e -> throwIO $ ClusterConnectError e
232232
Right infos -> do
233233
#if MIN_VERSION_resource_pool(0,3,0)
234-
pool <- newPool (defaultPoolConfig (Cluster.connect infos shardMapVar Nothing) Cluster.disconnect (realToFrac $ connectMaxIdleTime bootstrapConnInfo) (connectMaxConnections bootstrapConnInfo))
234+
pool <- newPool (defaultPoolConfig (Cluster.connect (connectTLSParams bootstrapConnInfo) infos shardMapVar Nothing) Cluster.disconnect (realToFrac $ connectMaxIdleTime bootstrapConnInfo) (connectMaxConnections bootstrapConnInfo))
235235
#else
236-
pool <- createPool (Cluster.connect infos shardMapVar Nothing) Cluster.disconnect 1 (connectMaxIdleTime bootstrapConnInfo) (connectMaxConnections bootstrapConnInfo)
236+
pool <- createPool (Cluster.connect (connectTLSParams bootstrapConnInfo) infos shardMapVar Nothing) Cluster.disconnect 1 (connectMaxIdleTime bootstrapConnInfo) (connectMaxConnections bootstrapConnInfo)
237237
#endif
238238
return $ ClusteredConnection shardMapVar pool
239239

0 commit comments

Comments
 (0)