Skip to content

check every keycloak login code instead of reusing the last token - #14367

Merged
nvazquez merged 1 commit into
apache:mainfrom
Damans227:nl/issue-13861
Dec 1, 2026
Merged

nvazquez merged 1 commit into
apache:mainfrom
Damans227:nl/issue-13861

Conversation

@Damans227

@Damans227 Damans227 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #13861

The keycloak login kept the token from the first code it checked and reused it for every later code until the management server restarted, so new codes were never checked with keycloak. This checks each code with keycloak on its own, and keeps the verified email for one minute only for that code, so the second check in the same login does not call keycloak again.

Test:

Ran keycloak 26.0 in a container next to the management server, with a realm, a cloudstack client and two keycloak users, alice and bob. Registered it as the keycloak provider with oauth2.enabled=true. Created a CloudStack user alice@example.com, with no user for bob. Each login gets a real code from keycloak's login page, then calls verifyOAuthCodeAndGetUser and oauthlogin. Without restarting:

  1. bob logs in. fails, no CloudStack user.
  2. alice logs in with a fresh code.

Before: alice's fresh code is never checked with keycloak. the server hands back the earlier email and her login fails until a restart.

== step 2: alice logs in through keycloak with a fresh code, no restart
keycloak gave alice the code 349fb200-8604...
verifyOAuthCodeAndGetUser: {"verifyoauthcodeandgetuserresponse":{"oauthemail":{"email":"bob@example.com","iscallerchilddomain":false}}}
ERROR [c.c.a.ApiServlet] (logid:e7a634ee) unknown exception writing api response com.cloud.utils.exception.CloudRuntimeException: Unable to verify the email address with the provided secret

After: alice's code is checked with keycloak on its own and she logs in.

== step 2: alice logs in through keycloak with a fresh code, no restart
keycloak gave alice the code f8770259-7747...
verifyOAuthCodeAndGetUser: {"verifyoauthcodeandgetuserresponse":{"oauthemail":{"email":"alice@example.com","iscallerchilddomain":false}}}
oauthlogin: {'username': 'alice@example.com', 'account': 'admin'}

@codecov

codecov Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.58333% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 19.91%. Comparing base (17294ca) to head (c0f9bec).
⚠️ Report is 136 commits behind head on main.

Files with missing lines Patch % Lines
...dstack/oauth2/keycloak/KeycloakOAuth2Provider.java 89.58% 3 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##               main   #14367   +/-   ##
=========================================
  Coverage     19.91%   19.91%           
- Complexity    20198    20202    +4     
=========================================
  Files          6373     6373           
  Lines        577234   577249   +15     
  Branches      70696    70698    +2     
=========================================
+ Hits         114945   114964   +19     
+ Misses       449724   449721    -3     
+ Partials      12565    12564    -1     
Flag Coverage Δ
uitests 3.70% <ø> (ø)
unittests 21.18% <89.58%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Damans227

Copy link
Copy Markdown
Contributor Author

@blueorangutan package

@blueorangutan

Copy link
Copy Markdown

@Damans227 a [SL] Jenkins job has been kicked to build packages. It will be bundled with no SystemVM templates. I'll keep you posted as I make progress.

@blueorangutan

Copy link
Copy Markdown

Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ el10 ✔️ debian ✔️ suse15. SL-JID 19480

Comment on lines +66 to +71
private final Cache<String, String> validatedEmailCache =
Caffeine.newBuilder()
.expireAfterWrite(60, TimeUnit.SECONDS)
.maximumSize(1024)
.build();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a question about multi-management-server setups. The UI login is two requests: verifyOAuthCodeAndGetUser exchanges the code and returns the email, then oauthlogin calls verifyUser with the same code. validatedEmailCache is an in-memory Caffeine cache, so it is local to one management server. Both requests have to land on the same MS for verifyUser to hit the cache.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, both calls need to land on the same server. thats the same as the google login today, and the old code kept the token on one server too, so this doesnt make it worse.

@nvazquez

Copy link
Copy Markdown
Contributor

Hi @Damans227 is this PR still in progress or ready for review?

@Damans227

Copy link
Copy Markdown
Contributor Author

Hi @Damans227 is this PR still in progress or ready for review?

@nvazquez ready for review. tested on a lab with a real keycloak, before and after are in the description.

@Damans227
Damans227 marked this pull request as ready for review October 30, 2026 12:21
@nvazquez nvazquez added this to the 24.0 milestone Oct 30, 2026
@nvazquez

Copy link
Copy Markdown
Contributor

@blueorangutan package

@blueorangutan

Copy link
Copy Markdown

