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

Only remove cached user if it doesn't exist #129

Merged
merged 1 commit into from
May 14, 2024
Merged
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
105 changes: 55 additions & 50 deletions src/common/src/idprovider/himmelblau.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,53 +486,58 @@ impl IdProvider for HimmelblauProvider {
Some(token) => token.spn.clone(),
None => id.to_string().clone(),
};
let prt = match self.refresh_cache.refresh_token(&account_id).await {
Ok(prt) => prt,
Err(_) => match old_token {
// If we have an existing token, just keep it
Some(token) => return Ok(token.clone()),
// Otherwise, see if we should fake it
None => {
// Check if the user exists
let exists = self
.client
.write()
.await
.check_user_exists(&account_id)
.await
.map_err(|e| {
error!("Failed checking if the user exists: {:?}", e);
IdpError::BadRequest
})?;
if exists {
// Generate a UserToken, with invalid uuid and gid. We can
// only fetch these from an authenticated token. We have to
// provide something, or SSH will fail.
let groups = vec![GroupToken {
name: account_id.clone(),
spn: account_id.clone(),
uuid: Uuid::max(),
gidnumber: i32::MAX as u32,
}];
let config = self.config.read().await;
return Ok(UserToken {
name: account_id.clone(),
spn: account_id.clone(),
uuid: Uuid::max(),
gidnumber: i32::MAX as u32,
displayname: "".to_string(),
shell: Some(config.get_shell(Some(&self.domain))),
groups,
sshkeys: vec![],
valid: true,
});
} else {
// This is the one time we really should return
// IdpError::NotFound, because this user doesn't exist.
return Err(IdpError::NotFound);
macro_rules! fake_user {
() => {
match old_token {
// If we have an existing token, just keep it
Some(token) => return Ok(token.clone()),
// Otherwise, see if we should fake it
None => {
// Check if the user exists
let exists = self
.client
.write()
.await
.check_user_exists(&account_id)
.await
.map_err(|e| {
error!("Failed checking if the user exists: {:?}", e);
IdpError::BadRequest
})?;
if exists {
// Generate a UserToken, with invalid uuid and gid. We can
// only fetch these from an authenticated token. We have to
// provide something, or SSH will fail.
let groups = vec![GroupToken {
name: account_id.clone(),
spn: account_id.clone(),
uuid: Uuid::max(),
gidnumber: i32::MAX as u32,
}];
let config = self.config.read().await;
return Ok(UserToken {
name: account_id.clone(),
spn: account_id.clone(),
uuid: Uuid::max(),
gidnumber: i32::MAX as u32,
displayname: "".to_string(),
shell: Some(config.get_shell(Some(&self.domain))),
groups,
sshkeys: vec![],
valid: true,
});
} else {
// This is the one time we really should return
// IdpError::NotFound, because this user doesn't exist.
return Err(IdpError::NotFound);
}
}
}
},
};
}
let prt = match self.refresh_cache.refresh_token(&account_id).await {
Ok(prt) => prt,
Err(_) => fake_user!(),
};
let scopes = vec!["GroupMember.Read.All"];
let token = match self
Expand Down Expand Up @@ -563,20 +568,20 @@ impl IdProvider for HimmelblauProvider {
error!("{:?}", e);
// Never return IdpError::NotFound. This deletes
// the existing user from the cache.
return Err(IdpError::BadRequest);
fake_user!()
}
}
} else {
// Never return IdpError::NotFound. This deletes the
// existing user from the cache.
return Err(IdpError::BadRequest);
fake_user!()
}
}
Err(e) => {
error!("{:?}", e);
// Never return IdpError::NotFound. This deletes the existing
// user from the cache.
return Err(IdpError::BadRequest);
fake_user!()
}
};
match self.token_validate(&account_id, &token).await {
Expand All @@ -591,8 +596,8 @@ impl IdProvider for HimmelblauProvider {
}
// Never return IdpError::NotFound. This deletes the existing
// user from the cache.
Ok(AuthResult::Denied) | Ok(AuthResult::Next(_)) => Err(IdpError::BadRequest),
Err(_) => Err(IdpError::BadRequest),
Ok(AuthResult::Denied) | Ok(AuthResult::Next(_)) => fake_user!(),
Err(_) => fake_user!(),
}
}

Expand Down