Skip to content
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

[fix][broker] Fix MultiRoles token provider NPE when using anonymous clients #21429

Merged
merged 3 commits into from
Oct 25, 2023

Conversation

Technoboy-
Copy link
Contributor

@Technoboy- Technoboy- commented Oct 24, 2023

Motivation

2023-10-24T01:39:33,477+0000 [pulsar-io-4-2] WARN  org.apache.pulsar.broker.service.ServerCnx - [/10.90.12.95:39414] Got exception java.lang.NullPointerException: Cannot invoke "org.apache.pulsar.broker.authentication.AuthenticationDataSource.hasDataFromCommand()" because "this.authData" is null
        at org.apache.pulsar.broker.authentication.AuthenticationDataSubscription.hasDataFromCommand(AuthenticationDataSubscription.java:37)
        at org.apache.pulsar.broker.authorization.MultiRolesTokenAuthorizationProvider.getRoles(MultiRolesTokenAuthorizationProvider.java:153)
        at org.apache.pulsar.broker.authorization.MultiRolesTokenAuthorizationProvider.authorize(MultiRolesTokenAuthorizationProvider.java:201)

Modification

  • Fix the NPE when consume.
  • Check if the role is super-user when using MultiRolesTokenAuthorizationProvider

Documentation

  • doc
  • doc-required
  • doc-not-needed
  • doc-complete

@codecov-commenter
Copy link

codecov-commenter commented Oct 24, 2023

Codecov Report

Merging #21429 (a00ed88) into master (618aede) will increase coverage by 43.10%.
Report is 9 commits behind head on master.
The diff coverage is 100.00%.

Impacted file tree graph

@@              Coverage Diff              @@
##             master   #21429       +/-   ##
=============================================
+ Coverage     30.15%   73.26%   +43.10%     
- Complexity      324    32575    +32251     
=============================================
  Files          1709     1888      +179     
  Lines        130685   140292     +9607     
  Branches      14245    15417     +1172     
=============================================
+ Hits          39414   102781    +63367     
+ Misses        85327    29424    -55903     
- Partials       5944     8087     +2143     
Flag Coverage Δ
inttests 24.22% <0.00%> (+0.07%) ⬆️
systests 24.79% <0.00%> (+0.06%) ⬆️
unittests 72.54% <100.00%> (?)

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

Files Coverage Δ
...authentication/AuthenticationDataSubscription.java 64.28% <100.00%> (+18.13%) ⬆️
...rization/MultiRolesTokenAuthorizationProvider.java 60.19% <100.00%> (+60.19%) ⬆️

... and 1508 files with indirect coverage changes

@lhotari lhotari requested a review from merlimat October 24, 2023 12:55
@lhotari
Copy link
Member

lhotari commented Oct 24, 2023

relates to #21338

@@ -71,4 +71,8 @@ public boolean hasSubscription() {
public String getSubscription() {
return subscription;
}

public AuthenticationDataSource getAuthData() {
Copy link
Member

Choose a reason for hiding this comment

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

Why not you check the authData in the constructor or override methods?

    @Override
    public boolean hasDataFromTls() {
        return authData != null && authData.hasDataFromTls();
    }

We should avoid type checking.

(authData instanceof AuthenticationDataSubscription && ((AuthenticationDataSubscription) authData).getAuthData() == null)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But the getRoles method will return empty

Copy link
Member

Choose a reason for hiding this comment

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

I have communicated with @Technoboy- T offline. I don't suggest that add getAuthData() method, and makes the code difficult to maintain.

The following are my ideas:

  • idea 1
    Add a new AuthenticationDataSource for the anonymous role, and then check the authData type.

  • idea 2
    Change the org.apache.pulsar.broker.authorization.MultiRolesTokenAuthorizationProvider#getRoles logic to quickly return role when role equals anonymous role:

    private Set<String> getRoles(String role, AuthenticationDataSource authData) {
        if (authData == null || role.equals(conf.getAnonymousUserRole())) {
            return Collections.singleton(role);
        }

There will be a pitfall here, if the client role is equal to the anonymous role, the real roles cannot be obtained from authData.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

idea 1 is more pretty, I will create a issue to track this.

@Technoboy- Technoboy- merged commit 8056987 into apache:master Oct 25, 2023
49 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.

None yet

8 participants