use std::io::IsTerminal;
use std::sync::Arc;
use log::info;
use crate::bootstrap::error::BootstrapError;
use crate::infrastructure::auth::get_garrison_dao;
use crate::infrastructure::database::entities::api_key::{
ActiveModel as ApiKeyActiveModel, Entity as ApiKeyEntity,
};
use crate::infrastructure::database::entities::team::{
ActiveModel as TeamActiveModel, Entity as TeamEntity,
};
use dbnexus::sea_orm::{ActiveValue, EntityTrait};
pub(super) fn bootstrap_admin_key_config() -> Option<String> {
match std::env::var("CRAWLRS__BOOTSTRAP_ADMIN_API_KEY") {
Ok(v) if !v.is_empty() => Some(v),
_ => None,
}
}
pub(super) fn resolve_bootstrap_team_id(cfg_val: &str) -> Option<crate::domain::id::Id> {
if cfg_val.len() == 36 {
match cfg_val.parse::<crate::domain::id::Id>() {
Ok(id) => Some(id),
Err(e) => {
log::warn!(
"CRAWLRS__BOOTSTRAP_ADMIN_API_KEY 长度 36 但 UUID 解析失败,回退自动生成 team: {e}"
);
None
}
}
} else {
if !cfg_val.is_empty() {
log::warn!(
"CRAWLRS__BOOTSTRAP_ADMIN_API_KEY 非空但不是 36 位 UUID(长度 {}),回退自动生成 team",
cfg_val.len()
);
}
None
}
}
pub(super) async fn ensure_bootstrap_team(
conn: &dbnexus::sea_orm::DatabaseConnection,
team_id: crate::domain::id::Id,
now: chrono::DateTime<chrono::FixedOffset>,
) -> Result<(), BootstrapError> {
const DEFAULT_TEAM_NAME: &str = "bootstrap-admin-team";
if TeamEntity::find_by_id(team_id)
.one(conn)
.await
.map_err(bootstrap_db_err("bootstrap_admin_key: find team"))?
.is_none()
{
let team_active = TeamActiveModel {
id: ActiveValue::Set(team_id.into()),
name: ActiveValue::Set(DEFAULT_TEAM_NAME.to_string()),
allowed_countries: ActiveValue::Set(None),
blocked_countries: ActiveValue::Set(None),
ip_whitelist: ActiveValue::Set(None),
domain_blacklist: ActiveValue::Set(None),
enable_geo_restrictions: ActiveValue::Set(false),
created_at: ActiveValue::Set(now),
updated_at: ActiveValue::Set(now),
};
TeamEntity::insert(team_active)
.exec(conn)
.await
.map_err(bootstrap_db_err("bootstrap_admin_key: insert team"))?;
}
Ok(())
}
pub(super) async fn issue_bootstrap_admin_key(
api_key_id: crate::domain::id::Id,
) -> Result<BootstrapApiKey, BootstrapError> {
use garrison::protocol::apikey::ApiKeyHandler;
const GARRISON_NS: &str = "crawlrs";
const PERM_ADMIN: &str = "crawlrs:admin";
const TTL_SECS: i64 = 30 * 24 * 60 * 60;
let dao_for_gen = get_garrison_dao().ok_or_else(|| {
BootstrapError::GarrisonManager(
"bootstrap_admin_key: get_garrison_dao returned None".to_string(),
)
})?;
let handler = ApiKeyHandler::new(Arc::clone(&dao_for_gen));
let plaintext_key = handler
.generate_with_namespace(
api_key_id.to_string(),
GARRISON_NS,
vec![PERM_ADMIN.to_string()],
TTL_SECS,
)
.await
.map_err(|e| {
BootstrapError::GarrisonManager(format!("bootstrap_admin_key: garrison generate: {e}"))
})?;
let key_id = plaintext_key
.split_once('.')
.map(|(k, _)| k.to_string())
.ok_or_else(|| {
BootstrapError::GarrisonManager(
"bootstrap_admin_key: garrison returned malformed key".to_string(),
)
})?;
Ok(BootstrapApiKey {
plaintext: plaintext_key,
key_id,
})
}
pub(super) async fn persist_bootstrap_api_key(
conn: &dbnexus::sea_orm::DatabaseConnection,
api_key_id: crate::domain::id::Id,
team_id: crate::domain::id::Id,
garrison_key_id: &str,
now: chrono::DateTime<chrono::FixedOffset>,
) -> Result<(), BootstrapError> {
let api_key_active = ApiKeyActiveModel {
id: ActiveValue::Set(api_key_id.into()),
team_id: ActiveValue::Set(team_id.into()),
key: ActiveValue::Set(garrison_key_id.to_string()),
created_at: ActiveValue::Set(now),
updated_at: ActiveValue::Set(None),
};
ApiKeyEntity::insert(api_key_active)
.exec(conn)
.await
.map_err(bootstrap_db_err("bootstrap_admin_key: insert api_key"))?;
Ok(())
}
pub(super) fn announce_bootstrap_admin_key(
plaintext_key: &str,
team_id: crate::domain::id::Id,
api_key_id: crate::domain::id::Id,
) {
info!(
"Bootstrap admin API key injected: team_id={}, api_key_id={} (TTY: use \
`CRAWLRS__BOOTSTRAP_ADMIN_API_KEY={}` to reuse team ID)",
team_id, api_key_id, team_id
);
if std::io::stdout().is_terminal() {
println!(
"\n\
====================================================\n\
BOOTSTRAP ADMIN API KEY (save this now!):\n\
{}\n\
TEAM_ID: {}\n\
API_KEY_ID: {}\n\
Valid for: 30 days\n\
====================================================\n",
plaintext_key, team_id, api_key_id
);
} else {
println!(
"\n\
====================================================\n\
BOOTSTRAP ADMIN API KEY (masked; rerun in a terminal to display):\n\
{}\n\
TEAM_ID: {}\n\
API_KEY_ID: {}\n\
Valid for: 30 days\n\
====================================================\n",
crate::common::mask_secret(plaintext_key),
team_id,
api_key_id
);
}
}
pub(super) struct BootstrapApiKey {
pub(super) plaintext: String,
pub(super) key_id: String,
}
fn bootstrap_db_err(ctx: &'static str) -> impl Fn(dbnexus::sea_orm::DbErr) -> BootstrapError {
move |e| BootstrapError::GarrisonManager(format!("{}: db: {}", ctx, e))
}