Skip to content

Fix bugs about connect to Azure AD by proxy - #31213

Closed
chenrujun wants to merge 9 commits into
Azure:mainfrom
chenrujun:fix-issue-31191-duplicated-scopes
Closed

Fix bugs about connect to Azure AD by proxy#31213
chenrujun wants to merge 9 commits into
Azure:mainfrom
chenrujun:fix-issue-31191-duplicated-scopes

Conversation

@chenrujun

@chenrujun chenrujun commented Sep 29, 2022

Copy link
Copy Markdown

Fix #31191, #31190, #31218

  1. Fix error: OAuth2AccessTokenResponseClientRestTemplate contains too many converters. It should only contain 2 converters: FormHttpMessageConverter and OAuth2AccessTokenResponseHttpMessageConverter.
  2. Fix error: Collections.emptyMap().put(..) is not supported.
  3. Fix error: addParametersConverter and addHeadersConverter executed multiple times.
  4. Fix error: RestOperations is not well-configured when jwkResolver is null.

@ghost ghost added the azure-spring All azure-spring related issues label Sep 29, 2022
@chenrujun chenrujun self-assigned this Sep 29, 2022
@chenrujun chenrujun added bug This issue requires a change to an existing behavior in the product in order to be resolved. Client This issue points to a problem in the data-plane of the library. labels Sep 29, 2022
@chenrujun chenrujun added this to the 2022-11 milestone Sep 29, 2022
@azure-sdk

Copy link
Copy Markdown
Collaborator

API change check

API changes are not detected in this pull request.

1. For RestTemplate used to get access token, only keep 2 converters: FormHttpMessageConverter and OAuth2AccessTokenResponseHttpMessageConverter. Because keep more converters may cause the 2 converters not been used.
2. Return new HashMap() instead of Collections.emptyMap(). Because the returned value needs to add new values, but Collections.emptyMap() does not support add new value.
@chenrujun
chenrujun force-pushed the fix-issue-31191-duplicated-scopes branch from abfa967 to 5470386 Compare September 29, 2022 10:16
Comment thread sdk/spring/CHANGELOG.md Outdated
Comment on lines +37 to +40
List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
if (notContainsElementOfType(converters, FormHttpMessageConverter.class)) {
converters.add(new FormHttpMessageConverter());
}
if (notContainsElementOfType(converters, OAuth2AccessTokenResponseHttpMessageConverter.class)) {
converters.add(new OAuth2AccessTokenResponseHttpMessageConverter());
}
converters.clear();
converters.add(new FormHttpMessageConverter());
converters.add(new OAuth2AccessTokenResponseHttpMessageConverter());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why not calling this method public RestTemplateBuilder messageConverters(HttpMessageConverter<?>... messageConverters)?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Updated.

@0x006EA1E5

0x006EA1E5 commented Oct 6, 2022

Copy link
Copy Markdown

This is not working for me, and indeed does not appear to address the root cause.

The problem as far as I can see is that AbstractOAuth2AuthorizationCodeGrantRequestEntityConverter#convert adds the parametersConverter (AadOAuth2AuthorizationCodeGrantRequestEntityConverter#getHttpBody) each time it is called, which will be once per request.

Looking at AbstractOAuth2AuthorizationGrantRequestEntityConverter#addParametersConverter in spring-security-oauth2-client 5.7.3, this creates a new composed lambda which first calls currentParametersConverter, and then calls the provided parametersConverter (AadOAuth2AuthorizationCodeGrantRequestEntityConverter#getHttpBody); the new lambda is set as the new value for AbstractOAuth2AuthorizationGrantRequestEntityConverter#parametersConverter.

