Skip to content

Commit

Permalink
add more debug logs v2
Browse files Browse the repository at this point in the history
  • Loading branch information
cpprian committed Aug 12, 2024
1 parent cda8925 commit c186db6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 24 deletions.
43 changes: 27 additions & 16 deletions src/db/models/enrollment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ impl Token {
where
E: PgExecutor<'e>,
{
debug!("Finding user by id {}.", self.user_id);
debug!("Find user by id {}.", self.user_id);
let Some(user) = User::find_by_id(executor, self.user_id).await? else {
error!("User not found for enrollment token {}", self.id);
return Err(TokenError::UserNotFound);
Expand All @@ -240,7 +240,7 @@ impl Token {
where
E: PgExecutor<'e>,
{
debug!("Fetching admin data.");
debug!("Fetch admin data.");
if self.admin_id.is_none() {
debug!("Admin don't have id. Stop fetching data...");
return Ok(None);
Expand All @@ -257,7 +257,7 @@ impl Token {
transaction: &mut PgConnection,
user_id: i64,
) -> Result<(), TokenError> {
debug!("Deleting unused enrollment tokens for user {user_id}");
debug!("Deleting unused tokens for the user.");
let result = query!(
"DELETE FROM token \
WHERE user_id = $1 \
Expand All @@ -267,7 +267,7 @@ impl Token {
.execute(transaction)
.await?;
info!(
"Deleted {} unused enrollment tokens for user {user_id}",
"Deleted {} unused enrollment tokens for the user.",
result.rows_affected()
);

Expand Down Expand Up @@ -392,14 +392,20 @@ impl User {
mail_tx: UnboundedSender<Mail>,
) -> Result<String, TokenError> {
info!(
"Start generating a new enrollment process for the user {}, notification enabled: {send_user_notification}",
"Start generating a new enrollment process for user {}.",
self.username
);
debug!(
"Notify user by mail about the enrollment process: {}",
send_user_notification
);
debug!("Check is {} has a password.", self.username);
if self.has_password() {
debug!("User that you want to start enrollment process has already password.");
return Err(TokenError::AlreadyActive);
}

debug!("Verify that {} is an active user.", self.username);
if !self.is_active {
warn!(
"Can't create enrollment token for disabled user {}",
Expand All @@ -422,6 +428,7 @@ impl User {
token_timeout_seconds,
Some(ENROLLMENT_TOKEN_TYPE.to_string()),
);
debug!("Saving a new enrollment token...");
enrollment.save(&mut *transaction).await?;
debug!(
"Saved a new enrollment token with id {} for user {}.",
Expand All @@ -431,7 +438,7 @@ impl User {
if send_user_notification {
if let Some(email) = email {
debug!(
"Sending enrollment start mail for user {} to {email}",
"Sending an enrollment mail for user {} to {email}.",
self.username
);
let base_message_context = enrollment
Expand Down Expand Up @@ -490,14 +497,16 @@ impl User {
send_user_notification: bool,
mail_tx: UnboundedSender<Mail>,
) -> Result<String, TokenError> {
info!(
"Start a new desktop activation for user {}, notification enabled: {send_user_notification}",
self.username
info!("Start a new desktop activation for user {}", self.username);
debug!(
"Notify {} by mail about the enrollment process: {}",
self.username, send_user_notification
);

let user_id = self.id.expect("User without ID");
let admin_id = admin.id.expect("Admin user without ID");

debug!("Verify that {} is an active user.", self.username);
if !self.is_active {
warn!(
"Can't create desktop activation token for disabled user {}.",
Expand All @@ -508,31 +517,33 @@ impl User {

self.clear_unused_enrollment_tokens(&mut *transaction)
.await?;
debug!("Cleared unused tokens for {}.", self.username);

debug!(
"Create a new desktop activation token for user {}.",
self.username
);
let enrollment = Token::new(
let desktop_configuration = Token::new(
user_id,
Some(admin_id),
email.clone(),
token_timeout_seconds,
Some(ENROLLMENT_TOKEN_TYPE.to_string()),
);
enrollment.save(&mut *transaction).await?;
debug!("Saving a new desktop configuration token...");
desktop_configuration.save(&mut *transaction).await?;
debug!(
"Saved a new desktop activation token with id {} for user {}.",
enrollment.id, self.username
desktop_configuration.id, self.username
);

if send_user_notification {
if let Some(email) = email {
debug!(
"Sending desktop configuration start mail for user {} to {email}",
"Sending a desktop configuration mail for user {} to {email}",
self.username
);
let base_message_context = enrollment
let base_message_context = desktop_configuration
.get_welcome_message_context(&mut *transaction)
.await?;
let mail = Mail {
Expand All @@ -541,7 +552,7 @@ impl User {
content: templates::desktop_start_mail(
base_message_context,
&enrollment_service_url,
&enrollment.id,
&desktop_configuration.id,
)
.map_err(|err| {
debug!(
Expand Down Expand Up @@ -572,7 +583,7 @@ impl User {
self.username
);

Ok(enrollment.id)
Ok(desktop_configuration.id)
}

// Remove unused tokens when triggering user enrollment
Expand Down
10 changes: 8 additions & 2 deletions src/grpc/enrollment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub(super) struct EnrollmentServer {
ldap_feature_active: bool,
}

#[derive(Debug)]
struct InstanceInfo {
id: uuid::Uuid,
name: String,
Expand Down Expand Up @@ -122,7 +123,7 @@ impl EnrollmentServer {
) -> Result<EnrollmentStartResponse, Status> {
debug!("Starting enrollment session, request: {request:?}");
// fetch enrollment token
debug!("Fetch enrollment token by id.");
debug!("Try to find an enrollment token {}.", request.token);
let mut enrollment = Token::find_by_id(&self.pool, &request.token).await?;

if let Some(token_type) = &enrollment.token_type {
Expand All @@ -135,6 +136,7 @@ impl EnrollmentServer {
let user = enrollment.fetch_user(&self.pool).await?;
let admin = enrollment.fetch_admin(&self.pool).await?;

debug!("Check is {} an active user.", user.username);
if !user.is_active {
warn!(
"Can't start enrollment for disabled user {}.",
Expand All @@ -144,7 +146,7 @@ impl EnrollmentServer {
};

let mut transaction = self.pool.begin().await.map_err(|_| {
error!("Failed to begin transaction for enrollment.");
error!("Failed to begin a transaction for enrollment.");
Status::internal("unexpected error")
})?;

Expand All @@ -168,9 +170,12 @@ impl EnrollmentServer {
error!("Failed to get settings.");
Status::internal("unexpected error")
})?;
debug!("Settings: {settings:?}");

let vpn_setup_optional = settings.enrollment_vpn_step_optional;
debug!("Retrive informations about instance for {}.", user.username);
let instance_info = InstanceInfo::new(settings, &user.username);
debug!("Instance info {instance_info:?}");

debug!("Prepare initial user info to send for user enrollment.");
let user_info = InitialUserInfo::from_user(&self.pool, user)
Expand Down Expand Up @@ -276,6 +281,7 @@ impl EnrollmentServer {
debug!("Updating user details ends with success.");

// sync with LDAP
debug!("Add user to ldap: {}.", self.ldap_feature_active);
if self.ldap_feature_active {
debug!("Syncing with LDAP.");
let _result = ldap_add_user(&self.pool, &user, &request.password).await;
Expand Down
27 changes: 21 additions & 6 deletions src/handlers/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ pub async fn start_enrollment(
)));
};

debug!("Create a new database transaction to save a new enrollment token into the database.");
let mut transaction = appstate.pool.begin().await?;

let config = server_config();
Expand All @@ -224,12 +225,19 @@ pub async fn start_enrollment(
)
.await?;

debug!("Try to submit transaction to save the enrollment token into the databse.");
transaction.commit().await?;
debug!("Transaction submitted.");

info!(
"User {} started enrollment for user {username}",
"The enrollment process for {} has ended with success.",
session.user.username
);
debug!(
"Enrollment token {}, enrollment url {}",
enrollment_token,
config.enrollment_url.to_string()
);

Ok(ApiResponse {
json: json!({"enrollment_token": enrollment_token, "enrollment_url": config.enrollment_url.to_string()}),
Expand All @@ -244,7 +252,7 @@ pub async fn start_remote_desktop_configuration(
Json(data): Json<StartEnrollmentRequest>,
) -> ApiResult {
debug!(
"User {} starting enrollment for user {username}",
"User {} has started a new desktop activation request.",
session.user.username
);

Expand All @@ -258,14 +266,15 @@ pub async fn start_remote_desktop_configuration(
None => user.email.clone(),
};

debug!("Create a new database transaction to save a desktop configuration token into the database.");
let mut transaction = appstate.pool.begin().await?;

debug!(
"Generating a new enrollment token by {}.",
"Generating a new desktop activation token by {}.",
session.user.username
);
let config = server_config();
let enrollment_token = user
let desktop_configuration_token = user
.start_remote_desktop_configuration(
&mut transaction,
&session.user,
Expand All @@ -276,17 +285,23 @@ pub async fn start_remote_desktop_configuration(
appstate.mail_tx.clone(),
)
.await?;
debug!("Generated a new enrollment token for {}.", username);

debug!("Try to submit transaction to save the desktop configuration token into the databse.");
transaction.commit().await?;
debug!("Transaction submitted.");

info!(
"User {} added a new desktop activation.",
session.user.username
);
debug!(
"Desktop configuration token {}, desktop configuration url {}",
desktop_configuration_token,
config.enrollment_url.to_string()
);

Ok(ApiResponse {
json: json!({"enrollment_token": enrollment_token, "enrollment_url": config.enrollment_url.to_string()}),
json: json!({"enrollment_token": desktop_configuration_token, "enrollment_url": config.enrollment_url.to_string()}),
status: StatusCode::CREATED,
})
}
Expand Down

0 comments on commit c186db6

Please sign in to comment.