From 186218b8b9fb829ad17eff06701fb4b9bada87f7 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Wed, 16 Nov 2022 07:19:42 +0000 Subject: [PATCH] more cli --- .../web3_proxy_cli/change_user_tier_by_key.rs | 59 +++++++++++++++++++ .../bin/web3_proxy_cli/drop_migration_lock.rs | 16 +++++ .../src/bin/web3_proxy_cli/list_user_tier.rs | 0 web3_proxy/src/bin/web3_proxy_cli/main.rs | 18 ++++-- 4 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 web3_proxy/src/bin/web3_proxy_cli/change_user_tier_by_key.rs create mode 100644 web3_proxy/src/bin/web3_proxy_cli/drop_migration_lock.rs create mode 100644 web3_proxy/src/bin/web3_proxy_cli/list_user_tier.rs diff --git a/web3_proxy/src/bin/web3_proxy_cli/change_user_tier_by_key.rs b/web3_proxy/src/bin/web3_proxy_cli/change_user_tier_by_key.rs new file mode 100644 index 00000000..7ae3c2fa --- /dev/null +++ b/web3_proxy/src/bin/web3_proxy_cli/change_user_tier_by_key.rs @@ -0,0 +1,59 @@ +use anyhow::Context; +use argh::FromArgs; +use entities::{rpc_key, user, user_tier}; +use log::{debug, info}; +use migration::sea_orm::{ + self, ColumnTrait, DatabaseConnection, EntityTrait, IntoActiveModel, QueryFilter, +}; +use uuid::Uuid; +use web3_proxy::frontend::authorization::RpcSecretKey; + +/// change a user's tier. +/// TODO: we probably shouldn't be exposing the RpcSecretKeys at all. Better to take a user/key id +#[derive(FromArgs, PartialEq, Eq, Debug)] +#[argh(subcommand, name = "change_user_tier_by_key")] +pub struct ChangeUserTierByKeyCommand { + #[argh(option)] + /// the RPC key owned by the user you want to change. + rpc_secret_key: RpcSecretKey, + + /// the title of the desired user tier. + #[argh(option)] + user_tier_title: String, +} + +impl ChangeUserTierByKeyCommand { + pub async fn main(self, db_conn: &DatabaseConnection) -> anyhow::Result<()> { + let rpc_secret_key: Uuid = self.rpc_secret_key.into(); + + let user_tier = user_tier::Entity::find() + .filter(user_tier::Column::Title.eq(self.user_tier_title)) + .one(db_conn) + .await? + .context("No user tier found with that name")?; + + debug!("user_tier: {:#?}", user_tier); + + // use the rpc secret key to get the user + let user = user::Entity::find() + .inner_join(rpc_key::Entity) + .filter(rpc_key::Column::SecretKey.eq(rpc_secret_key)) + .one(db_conn) + .await? + .context("No user found with that key")?; + + debug!("user: {:#?}", user); + + if user.user_tier_id == user_tier.id { + info!("user already has that tier"); + } else { + let mut user = user.into_active_model(); + + user.user_tier_id = sea_orm::Set(user_tier.id); + + info!("user's tier changed"); + } + + Ok(()) + } +} diff --git a/web3_proxy/src/bin/web3_proxy_cli/drop_migration_lock.rs b/web3_proxy/src/bin/web3_proxy_cli/drop_migration_lock.rs new file mode 100644 index 00000000..633a0610 --- /dev/null +++ b/web3_proxy/src/bin/web3_proxy_cli/drop_migration_lock.rs @@ -0,0 +1,16 @@ +use argh::FromArgs; +use migration::sea_orm::DatabaseConnection; +use web3_proxy::app::drop_migration_lock; + +#[derive(FromArgs, PartialEq, Debug, Eq)] +/// In case of emergency, break glass. +#[argh(subcommand, name = "drop_migration_lock")] +pub struct DropMigrationLockSubCommand {} + +impl DropMigrationLockSubCommand { + pub async fn main(&self, db_conn: &DatabaseConnection) -> anyhow::Result<()> { + drop_migration_lock(db_conn).await?; + + Ok(()) + } +} diff --git a/web3_proxy/src/bin/web3_proxy_cli/list_user_tier.rs b/web3_proxy/src/bin/web3_proxy_cli/list_user_tier.rs new file mode 100644 index 00000000..e69de29b diff --git a/web3_proxy/src/bin/web3_proxy_cli/main.rs b/web3_proxy/src/bin/web3_proxy_cli/main.rs index 08b68cb1..f656b4f4 100644 --- a/web3_proxy/src/bin/web3_proxy_cli/main.rs +++ b/web3_proxy/src/bin/web3_proxy_cli/main.rs @@ -1,10 +1,10 @@ +mod change_user_tier_by_key; mod check_config; -mod clear_migration_lock; mod create_user; - -use std::fs; +mod drop_migration_lock; use argh::FromArgs; +use std::fs; use web3_proxy::{ app::{get_db, get_migrated_db}, config::TopConfig, @@ -32,9 +32,10 @@ pub struct CliConfig { #[derive(FromArgs, PartialEq, Debug)] #[argh(subcommand)] enum SubCommand { - CreateUser(create_user::CreateUserSubCommand), + ChangeUserTierByKey(change_user_tier_by_key::ChangeUserTierByKeyCommand), CheckConfig(check_config::CheckConfigSubCommand), - DropMigrationLock(clear_migration_lock::DropMigrationLockSubCommand), + CreateUser(create_user::CreateUserSubCommand), + DropMigrationLock(drop_migration_lock::DropMigrationLockSubCommand), // TODO: sub command to downgrade migrations? // TODO: sub command to add new api keys to an existing user? // TODO: sub command to change a user's tier @@ -70,12 +71,17 @@ async fn main() -> anyhow::Result<()> { }; match cli_config.sub_command { + SubCommand::ChangeUserTierByKey(x) => { + let db_conn = get_db(cli_config.db_url, 1, 1).await?; + + x.main(&db_conn).await + } + SubCommand::CheckConfig(x) => x.main().await, SubCommand::CreateUser(x) => { let db_conn = get_migrated_db(cli_config.db_url, 1, 1).await?; x.main(&db_conn).await } - SubCommand::CheckConfig(x) => x.main().await, SubCommand::DropMigrationLock(x) => { let db_conn = get_db(cli_config.db_url, 1, 1).await?;