Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 75b1c63

Browse files
author
Ben Ford
committed
Switch to a json file for config
1 parent b278c95 commit 75b1c63

File tree

7 files changed

+100
-38
lines changed

7 files changed

+100
-38
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,7 @@ result*
103103

104104
# remove when done debugging
105105
1.1.1-1-w/
106+
107+
# Don't include config files with sensitive data or tls certs
108+
/faucet/test-config.json
109+
/faucet/tls/*

faucet/cardano-sl-faucet.cabal

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,28 +65,30 @@ executable faucet
6565
server
6666
ghc-options: -threaded -rtsopts -with-rtsopts=-N
6767
build-depends:
68-
base
69-
, cardano-sl-core
70-
, cardano-sl-faucet
71-
, cardano-sl-wallet
72-
, cardano-sl-wallet-new
73-
, ekg
74-
, ekg-core
75-
, ekg-statsd
76-
, exceptions
77-
, lens
78-
, log-warper
79-
, mmorph
80-
, mtl
81-
, optparse-applicative
82-
, servant
83-
, servant-client
84-
, servant-server
85-
, text
86-
, wai
87-
, wai-cors
88-
, wai-extra
89-
, warp
68+
base
69+
, aeson
70+
, bytestring
71+
, cardano-sl-core
72+
, cardano-sl-faucet
73+
, cardano-sl-wallet
74+
, cardano-sl-wallet-new
75+
, ekg
76+
, ekg-core
77+
, ekg-statsd
78+
, exceptions
79+
, lens
80+
, log-warper
81+
, mmorph
82+
, mtl
83+
, optparse-applicative
84+
, servant
85+
, servant-client
86+
, servant-server
87+
, text
88+
, wai
89+
, wai-cors
90+
, wai-extra
91+
, warp
9092
default-language: Haskell2010
9193

9294
test-suite cardano-sl-faucet-test

faucet/logging.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
termSeveritiesErr: All
2+
loggerTree:
3+
severity: Info+ # severities for «root» logger
4+
withdraw: # logger named «new-logger»
5+
severity: Debug+ # severities for logger «new-logger»

faucet/server/Main.hs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ module Main where
66
import Control.Lens
77
import Network.Wai.Handler.Warp (run)
88
import Servant
9+
import System.Environment (getArgs)
10+
import Data.ByteString.Lazy as BSL
11+
import Data.Aeson (eitherDecode)
912

1013
import Control.Monad.Except
1114
-- import Cardano.Wallet.API.V1.Types (PaymentSource (..), WalletId(..), AccountIndex)
@@ -16,9 +19,14 @@ import System.Remote.Monitoring.Statsd (forkStatsd)
1619
main :: IO ()
1720
main = do
1821
ekg <- forkServer "localhost" 8001
19-
let c = testFC -- mkFaucetConfig "wallet-url" 8000 (PaymentSource w idx) defaultStatsdOptions "./logging.cfg"
20-
fEnv <- initEnv c (serverMetricStore ekg)
21-
_statsd <- forkStatsd (c ^. fcStatsdOpts) (fEnv ^. feStore)
22+
args <- getArgs
23+
config <- case args of
24+
[ "--config", cfgFile ] -> do
25+
ecfg <- eitherDecode <$> BSL.readFile cfgFile
26+
either (error . ("Error decoding: " ++)) return ecfg
27+
_ -> error "Need a --config argument pointing to a json file"
28+
fEnv <- initEnv config (serverMetricStore ekg)
29+
_statsd <- forkStatsd (config ^. fcStatsdOpts . _Wrapped') (fEnv ^. feStore)
2230
run 8081 (serve serverAPI $ s fEnv)
2331
where
2432
nat :: FaucetEnv -> M a -> Handler a

faucet/src/Cardano/Faucet/Types.hs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
{-# LANGUAGE ConstraintKinds #-}
22
{-# LANGUAGE DeriveGeneric #-}
33
{-# LANGUAGE DuplicateRecordFields #-}
4+
{-# LANGUAGE FlexibleInstances #-}
45
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
6+
{-# LANGUAGE MultiParamTypeClasses #-}
57
{-# LANGUAGE OverloadedStrings #-}
68
{-# LANGUAGE TemplateHaskell #-}
9+
{-# LANGUAGE TypeFamilies #-}
710
{-# LANGUAGE ViewPatterns #-}
811
{-# OPTIONS_GHC -Wall #-}
912
module Cardano.Faucet.Types (
10-
FaucetConfig(..), mkFaucetConfig, testFC
13+
FaucetConfig(..), mkFaucetConfig
1114
, HasFaucetConfig(..)
1215
, FaucetEnv(..), initEnv
1316
, HasFaucetEnv(..)
@@ -46,12 +49,11 @@ import System.Metrics.Counter (Counter)
4649
import qualified System.Metrics.Counter as Counter
4750
import System.Metrics.Gauge (Gauge)
4851
import qualified System.Metrics.Gauge as Gauge
49-
import System.Remote.Monitoring.Statsd (StatsdOptions, defaultStatsdOptions)
52+
import System.Remote.Monitoring.Statsd (StatsdOptions (..))
5053
import System.Wlog (CanLog, HasLoggerName, LoggerName (..), LoggerNameBox (..),
5154
WithLogger, launchFromFile)
5255

53-
import Cardano.Wallet.API.V1.Types (PaymentSource (..), Transaction,
54-
V1, WalletId (..))
56+
import Cardano.Wallet.API.V1.Types (PaymentSource (..), Transaction, V1)
5557
import Cardano.Wallet.Client (ClientError (..), WalletClient)
5658
import Cardano.Wallet.Client.Http (mkHttpClient)
5759
import Pos.Core (Address (..), Coin (..))
@@ -104,34 +106,58 @@ data DepositResult = DepositResult
104106

105107
instance ToJSON DepositResult
106108

109+
--------------------------------------------------------------------------------
110+
newtype FaucetStatsdOpts = FaucetStatsdOpts StatsdOptions deriving (Generic)
111+
112+
makeWrapped ''FaucetStatsdOpts
107113
--------------------------------------------------------------------------------
108114
data FaucetConfig = FaucetConfig {
109115
_fcWalletApiHost :: String
110116
, _fcWalletApiPort :: Int
111117
, _fcFaucetPaymentSource :: PaymentSource
112-
, _fcStatsdOpts :: StatsdOptions
118+
, _fcSpendingPassword :: Text
119+
, _fcStatsdOpts :: FaucetStatsdOpts
113120
, _fcLoggerConfigFile :: FilePath
114121
, _fcPubCertFile :: FilePath
115122
, _fcPrivKeyFile :: FilePath
116123
}
117124

118125
makeClassy ''FaucetConfig
119126

127+
instance FromJSON FaucetStatsdOpts where
128+
parseJSON = fmap FaucetStatsdOpts . (withObject "StatsdOptions" $ \v ->
129+
StatsdOptions
130+
<$> v .: "host"
131+
<*> v .: "port"
132+
<*> v .: "flush-interval"
133+
<*> pure False
134+
<*> pure "faucet"
135+
<*> pure "")
136+
137+
instance FromJSON FaucetConfig where
138+
parseJSON = withObject "FaucetConfig" $ \v ->
139+
FaucetConfig
140+
<$> v .: "wallet-host"
141+
<*> v .: "wallet-port"
142+
<*> v .: "payment-source"
143+
<*> v .: "spending-password"
144+
<*> v .: "statsd"
145+
<*> v .: "logging-config"
146+
<*> v .: "public-certificate"
147+
<*> v .: "private-key"
148+
120149
mkFaucetConfig
121150
:: String
122151
-> Int
123152
-> PaymentSource
124-
-> StatsdOptions
153+
-> Text
154+
-> FaucetStatsdOpts
125155
-> FilePath
126156
-> FilePath
127157
-> FilePath
128158
-> FaucetConfig
129159
mkFaucetConfig = FaucetConfig
130160

131-
testFC :: FaucetConfig
132-
testFC = FaucetConfig "127.0.0.1" 8090 ps defaultStatsdOptions "./logging.cfg" "./tls/ca.crt" "./tls/server.key"
133-
where
134-
ps = PaymentSource (WalletId "Ae2tdPwUPEZLBG2sEmiv8Y6DqD4LoZKQ5wosXucbLnYoacg2YZSPhMn4ETi") 2147483648
135161

136162
--------------------------------------------------------------------------------
137163
data FaucetEnv = FaucetEnv {

faucet/src/Cardano/WalletClient.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import qualified Crypto.Hash as CryptoHash
1818
import qualified Data.ByteArray as BA
1919
import Data.ByteString (ByteString)
2020
import Data.List.NonEmpty (NonEmpty (..))
21+
import Data.Text.Strict.Lens (utf8)
2122
import Pos.Core (Address (..), Coin (..))
2223
import Pos.Crypto.Signing (PassPhrase)
2324

@@ -26,13 +27,12 @@ import Pos.Crypto.Signing (PassPhrase)
2627
withdraw :: (MonadFaucet c m) => V1 Address -> V1 Coin -> Resp m Transaction
2728
withdraw addr coin = do
2829
paymentSource <- view (feFaucetConfig . fcFaucetPaymentSource)
30+
spendingPassword <- view (feFaucetConfig . fcSpendingPassword . re utf8)
2931
client <- liftClient <$> view feWalletClient
3032
let paymentDist = (PaymentDistribution addr coin :| [])
33+
sp = Just $ V1 $ hashPwd spendingPassword
3134
payment = Payment paymentSource paymentDist Nothing sp
3235
postTransaction client payment
33-
where
34-
sp :: Maybe (V1 PassPhrase)
35-
sp = Just $ V1 $ hashPwd "XXX" -- TODO: get from config
3636

3737
hashPwd :: ByteString -> PassPhrase
3838
hashPwd bs =

faucet/test-config.json.template

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"wallet-host": "127.0.0.1"
3+
, "wallet-port": 8090
4+
, "payment-source": {
5+
"walletId": "XXX"
6+
, "accountIndex": 2147483648
7+
}
8+
, "spending-password": "XXX"
9+
, "statsd": {
10+
"host": "127.0.0.1"
11+
, "port": 8125
12+
, "flush-interval": 1000
13+
}
14+
, "logging-config": "./logging.cfg"
15+
, "public-certificate": "./tls/ca.crt"
16+
, "private-key": "./tls/server.key"
17+
}

0 commit comments

Comments
 (0)