Add `--generate-master-key` CLI option

This commit is contained in:
Louis Dureuil 2022-12-22 11:58:04 +01:00
parent 57e851d8a9
commit 3cba476a9f
No known key found for this signature in database
3 changed files with 14 additions and 0 deletions

View File

@ -277,6 +277,7 @@ impl From<Opt> for Infos {
indexer_options,
scheduler_options,
config_file_path,
generate_master_key: _,
#[cfg(all(not(debug_assertions), feature = "analytics"))]
no_analytics: _,
} = options;

View File

@ -33,6 +33,10 @@ async fn main() -> anyhow::Result<()> {
setup(&opt)?;
if opt.generate_master_key {
println!("{}", generate_master_key());
return Ok(());
}
match (opt.env.as_ref(), &opt.master_key) {
("production", Some(master_key)) if master_key.len() < MASTER_KEY_MIN_SIZE => {
@ -180,6 +184,7 @@ Anonymous telemetry:\t\"Enabled\""
"The provided Master Key is too short (< {MASTER_KEY_MIN_SIZE} characters)"
);
eprintln!("A Master Key of at least {MASTER_KEY_MIN_SIZE} characters will be required when switching to the production environment.");
eprintln!("Restart Meilisearch with the `--generate-master-key` flag to generate a secure Master Key you can use");
}
}
("development", None) => {

View File

@ -230,6 +230,13 @@ pub struct Opt {
#[serde(default = "default_log_level")]
pub log_level: String,
/// Generates a string of characters that can be used as a Master Key and exits.
///
/// Pass the generated Master Key using the `--master-key` argument or the `MEILI_MASTER_KEY` environment variable in a subsequent Meilisearch invocation.
#[clap(long)]
#[serde(default)]
pub generate_master_key: bool,
/// Enables Prometheus metrics and /metrics route.
#[cfg(feature = "metrics")]
#[clap(long, env = MEILI_ENABLE_METRICS_ROUTE)]
@ -328,6 +335,7 @@ impl Opt {
ignore_missing_snapshot: _,
ignore_snapshot_if_db_exists: _,
import_dump: _,
generate_master_key: _,
ignore_missing_dump: _,
ignore_dump_if_db_exists: _,
config_file_path: _,