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

Commit efd2112

Browse files
author
Ben Ford
committed
[DEVOPS-834] Use a file to read the recaptcha secret from a file
* Config parameter has changed to "recaptcha-secret-file" * The actual secret is now a field in FaucetEnv
1 parent 08b603a commit efd2112

File tree

6 files changed

+46
-28
lines changed

6 files changed

+46
-28
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,4 @@ venv.bak/
215215
/faucet/generated-wallet-details.json
216216
/faucet/default.nix
217217
/faucet/shell.nix
218+
/faucet/recaptcha-secret.txt

faucet/src/Cardano/Faucet.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ faucetServerAPI = Proxy
5252
withdraw :: (MonadFaucet c m) => WithdrawalRequest -> m WithdrawalResult
5353
withdraw wr = withSublogger (LoggerName "withdraw") $ do
5454
logInfo "Attempting to send ADA"
55-
mCaptchaSecret <- view (feFaucetConfig . fcRecaptchaSecret)
55+
mCaptchaSecret <- view feRecaptchaSecret
5656
forM_ mCaptchaSecret $ \captchaSecret -> do
5757
let cr = CaptchaRequest captchaSecret (wr ^. gRecaptchaResponse)
5858
logInfo "Found a secret for recaptcha in config, attempting validation"

faucet/src/Cardano/Faucet/Init.hs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ import Network.Connection (TLSSettings (..))
4040
import Network.HTTP.Client (Manager, newManager)
4141
import Network.HTTP.Client.TLS (mkManagerSettings)
4242
import Network.TLS (ClientParams (..), credentialLoadX509FromMemory,
43-
defaultParamsClient, onCertificateRequest,
44-
onServerCertificate, supportedCiphers)
43+
defaultParamsClient, onCertificateRequest,
44+
onServerCertificate, supportedCiphers)
4545
import Network.TLS.Extra.Cipher (ciphersuite_strong)
4646
import Servant.Client.Core (BaseUrl (..), Scheme (..))
4747
import System.Directory (createDirectoryIfMissing)
@@ -50,23 +50,27 @@ import System.IO.Error (isDoesNotExistError)
5050
import System.Metrics (Store, createCounter, createGauge)
5151
import qualified System.Metrics.Gauge as Gauge
5252
import System.Wlog (CanLog, HasLoggerName, LoggerNameBox (..),
53-
liftLogIO, logError, logInfo, withSublogger)
53+
liftLogIO, logError, logInfo, withSublogger)
5454

5555
import Cardano.Wallet.API.V1.Types (Account (..), Address,
56-
AssuranceLevel (NormalAssurance), NewWallet (..),
57-
NodeInfo (..), Payment (..), PaymentDistribution (..),
58-
PaymentSource (..), SyncPercentage, V1 (..), Wallet (..),
59-
WalletAddress (..), WalletId,
60-
WalletOperation (CreateWallet), mkSyncPercentage,
61-
txAmount, unV1)
56+
AssuranceLevel (NormalAssurance),
57+
NewWallet (..), NodeInfo (..),
58+
Payment (..),
59+
PaymentDistribution (..),
60+
PaymentSource (..),
61+
SyncPercentage, V1 (..),
62+
Wallet (..), WalletAddress (..),
63+
WalletId,
64+
WalletOperation (CreateWallet),
65+
mkSyncPercentage, txAmount, unV1)
6266
import Cardano.Wallet.Client (ClientError (..), WalletClient (..),
63-
WalletResponse (..), liftClient)
67+
WalletResponse (..), liftClient)
6468
import Cardano.Wallet.Client.Http (mkHttpClient)
6569
import Pos.Core (Coin (..))
6670
import Pos.Util.Mnemonic (Mnemonic, entropyToMnemonic, genEntropy)
6771

6872
import Cardano.Faucet.Types
69-
73+
import Cardano.Faucet.Types.Recaptcha
7074

7175
--------------------------------------------------------------------------------
7276
-- | Parses a 'SourceWalletConfig' from a file containing JSON
@@ -362,6 +366,7 @@ initEnv fc store = do
362366
logInfo "Initializing wallet"
363367
initialWallet <- makeInitializedWallet fc (liftClient client)
364368
pmtQ <- liftIO $ TBQ.newTBQueueIO 10
369+
mRecaptchaSecret <- liftIO $ traverse readCaptchaSecret (fc ^. fcRecaptchaSecretFile)
365370
case initialWallet of
366371
Left err -> do
367372
logError ( "Error initializing wallet. Exiting: "
@@ -378,6 +383,7 @@ initEnv fc store = do
378383
fc
379384
client
380385
pmtQ
386+
mRecaptchaSecret
381387

382388

383389
-- | Makes a http client 'Manager' for communicating with the wallet node

faucet/src/Cardano/Faucet/Types/Config.hs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import Control.Concurrent.STM.TMVar (TMVar)
3333
import Control.Exception (Exception)
3434
import Control.Lens hiding ((.=))
3535
import Data.Aeson (FromJSON (..), ToJSON (..), object, withObject,
36-
(.:), (.:?), (.=))
36+
(.:), (.:?), (.=))
3737
import Data.Int (Int64)
3838
import Data.Text (Text)
3939
import Data.Typeable (Typeable)
@@ -44,7 +44,8 @@ import System.Metrics.Gauge (Gauge)
4444
import System.Remote.Monitoring.Statsd (StatsdOptions (..))
4545

