Added command line flag to set mode (#130)

Added command line flag to set mode and removed it from the config. This is to provide a programmatic interface to doorman configuration for downstream
projects.
This commit is contained in:
Clinton 2017-12-01 15:13:41 +00:00 committed by GitHub
parent 7b54b82273
commit faf6b1d5bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 8 deletions

View File

@ -1,9 +1,11 @@
package com.r3.corda.networkmanage.doorman package com.r3.corda.networkmanage.doorman
import com.r3.corda.networkmanage.common.utils.ShowHelpException import com.r3.corda.networkmanage.common.utils.ShowHelpException
import com.typesafe.config.Config
import com.typesafe.config.ConfigFactory import com.typesafe.config.ConfigFactory
import com.typesafe.config.ConfigParseOptions import com.typesafe.config.ConfigParseOptions
import joptsimple.OptionParser import joptsimple.OptionParser
import joptsimple.util.EnumConverter
import net.corda.core.internal.isRegularFile import net.corda.core.internal.isRegularFile
import net.corda.nodeapi.config.parseAs import net.corda.nodeapi.config.parseAs
import java.nio.file.Path import java.nio.file.Path
@ -21,7 +23,6 @@ data class DoormanParameters(// TODO Create a localSigning sub-config and put th
val host: String, val host: String,
val port: Int, val port: Int,
val dataSourceProperties: Properties, val dataSourceProperties: Properties,
val mode: Mode = Mode.DOORMAN,
val approveAll: Boolean = false, val approveAll: Boolean = false,
val databaseProperties: Properties? = null, val databaseProperties: Properties? = null,
val jiraConfig: JiraConfig? = null, val jiraConfig: JiraConfig? = null,
@ -31,7 +32,6 @@ data class DoormanParameters(// TODO Create a localSigning sub-config and put th
val rootStorePath: Path? = null, val rootStorePath: Path? = null,
// TODO Change these to Duration in the future // TODO Change these to Duration in the future
val approveInterval: Long = DEFAULT_APPROVE_INTERVAL, val approveInterval: Long = DEFAULT_APPROVE_INTERVAL,
// TODO Should be part of a localSigning sub-config
val signInterval: Long = DEFAULT_SIGN_INTERVAL val signInterval: Long = DEFAULT_SIGN_INTERVAL
) { ) {
enum class Mode { enum class Mode {
@ -53,7 +53,8 @@ data class DoormanParameters(// TODO Create a localSigning sub-config and put th
} }
data class CommandLineOptions(val configFile: Path, data class CommandLineOptions(val configFile: Path,
val updateNetworkParametersFile: Path?) { val updateNetworkParametersFile: Path?,
val mode: DoormanParameters.Mode) {
init { init {
check(configFile.isRegularFile()) { "Config file $configFile does not exist" } check(configFile.isRegularFile()) { "Config file $configFile does not exist" }
if (updateNetworkParametersFile != null) { if (updateNetworkParametersFile != null) {
@ -76,7 +77,12 @@ fun parseCommandLine(vararg args: String): CommandLineOptions {
.withRequiredArg() .withRequiredArg()
.describedAs("The new network map") .describedAs("The new network map")
.describedAs("filepath") .describedAs("filepath")
val helpOption = optionParser.acceptsAll(listOf("h", "?", "help"), "show help").forHelp(); val modeArg = optionParser
.accepts("mode", "Set the mode of this application")
.withRequiredArg()
.withValuesConvertedBy(object : EnumConverter<DoormanParameters.Mode>(DoormanParameters.Mode::class.java) {})
.defaultsTo(DoormanParameters.Mode.DOORMAN)
val helpOption = optionParser.acceptsAll(listOf("h", "?", "help"), "show help").forHelp()
val optionSet = optionParser.parse(*args) val optionSet = optionParser.parse(*args)
// Print help and exit on help option or if there are missing options. // Print help and exit on help option or if there are missing options.
@ -90,16 +96,18 @@ fun parseCommandLine(vararg args: String): CommandLineOptions {
Paths.get(it).toAbsolutePath() Paths.get(it).toAbsolutePath()
} }
return CommandLineOptions(configFile, updateNetworkParameters) return CommandLineOptions(configFile, updateNetworkParameters, optionSet.valueOf(modeArg))
} }
/** /**
* Parses a configuration file, which contains all the configuration except the initial values for the network * Parses a configuration file, which contains all the configuration except the initial values for the network
* parameters. * parameters.
*/ */
fun parseParameters(configFile: Path): DoormanParameters { fun parseParameters(configFile: Path, overrides: Config = ConfigFactory.empty()): DoormanParameters {
return ConfigFactory val config = ConfigFactory
.parseFile(configFile.toFile(), ConfigParseOptions.defaults().setAllowMissing(true)) .parseFile(configFile.toFile(), ConfigParseOptions.defaults().setAllowMissing(true))
.resolve() .resolve()
return overrides
.withFallback(config)
.parseAs() .parseAs()
} }

View File

@ -11,6 +11,7 @@ import com.r3.corda.networkmanage.doorman.signer.JiraCsrHandler
import com.r3.corda.networkmanage.doorman.signer.LocalSigner import com.r3.corda.networkmanage.doorman.signer.LocalSigner
import com.r3.corda.networkmanage.doorman.webservice.NodeInfoWebService import com.r3.corda.networkmanage.doorman.webservice.NodeInfoWebService
import com.r3.corda.networkmanage.doorman.webservice.RegistrationWebService import com.r3.corda.networkmanage.doorman.webservice.RegistrationWebService
import com.typesafe.config.ConfigFactory
import net.corda.core.crypto.Crypto import net.corda.core.crypto.Crypto
import net.corda.core.identity.CordaX500Name import net.corda.core.identity.CordaX500Name
import net.corda.core.internal.createDirectories import net.corda.core.internal.createDirectories
@ -119,7 +120,7 @@ fun generateRootKeyPair(rootStorePath: Path, rootKeystorePass: String?, rootPriv
rootStore.addOrReplaceKey(X509Utilities.CORDA_ROOT_CA, selfSignKey.private, rootPrivateKeyPassword.toCharArray(), arrayOf(selfSignCert)) rootStore.addOrReplaceKey(X509Utilities.CORDA_ROOT_CA, selfSignKey.private, rootPrivateKeyPassword.toCharArray(), arrayOf(selfSignCert))
rootStore.save(rootStorePath, rootKeystorePassword) rootStore.save(rootStorePath, rootKeystorePassword)
println("Root CA keypair and certificate stored in $rootStorePath.") println("Root CA keypair and certificate stored in ${rootStorePath.toAbsolutePath()}.")
println(loadKeyStore(rootStorePath, rootKeystorePassword).getCertificate(X509Utilities.CORDA_ROOT_CA).publicKey) println(loadKeyStore(rootStorePath, rootKeystorePassword).getCertificate(X509Utilities.CORDA_ROOT_CA).publicKey)
} }
@ -260,7 +261,9 @@ private class ApproveAllCertificateRequestStorage(private val delegate: Certific
fun main(args: Array<String>) { fun main(args: Array<String>) {
try { try {
val commandLineOptions = parseCommandLine(*args) val commandLineOptions = parseCommandLine(*args)
val mode = commandLineOptions.mode
parseParameters(commandLineOptions.configFile).run { parseParameters(commandLineOptions.configFile).run {
println("Starting in $mode mode")
when (mode) { when (mode) {
DoormanParameters.Mode.ROOT_KEYGEN -> generateRootKeyPair( DoormanParameters.Mode.ROOT_KEYGEN -> generateRootKeyPair(
rootStorePath ?: throw IllegalArgumentException("The 'rootStorePath' parameter must be specified when generating keys!"), rootStorePath ?: throw IllegalArgumentException("The 'rootStorePath' parameter must be specified when generating keys!"),