Skip to content

Commit 4651980

Browse files
committed
remote: NixDaemonSpec MITM
1 parent 30baaf3 commit 4651980

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

hnix-store-remote/hnix-store-remote.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ test-suite remote-io
202202
, hnix-store-remote
203203
, hnix-store-tests
204204
, bytestring
205+
, concurrency
205206
, containers
206207
, crypton
207208
, directory

hnix-store-remote/tests-io/NixDaemonSpec.hs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ module NixDaemonSpec
55
, spec
66
) where
77

8-
import Control.Monad (forM_, unless, void)
8+
import Control.Monad (forM_, unless, void, (<=<))
99
import Control.Monad.Catch (MonadMask)
10+
import Control.Monad.Conc.Class (MonadConc)
1011
import Control.Monad.IO.Class (MonadIO, liftIO)
1112
import Crypto.Hash (SHA256)
1213
import Data.Some (Some(Some))
@@ -22,6 +23,7 @@ import System.Nix.DerivedPath (DerivedPath(..))
2223
import System.Nix.StorePath (StoreDir(..), StorePath)
2324
import System.Nix.StorePath.Metadata (Metadata(..))
2425
import System.Nix.Store.Remote
26+
import System.Nix.Store.Remote.Server (WorkerHelper)
2527
import System.Process (CreateProcess(..), ProcessHandle)
2628
import qualified Control.Concurrent
2729
import qualified Control.Exception
@@ -186,12 +188,40 @@ withNixDaemon
186188
-> IO a
187189
withNixDaemon action =
188190
withNixDaemon' $ \_tmpPath storeDir storeConn ->
189-
action $ \a ->
191+
action $ \(mstore :: RemoteStoreT m a) ->
190192
runStoreConnection storeConn
191193
( setStoreDir storeDir
192-
>> a
194+
>> mstore
193195
)
194196

197+
withManInTheMiddleNixDaemon
198+
:: forall m a
199+
. ( MonadIO m
200+
, MonadMask m
201+
, MonadConc m
202+
)
203+
=> ((RemoteStoreT m a -> Run m a) -> IO a)
204+
-> IO a
205+
withManInTheMiddleNixDaemon action =
206+
withNixDaemon' $ \tmpPath storeDir storeConn ->
207+
let
208+
sockFp2 = tmpPath </> "var/nix/daemon-socket/socket2"
209+
storeConn2 = StoreConnection_Socket $ StoreSocketPath sockFp2
210+
211+
handler :: WorkerHelper m
212+
handler = either (error . show) pure
213+
<=< fmap fst
214+
. runStoreConnection storeConn
215+
. (setStoreDir storeDir >>)
216+
. doReq
217+
218+
in action $ \(mstore :: RemoteStoreT m a) ->
219+
runDaemonConnection handler storeConn2
220+
$ runStoreConnection storeConn2
221+
( setStoreDir storeDir
222+
>> mstore
223+
)
224+
195225
checks
196226
:: ( Show a
197227
, Show b
@@ -289,6 +319,7 @@ spec :: Spec
289319
spec = do
290320
describe "Remote store protocol" $ do
291321
describe "Direct" $ makeProtoSpec withNixDaemon
322+
describe "MITM" $ makeProtoSpec withManInTheMiddleNixDaemon
292323

293324
makeProtoSpec
294325
:: (ActionWith

0 commit comments

Comments
 (0)