The initial call this works correctly (AbstractOAuth2AuthorizationCodeGrantRequestEntityConverter#convert adds the implementing class's AadOAuth2AuthorizationCodeGrantRequestEntityConverter#getHttpBody), which means we end up with AbstractOAuth2AuthorizationGrantRequestEntityConverter#parametersConverter being a lambda composed of the original default parametersConverter plus the provided AadOAuth2AuthorizationCodeGrantRequestEntityConverter#getHttpBody.

However, on any subsequent call, we call AbstractOAuth2AuthorizationGrantRequestEntityConverter#addParametersConverter again, meaning we update AbstractOAuth2AuthorizationGrantRequestEntityConverter#parametersConverter to be a composition of the previous correctly composed parametersConverter with another call to AadOAuth2AuthorizationCodeGrantRequestEntityConverter#getHttpBody.

Hence, on the second call, we do AadOAuth2AuthorizationCodeGrantRequestEntityConverter#getHttpBody twice, which results on the observed error: The request is not properly formatted. The parameter 'scope' is duplicated.

Indeed, on each additional request, we call AbstractOAuth2AuthorizationCodeGrantRequestEntityConverter#convert again, and add yet another AadOAuth2AuthorizationCodeGrantRequestEntityConverter#getHttpBody, meaning that AbstractOAuth2AuthorizationGrantRequestEntityConverter#parametersConverter grows, and is a memory leak that would eventually blow the stack, and that each additional request will contain an additional identical scope.

n.b., this is in part due to the fact that parametersConverter uses a MultiValueMap, so adding the same value multiple times will not overwrite / update, but will add.

It seems a correct solution would be instead to call AbstractOAuth2AuthorizationGrantRequestEntityConverter#addParametersConverter just once, in a constructor.

The same goes for addHeadersConverter(...).

@0x006EA1E5

0x006EA1E5 commented Oct 6, 2022

Copy link
Copy Markdown

PR to fix above #31290

@check-enforcer

check-enforcer Bot commented Oct 8, 2022

Copy link
Copy Markdown

This pull request is protected by Check Enforcer.

What is Check Enforcer?

Check Enforcer helps ensure all pull requests are covered by at least one check-run (typically an Azure Pipeline). When all check-runs associated with this pull request pass then Check Enforcer itself will pass.

Why am I getting this message?

You are getting this message because Check Enforcer did not detect any check-runs being associated with this pull request within five minutes. This may indicate that your pull request is not covered by any pipelines and so Check Enforcer is correctly blocking the pull request being merged.

What should I do now?

If the check-enforcer check-run is not passing and all other check-runs associated with this PR are passing (excluding license-cla) then you could try telling Check Enforcer to evaluate your pull request again. You can do this by adding a comment to this pull request as follows:
/check-enforcer evaluate
Typically evaulation only takes a few seconds. If you know that your pull request is not covered by a pipeline and this is expected you can override Check Enforcer using the following command:
/check-enforcer override
Note that using the override command triggers alerts so that follow-up investigations can occur (PRs still need to be approved as normal).

What if I am onboarding a new service?

Often, new services do not have validation pipelines associated with them, in order to bootstrap pipelines for a new service, you can issue the following command as a pull request comment:
/azp run prepare-pipelines
This will run a pipeline that analyzes the source tree and creates the pipelines necessary to build and validate your pull request. Once the pipeline has been created you can trigger the pipeline using the following comment:
/azp run java - [service] - ci

@chenrujun
chenrujun force-pushed the fix-issue-31191-duplicated-scopes branch from 1fda10b to a647731 Compare October 8, 2022 06:41
@chenrujun chenrujun mentioned this pull request Oct 8, 2022
6 tasks
@chenrujun

Copy link
Copy Markdown
Author

Hi, @saragluna , @0x006EA1E5 please help to review this PR when you have time.

@chenrujun chenrujun changed the title Fix issue 31191: duplicated scopes. Fix bugs about connect to Azure AD by proxy Oct 8, 2022
.map(s -> s.getAttribute(AUTHORIZED_CLIENTS_ATTR_NAME))
.map(Object::toString)
.map(SerializerUtils::deserializeOAuth2AuthorizedClientMap)
.orElse(Collections.emptyMap());

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I wouldn't do this, as it means the execution of an additional lambda, whereas the Collections.emptyMap() method just returns the static java.util.Collections#EMPTY_MAP field (so there is no allocation cost saving which would otherwise justify the orElse).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@0x006EA1E5
Thank you for your suggestion.

But I have different understanding:

  1. lambda is just a pointer to a method, it will not need extra cost.
  2. In orElse(doSomething()), doSomething() will always be executed, it's a kind of waste (a method call will cost Stack Frame). Here is code to verify this:
public class Test {

    public static void main(String[] args) {
        System.out.println("Start orElse");
        String orElseResult = Optional.of("string")
                .orElse(doSomething());
        System.out.println("orElseResult = " + orElseResult);

        System.out.println();
        System.out.println("Start orElseGet");
        String orElseGetResult = Optional.of("string")
                .orElseGet(Test::doSomething);
        System.out.println("orElseGetResult = " + orElseGetResult);
    }
    public static String doSomething() {
        System.out.println("doSomething");
        return "doSomething";
    }
}

Execution result:
image

@0x006EA1E5 0x006EA1E5 Oct 11, 2022

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Ah, yes, you are right, it is a method reference so the lambda doesn't need to be created.

When I mentioned cost saving, I meant different scenario which doesn't apply here:

Optional
    ...
    .orElse(new ExpensiveObject(param));

vs

Optional
    ...
    .orElseGet(() -> new ExpensiveObject(param));

Where the second is obviously preferable, even with the extra lambda.

In any case, I suspect that the performance of .orElse(Collections.emptyMap()) will be indistinguishable from that of .orElseGet(Collections::emptyMap) in the real world, and they would both get optimised and inlined to the same thing if executed enough to make any difference.

Thanks for the response, please ignore my change suggestion :)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Got it. Thank you for your detailed explanation.

*/
public MultiValueMap<String, String> getHttpBody(OAuth2AuthorizationCodeGrantRequest request) {
return null;
return new LinkedMultiValueMap<>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

My personal preference would be to return an immutable collection here, if possible. Hence my suggestion of new MultiValueMapAdapter<>(Collections.emptyMap());

Maybe even have a static field to return, so there is no repeated allocation.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good point. Will accept this suggestion.


/**
* Additional default headers information.
* @return HttpHeaders

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What do you think about making this public HttpHeaders getHttpHeaders(OAuth2AuthorizationCodeGrantRequest request) {?

This is an abstract class, it seems like it seems like it might be nice to give implementors access to the request parameter

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Will do.

@0x006EA1E5 0x006EA1E5 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.

Looks good to me

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

Labels

azure-spring All azure-spring related issues bug This issue requires a change to an existing behavior in the product in order to be resolved. Client This issue points to a problem in the data-plane of the library.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[BUG] The parameter 'scope' is duplicated

5 participants