Skip to content

Commit

Permalink
Fix setup program (#76)
Browse files Browse the repository at this point in the history
Co-authored-by: TwoDCube <[email protected]>
  • Loading branch information
smrtrfszm and TwoDCube authored Jul 8, 2024
1 parent 5a5d6a4 commit a6c7e61
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
15 changes: 8 additions & 7 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions iam-setup/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ kube = "0.92.1"
libiam = { version = "0.1.0", path = "../libiam" }
rand = "0.8.5"
tokio = { version = "1.38.0", features = ["rt", "macros"] }
url = "2.5.2"
40 changes: 33 additions & 7 deletions iam-setup/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ use rand::{
distributions::{Alphanumeric, DistString},
rngs::OsRng,
};
use url::Url;

async fn generate_mysql_password(client: Client) -> anyhow::Result<()> {
const SECRET_NAME: &str = "mysql";
const SECRET_KEY: &str = "MYSQL_ROOT_PASSWORD";
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(SECRET_NAME)
.get_opt(MYSQL_SECRET_NAME)
.await
.context("Failed to query secret")?
.is_some()
Expand All @@ -38,12 +39,12 @@ async fn generate_mysql_password(client: Client) -> anyhow::Result<()> {
&PostParams::default(),
&Secret {
metadata: ObjectMeta {
name: Some(SECRET_NAME.to_owned()),
name: Some(MYSQL_SECRET_NAME.to_owned()),
..Default::default()
},
string_data: Some({
let mut map = BTreeMap::new();
map.insert(SECRET_KEY.to_owned(), mysql_password);
map.insert(MYSQL_SECRET_KEY.to_owned(), mysql_password);
map
}),
..Default::default()
Expand Down Expand Up @@ -74,8 +75,33 @@ async fn create_admin_user(client: Client) -> anyhow::Result<()> {
let iam_url = env::var("IAM_URL").context("IAM_URL is not set")?;
let database_url = env::var("DATABASE_URL").context("DATABASE_URL is not set")?;

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 = testing::Database::connect(&database_url).await;
let db = testing::Database::connect(database_url.as_str()).await;

let admin_password = Alphanumeric.sample_string(&mut OsRng, 64);
let user = User::register(&iam, "admin", ADMIN_EMAIL, &admin_password).await?;
Expand Down

0 comments on commit a6c7e61

Please sign in to comment.