OIDCUtil { public Response updateGroupPolicies(String token, String groupName, List<String> policies, List<String> currentPolicies, String id) { OIDCIdentityGroupRequest oidcIdentityGroupRequest = new OIDCIdentityGroupRequest(); oidcIdentityGroupRequest.setName(groupName); oidcIdentityGroupRequest.setType(TVaultConstants.EXTERNAL_TYPE); Response response = deleteGroupAliasByID(token, id); if (response.getHttpstatus().equals(HttpStatus.NO_CONTENT)) { Response deleteGroupResponse = deleteGroupByName(token, groupName); if (deleteGroupResponse.getHttpstatus().equals(HttpStatus.NO_CONTENT)) { oidcIdentityGroupRequest.setPolicies(policies); String canonicalID = updateIdentityGroupByName(token, oidcIdentityGroupRequest); String mountAccessor = fetchMountAccessorForOidc(token); String ssoToken = getSSOToken(); String objectId = getGroupObjectResponse(ssoToken, groupName); if (!StringUtils.isEmpty(canonicalID) && !StringUtils.isEmpty(mountAccessor) && !StringUtils.isEmpty(objectId)) { GroupAliasRequest groupAliasRequest = new GroupAliasRequest(); groupAliasRequest.setCanonical_id(canonicalID); groupAliasRequest.setMount_accessor(mountAccessor); groupAliasRequest.setName(objectId); return createGroupAlias(token, groupAliasRequest); } } } oidcIdentityGroupRequest.setPolicies(currentPolicies); updateIdentityGroupByName(token, oidcIdentityGroupRequest); response.setHttpstatus(HttpStatus.BAD_REQUEST); return response; } OIDCUtil(); String fetchMountAccessorForOidc(String token); OIDCEntityResponse getEntityLookUpResponse(String authMountResponse); Response updateGroupPolicies(String token, String groupName, List<String> policies,
			List<String> currentPolicies, String id); Response deleteGroupByName(String token, String name); OIDCGroup getIdentityGroupDetails(String groupName, String token); String getSSOToken(); String getGroupObjectResponse(String ssoToken, String groupName); String updateIdentityGroupByName(String token, OIDCIdentityGroupRequest oidcIdentityGroupRequest); Response createGroupAlias(String token, GroupAliasRequest groupAliasRequest); ResponseEntity<OIDCEntityResponse> oidcFetchEntityDetails(String token, String username, UserDetails userDetails); ResponseEntity<OIDCEntityResponse> entityLookUp(String token,
			OIDCLookupEntityRequest oidcLookupEntityRequest); Response deleteGroupAliasByID(String token, String id); Response updateOIDCEntity(List<String> policies, String entityName); Response updateEntityByName(String token, OIDCEntityRequest oidcEntityRequest); void renewUserToken(String token); List<String> getPoliciedFromTokenLookUp(String authMountResponse); List<String> tokenLookUp(String token); Response createEntityAlias(String token, OidcEntityAliasRequest oidcEntityAliasRequest); String getUserName(String email); List<DirectoryGroup> getGroupsFromAAD(String ssoToken, String groupName); String getIdOfTheUser(String accessToken, String userEmail); List<String> getSelfServiceGroupsFromAADById(String accessToken, String userAADId, String userName); static final Logger log; }
