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]Super user role checked #35

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,18 @@ public void initialize(ServiceConfiguration conf, PulsarResources pulsarResource
@Override
public CompletableFuture<Boolean> isSuperUser(String role, AuthenticationDataSource authenticationData,
ServiceConfiguration serviceConfiguration) {
Set<String> roles = getRoles(authenticationData);
if (roles.isEmpty()) {
return CompletableFuture.completedFuture(false);
}
// if superUser role contains in config, return true.
Set<String> superUserRoles = serviceConfiguration.getSuperUserRoles();
if (superUserRoles.isEmpty()) {
return CompletableFuture.completedFuture(false);
}

if (role != null && superUserRoles.contains(role)) {
return CompletableFuture.completedFuture(true);
}
Set<String> roles = getRoles(authenticationData);
if (roles.isEmpty()) {
return CompletableFuture.completedFuture(false);
}
return CompletableFuture.completedFuture(roles.stream().anyMatch(superUserRoles::contains));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.testng.annotations.Test;

import javax.crypto.SecretKey;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

public class MultiRolesTokenAuthorizationProviderTest {
Expand Down Expand Up @@ -198,4 +199,35 @@ public String getHttpHeader(String name) {
return CompletableFuture.completedFuture(false);
}).get());
}

@Test
public void testMultiRolesAuthzWithSuperUser() throws Exception {
SecretKey secretKey = AuthTokenUtils.createSecretKey(SignatureAlgorithm.HS256);
String testAdminRole = "admin";
String token = Jwts.builder().claim("sub", testAdminRole).signWith(secretKey).compact();

ServiceConfiguration conf = new ServiceConfiguration();
conf.setSuperUserRoles(Set.of(testAdminRole));

MultiRolesTokenAuthorizationProvider provider = new MultiRolesTokenAuthorizationProvider();
provider.initialize(conf, mock(PulsarResources.class));

AuthenticationDataSource ads = new AuthenticationDataSource() {
@Override
public boolean hasDataFromHttp() {
return true;
}

@Override
public String getHttpHeader(String name) {
if (name.equals("Authorization")) {
return "Bearer " + token;
} else {
throw new IllegalArgumentException("Wrong HTTP header");
}
}
};

assertTrue(provider.isSuperUser(testAdminRole, ads, conf).get());
}
}
Loading