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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations