-
-
Notifications
You must be signed in to change notification settings - Fork 88
block adding users over license limits in openid and ldap #2104
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
Open
filipslezaklab
wants to merge
7
commits into
dev
Choose a base branch
from
block-sync
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
55f8656
block adding users over license limits in openid and ldap
filipslezaklab b5e46b9
block user import via ldap login on limit
filipslezaklab f5bd29a
send simple email's notifications on user import block
filipslezaklab eeb7bb8
Merge branch 'dev' into block-sync
filipslezaklab ac9c46c
fix migration
filipslezaklab 9283355
clippy fix
filipslezaklab 9ee4c33
remove sending emails for now
filipslezaklab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,8 +38,10 @@ use crate::{ | |
| appstate::AppState, | ||
| enterprise::{ | ||
| db::models::openid_provider::OpenIdProvider, | ||
| directory_sync::sync_user_groups_if_configured, ldap::utils::ldap_update_user_state, | ||
| limits::update_counts, | ||
| directory_sync::sync_user_groups_if_configured, | ||
| ldap::utils::ldap_update_user_state, | ||
| license::get_cached_license, | ||
| limits::{get_counts, update_counts}, | ||
| }, | ||
| error::WebError, | ||
| handlers::{ | ||
|
|
@@ -94,6 +96,16 @@ pub fn prune_username(username: &str, handling: OpenIdUsernameHandling) -> Strin | |
| result | ||
| } | ||
|
|
||
| fn reached_user_license_limit() -> Option<(u32, u32)> { | ||
| let user_count = get_counts().user(); | ||
| let user_limit = get_cached_license() | ||
| .as_ref() | ||
| .and_then(|license| license.limits.as_ref().map(|limits| limits.users)); | ||
| user_limit | ||
| .filter(|limit| user_count >= *limit) | ||
| .map(|limit| (user_count, limit)) | ||
| } | ||
|
|
||
| /// Create HTTP client and prevent following redirects | ||
| fn get_async_http_client() -> Result<reqwest::Client, WebError> { | ||
| reqwest::Client::builder() | ||
|
|
@@ -365,6 +377,19 @@ pub async fn user_from_claims( | |
| ))); | ||
| } | ||
|
|
||
| if let Some((user_count, limit)) = reached_user_license_limit() { | ||
| error!( | ||
| "Skipping OpenID account creation for user {} (email: {}) because \ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Embed variables in formatting string. |
||
| license user limit has been reached ({}/{})", | ||
| username, | ||
| email.as_str(), | ||
| user_count, | ||
| limit | ||
| ); | ||
| // TODO: send emails | ||
| return Err(WebError::Forbidden("License limit reached.".into())); | ||
| } | ||
|
|
||
| // Extract all necessary information from the token or call the userinfo endpoint. | ||
| let given_name = token_claims | ||
| .given_name() | ||
|
|
@@ -644,6 +669,14 @@ pub(crate) async fn auth_callback( | |
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use crate::{ | ||
| enterprise::{ | ||
| license::{License, LicenseTier, set_cached_license}, | ||
| limits::{Counts, set_counts}, | ||
| }, | ||
| grpc::proto::enterprise::license::LicenseLimits, | ||
| }; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[test] | ||
|
|
@@ -762,4 +795,62 @@ mod test { | |
| let extracted = extract_state_data(&encoded); | ||
| assert_eq!(extracted, Some("data.with.dots".to_string())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_reached_user_license_limit_reached() { | ||
| set_counts(Counts::new(2, 0, 0, 0)); | ||
| let license = License::new( | ||
| "test".to_string(), | ||
| false, | ||
| None, | ||
| Some(LicenseLimits { | ||
| users: 2, | ||
| devices: 100, | ||
| locations: 100, | ||
| network_devices: Some(100), | ||
| }), | ||
| None, | ||
| LicenseTier::Business, | ||
| ); | ||
| set_cached_license(Some(license)); | ||
|
|
||
| assert_eq!(reached_user_license_limit(), Some((2, 2))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_reached_user_license_limit_not_reached() { | ||
| set_counts(Counts::new(1, 0, 0, 0)); | ||
| let license = License::new( | ||
| "test".to_string(), | ||
| false, | ||
| None, | ||
| Some(LicenseLimits { | ||
| users: 2, | ||
| devices: 100, | ||
| locations: 100, | ||
| network_devices: Some(100), | ||
| }), | ||
| None, | ||
| LicenseTier::Business, | ||
| ); | ||
| set_cached_license(Some(license)); | ||
|
|
||
| assert_eq!(reached_user_license_limit(), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_reached_user_license_limit_unlimited() { | ||
| set_counts(Counts::new(100, 0, 0, 0)); | ||
| let license = License::new( | ||
| "test".to_string(), | ||
| false, | ||
| None, | ||
| None, | ||
| None, | ||
| LicenseTier::Business, | ||
| ); | ||
| set_cached_license(Some(license)); | ||
|
|
||
| assert_eq!(reached_user_license_limit(), None); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -66,9 +66,13 @@ use sqlx::{PgConnection, PgPool}; | |||||
|
|
||||||
| use super::{LDAPConfig, error::LdapError}; | ||||||
| use crate::{ | ||||||
| enterprise::ldap::model::{ | ||||||
| get_users_without_ldap_path, ldap_sync_allowed_for_user, update_from_ldap_user, | ||||||
| user_from_searchentry, | ||||||
| enterprise::{ | ||||||
| ldap::model::{ | ||||||
| get_users_without_ldap_path, ldap_sync_allowed_for_user, update_from_ldap_user, | ||||||
| user_from_searchentry, | ||||||
| }, | ||||||
| license::get_cached_license, | ||||||
| limits::{get_counts, update_counts}, | ||||||
| }, | ||||||
| hashset, | ||||||
| }; | ||||||
|
|
@@ -806,6 +810,13 @@ impl super::LDAPConnection { | |||||
| ) -> Result<(), LdapError> { | ||||||
| let mut transaction = pool.begin().await?; | ||||||
| let mut admin_count = User::find_admins(&mut *transaction).await?.len(); | ||||||
| let mut user_count = get_counts().user(); | ||||||
|
|
||||||
| let user_limit = get_cached_license() | ||||||
| .as_ref() | ||||||
| .and_then(|license| license.limits.as_ref().map(|limits| limits.users)); | ||||||
| let mut blocked_import_notification_sent = false; | ||||||
|
|
||||||
| for user in changes.delete_defguard { | ||||||
| if user.is_admin(&mut *transaction).await? { | ||||||
| if admin_count == 1 { | ||||||
|
|
@@ -849,12 +860,27 @@ impl super::LDAPConnection { | |||||
| "LDAP user {} does not exist in Defguard yet, adding...", | ||||||
| user.username | ||||||
| ); | ||||||
| if let Some(limit) = user_limit.filter(|limit| user_count >= *limit) { | ||||||
| error!( | ||||||
| "Skipping LDAP import of user {} (email: {}) because license user limit \ | ||||||
| has been reached ({}/{})", | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| user.username, user.email, user_count, limit | ||||||
| ); | ||||||
| if !blocked_import_notification_sent { | ||||||
| blocked_import_notification_sent = true; | ||||||
| // TODO: send emails | ||||||
| } | ||||||
| continue; | ||||||
| } | ||||||
| user.save(&mut *transaction).await?; | ||||||
| user_count += 1; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| transaction.commit().await?; | ||||||
|
|
||||||
| update_counts(pool).await?; | ||||||
|
|
||||||
| for user in changes.delete_ldap { | ||||||
| debug!("Deleting user {} from LDAP", user.username); | ||||||
| self.delete_user(&user).await?; | ||||||
|
|
||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably clone() is not needed.