Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix setup program #76

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading