use of com.aws.greengrass.authorization.Permission in project aws-greengrass-nucleus by aws-greengrass.
the class LifecycleIPCEventStreamAgentTest method GIVEN_pause_component_request_WHEN_component_not_present_THEN_return_resource_not_found_error.
@Test
@EnabledOnOs(OS.LINUX)
void GIVEN_pause_component_request_WHEN_component_not_present_THEN_return_resource_not_found_error() throws ServiceException, AuthorizationException {
when(kernel.locate(TEST_TARGET_COMPONENT)).thenThrow(new ServiceLoadException("Failed to load"));
when(authorizationHandler.isAuthorized(any(), any())).thenReturn(true);
PauseComponentRequest request = new PauseComponentRequest();
request.setComponentName(TEST_TARGET_COMPONENT);
assertThrows(ResourceNotFoundError.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, never()).getState();
verify(targetComponent, never()).pause();
}
use of com.aws.greengrass.authorization.Permission in project aws-greengrass-nucleus by aws-greengrass.
the class LifecycleIPCEventStreamAgentTest method GIVEN_pause_component_request_WHEN_successful_THEN_return_response.
// Pause component tests
@Test
@EnabledOnOs(OS.LINUX)
void GIVEN_pause_component_request_WHEN_successful_THEN_return_response() throws ServiceException, AuthorizationException {
when(kernel.locate(TEST_TARGET_COMPONENT)).thenReturn(targetComponent);
when(targetComponent.getState()).thenReturn(State.RUNNING);
when(authorizationHandler.isAuthorized(any(), any())).thenReturn(true);
PauseComponentRequest request = new PauseComponentRequest();
request.setComponentName(TEST_TARGET_COMPONENT);
assertNotNull(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();
}
use of com.aws.greengrass.authorization.Permission in project aws-greengrass-nucleus by aws-greengrass.
the class PubSubIPCEventStreamAgentTest method GIVEN_subscribed_to_topic_from_all_sources_WHEN_publish_many_json_message_THEN_publishes_message_inorder.
@Test
void GIVEN_subscribed_to_topic_from_all_sources_WHEN_publish_many_json_message_THEN_publishes_message_inorder() throws InterruptedException, AuthorizationException {
StreamEventPublisher publisher = mock(StreamEventPublisher.class);
Set<Object> set = new HashSet<>();
set.add(publisher);
pubSubIPCEventStreamAgent.getListeners().add(TEST_TOPIC, set);
when(publisher.sendStreamEvent(subscriptionResponseMessageCaptor.capture())).thenReturn(new CompletableFuture());
List<PublishToTopicRequest> publishToTopicRequests = new ArrayList<>();
for (int i = 0; i < 10; i++) {
PublishToTopicRequest publishToTopicRequest = new PublishToTopicRequest();
publishToTopicRequest.setTopic(TEST_TOPIC);
PublishMessage publishMessage = new PublishMessage();
JsonMessage jsonMessage = new JsonMessage();
Map<String, Object> message = new HashMap<>();
message.putIfAbsent("SomeKey", i);
jsonMessage.setMessage(message);
publishMessage.setJsonMessage(jsonMessage);
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(), is(TEST_TOPIC));
TimeUnit.SECONDS.sleep(2);
assertNotNull(subscriptionResponseMessageCaptor.getAllValues());
assertEquals(10, subscriptionResponseMessageCaptor.getAllValues().size());
int i = 0;
for (SubscriptionResponseMessage responseMessage : subscriptionResponseMessageCaptor.getAllValues()) {
assertNotNull(responseMessage.getJsonMessage());
assertNull(responseMessage.getBinaryMessage());
assertThat(responseMessage.getJsonMessage().getMessage(), IsMapContaining.hasEntry("SomeKey", i));
assertEquals(TEST_TOPIC, responseMessage.getJsonMessage().getEventTopic());
i++;
}
}
}
use of com.aws.greengrass.authorization.Permission in project aws-greengrass-nucleus by aws-greengrass.
the class PubSubIPCEventStreamAgentTest method GIVEN_subscribed_to_topic_from_all_sources_WHEN_publish_binary_message_THEN_publishes_message.
@Test
void GIVEN_subscribed_to_topic_from_all_sources_WHEN_publish_binary_message_THEN_publishes_message() throws InterruptedException, AuthorizationException {
StreamEventPublisher publisher = mock(StreamEventPublisher.class);
Set<Object> set = new HashSet<>();
set.add(publisher);
pubSubIPCEventStreamAgent.getListeners().add(TEST_TOPIC, set);
when(publisher.sendStreamEvent(subscriptionResponseMessageCaptor.capture())).thenReturn(new CompletableFuture());
PublishToTopicRequest publishToTopicRequest = new PublishToTopicRequest();
publishToTopicRequest.setTopic(TEST_TOPIC);
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(), is(TEST_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_TOPIC, message.getBinaryMessage().getEventTopic());
}
}
use of com.aws.greengrass.authorization.Permission in project aws-greengrass-nucleus by aws-greengrass.
the class PubSubIPCEventStreamAgentTest method GIVEN_subscribed_to_topic_from_all_sources_WHEN_publish_json_message_THEN_publishes_message.
@Test
void GIVEN_subscribed_to_topic_from_all_sources_WHEN_publish_json_message_THEN_publishes_message() throws InterruptedException, AuthorizationException {
StreamEventPublisher publisher = mock(StreamEventPublisher.class);
Set<Object> set = new HashSet<>();
set.add(publisher);
pubSubIPCEventStreamAgent.getListeners().add(TEST_TOPIC, set);
when(publisher.sendStreamEvent(subscriptionResponseMessageCaptor.capture())).thenReturn(new CompletableFuture());
PublishToTopicRequest publishToTopicRequest = new PublishToTopicRequest();
publishToTopicRequest.setTopic(TEST_TOPIC);
PublishMessage publishMessage = new PublishMessage();
JsonMessage jsonMessage = new JsonMessage();
Map<String, Object> message = new HashMap<>();
message.putIfAbsent("SomeKey", "SomValue");
jsonMessage.setMessage(message);
publishMessage.setJsonMessage(jsonMessage);
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(), is(TEST_TOPIC));
TimeUnit.SECONDS.sleep(2);
assertNotNull(subscriptionResponseMessageCaptor.getValue());
SubscriptionResponseMessage responseMessage = subscriptionResponseMessageCaptor.getValue();
assertNotNull(responseMessage.getJsonMessage());
assertNull(responseMessage.getBinaryMessage());
assertThat(responseMessage.getJsonMessage().getMessage(), IsMapContaining.hasEntry("SomeKey", "SomValue"));
assertEquals(TEST_TOPIC, responseMessage.getJsonMessage().getEventTopic());
}
}
Aggregations