use of com.sequenceiq.authorization.info.model.RightV4 in project cloudbreak by hortonworks.
the class UtilAuthorizationServiceTest method testHasRightsOnResources.
@Test
public void testHasRightsOnResources() {
CheckRightOnResourcesV4Request request = new CheckRightOnResourcesV4Request();
request.setRight(RightV4.DH_DESCRIBE);
request.setResourceCrns(List.of(DATAHUB_CRN));
when(entitlementService.listFilteringEnabled(eq("1234"))).thenReturn(true);
doAnswer(invocation -> ((List<String>) invocation.getArgument(1)).stream().map(crn -> new Resource(crn, Optional.empty())).collect(Collectors.toList())).when(authorizationResourceProvider).findResources(anyString(), anyList());
doAnswer(invocation -> ((Function<Predicate<String>, List<CheckResourceRightV4Response>>) invocation.getArgument(3)).apply(crn -> true)).when(resourceFilteringService).filter(any(), any(), anyList(), any());
CheckRightOnResourcesV4Response response = ThreadBasedUserCrnProvider.doAs(USER_CRN, () -> underTest.checkRightOnResources(request));
assertEquals(RightV4.DH_DESCRIBE, response.getRight());
assertEquals(1, response.getResponses().size());
CheckResourceRightV4Response checkResourceRightV4Response = response.getResponses().get(0);
assertTrue(checkResourceRightV4Response.isResult());
assertEquals(DATAHUB_CRN, checkResourceRightV4Response.getResourceCrn());
}
use of com.sequenceiq.authorization.info.model.RightV4 in project cloudbreak by hortonworks.
the class UtilAuthorizationServiceTest method testCheckResourceRight.
@Test
public void testCheckResourceRight() {
AuthorizationProto.RightCheck dhStartRightCheck = AuthorizationProto.RightCheck.newBuilder().setRight(RightV4.DH_START.getAction().getRight()).setResource("dhCrn").build();
AuthorizationProto.RightCheck dhStartEnvRightCheck = AuthorizationProto.RightCheck.newBuilder().setRight(RightV4.DH_START.getAction().getRight()).setResource("envCrn").build();
AuthorizationProto.RightCheck dhStopRightCheck = AuthorizationProto.RightCheck.newBuilder().setRight(RightV4.DH_STOP.getAction().getRight()).setResource("dhCrn").build();
AuthorizationProto.RightCheck dhStopEnvRightCheck = AuthorizationProto.RightCheck.newBuilder().setRight(RightV4.DH_STOP.getAction().getRight()).setResource("envCrn").build();
AuthorizationProto.RightCheck dlRepairRightCheck = AuthorizationProto.RightCheck.newBuilder().setRight(RightV4.SDX_REPAIR.getAction().getRight()).setResource("dlCrn").build();
AuthorizationProto.RightCheck dlRepairEnvRightCheck = AuthorizationProto.RightCheck.newBuilder().setRight(RightV4.SDX_REPAIR.getAction().getRight()).setResource("env2crn").build();
AuthorizationProto.RightCheck dlUpgradeRightCheck = AuthorizationProto.RightCheck.newBuilder().setRight(RightV4.SDX_UPGRADE.getAction().getRight()).setResource("dlCrn").build();
AuthorizationProto.RightCheck dlUpgradeEnvRightCheck = AuthorizationProto.RightCheck.newBuilder().setRight(RightV4.SDX_UPGRADE.getAction().getRight()).setResource("env2crn").build();
AuthorizationProto.RightCheck dlRecoveryRightCheck = AuthorizationProto.RightCheck.newBuilder().setRight(RightV4.SDX_RECOVER.getAction().getRight()).setResource("dlCrn").build();
AuthorizationProto.RightCheck dlRecoveryEnvRightCheck = AuthorizationProto.RightCheck.newBuilder().setRight(RightV4.SDX_RECOVER.getAction().getRight()).setResource("env2crn").build();
when(grpcUmsClient.hasRights(anyString(), eq(Arrays.asList(dhStartRightCheck, dhStartEnvRightCheck, dhStopRightCheck, dhStopEnvRightCheck, dlRepairRightCheck, dlRepairEnvRightCheck, dlUpgradeRightCheck, dlUpgradeEnvRightCheck, dlRecoveryRightCheck, dlRecoveryEnvRightCheck)), any(), any())).thenReturn(Lists.newArrayList(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE, Boolean.TRUE, Boolean.FALSE, Boolean.TRUE, Boolean.TRUE, Boolean.FALSE, Boolean.TRUE, Boolean.FALSE));
when(resourceCrnAthorizationFactory.calcAuthorization(eq("dhCrn"), eq(RightV4.DH_START.getAction()))).thenReturn(Optional.of(new HasRightOnAny(RightV4.DH_START.getAction(), Arrays.asList("dhCrn", "envCrn"))));
when(resourceCrnAthorizationFactory.calcAuthorization(eq("dhCrn"), eq(RightV4.DH_STOP.getAction()))).thenReturn(Optional.of(new HasRightOnAny(RightV4.DH_STOP.getAction(), Arrays.asList("dhCrn", "envCrn"))));
when(resourceCrnAthorizationFactory.calcAuthorization(eq("dlCrn"), eq(RightV4.SDX_REPAIR.getAction()))).thenReturn(Optional.of(new HasRightOnAny(RightV4.SDX_REPAIR.getAction(), Arrays.asList("dlCrn", "env2crn"))));
when(resourceCrnAthorizationFactory.calcAuthorization(eq("dlCrn"), eq(RightV4.SDX_UPGRADE.getAction()))).thenReturn(Optional.of(new HasRightOnAny(RightV4.SDX_UPGRADE.getAction(), Arrays.asList("dlCrn", "env2crn"))));
when(resourceCrnAthorizationFactory.calcAuthorization(eq("dlCrn"), eq(RightV4.SDX_RECOVER.getAction()))).thenReturn(Optional.of(new HasRightOnAny(RightV4.SDX_RECOVER.getAction(), Arrays.asList("dlCrn", "env2crn"))));
CheckResourceRightsV4Request rightReq = new CheckResourceRightsV4Request();
rightReq.setResourceRights(Lists.newArrayList(createResourceRightV4("dhCrn", RightV4.DH_START, RightV4.DH_STOP), createResourceRightV4("dlCrn", RightV4.SDX_REPAIR, RightV4.SDX_UPGRADE, RightV4.SDX_RECOVER)));
CheckResourceRightsV4Response rightResult = ThreadBasedUserCrnProvider.doAs(USER_CRN, () -> underTest.checkRightsOnResources(rightReq));
rightResult.getResponses().forEach(checkResourceRightV4SingleResponse -> checkResourceRightV4SingleResponse.getRights().forEach(checkRightV4SingleResponse -> {
if (checkRightV4SingleResponse.getRight().equals(RightV4.DH_START)) {
assertFalse(checkRightV4SingleResponse.getResult());
}
if (checkRightV4SingleResponse.getRight().equals(RightV4.DH_STOP)) {
assertTrue(checkRightV4SingleResponse.getResult());
}
if (checkRightV4SingleResponse.getRight().equals(RightV4.SDX_REPAIR)) {
assertTrue(checkRightV4SingleResponse.getResult());
}
if (checkRightV4SingleResponse.getRight().equals(RightV4.SDX_UPGRADE)) {
assertTrue(checkRightV4SingleResponse.getResult());
}
if (checkRightV4SingleResponse.getRight().equals(RightV4.SDX_RECOVER)) {
assertTrue(checkRightV4SingleResponse.getResult());
}
}));
verify(grpcUmsClient, times(1)).hasRights(anyString(), any(), any(), any());
}
use of com.sequenceiq.authorization.info.model.RightV4 in project cloudbreak by hortonworks.
the class UtilAuthorizationServiceTest method testCheckResourceRightFallback.
@Test
public void testCheckResourceRightFallback() {
when(grpcUmsClient.hasRights(anyString(), any(), any(), any())).thenReturn(Lists.newLinkedList(Arrays.asList(Boolean.TRUE, Boolean.FALSE, Boolean.FALSE, Boolean.TRUE)));
CheckResourceRightsV4Request rightReq = new CheckResourceRightsV4Request();
List<ResourceRightsV4> resourceRights = new LinkedList<>();
resourceRights.add(createResourceRightV4("envCrn", RightV4.ENV_STOP, RightV4.ENV_START));
resourceRights.add(createResourceRightV4("dhCrn", RightV4.DH_START, RightV4.DH_STOP));
rightReq.setResourceRights(resourceRights);
CheckResourceRightsV4Response rightResult = ThreadBasedUserCrnProvider.doAs(USER_CRN, () -> underTest.checkRightsOnResources(rightReq));
rightResult.getResponses().forEach(checkResourceRightV4SingleResponse -> checkResourceRightV4SingleResponse.getRights().forEach(checkRightV4SingleResponse -> {
if (checkRightV4SingleResponse.getRight().equals(RightV4.ENV_STOP) || checkRightV4SingleResponse.getRight().equals(RightV4.DH_STOP)) {
assertTrue(checkRightV4SingleResponse.getResult());
}
if (checkRightV4SingleResponse.getRight().equals(RightV4.DH_START) || checkRightV4SingleResponse.getRight().equals(RightV4.ENV_START)) {
assertFalse(checkRightV4SingleResponse.getResult());
}
}));
verify(grpcUmsClient, times(1)).hasRights(anyString(), any(), any(), any());
}
use of com.sequenceiq.authorization.info.model.RightV4 in project cloudbreak by hortonworks.
the class UtilAuthorizationService method checkRights.
public CheckRightV4Response checkRights(CheckRightV4Request rightReq) {
String userCrn = ThreadBasedUserCrnProvider.getUserCrn();
List<AuthorizationProto.RightCheck> rightChecks = rightReq.getRights().stream().map(rightV4 -> createRightCheckObject(umsRightProvider.getRight(rightV4.getAction()), null)).collect(Collectors.toList());
List<Boolean> results = grpcUmsClient.hasRights(userCrn, rightChecks, MDCUtils.getRequestId(), regionAwareInternalCrnGeneratorFactory);
return new CheckRightV4Response(rightReq.getRights().stream().map(rightV4 -> new CheckRightV4SingleResponse(rightV4, results.get(rightReq.getRights().indexOf(rightV4)))).collect(Collectors.toList()));
}
use of com.sequenceiq.authorization.info.model.RightV4 in project cloudbreak by hortonworks.
the class UtilAuthorizationService method generateResponse.
private CheckResourceRightsV4Response generateResponse(Multimap<ImmutablePair<String, RightV4>, AuthorizationProto.RightCheck> resourceRightsChecks, Map<AuthorizationProto.RightCheck, Boolean> rightCheckResultMap) {
CheckResourceRightsV4Response response = new CheckResourceRightsV4Response(Lists.newArrayList());
Map<ImmutablePair<String, RightV4>, Boolean> rightCheckResults = new HashMap<>();
for (ImmutablePair<String, RightV4> resourceRight : resourceRightsChecks.keys()) {
Collection<AuthorizationProto.RightCheck> rightChecks = resourceRightsChecks.get(resourceRight);
Boolean hasRight = hasRight(rightCheckResultMap, rightChecks);
rightCheckResults.put(resourceRight, hasRight);
}
rightCheckResults.forEach((rightCheck, result) -> {
if (getResourceRightSingleResponse(response, rightCheck.getLeft()).isEmpty()) {
response.getResponses().add(new CheckResourceRightV4SingleResponse(rightCheck.getLeft(), Lists.newArrayList()));
}
CheckRightV4SingleResponse singleResponse = new CheckRightV4SingleResponse(rightCheck.getRight(), result);
getResourceRightSingleResponse(response, rightCheck.getLeft()).get().getRights().add(singleResponse);
});
return response;
}
Aggregations