Skip to content

Commit 18995d2

Browse files
authored
Fixed issue with file-download and service-transport CredentialsSupplier (#881)
1 parent 9c0acf7 commit 18995d2

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

services-gateway/src/test/java/io/scalecube/services/gateway/files/FileDownloadTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77
import static org.junit.jupiter.api.Assertions.assertNotNull;
88
import static org.junit.jupiter.api.Assertions.assertTrue;
99
import static org.junit.jupiter.api.Assertions.fail;
10+
import static org.mockito.ArgumentMatchers.any;
11+
import static org.mockito.Mockito.mock;
12+
import static org.mockito.Mockito.when;
1013

1114
import io.scalecube.services.Address;
1215
import io.scalecube.services.Microservices;
1316
import io.scalecube.services.Microservices.Context;
1417
import io.scalecube.services.ServiceCall;
18+
import io.scalecube.services.ServiceReference;
1519
import io.scalecube.services.discovery.ScalecubeServiceDiscovery;
1620
import io.scalecube.services.exceptions.InternalServiceException;
1721
import io.scalecube.services.exceptions.ServiceUnavailableException;
@@ -20,6 +24,7 @@
2024
import io.scalecube.services.gateway.client.websocket.WebsocketGatewayClientTransport;
2125
import io.scalecube.services.gateway.http.HttpGateway;
2226
import io.scalecube.services.gateway.websocket.WebsocketGateway;
27+
import io.scalecube.services.transport.api.ServiceTransport.CredentialsSupplier;
2328
import io.scalecube.services.transport.rsocket.RSocketServiceTransport;
2429
import io.scalecube.transport.netty.websocket.WebsocketTransportFactory;
2530
import java.io.IOException;
@@ -36,6 +41,7 @@
3641
import org.junit.jupiter.api.BeforeEach;
3742
import org.junit.jupiter.api.Test;
3843
import reactor.core.Exceptions;
44+
import reactor.core.publisher.Mono;
3945
import reactor.test.StepVerifier;
4046

4147
public class FileDownloadTest {
@@ -46,11 +52,16 @@ public class FileDownloadTest {
4652
private static Microservices microservices;
4753
private static Address httpAddress;
4854
private static Address wsAddress;
55+
private static CredentialsSupplier credentialsSupplier;
4956

5057
private ServiceCall serviceCall;
5158

5259
@BeforeAll
5360
static void beforeAll() {
61+
credentialsSupplier = mock(CredentialsSupplier.class);
62+
when(credentialsSupplier.apply(any(ServiceReference.class)))
63+
.thenReturn(Mono.never());
64+
5465
gateway =
5566
Microservices.start(
5667
new Context()
@@ -59,7 +70,8 @@ static void beforeAll() {
5970
new ScalecubeServiceDiscovery()
6071
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
6172
.options(opts -> opts.metadata(serviceEndpoint)))
62-
.transport(RSocketServiceTransport::new)
73+
.transport(
74+
() -> new RSocketServiceTransport().credentialsSupplier(credentialsSupplier))
6375
.gateway(() -> new HttpGateway.Builder().id("HTTP").build())
6476
.gateway(() -> new WebsocketGateway.Builder().id("WS").build()));
6577

services-transport-parent/services-transport-rsocket/src/main/java/io/scalecube/services/transport/rsocket/RSocketClientTransport.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ private Mono<Map<String, String>> getCredentials(ServiceReference serviceReferen
8181
if (credentialsSupplier == null) {
8282
return Mono.just(Collections.emptyMap());
8383
}
84+
85+
if (!serviceReference.isSecured()) {
86+
return Mono.just(Collections.emptyMap());
87+
}
88+
8489
return credentialsSupplier
8590
.apply(serviceReference)
8691
.switchIfEmpty(Mono.just(Collections.emptyMap()))
@@ -140,8 +145,7 @@ private Payload encodeConnectionSetup(ConnectionSetup connectionSetup) {
140145
}
141146

142147
private UnauthorizedException toUnauthorizedException(Throwable th) {
143-
if (th instanceof ServiceException) {
144-
ServiceException e = (ServiceException) th;
148+
if (th instanceof ServiceException e) {
145149
return new UnauthorizedException(e.errorCode(), e.getMessage());
146150
} else {
147151
return new UnauthorizedException(th);

services/src/main/java/io/scalecube/services/files/FileService.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,22 @@
66

77
/**
88
* Service interface for adding files locally, those added files will be accessible by {@link
9-
* FileStreamer}. Typical usage: client generates a file in the app service, then calls {@link
10-
* #addFile(AddFileRequest)}, then returns result (file path qualifier) all the way back to the
11-
* caller of app service. On the caller side file path qualifier gets combined with http-gateway
12-
* address, and then url for file streaming is ready. Then caller side has time to download a file
13-
* until file is not expired.
9+
* FileStreamer}. Typical usage: client defines an app service with injected {@link FileService},
10+
* client generates a file in the app service, then calls {@link #addFile(AddFileRequest)}, then
11+
* returns result (file path qualifier) all the way back to the caller of app service. On the caller
12+
* side file path qualifier gets combined with http-gateway address, and then url for file streaming
13+
* is ready. Then caller side has time to download a file until file gets expired.
1414
*/
1515
@Service
1616
public interface FileService {
1717

1818
/**
1919
* Adding file and returning path qualifier for the added file. {@link AddFileRequest} must
2020
* contain {@code file} that exists, that is not directory, and must have valid path, another
21-
* parameter - {@code ttl} (optional) represents time after which file will be deleted.
22-
*
23-
* <p>Returned file path qualifier comes as: {@code
24-
* v1/scalecube.endpoints/${microservices:id}/files/:name}, for example: {@code
25-
* v1/scalecube.endpoints/19e3afc1-fa46-4a55-8a41-1bdf4afc8a5b/files/report_03_01_2025.txt}.
21+
* parameter - {@code ttl} (optional) represents time after which file will be deleted. Returned
22+
* file path qualifier comes as: {@code v1/scalecube.endpoints/${microservices:id}/files/:name},
23+
* for example: {@code
24+
* v1/scalecube.endpoints/19e3afc1-fa46-4a55-8a41-1bdf4afc8a5b/files/report_03_01_2025.txt}
2625
*
2726
* @param request request
2827
* @return async result with path qualifier

services/src/main/java/io/scalecube/services/files/FileStreamer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
/**
1010
* System service interface for streaming files after they have been added locally with {@link
11-
* FileService#addFile(AddFileRequest)}.
11+
* FileService#addFile(AddFileRequest)}. NOTE: this is system service inerface, clients are not
12+
* supposed to inject it into their app services and call it directly.
1213
*/
1314
@Service(FileStreamer.NAMESPACE)
1415
public interface FileStreamer {

0 commit comments

Comments
 (0)