This repository has been archived by the owner on Mar 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
CLI.hs
238 lines (211 loc) · 9.42 KB
/
CLI.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
{- | The module which contains parsing facilities for
the CLI options passed to this edge node.
-}
module Cardano.Wallet.Server.CLI where
import Universum
import Data.Time.Units (Minute)
import Data.Version (showVersion)
import Options.Applicative (Mod, OptionFields, Parser, auto,
execParser, footerDoc, fullDesc, header, help, helper,
info, infoOption, long, metavar, option, progDesc,
showDefault, strOption, switch, value)
import Paths_cardano_sl (version)
import Pos.Client.CLI (CommonNodeArgs (..))
import qualified Pos.Client.CLI as CLI
import Pos.Core.NetworkAddress (NetworkAddress, addrParser, localhost)
import Pos.Util.CompileInfo (CompileTimeInfo (..), HasCompileInfo,
compileInfo)
import Pos.Util.OptParse (fromParsec)
import Pos.Web (TlsParams (..))
-- | The options parsed from the CLI when starting up this wallet node.
-- This umbrella data type includes the node-specific options for this edge node
-- plus the wallet backend specific options.
data WalletStartupOptions = WalletStartupOptions {
wsoNodeArgs :: !CommonNodeArgs
, wsoWalletBackendParams :: !WalletBackendParams
} deriving Show
-- | DB-specific options.
data WalletDBOptions = WalletDBOptions {
walletDbPath :: !FilePath
-- ^ The path for the wallet-backend DB.
, walletRebuildDb :: !Bool
-- ^ Whether or not to wipe and rebuild the DB.
, walletAcidInterval :: !Minute
-- ^ The delay between one operation on the acid-state DB and the other.
-- Such @operation@ entails things like checkpointing the DB.
, walletFlushDb :: !Bool
} deriving Show
-- | The startup parameters for the legacy wallet backend.
-- Named with the suffix `Params` to honour other types of
-- parameters like `NodeParams` or `SscParams`.
data WalletBackendParams = WalletBackendParams
{ walletTLSParams :: !TlsParams
-- ^ The TLS parameters for the wallet server
, nodeTLSParams :: !TlsParams
-- ^ The TLS parameters for the node server
, walletAddress :: !NetworkAddress
-- ^ The wallet address.
, walletDocAddress :: !(Maybe NetworkAddress)
-- ^ The wallet documentation address.
, walletDbOptions :: !WalletDBOptions
-- ^ DB-specific options.
, forceFullMigration :: !Bool
-- ^ Force non-lenient migration
, walletNodeAddress :: !NetworkAddress
-- ^ The IP address and port for the node backend.
, walletNodeDocAddress :: !NetworkAddress
-- ^ The IP address and port for the node backend documentation
} deriving Show
-- | Parses and returns the @WalletStartupOptions@ from the command line.
getWalletNodeOptions :: HasCompileInfo => IO WalletStartupOptions
getWalletNodeOptions = execParser programInfo
where
programInfo = info (helper <*> versionOption <*> walletStartupOptionsParser) $
fullDesc <> progDesc "Cardano SL edge node w/ wallet."
<> header "Cardano SL edge node."
<> footerDoc CLI.usageExample
versionOption = infoOption
("cardano-wallet-server-" <> showVersion version <>
", git revision " <> toString (ctiGitRevision compileInfo))
(long "version" <> help "Show version.")
-- | The main @Parser@ for the @WalletStartupOptions@
walletStartupOptionsParser :: Parser WalletStartupOptions
walletStartupOptionsParser = WalletStartupOptions
<$> CLI.commonNodeArgsParser
<*> walletBackendParamsParser
-- | The @Parser@ for the @WalletBackendParams@.
walletBackendParamsParser :: Parser WalletBackendParams
walletBackendParamsParser = mkWalletBackendParams
<$> (not <$> noClientAuthParser)
<*> caPathParser
<*> serverCertPathParser
<*> serverKeyPathParser
<*> clientCertPathParser
<*> clientKeyPathParser
<*> walletAddressParser
<*> docAddressParser
<*> dbOptionsParser
<*> forceFullMigrationParser
<*> nodeAddressParser
<*> nodeDocAddressParser
where
-- NOTE
-- We do this to avoid having two flag for the CA cert and the --no-client-auth
-- (one for the wallet server and one for the node).
-- This way, we enforce, by construction, that they are the same.
mkWalletBackendParams auth caCrt srvCrt srvKey clCrt clKey =
WalletBackendParams
(TlsParams
{ tpCertPath = srvCrt
, tpCaPath = caCrt
, tpKeyPath = srvKey
, tpClientAuth = auth
}
)
(TlsParams
{ tpCertPath = clCrt
, tpCaPath = caCrt
, tpKeyPath = clKey
, tpClientAuth = auth
}
)
walletAddressParser :: Parser NetworkAddress
walletAddressParser = networkAddrOption
$ long "wallet-api-address"
<> help "IP and port for backend wallet API."
<> value (localhost, 8090)
nodeAddressParser :: Parser NetworkAddress
nodeAddressParser = networkAddrOption
$ long "node-api-address"
<> help "IP and port for underlying wallet's node monitoring API."
<> value (localhost, 8080)
nodeDocAddressParser :: Parser NetworkAddress
nodeDocAddressParser = networkAddrOption
$ long "node-doc-address"
<> help "IP and port for underlying wallet's node monitoring API Documentation."
<> value (localhost, 8180)
docAddressParser :: Parser (Maybe NetworkAddress)
docAddressParser = optional $ networkAddrOption
$ long "wallet-doc-address"
<> help "IP and port for backend wallet API documentation."
forceFullMigrationParser :: Parser Bool
forceFullMigrationParser = switch $
long "force-full-wallet-migration" <>
help "Enforces a non-lenient migration. \
\If something fails (for example a wallet fails to decode from the old format) \
\migration will stop and the node will crash, \
\instead of just logging the error."
caPathParser :: Parser FilePath
caPathParser = strOption
$ long "tls-ca-cert"
<> metavar "FILEPATH"
<> help "Path to TLS CA public certificate which signed both wallet and node certificates"
noClientAuthParser :: Parser Bool
noClientAuthParser = switch $
long "tls-no-client-auth" <>
help "Disable TLS client verification. If turned on,\
\ no client certificate is required to talk to the API."
serverCertPathParser :: Parser FilePath
serverCertPathParser = strOption
$ long "tls-wallet-server-cert"
<> metavar "FILEPATH"
<> help "Path to TLS server public certificate used to identify the wallet server"
serverKeyPathParser :: Parser FilePath
serverKeyPathParser = strOption
$ long "tls-wallet-server-key"
<> metavar "FILEPATH"
<> help "Path to TLS server private key used to identify the wallet server"
clientCertPathParser :: Parser FilePath
clientCertPathParser = strOption
$ long "tls-node-client-cert"
<> metavar "FILEPATH"
<> help "Path to TLS client public certificate used to authenticate to the node server"
clientKeyPathParser :: Parser FilePath
clientKeyPathParser = strOption
$ long "tls-node-client-key"
<> metavar "FILEPATH"
<> help "Path to TLS client private key used to authenticate to the node server"
-- | The parser for the @WalletDBOptions@.
dbOptionsParser :: Parser WalletDBOptions
dbOptionsParser = WalletDBOptions
<$> dbPathParser
<*> rebuildDbParser
<*> acidIntervalParser
<*> flushDbParser
where
dbPathParser :: Parser FilePath
dbPathParser = strOption (long "wallet-db-path" <>
help "Path to the wallet's database." <>
value "wallet-db"
)
rebuildDbParser :: Parser Bool
rebuildDbParser = switch (long "wallet-rebuild-db" <>
help "If wallet's database already exists, discard \
\its contents and create a new one from scratch."
)
acidIntervalParser :: Parser Minute
acidIntervalParser = fromInteger <$>
option auto (long "wallet-acid-cleanup-interval" <>
help "Interval on which to execute wallet cleanup \
\action (create checkpoint and archive and \
\cleanup archive partially)" <>
metavar "MINUTES" <>
value 5
)
flushDbParser :: Parser Bool
flushDbParser = switch (long "flush-wallet-db" <>
help "Flushes all blockchain-recoverable data from DB \
\(everything excluding wallets/accounts/addresses, \
\metadata)"
)
-- | Base parser for network addresses. Caller is expected to fill in
-- the flag name, help message and default value if any.
-- e.g.
--
-- networkAddrOption $ long "wallet-address" <> help "API Server Address"
--
networkAddrOption :: Mod OptionFields NetworkAddress -> Parser NetworkAddress
networkAddrOption opts = option (fromParsec addrParser) $ mempty
<> metavar "IP:PORT"
<> showDefault
<> opts