Skip to content

Commit 356bddb

Browse files
author
Konstantinos Chalkias
authored
check for empty rpc keystore/trustore passwords + extract method refactoring. (corda#3944)
1 parent 5be7d5c commit 356bddb

File tree

1 file changed

+78
-61
lines changed

1 file changed

+78
-61
lines changed

node/src/main/kotlin/net/corda/node/internal/NodeStartup.kt

Lines changed: 78 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -256,66 +256,7 @@ open class NodeStartup: CordaCliWrapper("corda", "Runs a Corda Node") {
256256
return
257257
}
258258
if (cmdLineOptions.justGenerateRpcSslCerts) {
259-
val (keyPair, cert) = createKeyPairAndSelfSignedTLSCertificate(conf.myLegalName.x500Principal)
260-
261-
val keyStorePath = conf.baseDirectory / "certificates" / "rpcsslkeystore.jks"
262-
val trustStorePath = conf.baseDirectory / "certificates" / "export" / "rpcssltruststore.jks"
263-
264-
if (keyStorePath.exists() || trustStorePath.exists()) {
265-
println("Found existing RPC SSL keystores. Command was already run. Exiting..")
266-
exitProcess(0)
267-
}
268-
269-
val console: Console? = System.console()
270-
271-
when (console) {
272-
// In this case, the JVM is not connected to the console so we need to exit
273-
null -> {
274-
println("Not connected to console. Exiting")
275-
exitProcess(1)
276-
}
277-
// Otherwise we can proceed normally
278-
else -> {
279-
while (true) {
280-
val keystorePassword1 = console.readPassword("Enter the keystore password => ")
281-
val keystorePassword2 = console.readPassword("Re-enter the keystore password => ")
282-
if (!keystorePassword1.contentEquals(keystorePassword2)) {
283-
println("The keystore passwords don't match.")
284-
continue
285-
}
286-
saveToKeyStore(keyStorePath, keyPair, cert, String(keystorePassword1), "rpcssl")
287-
println("The keystore was saved to: $keyStorePath .")
288-
break
289-
}
290-
291-
while (true) {
292-
val trustStorePassword1 = console.readPassword("Enter the truststore password => ")
293-
val trustStorePassword2 = console.readPassword("Re-enter the truststore password => ")
294-
if (!trustStorePassword1.contentEquals(trustStorePassword2)) {
295-
println("The truststore passwords don't match.")
296-
continue
297-
}
298-
299-
saveToTrustStore(trustStorePath, cert, String(trustStorePassword1), "rpcssl")
300-
println("The truststore was saved to: $trustStorePath .")
301-
println("You need to distribute this file along with the password in a secure way to all RPC clients.")
302-
break
303-
}
304-
305-
val dollar = '$'
306-
println("""
307-
|
308-
|The SSL certificates were generated successfully.
309-
|
310-
|Add this snippet to the "rpcSettings" section of your node.conf:
311-
| useSsl=true
312-
| ssl {
313-
| keyStorePath=$dollar{baseDirectory}/certificates/rpcsslkeystore.jks
314-
| keyStorePassword=the_above_password
315-
| }
316-
|""".trimMargin())
317-
}
318-
}
259+
generateRpcSslCertificates(conf)
319260
return
320261
}
321262

@@ -355,6 +296,82 @@ open class NodeStartup: CordaCliWrapper("corda", "Runs a Corda Node") {
355296
node.run()
356297
}
357298

299+
private fun generateRpcSslCertificates(conf: NodeConfiguration) {
300+
val (keyPair, cert) = createKeyPairAndSelfSignedTLSCertificate(conf.myLegalName.x500Principal)
301+
302+
val keyStorePath = conf.baseDirectory / "certificates" / "rpcsslkeystore.jks"
303+
val trustStorePath = conf.baseDirectory / "certificates" / "export" / "rpcssltruststore.jks"
304+
305+
if (keyStorePath.exists() || trustStorePath.exists()) {
306+
println("Found existing RPC SSL keystores. Command was already run. Exiting..")
307+
exitProcess(0)
308+
}
309+
310+
val console: Console? = System.console()
311+
312+
when (console) {
313+
// In this case, the JVM is not connected to the console so we need to exit.
314+
null -> {
315+
println("Not connected to console. Exiting")
316+
exitProcess(1)
317+
}
318+
// Otherwise we can proceed normally.
319+
else -> {
320+
while (true) {
321+
val keystorePassword1 = console.readPassword("Enter the RPC keystore password => ")
322+
// TODO: consider adding a password strength policy.
323+
if (keystorePassword1.isEmpty()) {
324+
println("The RPC keystore password cannot be an empty String.")
325+
continue
326+
}
327+
328+
val keystorePassword2 = console.readPassword("Re-enter the RPC keystore password => ")
329+
if (!keystorePassword1.contentEquals(keystorePassword2)) {
330+
println("The RPC keystore passwords don't match.")
331+
continue
332+
}
333+
334+
saveToKeyStore(keyStorePath, keyPair, cert, String(keystorePassword1), "rpcssl")
335+
println("The RPC keystore was saved to: $keyStorePath .")
336+
break
337+
}
338+
339+
while (true) {
340+
val trustStorePassword1 = console.readPassword("Enter the RPC truststore password => ")
341+
// TODO: consider adding a password strength policy.
342+
if (trustStorePassword1.isEmpty()) {
343+
println("The RPC truststore password cannot be an empty String.")
344+
continue
345+
}
346+
347+
val trustStorePassword2 = console.readPassword("Re-enter the RPC truststore password => ")
348+
if (!trustStorePassword1.contentEquals(trustStorePassword2)) {
349+
println("The RPC truststore passwords don't match.")
350+
continue
351+
}
352+
353+
saveToTrustStore(trustStorePath, cert, String(trustStorePassword1), "rpcssl")
354+
println("The RPC truststore was saved to: $trustStorePath .")
355+
println("You need to distribute this file along with the password in a secure way to all RPC clients.")
356+
break
357+
}
358+
359+
val dollar = '$'
360+
println("""
361+
|
362+
|The SSL certificates for RPC were generated successfully.
363+
|
364+
|Add this snippet to the "rpcSettings" section of your node.conf:
365+
| useSsl=true
366+
| ssl {
367+
| keyStorePath=$dollar{baseDirectory}/certificates/rpcsslkeystore.jks
368+
| keyStorePassword=the_above_password
369+
| }
370+
|""".trimMargin())
371+
}
372+
}
373+
}
374+
358375
protected open fun logStartupInfo(versionInfo: VersionInfo, conf: NodeConfiguration) {
359376
logger.info("Vendor: ${versionInfo.vendor}")
360377
logger.info("Release: ${versionInfo.releaseVersion}")
@@ -411,7 +428,7 @@ open class NodeStartup: CordaCliWrapper("corda", "Runs a Corda Node") {
411428
)
412429
}
413430

414-
open protected fun logLoadedCorDapps(corDapps: List<CordappImpl>) {
431+
protected open fun logLoadedCorDapps(corDapps: List<CordappImpl>) {
415432
fun CordappImpl.Info.description() = "$shortName version $version by $vendor"
416433

417434
Node.printBasicNodeInfo("Loaded ${corDapps.size} CorDapp(s)", corDapps.map { it.info }.joinToString(", ", transform = CordappImpl.Info::description))

0 commit comments

Comments
 (0)