Skip to content
Merged
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 @@ -644,7 +644,7 @@ public void startFunction(
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void uploadFunction(final @FormDataParam("data") InputStream uploadedInputStream,
final @FormDataParam("path") String path) {
functions.uploadFunction(uploadedInputStream, path, clientAppId());
functions.uploadFunction(uploadedInputStream, path, clientAppId(), clientAuthData());
}

@GET
Expand Down Expand Up @@ -711,6 +711,6 @@ public void updateFunctionOnWorkerLeader(final @PathParam("tenant") String tenan
final @FormDataParam("delete") boolean delete) {

functions.updateFunctionOnWorkerLeader(tenant, namespace, functionName, uploadedInputStream,
delete, uri.getRequestUri(), clientAppId());
delete, uri.getRequestUri(), clientAppId(), clientAuthData());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,6 @@ public List<ConfigFieldDefinition> getSinkConfigDefinition(
})
@Path("/reloadBuiltInSinks")
public void reloadSinks() {
sink.reloadConnectors(clientAppId());
sink.reloadConnectors(clientAppId(), clientAuthData());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,6 @@ public List<ConfigFieldDefinition> getSourceConfigDefinition(
})
@Path("/reloadBuiltInSources")
public void reloadSources() {
source.reloadConnectors(clientAppId());
source.reloadConnectors(clientAppId(), clientAuthData());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public Response stopFunction(final @PathParam("tenant") String tenant,
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFunction(final @FormDataParam("data") InputStream uploadedInputStream,
final @FormDataParam("path") String path) {
return functions.uploadFunction(uploadedInputStream, path, clientAppId());
return functions.uploadFunction(uploadedInputStream, path, clientAppId(), clientAuthData());
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -878,13 +878,13 @@ public List<ConnectorDefinition> getListOfConnectors() {
return this.worker().getConnectorsManager().getConnectors();
}

public void reloadConnectors(String clientRole) {
public void reloadConnectors(String clientRole, AuthenticationDataSource authenticationData) {
if (!isWorkerServiceAvailable()) {
throwUnavailableException();
}
if (worker().getWorkerConfig().isAuthorizationEnabled()) {
// Only superuser has permission to do this operation.
if (!isSuperUser(clientRole)) {
if (!isSuperUser(clientRole, authenticationData)) {
throw new RestException(Status.UNAUTHORIZED, "This operation requires super-user access");
}
}
Expand Down Expand Up @@ -1178,13 +1178,14 @@ public void putFunctionState(final String tenant,
}
}

public void uploadFunction(final InputStream uploadedInputStream, final String path, String clientRole) {
public void uploadFunction(final InputStream uploadedInputStream, final String path, String clientRole,
AuthenticationDataSource authenticationData) {

if (!isWorkerServiceAvailable()) {
throwUnavailableException();
}

if (worker().getWorkerConfig().isAuthorizationEnabled() && !isSuperUser(clientRole)) {
if (worker().getWorkerConfig().isAuthorizationEnabled() && !isSuperUser(clientRole, authenticationData)) {
throw new RestException(Status.UNAUTHORIZED, "client is not authorize to perform operation");
}

Expand Down Expand Up @@ -1280,7 +1281,7 @@ public StreamingOutput downloadFunction(final String path, String clientRole, Au
throw new RestException(Status.INTERNAL_SERVER_ERROR, e.getMessage());
}
} else {
if (!isSuperUser(clientRole)) {
if (!isSuperUser(clientRole, clientAuthenticationDataHttps)) {
throw new RestException(Status.UNAUTHORIZED, "client is not authorize to perform operation");
}
}
Expand Down Expand Up @@ -1430,7 +1431,7 @@ public boolean isAuthorizedRole(String tenant, String namespace, String clientRo
AuthenticationDataSource authenticationData) throws PulsarAdminException {
if (worker().getWorkerConfig().isAuthorizationEnabled()) {
// skip authorization if client role is super-user
if (isSuperUser(clientRole)) {
if (isSuperUser(clientRole, authenticationData)) {
return true;
}

Expand Down Expand Up @@ -1513,14 +1514,14 @@ protected void componentInstanceStatusRequestValidate (final String tenant,
}
}

public boolean isSuperUser(String clientRole) {
public boolean isSuperUser(String clientRole, AuthenticationDataSource authenticationData) {
if (clientRole != null) {
try {
if ((worker().getWorkerConfig().getSuperUserRoles() != null
&& worker().getWorkerConfig().getSuperUserRoles().contains(clientRole))) {
return true;
}
return worker().getAuthorizationService().isSuperUser(clientRole, null)
return worker().getAuthorizationService().isSuperUser(clientRole, authenticationData)
.get(worker().getWorkerConfig().getZooKeeperOperationTimeoutSeconds(), SECONDS);
} catch (InterruptedException e) {
log.warn("Time-out {} sec while checking the role {} is a super user role ",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,14 +655,15 @@ public void updateFunctionOnWorkerLeader(final String tenant,
final InputStream uploadedInputStream,
final boolean delete,
URI uri,
final String clientRole) {
final String clientRole,
AuthenticationDataSource authenticationData) {

if (!isWorkerServiceAvailable()) {
throwUnavailableException();
}

if (worker().getWorkerConfig().isAuthorizationEnabled()) {
if (!isSuperUser(clientRole)) {
if (!isSuperUser(clientRole, authenticationData)) {
log.error("{}/{}/{} Client [{}] is not superuser to update on worker leader {}", tenant, namespace,
functionName, clientRole, ComponentTypeUtils.toString(componentType));
throw new RestException(Response.Status.UNAUTHORIZED, "client is not authorize to perform operation");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.pulsar.broker.authentication.AuthenticationDataSource;
import org.apache.pulsar.common.functions.FunctionConfig;
import org.apache.pulsar.common.functions.FunctionState;
import org.apache.pulsar.common.io.ConnectorDefinition;
Expand Down Expand Up @@ -177,8 +178,9 @@ public Response stopFunctionInstances(String tenant, String namespace, String fu
return Response.ok().build();
}

public Response uploadFunction(InputStream uploadedInputStream, String path, String clientRole) {
delegate.uploadFunction(uploadedInputStream, path, clientRole);
public Response uploadFunction(InputStream uploadedInputStream, String path, String clientRole,
AuthenticationDataSource authenticationData) {
delegate.uploadFunction(uploadedInputStream, path, clientRole, authenticationData);
return Response.ok().build();
}

Expand Down Expand Up @@ -229,4 +231,4 @@ private InstanceCommunication.FunctionStatus toProto(

return functionStatus;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public Response stopFunction(final @PathParam("tenant") String tenant,
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFunction(final @FormDataParam("data") InputStream uploadedInputStream,
final @FormDataParam("path") String path) {
return functions.uploadFunction(uploadedInputStream, path, clientAppId());
return functions.uploadFunction(uploadedInputStream, path, clientAppId(), clientAuthData());
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public void startFunction(final @PathParam("tenant") String tenant,
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void uploadFunction(final @FormDataParam("data") InputStream uploadedInputStream,
final @FormDataParam("path") String path) {
functions.uploadFunction(uploadedInputStream, path, clientAppId());
functions.uploadFunction(uploadedInputStream, path, clientAppId(), clientAuthData());
}

@GET
Expand Down Expand Up @@ -386,6 +386,6 @@ public void updateFunctionOnWorkerLeader(final @PathParam("tenant") String tenan
final @FormDataParam("delete") boolean delete) {

functions.updateFunctionOnWorkerLeader(tenant, namespace, functionName, uploadedInputStream,
delete, uri.getRequestUri(), clientAppId());
delete, uri.getRequestUri(), clientAppId(), clientAuthData());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,6 @@ public List<ConfigFieldDefinition> getSinkConfigDefinition(
})
@Path("/reloadBuiltInSinks")
public void reloadSinks() {
sink.reloadConnectors(clientAppId());
sink.reloadConnectors(clientAppId(), clientAuthData());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,6 @@ public List<ConfigFieldDefinition> getSourceConfigDefinition(
})
@Path("/reloadBuiltInSources")
public void reloadSources() {
source.reloadConnectors(clientAppId());
source.reloadConnectors(clientAppId(), clientAuthData());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ public void testIsAuthorizedRole() throws PulsarAdminException, InterruptedExcep

// test pulsar super user
final String pulsarSuperUser = "pulsarSuperUser";
when(authorizationService.isSuperUser(pulsarSuperUser, null)).thenReturn(CompletableFuture.completedFuture(true));
when(authorizationService.isSuperUser(eq(pulsarSuperUser), any())).thenReturn(CompletableFuture.completedFuture(true));
assertTrue(functionImpl.isAuthorizedRole("test-tenant", "test-ns", pulsarSuperUser, authenticationDataSource));
assertTrue(functionImpl.isSuperUser(pulsarSuperUser));
assertTrue(functionImpl.isSuperUser(pulsarSuperUser, null));

// test normal user
functionImpl = spy(new FunctionsImpl(() -> mockedWorkerService));
Expand All @@ -263,7 +263,7 @@ public void testIsAuthorizedRole() throws PulsarAdminException, InterruptedExcep
when(admin.tenants()).thenReturn(tenants);
when(this.mockedWorkerService.getBrokerAdmin()).thenReturn(admin);
when(authorizationService.isTenantAdmin("test-tenant", "test-user", tenantInfo, authenticationDataSource)).thenReturn(CompletableFuture.completedFuture(false));
when(authorizationService.isSuperUser("test-user", null)).thenReturn(CompletableFuture.completedFuture(false));
when(authorizationService.isSuperUser(eq("test-user"), any())).thenReturn(CompletableFuture.completedFuture(false));
assertFalse(functionImpl.isAuthorizedRole("test-tenant", "test-ns", "test-user", authenticationDataSource));

// if user is tenant admin
Expand Down Expand Up @@ -322,10 +322,10 @@ public void testIsSuperUser() throws PulsarAdminException {
});

AuthenticationDataSource authenticationDataSource = mock(AuthenticationDataSource.class);
assertTrue(functionImpl.isSuperUser(superUser));
assertTrue(functionImpl.isSuperUser(superUser, null));

assertFalse(functionImpl.isSuperUser("normal-user"));
assertFalse(functionImpl.isSuperUser( null));
assertFalse(functionImpl.isSuperUser("normal-user", null));
assertFalse(functionImpl.isSuperUser( null, null));

// test super roles is null and it's not a pulsar super user
when(authorizationService.isSuperUser(superUser, null))
Expand All @@ -334,7 +334,7 @@ public void testIsSuperUser() throws PulsarAdminException {
workerConfig = new WorkerConfig();
workerConfig.setAuthorizationEnabled(true);
doReturn(workerConfig).when(mockedWorkerService).getWorkerConfig();
assertFalse(functionImpl.isSuperUser(superUser));
assertFalse(functionImpl.isSuperUser(superUser, null));
}

public static FunctionConfig createDefaultFunctionConfig() {
Expand Down