Search in sources :

Example 16 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_external_THEN_return_invalid_error.

@Test
@EnabledOnOs(OS.LINUX)
void GIVEN_resume_component_request_WHEN_component_not_external_THEN_return_invalid_error() throws ServiceException, AuthorizationException {
    GreengrassService mockInternalComponent = mock(GreengrassService.class);
    when(kernel.locate(TEST_TARGET_COMPONENT)).thenReturn(mockInternalComponent);
    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, never()).isPaused();
    verify(targetComponent, never()).resume();
}
Also used : GreengrassService(com.aws.greengrass.lifecyclemanager.GreengrassService) 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 17 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_present_THEN_return_resource_not_found_error.

@Test
@EnabledOnOs(OS.LINUX)
void GIVEN_resume_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);
    ResumeComponentRequest request = new ResumeComponentRequest();
    request.setComponentName(TEST_TARGET_COMPONENT);
    assertThrows(ResourceNotFoundError.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, never()).isPaused();
    verify(targetComponent, never()).resume();
}
Also used : Permission(com.aws.greengrass.authorization.Permission) ServiceLoadException(com.aws.greengrass.lifecyclemanager.exceptions.ServiceLoadException) ResumeComponentRequest(software.amazon.awssdk.aws.greengrass.model.ResumeComponentRequest) EnabledOnOs(org.junit.jupiter.api.condition.EnabledOnOs) Test(org.junit.jupiter.api.Test)

Example 18 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_unauthorized_THEN_return_auth_error.

@Test
@EnabledOnOs(OS.LINUX)
void GIVEN_resume_component_request_WHEN_unauthorized_THEN_return_auth_error() throws AuthorizationException, ServiceException {
    when(authorizationHandler.isAuthorized(any(), any())).thenThrow(new AuthorizationException("Unauthorized"));
    ResumeComponentRequest request = new ResumeComponentRequest();
    request.setComponentName(TEST_TARGET_COMPONENT);
    assertThrows(UnauthorizedError.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, never()).locate(TEST_TARGET_COMPONENT);
    verify(targetComponent, never()).isPaused();
    verify(targetComponent, never()).resume();
}
Also used : AuthorizationException(com.aws.greengrass.authorization.exceptions.AuthorizationException) 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 19 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_component_not_running_THEN_return_invalid_error.

@Test
@EnabledOnOs(OS.LINUX)
void GIVEN_pause_component_request_WHEN_component_not_running_THEN_return_invalid_error() throws ServiceException, AuthorizationException {
    when(kernel.locate(TEST_TARGET_COMPONENT)).thenReturn(targetComponent);
    when(targetComponent.getState()).thenReturn(State.FINISHED);
    when(authorizationHandler.isAuthorized(any(), any())).thenReturn(true);
    PauseComponentRequest request = new PauseComponentRequest();
    request.setComponentName(TEST_TARGET_COMPONENT);
    assertThrows(InvalidArgumentsError.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, never()).pause();
}
Also used : PauseComponentRequest(software.amazon.awssdk.aws.greengrass.model.PauseComponentRequest) Permission(com.aws.greengrass.authorization.Permission) EnabledOnOs(org.junit.jupiter.api.condition.EnabledOnOs) Test(org.junit.jupiter.api.Test)

Example 20 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_successful_THEN_return_response.

// Resume component tests
@Test
@EnabledOnOs(OS.LINUX)
void GIVEN_resume_component_request_WHEN_successful_THEN_return_response() throws AuthorizationException, ServiceException {
    when(kernel.locate(TEST_TARGET_COMPONENT)).thenReturn(targetComponent);
    when(targetComponent.isPaused()).thenReturn(true);
    when(authorizationHandler.isAuthorized(any(), any())).thenReturn(true);
    ResumeComponentRequest request = new ResumeComponentRequest();
    request.setComponentName(TEST_TARGET_COMPONENT);
    assertNotNull(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 : 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