@Test public void test_updateGroupPolicies_success() throws Exception { String token = "4EpPYDSfgN2D4Gf7UmNO3nuL"; String id = "12wdsadsad"; Response deleteByIdResponse = getMockResponse(HttpStatus.NO_CONTENT, true, ""); when(reqProcessor.process("/identity/group-alias/id/delete", "{\"id\":\"" + id + "\"}", token)).thenReturn(deleteByIdResponse); String name = "r_vault_demo"; Response deleteResponse = getMockResponse(HttpStatus.NO_CONTENT, true, ""); when(reqProcessor.process("/identity/group/name/delete", "{\"name\":\"" + name + "\"}", token)).thenReturn(deleteResponse); when(httpUtils.getHttpClient()).thenReturn(httpClient); when(httpClient.execute(any())).thenReturn(httpResponse); when(httpResponse.getStatusLine()).thenReturn(statusLine); when(statusLine.getStatusCode()).thenReturn(200); when(httpResponse.getEntity()).thenReturn(mockHttpEntity); when(ControllerUtil.getOidcADLoginUrl()).thenReturn("testurl"); String groupResponseString = "{\"value\": [ {\"id\": \"abcdefg\", \"onPremisesSyncEnabled\":null}], \"access_token\": \"abcd\"}"; ReflectionTestUtils.setField(oidcUtil, "ssoGroupsEndpoint", "testgroupurl"); when(mockHttpEntity.getContent()).thenReturn( new ByteArrayInputStream(groupResponseString.getBytes())).thenAnswer(new Answer() { private int count = 0; public Object answer(InvocationOnMock invocation) { if (count++ == 1) return new ByteArrayInputStream(groupResponseString.getBytes()); return new ByteArrayInputStream(groupResponseString.getBytes()); } }); String dataOutput = "{\"data\":{\"oidc/\":{\"accessor\":\"auth_oidc_8b51f292\",\"config\":{\"default_lease_ttl\":0,\"force_no_cache\":false,\"max_lease_ttl\":0,\"token_type\":\"default-service\"},\"description\":\"\",\"external_entropy_access\":false,\"local\":false,\"options\":null,\"seal_wrap\":false,\"type\":\"oidc\",\"uuid\":\"fbd45cc4-d6b6-8b49-6d1a-d4d931345df9\"}}}"; Response responsemock = getMockResponse(HttpStatus.OK, true, dataOutput); when(reqProcessor.process(eq("/sys/list"),Mockito.any(),eq(token))).thenReturn(responsemock); List<String> currentPolicies = new ArrayList<>(); currentPolicies.add("testpolicy2"); GroupAliasRequest groupAliasRequest = new GroupAliasRequest(); groupAliasRequest.setCanonical_id("canonical_id"); groupAliasRequest.setId("id"); groupAliasRequest.setMount_accessor("mount_accessor"); groupAliasRequest.setName("name"); String jsonStr = JSONUtil.getJSON(groupAliasRequest); when(reqProcessor.process("/identity/group-alias", jsonStr, token)).thenReturn(getMockResponse(HttpStatus.OK, true, "")); String responseJson = "{\"data\":{\"id\": \"canonicalID\"}}"; OIDCIdentityGroupRequest oidcIdentityGroupRequest = new OIDCIdentityGroupRequest(); oidcIdentityGroupRequest.setName("name"); List<String> policies = new ArrayList<>(); policies.add("testpolicy1"); policies.add("safeadmin"); oidcIdentityGroupRequest.setPolicies(policies); Response rsResponse = getMockResponse(HttpStatus.OK, true, responseJson); when(reqProcessor.process("/identity/group/name/update", JSONUtil.getJSON(oidcIdentityGroupRequest), token)).thenReturn(rsResponse); Response expectedResponse = getMockResponse(HttpStatus.OK, true, ""); Response updateResponse = oidcUtil.updateGroupPolicies(token, name, policies, currentPolicies, id); assertEquals(expectedResponse.getHttpstatus(), updateResponse.getHttpstatus()); }
@Test public void test_updateGroupPolicies_failed() throws Exception { String token = "4EpPYDSfgN2D4Gf7UmNO3nuL"; String id = "12wdsadsad"; Response deleteByIdResponse = getMockResponse(HttpStatus.BAD_REQUEST, true, ""); when(reqProcessor.process("/identity/group-alias/id/delete", "{\"id\":\"" + id + "\"}", token)).thenReturn(deleteByIdResponse); String name = "r_vault_demo"; List<String> policies = new ArrayList<>(); policies.add("testpolicy1"); policies.add("safeadmin"); List<String> currentPolicies = new ArrayList<>(); currentPolicies.add("testpolicy2"); String responseJson = "{\"data\":{\"id\": \"canonicalID\"}}"; OIDCIdentityGroupRequest oidcIdentityGroupRequest = new OIDCIdentityGroupRequest(); oidcIdentityGroupRequest.setName("name"); oidcIdentityGroupRequest.setPolicies(policies); Response rsResponse = getMockResponse(HttpStatus.OK, true, responseJson); when(reqProcessor.process("/identity/group/name/update", JSONUtil.getJSON(oidcIdentityGroupRequest), token)).thenReturn(rsResponse); Response expectedResponse = getMockResponse(HttpStatus.BAD_REQUEST, true, ""); Response updateResponse = oidcUtil.updateGroupPolicies(token, name, policies, currentPolicies, id); assertEquals(expectedResponse.getHttpstatus(), updateResponse.getHttpstatus()); }