Skip to content

Use user guid for token cache#1642

Merged
theghost5800 merged 1 commit into
masterfrom
improveTokenCache
Jun 5, 2025
Merged

Use user guid for token cache#1642
theghost5800 merged 1 commit into
masterfrom
improveTokenCache

Conversation

@theghost5800
Copy link
Copy Markdown
Contributor

JIRA:LMCROSSITXSADEPLOY-3207

Description:

Issue:


/**
* Returns a client for the specified user name and space id by either getting it from the clients cache or creating a new one.
* Returns a client for the specified username and space id by either getting it from the clients cache or creating a new one.
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.

maybe rename to "specified user guid"? as the username will be removed soon?


<include file="/org/cloudfoundry/multiapps/controller/persistence/db/changelog/db-changelog-1.183.0-persistence.xml"/>

<include file="/org/cloudfoundry/multiapps/controller/persistence/db/changelog/db-changelog-1.191.0-persistence.xml"/>
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.

reminder: align with the latest release

public static String determineCurrentUserGuid(VariableScope scope) {
String userGuid = VariableHandling.get(scope, Variables.USER_GUID);
if (userGuid == null) {
throw new SLException(Messages.CANT_DETERMINE_CURRENT_USER_GUID);
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.

is it possible not to have the user guid?
what will happen with the running processes during the blue green deploy service update?
will this variable be set?

3,
30,
TimeUnit.SECONDS,
private final ExecutorService threadPoolForTokensDeletion = new ThreadPoolExecutor(1, 3, 30, TimeUnit.SECONDS,
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.

Magic numbers can be extracted (not mandatory)

OAuth2AccessTokenWithAdditionalInfo cachedAccessToken = cachedTokens.get(username);
if (shouldUseCachedToken(cachedAccessToken)) {
return cachedAccessToken;
public OAuth2AccessTokenWithAdditionalInfo getToken(String username, String userGuid) {
Copy link
Copy Markdown
Contributor

@s-yonkov-yonkov s-yonkov-yonkov Jun 4, 2025

Choose a reason for hiding this comment

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

Code suggestion:

    if (userGuid != null) {
        OAuth2AccessTokenWithAdditionalInfo cached = cachedTokens.get(userGuid);
        if (shouldUseCachedToken(cached)) {
            return cached;
        }

        List<AccessToken> tokensByGuid = getSortedAccessTokensByUserGuid(userGuid);
        if (!tokensByGuid.isEmpty()) {
            return cacheAndReturnLatest(tokensByGuid);
        }
    }

    List<AccessToken> tokensByUsername = getSortedAccessTokensByUsername(username);
    if (tokensByUsername.isEmpty()) {
        throw new IllegalStateException(MessageFormat.format(Messages.NO_VALID_TOKEN_FOUND, userGuid));
    }

    return cacheAndReturnLatest(tokensByUsername);
}

private OAuth2AccessTokenWithAdditionalInfo cacheAndReturnLatest(List<AccessToken> tokens) {
    OAuth2AccessTokenWithAdditionalInfo latest = getLatestToken(tokens);
    String userId = (String) latest.getAdditionalInfo().get(TokenProperties.USER_ID_KEY);
    cachedTokens.put(userId, latest);

    if (tokens.size() > 1) {
        deleteTokens(tokens.subList(1, tokens.size()));
    }

    return latest;
}

JIRA:LMCROSSITXSADEPLOY-3207
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Jun 5, 2025

Quality Gate Failed Quality Gate failed

Failed conditions
72.1% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@theghost5800 theghost5800 merged commit 09bbaa7 into master Jun 5, 2025
7 of 8 checks passed
@theghost5800 theghost5800 deleted the improveTokenCache branch June 5, 2025 13:15
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.

3 participants