Skip to content

Commit

Permalink
Remove generate mysql password step from setup script
Browse files Browse the repository at this point in the history
  • Loading branch information
smrtrfszm committed Nov 11, 2024
1 parent 5d1c9fd commit 7e53d79
Showing 1 changed file with 1 addition and 68 deletions.
69 changes: 1 addition & 68 deletions iam-cli/commands/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ use rand::{
};
use sea_orm::Database;
use std::collections::BTreeMap;
use url::Url;

pub fn command() -> Command {
Command::new("setup")
.about("Creates mysql password and admin user")
.about("Creates admin user")
.arg(
Arg::new("database")
.long("database")
Expand All @@ -40,52 +39,11 @@ pub fn command() -> Command {
pub async fn run(matches: &ArgMatches) -> anyhow::Result<()> {
let client = Client::try_default().await?;

generate_mysql_password(client.clone()).await?;
create_admin_user(matches, client).await?;

Ok(())
}

const MYSQL_SECRET_NAME: &str = "mysql";
const MYSQL_SECRET_KEY: &str = "MYSQL_ROOT_PASSWORD";

async fn generate_mysql_password(client: Client) -> anyhow::Result<()> {
let secrets: Api<Secret> = Api::default_namespaced(client);

if secrets
.get_opt(MYSQL_SECRET_NAME)
.await
.context("Failed to query secret")?
.is_some()
{
println!("Mysql password already exists.");
return Ok(());
}

let mysql_password = Alphanumeric.sample_string(&mut OsRng, 64);

secrets
.create(
&PostParams::default(),
&Secret {
metadata: ObjectMeta {
name: Some(MYSQL_SECRET_NAME.to_owned()),
..Default::default()
},
string_data: Some({
let mut map = BTreeMap::new();
map.insert(MYSQL_SECRET_KEY.to_owned(), mysql_password);
map
}),
..Default::default()
},
)
.await
.context("Failed to create secret")?;

Ok(())
}

async fn create_admin_user(matches: &ArgMatches, client: Client) -> anyhow::Result<()> {
const SECRET_NAME: &str = "iam";
const ADMIN_EMAIL: &str = "[email protected]";
Expand All @@ -105,31 +63,6 @@ async fn create_admin_user(matches: &ArgMatches, client: Client) -> anyhow::Resu
let iam_url = matches.get_one::<String>("iam").unwrap();
let database_url = matches.get_one::<String>("database").unwrap();

let database_password = {
let secret = secrets
.get_opt(MYSQL_SECRET_NAME)
.await
.context("Failed to query secret")?
.context("No mysql secret")?
.data
.unwrap();

String::from_utf8(
secret
.get(MYSQL_SECRET_KEY)
.context("No mysql password")?
.0
.clone(),
)
.context("Not utf8 from kube rs")?
};

let database_url = {
let mut url = Url::parse(database_url).context("invalid url")?;
url.set_password(Some(&database_password)).unwrap();
url
};

let iam = Iam::new(iam_url);
let db = Database::connect(database_url.as_str()).await?;

Expand Down

0 comments on commit 7e53d79

Please sign in to comment.