4646
import Cardano.Wallet.API.V1.Types (AccountIndex, Payment,
47-
PaymentSource (..), V1, WalletId (..))
47+
PaymentSource (..), V1,
48+
WalletId (..))
4849
import Cardano.Wallet.Client (ClientError (..), WalletClient (..))
4950
import Pos.Core (Address (..))
5051
import Pos.Util.Mnemonic (Mnemonic)
@@ -173,8 +174,10 @@ data FaucetConfig = FaucetConfig {
173174
, _fcPubCertFile :: !FilePath
174175
-- | TLS private key
175176
, _fcPrivKeyFile :: !FilePath
176-
-- | Recapctch sectret key. Absence indicates not to use recaptcha
177-
, _fcRecaptchaSecret :: !(Maybe CaptchaSecret)
177+
-- | File path containing recapctch sectret key.
178+
--
179+
-- Absence indicates not to use recaptcha
180+
, _fcRecaptchaSecretFile :: !(Maybe FilePath)
178181
}
179182

180183
makeClassy ''FaucetConfig
@@ -191,7 +194,7 @@ instance FromJSON FaucetConfig where
191194
<*> v .: "logging-config"
192195
<*> v .: "public-certificate"
193196
<*> v .: "private-key"
194-
<*> (fmap CaptchaSecret <$> v .:? "recaptcha-secret")
197+
<*> v .:? "recaptcha-secret-file"
195198

196199
--------------------------------------------------------------------------------
197200
-- | Details of a wallet created by the faucet at run time if 'Generate' is used
@@ -257,23 +260,25 @@ makeLenses ''ProcessorPayload
257260
-- | Run time environment for faucet's reader Monad
258261
data FaucetEnv = FaucetEnv {
259262
-- | Counter for total amount withdawn from a wallet while faucet is running
260-
_feWithdrawn :: !Counter
263+
_feWithdrawn :: !Counter
261264
-- | Counter for number of withdrawals made
262-
, _feNumWithdrawn :: !Counter
265+
, _feNumWithdrawn :: !Counter
263266
-- | Gauge for wallet balance
264-
, _feWalletBalance :: !Gauge
267+
, _feWalletBalance :: !Gauge
265268
-- | Metrics store
266-
, _feStore :: !Store
269+
, _feStore :: !Store
267270
-- | Config for source of funds
268-
, _feSourceWallet :: !SourceWalletConfig
271+
, _feSourceWallet :: !SourceWalletConfig
269272
-- | Return address for sending ADA back to the faucet
270-
, _feReturnAddress :: !(V1 Address)
273+
, _feReturnAddress :: !(V1 Address)
271274
-- | Original static config object
272-
, _feFaucetConfig :: !FaucetConfig
275+
, _feFaucetConfig :: !FaucetConfig
273276
-- | Client for communicating with wallet API
274-
, _feWalletClient :: !(WalletClient IO)
277+
, _feWalletClient :: !(WalletClient IO)
275278
-- | Lock to ensure only one withdrawal at a time
276-
, _feWithdrawalQ :: !(TBQueue ProcessorPayload)
279+
, _feWithdrawalQ :: !(TBQueue ProcessorPayload)
280+
-- | Recaptcha secret read from 'fcRecaptchaSecretFile'
281+
, _feRecaptchaSecret :: !(Maybe CaptchaSecret)
277282
}
278283

279284
makeClassy ''FaucetEnv

faucet/src/Cardano/Faucet/Types/Recaptcha.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ module Cardano.Faucet.Types.Recaptcha
1212
( CaptchaSecret(..)
1313
, CaptchaRequest(..), secret, response
1414
, CaptchaResponse(..), success, challengeTS, hostname, errorCodes
15-
, captchaRequest) where
15+
, readCaptchaSecret
16+
, captchaRequest
17+
) where
1618

1719
import Control.Lens hiding ((.=))
1820
import Data.Maybe
@@ -22,6 +24,7 @@ import qualified Network.Wreq as Wreq
2224
-- import Data.Proxy
2325
import Data.Aeson
2426
import Data.Text (Text)
27+
import qualified Data.Text.IO as Text
2528
import Data.Time.Clock (UTCTime)
2629
import GHC.Generics (Generic)
2730

@@ -69,6 +72,9 @@ instance FromJSON CaptchaResponse where
6972
<*> v .:? "hostname"
7073
<*> (fromMaybe [] <$> v .:? "error-codes")
7174

75+
-- | Reads a CaptchaSecret out of a file
76+
readCaptchaSecret :: FilePath -> IO CaptchaSecret
77+
readCaptchaSecret = fmap CaptchaSecret . Text.readFile
7278

7379
-- | Makes the 'CaptchaRequest' to google
7480
captchaRequest :: CaptchaRequest -> IO CaptchaResponse

faucet/test-config.json.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
, "logging-config": "./logging.cfg"
1919
, "public-certificate": "./tls/ca.crt"
2020
, "private-key": "./tls/server.key"
21-
, "recaptcha-secret": "XXX"
21+
, "recaptcha-secret-file": "recaptcha-secret.txt"
2222
}

0 commit comments

Comments
 (0)