Search in sources :

Example 11 with Permission

use of com.aws.greengrass.authorization.Permission in project aws-greengrass-nucleus by aws-greengrass.

the class PubSubIPCEventStreamAgentTest method GIVEN_subscribed_to_wildcard_topic_from_all_sources_WHEN_publish_many_binary_message_to_subtopics_THEN_publishes_message_and_gets_event_topics_inorder.

@Test
void GIVEN_subscribed_to_wildcard_topic_from_all_sources_WHEN_publish_many_binary_message_to_subtopics_THEN_publishes_message_and_gets_event_topics_inorder() throws InterruptedException, AuthorizationException {
    StreamEventPublisher publisher = mock(StreamEventPublisher.class);
    Set<Object> set = new HashSet<>();
    set.add(publisher);
    pubSubIPCEventStreamAgent.getListeners().add(TEST_WILDCARD_TOPIC, set);
    when(publisher.sendStreamEvent(subscriptionResponseMessageCaptor.capture())).thenReturn(new CompletableFuture());
    List<PublishToTopicRequest> publishToTopicRequests = new ArrayList<>();
    String subTopic = "Test/A/Topic/%d";
    for (int i = 0; i < 10; i++) {
        PublishToTopicRequest publishToTopicRequest = new PublishToTopicRequest();
        String topic1 = String.format(subTopic, i);
        publishToTopicRequest.setTopic(topic1);
        PublishMessage publishMessage = new PublishMessage();
        BinaryMessage binaryMessage = new BinaryMessage();
        binaryMessage.setMessage(String.valueOf(i).getBytes());
        publishMessage.setBinaryMessage(binaryMessage);
        publishToTopicRequest.setPublishMessage(publishMessage);
        publishToTopicRequests.add(publishToTopicRequest);
    }
    try (PubSubIPCEventStreamAgent.PublishToTopicOperationHandler publishToTopicHandler = pubSubIPCEventStreamAgent.getPublishToTopicHandler(mockContext)) {
        for (PublishToTopicRequest publishToTopicRequest : publishToTopicRequests) {
            PublishToTopicResponse publishToTopicResponse = publishToTopicHandler.handleRequest(publishToTopicRequest);
            assertNotNull(publishToTopicResponse);
        }
        verify(authorizationHandler, times(10)).isAuthorized(eq(PUB_SUB_SERVICE_NAME), permissionArgumentCaptor.capture(), eq(ResourceLookupPolicy.MQTT_STYLE));
        Permission capturedPermission = permissionArgumentCaptor.getValue();
        assertThat(capturedPermission.getOperation(), is(GreengrassCoreIPCService.PUBLISH_TO_TOPIC));
        assertThat(capturedPermission.getPrincipal(), is(TEST_SERVICE));
        assertThat(capturedPermission.getResource(), containsString("Test/A/Topic/"));
        TimeUnit.SECONDS.sleep(2);
        assertNotNull(subscriptionResponseMessageCaptor.getAllValues());
        assertEquals(10, subscriptionResponseMessageCaptor.getAllValues().size());
        int i = 0;
        for (SubscriptionResponseMessage responseMessage : subscriptionResponseMessageCaptor.getAllValues()) {
            assertNull(responseMessage.getJsonMessage());
            assertNotNull(responseMessage.getBinaryMessage());
            assertEquals(String.valueOf(i), new String(responseMessage.getBinaryMessage().getMessage()));
            assertEquals(String.format(subTopic, i), responseMessage.getBinaryMessage().getEventTopic());
            i++;
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) BinaryMessage(software.amazon.awssdk.aws.greengrass.model.BinaryMessage) PublishMessage(software.amazon.awssdk.aws.greengrass.model.PublishMessage) CompletableFuture(java.util.concurrent.CompletableFuture) PublishToTopicResponse(software.amazon.awssdk.aws.greengrass.model.PublishToTopicResponse) SubscriptionResponseMessage(software.amazon.awssdk.aws.greengrass.model.SubscriptionResponseMessage) Permission(com.aws.greengrass.authorization.Permission) StreamEventPublisher(software.amazon.awssdk.eventstreamrpc.StreamEventPublisher) PublishToTopicRequest(software.amazon.awssdk.aws.greengrass.model.PublishToTopicRequest) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 12 with Permission

use of com.aws.greengrass.authorization.Permission in project aws-greengrass-nucleus by aws-greengrass.

the class PubSubIPCEventStreamAgentTest method GIVEN_subscribed_to_wildcard_topic_from_all_sources_WHEN_publish_binary_message_to_subtopic_THEN_publishes_message_and_gets_event_topic.

@Test
void GIVEN_subscribed_to_wildcard_topic_from_all_sources_WHEN_publish_binary_message_to_subtopic_THEN_publishes_message_and_gets_event_topic() throws InterruptedException, AuthorizationException {
    StreamEventPublisher publisher = mock(StreamEventPublisher.class);
    Set<Object> set = new HashSet<>();
    set.add(publisher);
    pubSubIPCEventStreamAgent.getListeners().add(TEST_WILDCARD_TOPIC, set);
    when(publisher.sendStreamEvent(subscriptionResponseMessageCaptor.capture())).thenReturn(new CompletableFuture());
    PublishToTopicRequest publishToTopicRequest = new PublishToTopicRequest();
    publishToTopicRequest.setTopic("Test/A/Topic/B/C");
    PublishMessage publishMessage = new PublishMessage();
    BinaryMessage binaryMessage = new BinaryMessage();
    binaryMessage.setMessage("ABCD".getBytes());
    publishMessage.setBinaryMessage(binaryMessage);
    publishToTopicRequest.setPublishMessage(publishMessage);
    try (PubSubIPCEventStreamAgent.PublishToTopicOperationHandler publishToTopicHandler = pubSubIPCEventStreamAgent.getPublishToTopicHandler(mockContext)) {
        PublishToTopicResponse publishToTopicResponse = publishToTopicHandler.handleRequest(publishToTopicRequest);
        assertNotNull(publishToTopicResponse);
        verify(authorizationHandler).isAuthorized(eq(PUB_SUB_SERVICE_NAME), permissionArgumentCaptor.capture(), eq(ResourceLookupPolicy.MQTT_STYLE));
        Permission capturedPermission = permissionArgumentCaptor.getValue();
        assertThat(capturedPermission.getOperation(), is(GreengrassCoreIPCService.PUBLISH_TO_TOPIC));
        assertThat(capturedPermission.getPrincipal(), is(TEST_SERVICE));
        assertThat(capturedPermission.getResource(), containsString("Test/A/Topic/"));
        TimeUnit.SECONDS.sleep(2);
        assertNotNull(subscriptionResponseMessageCaptor.getValue());
        SubscriptionResponseMessage message = subscriptionResponseMessageCaptor.getValue();
        assertNull(message.getJsonMessage());
        assertNotNull(message.getBinaryMessage());
        assertEquals("ABCD", new String(message.getBinaryMessage().getMessage()));
        assertEquals("Test/A/Topic/B/C", message.getBinaryMessage().getEventTopic());
    }
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) BinaryMessage(software.amazon.awssdk.aws.greengrass.model.BinaryMessage) PublishMessage(software.amazon.awssdk.aws.greengrass.model.PublishMessage) CompletableFuture(java.util.concurrent.CompletableFuture) PublishToTopicResponse(software.amazon.awssdk.aws.greengrass.model.PublishToTopicResponse) SubscriptionResponseMessage(software.amazon.awssdk.aws.greengrass.model.SubscriptionResponseMessage) Permission(com.aws.greengrass.authorization.Permission) StreamEventPublisher(software.amazon.awssdk.eventstreamrpc.StreamEventPublisher) PublishToTopicRequest(software.amazon.awssdk.aws.greengrass.model.PublishToTopicRequest) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 13 with Permission

use of com.aws.greengrass.authorization.Permission in project aws-greengrass-nucleus by aws-greengrass.

the class LifecycleIPCEventStreamAgentTest method GIVEN_resume_component_request_WHEN_component_not_paused_THEN_return_invalid_error.

@Test
@EnabledOnOs(OS.LINUX)
void GIVEN_resume_component_request_WHEN_component_not_paused_THEN_return_invalid_error() throws ServiceException, AuthorizationException {
    when(kernel.locate(TEST_TARGET_COMPONENT)).thenReturn(targetComponent);
    when(targetComponent.isPaused()).thenReturn(false);
    when(authorizationHandler.isAuthorized(any(), any())).thenReturn(true);
    ResumeComponentRequest request = new ResumeComponentRequest();
    request.setComponentName(TEST_TARGET_COMPONENT);
    assertThrows(InvalidArgumentsError.class, () -> lifecycleIPCEventStreamAgent.getResumeComponentHandler(mockContext).handleRequest(request));
    ArgumentCaptor<Permission> permissionArg = ArgumentCaptor.forClass(Permission.class);
    verify(authorizationHandler).isAuthorized(eq(LIFECYCLE_SERVICE_NAME), permissionArg.capture());
    Permission permission = permissionArg.getValue();
    assertThat(permission.getOperation(), is(GreengrassCoreIPCService.RESUME_COMPONENT));
    assertThat(permission.getPrincipal(), is(TEST_SERVICE));
    assertThat(permission.getResource(), is(TEST_TARGET_COMPONENT));
    verify(kernel).locate(TEST_TARGET_COMPONENT);
    verify(targetComponent).isPaused();
    verify(targetComponent, never()).resume();
}
Also used : Permission(com.aws.greengrass.authorization.Permission) ResumeComponentRequest(software.amazon.awssdk.aws.greengrass.model.ResumeComponentRequest) EnabledOnOs(org.junit.jupiter.api.condition.EnabledOnOs) Test(org.junit.jupiter.api.Test)

Example 14 with Permission

use of com.aws.greengrass.authorization.Permission in project aws-greengrass-nucleus by aws-greengrass.

the class LifecycleIPCEventStreamAgentTest method GIVEN_pause_component_request_WHEN_failure_THEN_return_service_error.

@Test
@EnabledOnOs(OS.LINUX)
void GIVEN_pause_component_request_WHEN_failure_THEN_return_service_error() throws AuthorizationException, ServiceException {
    when(kernel.locate(TEST_TARGET_COMPONENT)).thenReturn(targetComponent);
    when(targetComponent.getState()).thenReturn(State.RUNNING);
    doThrow(new ServiceException("Failed to pause")).when(targetComponent).pause();
    when(authorizationHandler.isAuthorized(any(), any())).thenReturn(true);
    PauseComponentRequest request = new PauseComponentRequest();
    request.setComponentName(TEST_TARGET_COMPONENT);
    assertThrows(ServiceError.class, () -> lifecycleIPCEventStreamAgent.getPauseComponentHandler(mockContext).handleRequest(request));
    ArgumentCaptor<Permission> permissionArg = ArgumentCaptor.forClass(Permission.class);
    verify(authorizationHandler).isAuthorized(eq(LIFECYCLE_SERVICE_NAME), permissionArg.capture());
    Permission permission = permissionArg.getValue();
    assertThat(permission.getOperation(), is(GreengrassCoreIPCService.PAUSE_COMPONENT));
    assertThat(permission.getPrincipal(), is(TEST_SERVICE));
    assertThat(permission.getResource(), is(TEST_TARGET_COMPONENT));
    verify(kernel).locate(TEST_TARGET_COMPONENT);
    verify(targetComponent).getState();
    verify(targetComponent).pause();
}
Also used : PauseComponentRequest(software.amazon.awssdk.aws.greengrass.model.PauseComponentRequest) ServiceException(com.aws.greengrass.lifecyclemanager.exceptions.ServiceException) Permission(com.aws.greengrass.authorization.Permission) EnabledOnOs(org.junit.jupiter.api.condition.EnabledOnOs) Test(org.junit.jupiter.api.Test)

Example 15 with Permission

use of com.aws.greengrass.authorization.Permission in project aws-greengrass-nucleus by aws-greengrass.

the class LifecycleIPCEventStreamAgentTest method GIVEN_resume_component_request_WHEN_failure_THEN_return_service_error.

@Test
@EnabledOnOs(OS.LINUX)
void GIVEN_resume_component_request_WHEN_failure_THEN_return_service_error() throws AuthorizationException, ServiceException {
    when(kernel.locate(TEST_TARGET_COMPONENT)).thenReturn(targetComponent);
    when(targetComponent.isPaused()).thenReturn(true);
    doThrow(new ServiceException("Failed to resume")).when(targetComponent).resume();
    when(authorizationHandler.isAuthorized(any(), any())).thenReturn(true);
    ResumeComponentRequest request = new ResumeComponentRequest();
    request.setComponentName(TEST_TARGET_COMPONENT);
    assertThrows(ServiceError.class, () -> lifecycleIPCEventStreamAgent.getResumeComponentHandler(mockContext).handleRequest(request));
    ArgumentCaptor<Permission> permissionArg = ArgumentCaptor.forClass(Permission.class);
    verify(authorizationHandler).isAuthorized(eq(LIFECYCLE_SERVICE_NAME), permissionArg.capture());
    Permission permission = permissionArg.getValue();
    assertThat(permission.getOperation(), is(GreengrassCoreIPCService.RESUME_COMPONENT));
    assertThat(permission.getPrincipal(), is(TEST_SERVICE));
    assertThat(permission.getResource(), is(TEST_TARGET_COMPONENT));
    verify(kernel).locate(TEST_TARGET_COMPONENT);
    verify(targetComponent).isPaused();
    verify(targetComponent).resume();
}
Also used : ServiceException(com.aws.greengrass.lifecyclemanager.exceptions.ServiceException) Permission(com.aws.greengrass.authorization.Permission) ResumeComponentRequest(software.amazon.awssdk.aws.greengrass.model.ResumeComponentRequest) EnabledOnOs(org.junit.jupiter.api.condition.EnabledOnOs) Test(org.junit.jupiter.api.Test)

Aggregations

Permission (com.aws.greengrass.authorization.Permission)22 Test (org.junit.jupiter.api.Test)22 EnabledOnOs (org.junit.jupiter.api.condition.EnabledOnOs)12 CompletableFuture (java.util.concurrent.CompletableFuture)9 HashSet (java.util.HashSet)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 PauseComponentRequest (software.amazon.awssdk.aws.greengrass.model.PauseComponentRequest)6 PublishMessage (software.amazon.awssdk.aws.greengrass.model.PublishMessage)6 PublishToTopicRequest (software.amazon.awssdk.aws.greengrass.model.PublishToTopicRequest)6 PublishToTopicResponse (software.amazon.awssdk.aws.greengrass.model.PublishToTopicResponse)6 ResumeComponentRequest (software.amazon.awssdk.aws.greengrass.model.ResumeComponentRequest)6 SubscriptionResponseMessage (software.amazon.awssdk.aws.greengrass.model.SubscriptionResponseMessage)6 StreamEventPublisher (software.amazon.awssdk.eventstreamrpc.StreamEventPublisher)6 BinaryMessage (software.amazon.awssdk.aws.greengrass.model.BinaryMessage)4 GreengrassService (com.aws.greengrass.lifecyclemanager.GreengrassService)3 TestUtils.asyncAssertOnConsumer (com.aws.greengrass.testcommons.testutilities.TestUtils.asyncAssertOnConsumer)3 ArrayList (java.util.ArrayList)3 ExecutionException (java.util.concurrent.ExecutionException)3 Consumer (java.util.function.Consumer)3 GreengrassCoreIPCClient (software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClient)3