Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

services/horizon: Improve Captive Stellar-Core validation #3477

Merged
merged 1 commit into from
Mar 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 45 additions & 33 deletions services/horizon/internal/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const (
StellarCoreBinaryPathName = "stellar-core-binary-path"
// CaptiveCoreConfigAppendPathName is the command line flag for configuring the path to the captive core additional configuration
CaptiveCoreConfigAppendPathName = "captive-core-config-append-path"

captiveCoreMigrationHint = "If you are migrating from Horizon 1.x.y read the Migration Guide here: https://github.com/stellar/go/blob/master/services/horizon/internal/docs/captive_core.md"
)

// validateBothOrNeither ensures that both options are provided, if either is provided.
Expand Down Expand Up @@ -413,46 +415,56 @@ func ApplyFlags(config *Config, flags support.ConfigOptions) {
// Validate options that should be provided together
validateBothOrNeither("tls-cert", "tls-key")

// config.HistoryArchiveURLs contains a single empty value when empty so using
// viper.GetString is easier.
if config.Ingest && viper.GetString("history-archive-urls") == "" {
stdLog.Fatalf("--history-archive-urls must be set when --ingest is set")
}
if config.Ingest {
// When running live ingestion a config file is required too
validateBothOrNeither(StellarCoreBinaryPathName, CaptiveCoreConfigAppendPathName)

if config.EnableCaptiveCoreIngestion {
binaryPath := viper.GetString(StellarCoreBinaryPathName)
// config.HistoryArchiveURLs contains a single empty value when empty so using
// viper.GetString is easier.
if len(config.HistoryArchiveURLs) == 0 {
stdLog.Fatalf("--history-archive-urls must be set when --ingest is set")
}

if config.EnableCaptiveCoreIngestion {
binaryPath := viper.GetString(StellarCoreBinaryPathName)

// If the user didn't specify a Stellar Core binary, we can check the
// $PATH and possibly fill it in for them.
if binaryPath == "" {
if result, err := exec.LookPath("stellar-core"); err == nil {
binaryPath = result
viper.Set(StellarCoreBinaryPathName, binaryPath)
config.CaptiveCoreBinaryPath = binaryPath
// If the user didn't specify a Stellar Core binary, we can check the
// $PATH and possibly fill it in for them.
if binaryPath == "" {
if result, err := exec.LookPath("stellar-core"); err == nil {
binaryPath = result
viper.Set(StellarCoreBinaryPathName, binaryPath)
config.CaptiveCoreBinaryPath = binaryPath
}
}

// NOTE: If both of these are set (regardless of user- or PATH-supplied
// defaults for the binary path), the Remote Captive Core URL
// takes precedence.
if binaryPath == "" && config.RemoteCaptiveCoreURL == "" {
stdLog.Fatalf("Invalid config: captive core requires that either --stellar-core-binary-path or --remote-captive-core-url is set. " + captiveCoreMigrationHint)
}
}

remoteURL := viper.GetString("remote-captive-core-url")
if config.CaptiveCoreBinaryPath == "" || config.CaptiveCoreConfigAppendPath == "" {
stdLog.Fatalf("Invalid config: captive core requires that both --%s and --%s are set. "+captiveCoreMigrationHint,
StellarCoreBinaryPathName, CaptiveCoreConfigAppendPathName)
}

// NOTE: If both of these are set (regardless of user- or PATH-supplied
// defaults for the binary path), the Remote Captive Core URL
// takes precedence.
if binaryPath == "" && remoteURL == "" {
stdLog.Fatalf("Invalid config: captive core requires that either --stellar-core-binary-path or --remote-captive-core-url is set. " +
"If you are migrating from Horizon 1.x.y read the Migration Guide here: https://github.com/stellar/go/blob/master/services/horizon/internal/docs/captive_core.md")
// If we don't supply an explicit core URL and we are running a local
// captive core process with the http port enabled, point to it.
if config.StellarCoreURL == "" && config.RemoteCaptiveCoreURL == "" && config.CaptiveCoreHTTPPort != 0 {
config.StellarCoreURL = fmt.Sprintf("http://localhost:%d", config.CaptiveCoreHTTPPort)
viper.Set(StellarCoreURLFlagName, config.StellarCoreURL)
}
}

// If we don't supply an explicit core URL and we are running a local
// captive core process with the http port enabled, point to it.
if config.StellarCoreURL == "" && config.RemoteCaptiveCoreURL == "" && config.CaptiveCoreHTTPPort != 0 {
config.StellarCoreURL = fmt.Sprintf("http://localhost:%d", config.CaptiveCoreHTTPPort)
viper.Set(StellarCoreURLFlagName, config.StellarCoreURL)
} else {
if config.CaptiveCoreBinaryPath != "" || config.CaptiveCoreConfigAppendPath != "" {
stdLog.Fatalf("Invalid config: one or more captive core params passed (--%s or --%s) but --ingest not set. "+captiveCoreMigrationHint,
StellarCoreBinaryPathName, CaptiveCoreConfigAppendPathName)
}
if config.StellarCoreDatabaseURL != "" {
stdLog.Fatalf("Invalid config: --%s passed but --ingest not set. ", StellarCoreDBURLFlagName)
}
}

if config.Ingest {
// When running live ingestion a config file is required too
validateBothOrNeither(StellarCoreBinaryPathName, CaptiveCoreConfigAppendPathName)
}

// Configure log file
Expand Down