Skip to content

Commit

Permalink
Enhance auth interfaces (#850)
Browse files Browse the repository at this point in the history
* Added CredentialsCodec
* Disabled unstable - test_remote_node_died_mono_never
* Moved CredentialsSupplier to ServiceTransport
  • Loading branch information
artem-v authored Sep 28, 2024
1 parent 0573b39 commit f56e900
Showing 9 changed files with 113 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package io.scalecube.services.auth;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import reactor.core.Exceptions;

public class CredentialsCodec {

private CredentialsCodec() {
// Do not instantiate
}

/**
* Encodes the given credentials to the given stream.
*
* @param stream stream
* @param credentials credentials
*/
public static void encode(OutputStream stream, Map<String, String> credentials) {
if (credentials == null) {
return;
}
Objects.requireNonNull(stream, "output stream");
try (ObjectOutputStream out = new ObjectOutputStream(stream)) {
// credentials
out.writeInt(credentials.size());
for (Entry<String, String> entry : credentials.entrySet()) {
out.writeUTF(entry.getKey());
out.writeObject(entry.getValue()); // value is nullable
}

out.flush();
} catch (Throwable th) {
throw Exceptions.propagate(th);
}
}

/**
* Encodes the given credentials to a byte array.
*
* @param credentials credentials
* @return byte array representation of credentials
*/
public static byte[] toByteArray(Map<String, String> credentials) {
if (credentials == null || credentials.isEmpty()) {
return new byte[0];
}
ByteArrayOutputStream output = new ByteArrayOutputStream();
encode(output, credentials);
return output.toByteArray();
}

/**
* Decodes the given stream to credentials as {@code Map<String, String>}.
*
* @return credentials
*/
public static Map<String, String> decode(InputStream stream) {
Objects.requireNonNull(stream, "input stream");
try (ObjectInputStream in = new ObjectInputStream(stream)) {
// credentials
int credentialsSize = in.readInt();
Map<String, String> credentials = new HashMap<>(credentialsSize);
for (int i = 0; i < credentialsSize; i++) {
String key = in.readUTF();
String value = (String) in.readObject(); // value is nullable
credentials.put(key, value);
}
return Collections.unmodifiableMap(credentials);
} catch (Throwable th) {
throw Exceptions.propagate(th);
}
}

/**
* Decodes the given byte array to credentials as {@code Map<String, String>}.
*
* @return credentials
*/
public static Map<String, String> decode(byte[] bytes) {
if (bytes == null || bytes.length == 0) {
return Collections.emptyMap();
}
ByteArrayInputStream input = new ByteArrayInputStream(bytes);
return decode(input);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package io.scalecube.services.transport.api;

import io.scalecube.services.ServiceReference;
import io.scalecube.services.registry.api.ServiceRegistry;
import java.util.Map;
import java.util.function.Function;
import reactor.core.publisher.Mono;

public interface ServiceTransport {

@@ -28,4 +32,11 @@ public interface ServiceTransport {

/** Shutdowns transport and release occupied resources. */
void stop();

/**
* Returns credentials for the given {@link ServiceReference}. Credentials are being returned in
* most generic form which is {@code Map<String, String>}.
*/
@FunctionalInterface
interface CredentialsSupplier extends Function<ServiceReference, Mono<Map<String, String>>> {}
}
Original file line number Diff line number Diff line change
@@ -7,9 +7,9 @@
import io.scalecube.services.ServiceInfo;
import io.scalecube.services.api.ServiceMessage;
import io.scalecube.services.auth.Authenticator;
import io.scalecube.services.auth.CredentialsSupplier;
import io.scalecube.services.discovery.ScalecubeServiceDiscovery;
import io.scalecube.services.exceptions.UnauthorizedException;
import io.scalecube.services.transport.api.ServiceTransport.CredentialsSupplier;
import io.scalecube.services.transport.rsocket.RSocketServiceTransport;
import io.scalecube.transport.netty.websocket.WebsocketTransportFactory;
import java.time.Duration;
Original file line number Diff line number Diff line change
@@ -4,9 +4,9 @@
import io.scalecube.services.ServiceEndpoint;
import io.scalecube.services.ServiceInfo;
import io.scalecube.services.auth.Authenticator;
import io.scalecube.services.auth.CredentialsSupplier;
import io.scalecube.services.discovery.ScalecubeServiceDiscovery;
import io.scalecube.services.exceptions.UnauthorizedException;
import io.scalecube.services.transport.api.ServiceTransport.CredentialsSupplier;
import io.scalecube.services.transport.rsocket.RSocketServiceTransport;
import io.scalecube.transport.netty.websocket.WebsocketTransportFactory;
import java.time.Duration;
Original file line number Diff line number Diff line change
@@ -3,9 +3,9 @@
import io.scalecube.services.Microservices;
import io.scalecube.services.ServiceEndpoint;
import io.scalecube.services.auth.Authenticator;
import io.scalecube.services.auth.CredentialsSupplier;
import io.scalecube.services.discovery.ScalecubeServiceDiscovery;
import io.scalecube.services.exceptions.UnauthorizedException;
import io.scalecube.services.transport.api.ServiceTransport.CredentialsSupplier;
import io.scalecube.services.transport.rsocket.RSocketServiceTransport;
import io.scalecube.transport.netty.websocket.WebsocketTransportFactory;
import java.time.Duration;
Original file line number Diff line number Diff line change
@@ -10,14 +10,14 @@
import io.rsocket.util.ByteBufPayload;
import io.scalecube.services.Address;
import io.scalecube.services.ServiceReference;
import io.scalecube.services.auth.CredentialsSupplier;
import io.scalecube.services.exceptions.MessageCodecException;
import io.scalecube.services.exceptions.ServiceException;
import io.scalecube.services.exceptions.UnauthorizedException;
import io.scalecube.services.transport.api.ClientChannel;
import io.scalecube.services.transport.api.ClientTransport;
import io.scalecube.services.transport.api.DataCodec;
import io.scalecube.services.transport.api.HeadersCodec;
import io.scalecube.services.transport.api.ServiceTransport.CredentialsSupplier;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@
import io.netty.util.concurrent.DefaultThreadFactory;
import io.netty.util.concurrent.Future;
import io.scalecube.services.auth.Authenticator;
import io.scalecube.services.auth.CredentialsSupplier;
import io.scalecube.services.exceptions.ConnectionClosedException;
import io.scalecube.services.registry.api.ServiceRegistry;
import io.scalecube.services.transport.api.ClientTransport;
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@
import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import reactor.core.Disposable;
import reactor.core.publisher.Mono;
@@ -78,6 +79,7 @@ public void cleanUp() {
}
}

@Disabled
@Test
public void test_remote_node_died_mono_never() throws Exception {
int batchSize = 1;

0 comments on commit f56e900

Please sign in to comment.