-
Notifications
You must be signed in to change notification settings - Fork 81
[AAP-50407] Refactor JWT claims handling to use gateway endpoint #789
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
base: devel
Are you sure you want to change the base?
Changes from 1 commit
77db991
bbd74c1
cac9bf2
bd16d2f
7f5e2ee
2d379b6
68ebe9c
cd32782
a626222
0384836
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
from ansible_base.lib.logging.runtime import log_excess_runtime | ||
from ansible_base.lib.utils.auth import get_user_by_ansible_id | ||
from ansible_base.lib.utils.translations import translatableConditionally as _ | ||
from ansible_base.rbac.claims import get_user_claims, get_user_claims_hashable_form, get_claims_hash | ||
from ansible_base.resource_registry.models import Resource, ResourceType | ||
from ansible_base.resource_registry.rest_client import get_resource_server_client | ||
from ansible_base.resource_registry.signals.handlers import no_reverse_sync | ||
|
@@ -177,8 +178,31 @@ def _should_fetch_claims_from_gateway(self, user_ansible_id, current_claims_hash | |
|
||
cached_hash = self.cache.get_claims_hash(user_ansible_id) | ||
if cached_hash != current_claims_hash: | ||
logger.debug(f"Claims hash changed for user {user_ansible_id}: cached={cached_hash}, current={current_claims_hash}") | ||
return True | ||
# Recalculate hash from local database to verify the mismatch | ||
# It is possible that the cached hash is stale, but the local data is synced to the resource server. | ||
# This is an optimization to avoid fetching claims from the resource server if the local data is synced. | ||
logger.debug(f"Claims hash mismatch for user {user_ansible_id}: cached={cached_hash}, current={current_claims_hash}") | ||
logger.debug(f"Recalculating hash from local database for user {user_ansible_id}") | ||
|
||
try: | ||
# Get user claims from local database | ||
user_claims = get_user_claims(self.user) | ||
hashable_claims = get_user_claims_hashable_form(user_claims) | ||
recalculated_hash = get_claims_hash(hashable_claims) | ||
|
||
logger.debug(f"Recalculated hash for user {user_ansible_id}: {recalculated_hash}") | ||
# Compare recalculated hash with current hash from token | ||
if recalculated_hash != current_claims_hash: | ||
logger.debug(f"Claims hash still differs after recalculation for user {user_ansible_id}: local={recalculated_hash}, current={current_claims_hash}") | ||
return True | ||
else: | ||
logger.debug(f"Recalculated hash matches current hash for user {user_ansible_id}") | ||
return False | ||
|
||
except Exception as e: | ||
logger.error(f"Failed to recalculate claims hash for user {user_ansible_id}: {e}") | ||
# If recalculation fails, fall back to treating it as a hash mismatch | ||
return True | ||
|
||
# Hash matches cached value, try to get cached claims | ||
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. No, don't do that. If hashes match, do nothing. Don't do approximately nothing, do exactly nothing. Don't save the claims for later. If the hashes match that means the claims have already been saved. 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. will update to do approximately nothing 🤣 |
||
cached_claims = self.cache.get_cached_claims(user_ansible_id) | ||
|
Uh oh!
There was an error while loading. Please reload this page.