Search in sources :

Example 16 with SocketOptions

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();
}
Also used : EventLoopGroup(software.amazon.awssdk.crt.io.EventLoopGroup) SocketOptions(software.amazon.awssdk.crt.io.SocketOptions) EventStreamJsonMessage(software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 17 with SocketOptions

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();
}
Also used : EventLoopGroup(software.amazon.awssdk.crt.io.EventLoopGroup) SocketOptions(software.amazon.awssdk.crt.io.SocketOptions) EventStreamJsonMessage(software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 18 with SocketOptions

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);
    }
}
Also used : SocketOptions(software.amazon.awssdk.crt.io.SocketOptions) GreengrassCoreIPCClient(software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClient) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 19 with SocketOptions

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();
}
Also used : TestIpcServiceHandler(software.amazon.awssdk.eventstreamrpc.test.TestIpcServiceHandler) SocketOptions(software.amazon.awssdk.crt.io.SocketOptions) HostResolver(software.amazon.awssdk.crt.io.HostResolver) LinkedList(java.util.LinkedList) CompletableFuture(java.util.concurrent.CompletableFuture) EventLoopGroup(software.amazon.awssdk.crt.io.EventLoopGroup) Header(software.amazon.awssdk.crt.eventstream.Header) ClientBootstrap(software.amazon.awssdk.crt.io.ClientBootstrap) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test)

Example 20 with SocketOptions

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();
}
Also used : AccessDeniedException(software.amazon.awssdk.eventstreamrpc.model.AccessDeniedException) TestIpcServiceHandler(software.amazon.awssdk.eventstreamrpc.test.TestIpcServiceHandler) SocketOptions(software.amazon.awssdk.crt.io.SocketOptions) Semaphore(java.util.concurrent.Semaphore) HostResolver(software.amazon.awssdk.crt.io.HostResolver) LinkedList(java.util.LinkedList) EventLoopGroup(software.amazon.awssdk.crt.io.EventLoopGroup) Header(software.amazon.awssdk.crt.eventstream.Header) ClientBootstrap(software.amazon.awssdk.crt.io.ClientBootstrap) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Aggregations

SocketOptions (software.amazon.awssdk.crt.io.SocketOptions)43 EventLoopGroup (software.amazon.awssdk.crt.io.EventLoopGroup)30 Test (org.junit.jupiter.api.Test)18 ClientBootstrap (software.amazon.awssdk.crt.io.ClientBootstrap)18 ExecutionException (java.util.concurrent.ExecutionException)13 Test (org.junit.Test)11 HostResolver (software.amazon.awssdk.crt.io.HostResolver)11 EventStreamRPCConnection (software.amazon.awssdk.eventstreamrpc.EventStreamRPCConnection)11 ServerBootstrap (software.amazon.awssdk.crt.io.ServerBootstrap)10 List (java.util.List)9 IOException (java.io.IOException)8 GreengrassCoreIPCClient (software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClient)8 ArrayList (java.util.ArrayList)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 TimeoutException (java.util.concurrent.TimeoutException)6 Condition (java.util.concurrent.locks.Condition)6 Lock (java.util.concurrent.locks.Lock)6 ReentrantLock (java.util.concurrent.locks.ReentrantLock)6 Header (software.amazon.awssdk.crt.eventstream.Header)6 HashSet (java.util.HashSet)5