From 9f170a6ba7e77e8dc298b0c593e88134ae0229e2 Mon Sep 17 00:00:00 2001 From: Denis Tereshkin Date: Sun, 14 Nov 2021 15:13:47 +0700 Subject: [PATCH] Cert support --- broker-client-test.cabal | 4 ++- client-cert.json | 1 + client-cert.pub.json | 1 + src/Main.hs | 55 +++++++++++++++++++++++++++++++--------- 4 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 client-cert.json create mode 100644 client-cert.pub.json diff --git a/broker-client-test.cabal b/broker-client-test.cabal index 9a3c85a..550f9f0 100644 --- a/broker-client-test.cabal +++ b/broker-client-test.cabal @@ -25,4 +25,6 @@ executable broker-client-test , zeromq4-haskell , containers , optparse-applicative - , libatrade \ No newline at end of file + , libatrade + , random + , text \ No newline at end of file diff --git a/client-cert.json b/client-cert.json new file mode 100644 index 0000000..2e01cf8 --- /dev/null +++ b/client-cert.json @@ -0,0 +1 @@ +{"public_key":"GJpIIMI1/EiClY5S2HN7e+22HqEdO+OlwpjMFwqLAiA=","secret_key":"qnOKIVob5m7fIEI2SllRV2eQ8WtqcQtBJztLk7KGIb4="} \ No newline at end of file diff --git a/client-cert.pub.json b/client-cert.pub.json new file mode 100644 index 0000000..ab86ea5 --- /dev/null +++ b/client-cert.pub.json @@ -0,0 +1 @@ +{"public_key":"GJpIIMI1/EiClY5S2HN7e+22HqEdO+OlwpjMFwqLAiA="} \ No newline at end of file diff --git a/src/Main.hs b/src/Main.hs index 69643d8..4f04f0b 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -5,21 +5,38 @@ module Main where import ATrade.Broker.Client (NotificationCallback, cancelOrder, startBrokerClient, stopBrokerClient, submitOrder) -import ATrade.Types (Operation (..), Order (..), +import ATrade.Types (ClientSecurityParams (ClientSecurityParams), + Operation (..), Order (..), OrderPrice (..), OrderState (Unsubmitted), - SignalId (..), - defaultClientSecurityParams) + SignalId (..)) import Control.Concurrent (threadDelay) import Control.Monad (forever, void) +import Data.String (IsString (fromString)) +import qualified Data.Text as T +import Options.Applicative import System.IO import System.Log.Formatter import System.Log.Handler (setFormatter) import System.Log.Handler.Simple import System.Log.Logger -import System.ZMQ4 +import System.Random +import System.ZMQ4 hiding (identity) import System.ZMQ4.ZAP +data Config = Config + { + identity :: String + } deriving (Show, Eq) + +argParser :: Parser Config +argParser = Config <$> + strOption + ( long "identity" + <> short 'i' + <> metavar "ID" + <> help "Client identity") + initLogging :: [Char] -> IO () initLogging inst = do handler <- streamHandler stderr DEBUG >>= @@ -36,18 +53,27 @@ callback = infoM "notification" . show main :: IO () main = do + cfg <- execParser opts initLogging "broker-client-test" infoM "broker-client-test" "Starting" - withContext (\ctx -> do - bro <- startBrokerClient "test" ctx "tcp://127.0.0.1:5530" "tcp://127.0.0.1:5531" [callback] defaultClientSecurityParams + withContext (\ctx -> + withZapHandler ctx $ \_ -> do + maybeServerCert <- loadCertificateFromFile "../broker-server-test/server-cert.pub.json" + maybeMyCert <- loadCertificateFromFile "./client-cert.json" + case (maybeServerCert, maybeMyCert) of + (Right serverCert, Right myCert) -> do + let clientParams = ClientSecurityParams (Just myCert) (Just serverCert) + bro <- startBrokerClient (fromString . identity $ cfg) ctx "tcp://127.0.0.1:5530" "tcp://127.0.0.1:5531" [callback] clientParams - void $ submitAndCancelOrder bro 1 - void $ forever $ do - threadDelay 10000000 - stopBrokerClient bro) + void $ submitAndCancelOrder bro 1 + void $ forever $ do + threadDelay 10000000 + stopBrokerClient bro + (_, _) -> error "Unable to load certs" + ) where submitAndCancelOrder bro oid = do - threadDelay 1000000 + randomRIO (1000000, 10000000) >>= threadDelay infoM "broker-client-test" "Submitting order" void $ submitOrder bro Order { @@ -61,9 +87,14 @@ main = do orderState = Unsubmitted, orderSignalId = SignalId "" "" "" } - threadDelay 1000000 + randomRIO (1000000, 10000000) >>= threadDelay infoM "broker-client-test" "Cancelling order" void $ cancelOrder bro oid submitAndCancelOrder bro (oid + 1) + opts = info (argParser <**> helper) + ( fullDesc + <> progDesc "Testing utility for broker-client facility" + <> header "broker-client-test" ) +