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

feat(cli): Add new command for registering relay with sentry #4424

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions relay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ relay-log = { workspace = true, features = ["init"] }
relay-server = { workspace = true }
relay-statsd = { workspace = true }
relay-kafka = { workspace = true, optional = true }
reqwest = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
uuid = { workspace = true }

[target.'cfg(target_os = "linux")'.dependencies]
Expand Down
62 changes: 61 additions & 1 deletion relay/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use dialoguer::{Confirm, Select};
use relay_config::{
Config, ConfigError, ConfigErrorKind, Credentials, MinimalConfig, OverridableConfig, RelayMode,
};
use reqwest::blocking::Client;
use serde::Serialize;
use uuid::Uuid;

use crate::cliapp::make_app;
Expand All @@ -29,6 +31,62 @@ fn load_config(path: impl AsRef<Path>, require: bool) -> Result<Config> {
}
}

/// Represents the registration request payload
#[derive(Debug, Serialize)]
struct RegistrationRequest {
public_key: String,
name: String,
description: Option<String>,
}

/// Handle the register command
fn register(config: &Config, matches: &ArgMatches) -> Result<()> {
let Some(credentials) = config.credentials() else {
bail!("No credentials found. Please run 'relay credentials generate' first");
};

let auth_token = matches
.get_one::<String>("auth_token")
.expect("auth_token is required");
let display_name = matches
.get_one::<String>("display_name")
.expect("display_name is required");
let description = matches.get_one::<String>("description").cloned();

let registration = RegistrationRequest {
public_key: credentials.public_key.to_string(),
name: display_name.clone(),
description,
};

let registration_url = "https://sentry.io/api/0/internal/register-trusted-relay/";

println!("🔄 Registering Relay with name '{}'...", display_name);

// Create HTTP client
let client = Client::new();

// Send registration request
let response = client
.post(registration_url)
.json(&registration)
.header("Authorization", format!("Bearer {}", auth_token))
.send()?;

if response.status().is_success() {
let response_data = response.json::<serde_json::Value>()?;
println!("\n✅ Registration successful!\n");
println!("Registration details:");
println!(" • Name: {}", response_data["name"]);
println!(" • Description: {}", response_data["description"]);
println!(" • Public Key: {}", response_data["public_key"]);
Ok(())
} else {
let error = response.text()?;
bail!("❌ Registration failed: {}", error)
}
}

/// Runs the command line application.
pub fn execute() -> Result<()> {
let app = make_app();
Expand All @@ -54,7 +112,9 @@ pub fn execute() -> Result<()> {

relay_log::init(config.logging(), config.sentry());

if let Some(matches) = matches.subcommand_matches("config") {
if let Some(matches) = matches.subcommand_matches("register") {
register(&config, matches)
} else if let Some(matches) = matches.subcommand_matches("config") {
manage_config(&config, matches)
} else if let Some(matches) = matches.subcommand_matches("credentials") {
manage_credentials(config, matches)
Expand Down
28 changes: 28 additions & 0 deletions relay/src/cliapp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,4 +338,32 @@ pub fn make_app() -> Command {
)
)
)
.subcommand(
Command::new("register")
.about("Register this relay instance with Sentry")
.after_help(
"This command registers the relay instance with Sentry using an authentication token. \
The auth token needs to be generated from the Sentry side first."
)
.arg(
Arg::new("auth_token")
.long("token")
.short('t')
.required(true)
.help("The authentication token generated from Sentry"),
)
.arg(
Arg::new("display_name")
.long("name")
.short('n')
.required(true)
.help("Display name for this relay instance"),
)
.arg(
Arg::new("description")
.long("description")
.short('d')
.help("Optional description of this relay instance"),
),
)
}
Loading