Skip to content

fix: use SW360KeycloakClient to renew Keycloak tokens automatically - #248

Merged
tngraf merged 2 commits into
sw360:mainfrom
berke581:fix/keycloak-token-renewal
Nov 15, 2026
Merged

tngraf merged 2 commits into
sw360:mainfrom
berke581:fix/keycloak-token-renewal

Conversation

@berke581

Copy link
Copy Markdown

Fixes #241

capycli fetched one Keycloak token at startup, so long running commands failed once it expired. This switches to SW360KeycloakClient (sw360 >= 1.13.0), which renews the token itself.

  1. refactor: move the Keycloak login, repeated in 15 commands, into ScriptBase.login_with_args(). No behaviour change.
  2. fix: use SW360KeycloakClient there, and reuse that client for the component cache refresh.

Login with a token (-t / SW360ProductionToken) is unchanged.

Side effects: project createbom now reads the client id/secret from the environment and only prints token details with --verbose; bom findsources checks the login result; invalid credentials give a clean auth error instead of a traceback.

Testing: new unit tests for the login paths. SW360KeycloakClient was also verified against a real SW360/Keycloak with forced renewal in another tool; I couldn't run capycli itself end to end.

Based on the approach in #242 by @dhruvv16-hash. The 401 retry from #242 is dropped, since sw360python now handles renewal.

Berke Yoncaci added 2 commits November 11, 2026 17:11
Every bom and project command repeated the same block to read client_id and
client_secret from the command line or the environment, create a Keycloak
token and log in. Move it to the new ScriptBase.login_with_args() and leave
ScriptBase.login() unchanged, so the static token path is unaffected.

Two inconsistencies disappear with the deduplication: "project createbom" only
honoured the command line options and ignored the environment variables
SW360Client_id and SW360Client_secret, and "bom findsources" never checked the
result of login().

Signed-off-by: Berke Yoncaci <berke.yoncaci@siemens.com>
Commands which run longer than the Keycloak token lifetime failed once the
initial token expired. Use SW360KeycloakClient from sw360 1.13.0, which takes
the client credentials instead of a token and renews the token shortly before
it expires, so that every request carries a valid token.

The component cache refresh created a second client from the token string. It
now reuses the authenticated client, as this is the operation which runs long
enough for the token to expire.

An invalid client_id or client_secret reported an unhandled SW360Error before
and is now reported as an authorization error.

Fixes sw360#241

Signed-off-by: Berke Yoncaci <berke.yoncaci@siemens.com>
@tngraf tngraf self-assigned this Nov 15, 2026
@tngraf
tngraf merged commit a78514b into sw360:main Nov 15, 2026
5 checks passed
@tngraf

tngraf commented Nov 15, 2026

Copy link
Copy Markdown
Collaborator

Thanks, @berke581, for this PR and the other PR for sw360python.

@gernot-h

Copy link
Copy Markdown
Collaborator

Sorry for not answering earlier! Also big thanks from my side, great rework!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refresh Keycloak tokens during long-running CaPyCLI SW360 operations

3 participants