use of software.amazon.awssdk.crt.io.SocketOptions in project aws-iot-device-sdk-java-v2 by aws.
the class IpcServerTests method testIpcServerAuthNUnset.
@Test
public void testIpcServerAuthNUnset() {
final int port = randomPort();
final Set<String> OPERATION_SET = new HashSet<>();
OPERATION_SET.add("dummyOperationName");
final EventStreamRPCServiceHandler handler = new EventStreamRPCServiceHandler() {
@Override
protected EventStreamRPCServiceModel getServiceModel() {
return new EventStreamRPCServiceModel() {
@Override
public String getServiceName() {
return "TestService";
}
@Override
public Collection<String> getAllOperations() {
return new HashSet<>();
}
@Override
protected Optional<Class<? extends EventStreamJsonMessage>> getServiceClassType(String applicationModelType) {
return Optional.empty();
}
@Override
public OperationModelContext getOperationModelContext(String operationName) {
return null;
}
};
}
@Override
public Function<OperationContinuationHandlerContext, ? extends ServerConnectionContinuationHandler> getOperationHandler(String operationName) {
return null;
}
@Override
public boolean hasHandlerForOperation(String operation) {
return true;
}
};
// missing handler.setAuthenticationHandler(TestAuthNZHandlers.getAuthNHandler());
handler.setAuthorizationHandler(TestAuthNZHandlers.getAuthZHandler());
try (final EventLoopGroup elGroup = new EventLoopGroup(1);
SocketOptions socketOptions = new SocketOptions()) {
socketOptions.connectTimeoutMs = 3000;
socketOptions.domain = SocketOptions.SocketDomain.IPv4;
socketOptions.type = SocketOptions.SocketType.STREAM;
try (final IpcServer ipcServer = new IpcServer(elGroup, socketOptions, null, "127.0.0.1", port, handler)) {
Assertions.assertThrows(InvalidServiceConfigurationException.class, () -> {
ipcServer.runServer();
});
}
}
CrtResource.waitForNoResources();
}
use of software.amazon.awssdk.crt.io.SocketOptions in project aws-iot-device-sdk-java-v2 by aws.
the class IpcServerTests method testIpcServerAuthZUnset.
@Test
public void testIpcServerAuthZUnset() {
final int port = randomPort();
final Set<String> OPERATION_SET = new HashSet<>();
OPERATION_SET.add("dummyOperationName");
final EventStreamRPCServiceHandler handler = new EventStreamRPCServiceHandler() {
@Override
protected EventStreamRPCServiceModel getServiceModel() {
return new EventStreamRPCServiceModel() {
@Override
public String getServiceName() {
return "TestService";
}
@Override
public Collection<String> getAllOperations() {
return new HashSet<>();
}
@Override
protected Optional<Class<? extends EventStreamJsonMessage>> getServiceClassType(String applicationModelType) {
return Optional.empty();
}
@Override
public OperationModelContext getOperationModelContext(String operationName) {
return null;
}
};
}
@Override
public Function<OperationContinuationHandlerContext, ? extends ServerConnectionContinuationHandler> getOperationHandler(String operationName) {
return null;
}
@Override
public boolean hasHandlerForOperation(String operation) {
return true;
}
};
handler.setAuthenticationHandler(TestAuthNZHandlers.getAuthNHandler());
try (final EventLoopGroup elGroup = new EventLoopGroup(1);
SocketOptions socketOptions = new SocketOptions()) {
socketOptions.connectTimeoutMs = 3000;
socketOptions.domain = SocketOptions.SocketDomain.IPv4;
socketOptions.type = SocketOptions.SocketType.STREAM;
try (final IpcServer ipcServer = new IpcServer(elGroup, socketOptions, null, "127.0.0.1", port, handler)) {
Assertions.assertThrows(InvalidServiceConfigurationException.class, () -> {
ipcServer.runServer();
});
}
}
CrtResource.waitForNoResources();
}
use of software.amazon.awssdk.crt.io.SocketOptions in project aws-greengrass-cli by aws-greengrass.
the class NucleusAdapterIpcClientImpl method getIpcClient.
private GreengrassCoreIPCClient getIpcClient() {
if (ipcClient != null) {
return ipcClient;
}
String ggcRootPath = getGgcRoot();
try {
Map<String, String> ipcInfoMap = OBJECT_MAPPER.readValue(loadCliIpcInfo(ggcRootPath), HashMap.class);
String domainSocketPath = ipcInfoMap.get(DOMAIN_SOCKET_PATH);
if (Files.exists(Paths.get(IPC_SERVER_SOCKET_SYMLINK), LinkOption.NOFOLLOW_LINKS)) {
Files.delete(Paths.get(IPC_SERVER_SOCKET_SYMLINK));
}
boolean symlinkCreated = false;
// Only symlink when the absolute path would overflow
if (Paths.get(domainSocketPath).toString().length() >= 108) {
try {
Files.createSymbolicLink(Paths.get(IPC_SERVER_SOCKET_SYMLINK), Paths.get(domainSocketPath));
symlinkCreated = true;
} catch (IOException e) {
// Symlink not created, ignoring and using absolute path
}
}
String token = ipcInfoMap.get(CLI_AUTH_TOKEN);
socketOptions = new SocketOptions();
// timeout for establishing the connection
socketOptions.connectTimeoutMs = 3000;
socketOptions.domain = SocketOptions.SocketDomain.LOCAL;
socketOptions.type = SocketOptions.SocketType.STREAM;
clientConnection = connectToGGCOverEventStreamIPC(socketOptions, token, symlinkCreated ? IPC_SERVER_SOCKET_SYMLINK : domainSocketPath);
ipcClient = new GreengrassCoreIPCClient(clientConnection);
return ipcClient;
} catch (Exception e) {
throw new RuntimeException("Unable to create ipc client", e);
}
}
use of software.amazon.awssdk.crt.io.SocketOptions in project aws-iot-device-sdk-java-v2 by aws.
the class EventStreamRPCClientTests method testConnectionEstablished.
@Test
public void testConnectionEstablished() {
final int port = randomPort();
// below class is generated and just gets instantiated for what it is
final TestIpcServiceHandler service = new TestIpcServiceHandler(false, request -> request, EventStreamJsonMessage.class, EventStreamJsonMessage.class, EventStreamJsonMessage.class, EventStreamJsonMessage.class);
// handlers aren't relevant since no request will be made
service.setAuthenticationHandler(TestAuthNZHandlers.getAuthNHandler());
service.setAuthorizationHandler(TestAuthNZHandlers.getAuthZHandler());
final CompletableFuture<Void> disconnectFuture = new CompletableFuture<>();
final CompletableFuture<Void> connectedFuture = new CompletableFuture<>();
try (final EventLoopGroup elGroup = new EventLoopGroup(1);
final HostResolver hostResolver = new HostResolver(elGroup, 64);
final ClientBootstrap clientBootstrap = new ClientBootstrap(elGroup, hostResolver);
SocketOptions socketOptions = new SocketOptions()) {
socketOptions.connectTimeoutMs = 3000;
socketOptions.domain = SocketOptions.SocketDomain.IPv4;
socketOptions.type = SocketOptions.SocketType.STREAM;
try (final RpcServer ipcServer = new RpcServer(elGroup, socketOptions, null, "127.0.0.1", port, service)) {
ipcServer.runServer();
final EventStreamRPCConnectionConfig config = new EventStreamRPCConnectionConfig(clientBootstrap, elGroup, socketOptions, null, "127.0.0.1", port, () -> {
final List<Header> headers = new LinkedList<>();
headers.add(Header.createHeader("client-name", "accepted.foo"));
return CompletableFuture.completedFuture(new MessageAmendInfo(headers, null));
});
final EventStreamRPCConnection connection = new EventStreamRPCConnection(config);
final EventStreamRPCConnection.LifecycleHandler lifecycleHandler = new EventStreamRPCConnection.LifecycleHandler() {
@Override
public void onConnect() {
connectedFuture.complete(null);
}
@Override
public void onDisconnect(int errorCode) {
disconnectFuture.complete(null);
}
@Override
public boolean onError(Throwable t) {
if (!connectedFuture.isDone()) {
connectedFuture.completeExceptionally(t);
}
if (!disconnectFuture.isDone()) {
disconnectFuture.completeExceptionally(t);
}
return true;
}
};
final CompletableFuture<Void> initialConnect = connection.connect(lifecycleHandler);
// highly likely above line is establishing connection
Assertions.assertThrows(IllegalStateException.class, () -> connection.connect(lifecycleHandler));
connectedFuture.get(2, TimeUnit.SECONDS);
Assertions.assertTrue(initialConnect.isDone() && !initialConnect.isCompletedExceptionally());
// connection is fully established
Assertions.assertThrows(IllegalStateException.class, () -> connection.connect(lifecycleHandler));
connection.disconnect();
} catch (ExecutionException | TimeoutException | InterruptedException e) {
throw new RuntimeException(e);
} finally {
try {
disconnectFuture.get(5, TimeUnit.SECONDS);
} catch (ExecutionException | TimeoutException | InterruptedException e) {
throw new RuntimeException(e);
}
}
}
CrtResource.waitForNoResources();
}
use of software.amazon.awssdk.crt.io.SocketOptions in project aws-iot-device-sdk-java-v2 by aws.
the class EventStreamRPCClientTests method testConnectionAccessDenied.
@Test
public void testConnectionAccessDenied() {
final int port = randomPort();
// below class is generated and just gets instantiated for what it is
final TestIpcServiceHandler service = new TestIpcServiceHandler(false, request -> request, EventStreamJsonMessage.class, EventStreamJsonMessage.class, EventStreamJsonMessage.class, EventStreamJsonMessage.class);
// handlers aren't relevant since no request will be made
service.setAuthenticationHandler(TestAuthNZHandlers.getAuthNHandler());
service.setAuthorizationHandler(TestAuthNZHandlers.getAuthZHandler());
final Semaphore semaphore = new Semaphore(1);
try (final EventLoopGroup elGroup = new EventLoopGroup(1);
final HostResolver hostResolver = new HostResolver(elGroup, 64);
final ClientBootstrap clientBootstrap = new ClientBootstrap(elGroup, hostResolver);
SocketOptions socketOptions = new SocketOptions()) {
socketOptions.connectTimeoutMs = 3000;
socketOptions.domain = SocketOptions.SocketDomain.IPv4;
socketOptions.type = SocketOptions.SocketType.STREAM;
try (final RpcServer ipcServer = new RpcServer(elGroup, socketOptions, null, "127.0.0.1", port, service)) {
ipcServer.runServer();
semaphore.acquire();
final EventStreamRPCConnectionConfig config = new EventStreamRPCConnectionConfig(clientBootstrap, elGroup, socketOptions, null, "127.0.0.1", port, () -> {
final List<Header> headers = new LinkedList<>();
headers.add(Header.createHeader("client-name", "rejected.foo"));
return CompletableFuture.completedFuture(new MessageAmendInfo(headers, null));
});
try (final EventStreamRPCConnection connection = new EventStreamRPCConnection(config)) {
final CompletableFuture<Void> initialConnect = connection.connect(new EventStreamRPCConnection.LifecycleHandler() {
@Override
public void onConnect() {
Assertions.fail("Full connection expected to be rejected");
semaphore.release();
}
@Override
public void onDisconnect(int errorCode) {
Assertions.fail("Disconnect callback should not be invoked on access denied");
}
@Override
public boolean onError(Throwable t) {
Assertions.assertEquals(t.getClass(), AccessDeniedException.class, "Expected access denied exception type!");
semaphore.release();
return false;
}
});
semaphore.acquire();
Assertions.assertTrue(initialConnect.isDone());
try {
initialConnect.get();
} catch (ExecutionException e) {
Assertions.assertTrue(e.getCause() instanceof AccessDeniedException);
}
}
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
CrtResource.waitForNoResources();
}
Aggregations