-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove generate mysql password step from setup script
- Loading branch information
Showing
1 changed file
with
1 addition
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") | ||
|
@@ -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]"; | ||
|
@@ -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?; | ||
|
||
|