@nvazquez a [SL] Jenkins job has been kicked to build packages. It will be bundled with no SystemVM templates. I'll keep you posted as I make progress.

@blueorangutan

Copy link
Copy Markdown

Packaging result [SF]: ✖️ el8 ✖️ el9 ✔️ debian ✔️ suse15. SL-JID 19860

@nvazquez

nvazquez commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

@blueorangutan package

@nvazquez nvazquez left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM

@nvazquez

nvazquez commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

@blueorangutan package

@blueorangutan

Copy link
Copy Markdown

@nvazquez a [SL] Jenkins job has been kicked to build packages. It will be bundled with no SystemVM templates. I'll keep you posted as I make progress.

@blueorangutan

Copy link
Copy Markdown

Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ el10 ✔️ debian ✔️ suse15. SL-JID 19962

@nvazquez

Copy link
Copy Markdown
Contributor

@blueorangutan test

@blueorangutan

Copy link
Copy Markdown

@nvazquez a [SL] Trillian-Jenkins test job (ol8 mgmt + kvm-ol8) has been kicked to run smoke tests

@nvazquez
nvazquez marked this pull request as draft November 10, 2026 02:37
@blueorangutan

blueorangutan commented Nov 11, 2026 •

Copy link
Copy Markdown

[SF] Trillian test result (tid-17527)
Environment: kvm-ol8 (x2), zone: Advanced Networking with Mgmt server ol8
Total time taken: 79746 seconds
Marvin logs: [archive removed]
Smoke tests completed. 151 look OK, 6 have errors, 0 did not run
Only failed and skipped tests results shown below:

Test Result Time (s) Test File
test_uservm_host_control_state Failure 17.31 test_host_control_state.py
test_04_readiness_cache_invalidated_on_out_of_band_restart Error 118.88 test_instance_boot_group.py
test_08_upgrade_kubernetes_ha_cluster Failure 3674.35 test_kubernetes_clusters.py
test_01_primary_storage_nfs Error 0.27 test_primary_storage.py
ContextSuite context=TestStorageTags>:setup Error 0.44 test_primary_storage.py
test_01_primary_storage_scope_change Error 0.17 test_primary_storage_scope.py
test_03_ssvm_internals Failure 3.33 test_ssvm.py
test_05_stop_ssvm Failure 86.40 test_ssvm.py
test_07_reboot_ssvm Failure 137.42 test_ssvm.py
test_09_reboot_ssvm_forced Failure 161.20 test_ssvm.py

@RosiKyu
RosiKyu self-requested a review November 11, 2026 11:39
@Damans227
Damans227 marked this pull request as ready for review November 23, 2026 14:17
@Damans227

Copy link
Copy Markdown
Contributor Author

The smoke test failures in tid-17527 (ssvm, primary storage, kubernetes, host control state) are unrelated. This PR only touches KeycloakOAuth2Provider and its unit test in the oauth2 plugin. Marked ready for review, aiming for 24.0.

@la-wu

la-wu commented Nov 25, 2026

Copy link
Copy Markdown

Gave this code a try within our test environment as we have high a demand for this fix to be in version 24.
And it works as expected.
A keycloak authenticated user without corresponding cloudstack user/email lands back on the cloudstack login page while other other keycloak users still can login to cloudstack.

Testsetup:
keycloak 26.8.2
Cloudstack Management Server on Rocky Linux 10
Java version 21

@nvazquez

Copy link
Copy Markdown
Contributor

Thanks for the testing @la-wu. In this case, could you please approve the PR through Files changed -> Review changes -> Approve as well?

@la-wu la-wu left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testsetup:
keycloak 26.8.2
Cloudstack Management Server on Rocky Linux 10
Java version 21

@nvazquez

Copy link
Copy Markdown
Contributor

@blueorangutan test

@blueorangutan

Copy link
Copy Markdown

@nvazquez a [SL] Trillian-Jenkins test job (ol8 mgmt + kvm-ol8) has been kicked to run smoke tests

@blueorangutan

blueorangutan commented Nov 27, 2026 •

Copy link
Copy Markdown

[SF] Trillian test result (tid-17637)
Environment: kvm-ol8 (x2), zone: Advanced Networking with Mgmt server ol8
Total time taken: 55573 seconds
Marvin logs: [archive removed]
Smoke tests completed. 156 look OK, 1 have errors, 0 did not run
Only failed and skipped tests results shown below:

Test Result Time (s) Test File
test_11_destroy_ssvm Error 3.28 test_ssvm.py

@nvazquez
nvazquez merged commit c34b152 into apache:main Dec 1, 2026
26 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keycloak oauth configuration code not cleared

5 participants