This commit is contained in:
Bryan Stitt 2022-11-16 07:19:42 +00:00
parent 6a5b0e8653
commit 186218b8b9
4 changed files with 87 additions and 6 deletions

View File

@ -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(())
}
}

View File

@ -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(())
}
}

View File

@ -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?;