aboutsummaryrefslogtreecommitdiff
path: root/src/garage/cli/init.rs
blob: 20813f1c320039b0de16d9a2c4bdc82b95c5e282 (plain) (blame)
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
use std::path::PathBuf;

use garage_util::error::*;

pub const READ_KEY_ERROR: &str = "Unable to read node key. It will be generated by your garage node the first time is it launched. Ensure that your garage node is currently running. (The node key is supposed to be stored in your metadata directory.)";

pub fn node_id_command(config_file: PathBuf, quiet: bool) -> Result<(), Error> {
	let config = garage_util::config::read_config(config_file.clone()).err_context(format!(
		"Unable to read configuration file {}",
		config_file.to_string_lossy(),
	))?;

	let node_id =
		garage_rpc::system::read_node_id(&config.metadata_dir).err_context(READ_KEY_ERROR)?;

	let idstr = if let Some(addr) = config.rpc_public_addr {
		let idstr = format!("{}@{}", hex::encode(node_id), addr);
		println!("{}", idstr);
		idstr
	} else {
		let idstr = hex::encode(node_id);
		println!("{}", idstr);

		if !quiet {
			warn!("WARNING: I don't know the public address to reach this node.");
			warn!("In all of the instructions below, replace 127.0.0.1:{} by the appropriate address and port.", config.rpc_bind_addr.port());
		}

		format!("{}@127.0.0.1:{}", idstr, config.rpc_bind_addr.port())
	};

	if !quiet {
		eprintln!();
		eprintln!(
			"To instruct a node to connect to this node, run the following command on that node:"
		);
		eprintln!("    garage [-c <config file path>] node connect {}", idstr);
		eprintln!();
		eprintln!("Or instruct them to connect from here by running:");
		eprintln!(
			"    garage -c {} -h <remote node> node connect {}",
			config_file.to_string_lossy(),
			idstr
		);
		eprintln!(
			"where <remote_node> is their own node identifier in the format: <pubkey>@<ip>:<port>"
		);
		eprintln!();
		eprintln!("This node identifier can also be added as a bootstrap node in other node's garage.toml files:");
		eprintln!("    bootstrap_peers = [");
		eprintln!("        \"{}\",", idstr);
		eprintln!("        ...");
		eprintln!("    ]");
		eprintln!();
		eprintln!(
			r#"Security notice: Garage's intra-cluster communications are secured primarily by the shared
secret value rpc_secret.  However, an attacker that knows rpc_secret (for example if it
leaks) cannot connect if they do not know any of the identifiers of the nodes in the
cluster. It is thus a good security measure to try to keep them secret if possible.
				  "#
		);
	}

	Ok(())
}