ServiceAccountsService { public ResponseEntity<ADServiceAccountObjects> getADServiceAccounts(String token, UserDetails userDetails, String UserPrincipalName, boolean excludeOnboarded) { AndFilter andFilter = new AndFilter(); andFilter.and(new LikeFilter("userPrincipalName", UserPrincipalName+"*")); andFilter.and(new EqualsFilter("objectClass", "user")); andFilter.and(new NotFilter(new EqualsFilter("CN", adMasterServiveAccount))); if (excludeOnboarded) { ResponseEntity<String> responseEntity = getOnboardedServiceAccounts(token, userDetails); if (HttpStatus.OK.equals(responseEntity.getStatusCode())) { String response = responseEntity.getBody(); List<String> onboardedSvcAccs = new ArrayList<String>(); try { Map<String, Object> requestParams = new ObjectMapper().readValue(response, new TypeReference<Map<String, Object>>(){}); onboardedSvcAccs = (ArrayList<String>) requestParams.get("keys"); } catch(Exception ex) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "getADServiceAccounts"). put(LogMessage.MESSAGE, String.format("There are no service accounts currently onboarded or error in retrieving onboarded service accounts")). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); } for (String onboardedSvcAcc: onboardedSvcAccs) { andFilter.and(new NotFilter(new EqualsFilter("CN", onboardedSvcAcc))); } } } List<ADServiceAccount> allServiceAccounts = getADServiceAccounts(andFilter); if (allServiceAccounts != null && !allServiceAccounts.isEmpty()) { List<String> ownerlist = allServiceAccounts.stream().map(m -> m.getManagedBy().getUserName()).collect(Collectors.toList()); ownerlist = new ArrayList<>(new HashSet<>(ownerlist)); ownerlist.removeAll(Collections.singleton(TVaultConstants.EMPTY)); ownerlist.removeAll(Collections.singleton(null)); if (!ownerlist.isEmpty()) { StringBuffer filterQuery = new StringBuffer(); filterQuery.append("(&(objectclass=user)(|"); for (String owner : ownerlist) { filterQuery.append("(cn=" + owner + ")"); } filterQuery.append("))"); List<ADUserAccount> managedServiceAccounts = getServiceAccountManagerDetails(filterQuery.toString()); for (ADServiceAccount adServiceAccount : allServiceAccounts) { if (!StringUtils.isEmpty(adServiceAccount.getManagedBy().getUserName())) { List<ADUserAccount> adUserAccount = managedServiceAccounts.stream().filter(f -> (f.getUserName()!=null && f.getUserName().equalsIgnoreCase(adServiceAccount.getManagedBy().getUserName()))).collect(Collectors.toList()); if (!adUserAccount.isEmpty()) { adServiceAccount.setManagedBy(adUserAccount.get(0)); } } } } } ADServiceAccountObjects adServiceAccountObjects = new ADServiceAccountObjects(); ADServiceAccountObjectsList adServiceAccountObjectsList = new ADServiceAccountObjectsList(); Object[] values = new Object[] {}; if (allServiceAccounts !=null && !CollectionUtils.isEmpty(allServiceAccounts)) { values = allServiceAccounts.toArray(new ADServiceAccount[allServiceAccounts.size()]); } adServiceAccountObjectsList.setValues(values); adServiceAccountObjects.setData(adServiceAccountObjectsList); return ResponseEntity.status(HttpStatus.OK).body(adServiceAccountObjects); }  ResponseEntity<ADServiceAccountObjects> getADServiceAccounts(String token, UserDetails userDetails, String UserPrincipalName, boolean excludeOnboarded); ResponseEntity<String> onboardServiceAccount(String token, ServiceAccount serviceAccount, UserDetails userDetails); ResponseEntity<String> offboardServiceAccount(String token, OnboardedServiceAccount serviceAccount, UserDetails userDetails); ResponseEntity<String> addUserToServiceAccount(String token, ServiceAccountUser serviceAccountUser, UserDetails userDetails, boolean isPartOfSvcAccOnboard); ResponseEntity<String> removeUserFromServiceAccount(String token, ServiceAccountUser serviceAccountUser, UserDetails userDetails); ResponseEntity<String> resetSvcAccPassword(String token, String svcAccName, UserDetails userDetails); ResponseEntity<String> readSvcAccPassword(String token, String svcAccName, UserDetails userDetails); ResponseEntity<String> getOnboarderdServiceAccount(String token, String svcAccName, UserDetails userDetails); boolean canAddOrRemoveUser(UserDetails userDetails, ServiceAccountUser serviceAccountUser, String action); ResponseEntity<String> getOnboardedServiceAccounts(String token,  UserDetails userDetails); boolean hasAddOrRemovePermission(UserDetails userDetails, String serviceAccount, String token); boolean hasAddUserPermission(UserDetails userDetails, String serviceAccount, String token, boolean isPartOfSvcAccOnboard); static boolean isSvcaccPermissionInputValid(String access); ResponseEntity<String> addGroupToServiceAccount(String token, ServiceAccountGroup serviceAccountGroup, UserDetails userDetails); ResponseEntity<String> removeGroupFromServiceAccount(String token, ServiceAccountGroup serviceAccountGroup, UserDetails userDetails); ResponseEntity<String> associateApproletoSvcAcc(UserDetails userDetails, String token, ServiceAccountApprole serviceAccountApprole); ResponseEntity<String> getServiceAccountMeta(String token, UserDetails userDetails, String path); ResponseEntity<String> removeApproleFromSvcAcc(UserDetails userDetails, String token, ServiceAccountApprole serviceAccountApprole); ResponseEntity<String> addAwsRoleToSvcacc(UserDetails userDetails, String token, ServiceAccountAWSRole serviceAccountAWSRole); ResponseEntity<String> removeAWSRoleFromSvcacc(UserDetails userDetails, String token, ServiceAccountAWSRole serviceAccountAWSRole); ResponseEntity<String> createAWSRole(UserDetails userDetails, String token, AWSLoginRole awsLoginRole); ResponseEntity<String> createIAMRole(UserDetails userDetails, String token, AWSIAMRole awsiamRole); ResponseEntity<String> updateOnboardedServiceAccount(String token, ServiceAccount serviceAccount, UserDetails userDetails); ResponseEntity<String> transferSvcAccountOwner(UserDetails userDetails, String token, String svcAccName); ResponseEntity<String> getServiceAccounts(UserDetails userDetails, String userToken);  }
@Test public void test_getADServiceAccounts_excluded_success() { UserDetails userDetails = getMockUser(true); String token = userDetails.getClientToken(); String userPrincipalName = "test"; boolean excludeOnboarded = true; String encodedFilter = "(&(userPrincipalName=test*)(objectClass=user)(!(CN=null))(!(CN=testacc02)))"; List<ADServiceAccount> allServiceAccounts = generateADSerivceAccounts(); ResponseEntity<ADServiceAccountObjects> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body(generateADServiceAccountObjects(allServiceAccounts)); List<ADUserAccount> list = new ArrayList<>(); ADUserAccount adUserAccount = new ADUserAccount(); adUserAccount.setUserId("user.user11"); adUserAccount.setUserName("user11"); adUserAccount.setDisplayName("user user11"); adUserAccount.setGivenName("user11"); adUserAccount.setUserEmail("user11@abc.com"); list.add(adUserAccount); ReflectionTestUtils.setField(serviceAccountsService, "ldapTemplate", ldapTemplate); when(ldapTemplate.search(Mockito.anyString(), Mockito.anyString(), Mockito.any(AttributesMapper.class))).thenReturn(list); ReflectionTestUtils.setField(serviceAccountsService, "adUserLdapTemplate", ldapTemplate); when(ldapTemplate.search(Mockito.anyString(), Mockito.eq(encodedFilter), Mockito.any(AttributesMapper.class))).thenReturn(allServiceAccounts); Response response = getMockResponse(HttpStatus.OK, true, "{\"keys\":[\"testacc02\"]}"); when(reqProcessor.process("/ad/serviceaccount/onboardedlist","{}",token)).thenReturn(response); ResponseEntity<ADServiceAccountObjects> responseEntity = serviceAccountsService.getADServiceAccounts(token, userDetails, userPrincipalName, excludeOnboarded); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(1, responseEntity.getBody().getData().getValues().length); }
@Test public void test_getADServiceAccounts_excluded_success_NoResults() { UserDetails userDetails = getMockUser(true); String token = userDetails.getClientToken(); String userPrincipalName = "test"; boolean excludeOnboarded = true; String encodedFilter = "(&(userPrincipalName=test*)(objectClass=user)(!(CN=null))(!(CN=testacc02)))"; List<ADServiceAccount> allServiceAccounts = generateADSerivceAccounts(); allServiceAccounts.add(generateADServiceAccount("testacc02")); ResponseEntity<ADServiceAccountObjects> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body(generateADServiceAccountObjects(allServiceAccounts)); ReflectionTestUtils.setField(serviceAccountsService, "ldapTemplate", ldapTemplate); when(ldapTemplate.search(Mockito.anyString(), Mockito.eq(encodedFilter), Mockito.any(AttributesMapper.class))).thenReturn(allServiceAccounts); Response response = getMockResponse(HttpStatus.OK, true, "{\"keys\":[]}"); when(reqProcessor.process("/ad/serviceaccount/onboardedlist","{}",token)).thenReturn(response); ResponseEntity<ADServiceAccountObjects> responseEntity = serviceAccountsService.getADServiceAccounts(token, userDetails, userPrincipalName, excludeOnboarded); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(0, responseEntity.getBody().getData().getValues().length); }
@Test public void test_getADServiceAccounts_excluded_success_NotFound_Onboarded() { UserDetails userDetails = getMockUser(true); String token = userDetails.getClientToken(); String userPrincipalName = "test"; boolean excludeOnboarded = true; String encodedFilter = "(&(userPrincipalName=test*)(objectClass=user)(!(CN=null)))"; List<ADServiceAccount> allServiceAccounts = generateADSerivceAccounts(); allServiceAccounts.add(generateADServiceAccount("testacc02")); allServiceAccounts.add(generateADServiceAccount("testacc03")); ResponseEntity<ADServiceAccountObjects> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body(generateADServiceAccountObjects(allServiceAccounts)); List<ADUserAccount> list = new ArrayList<>(); ADUserAccount adUserAccount = new ADUserAccount(); adUserAccount.setUserId("user.user11"); adUserAccount.setUserName("user11"); adUserAccount.setDisplayName("user user11"); adUserAccount.setGivenName("user11"); adUserAccount.setUserEmail("user11@abc.com"); list.add(adUserAccount); ReflectionTestUtils.setField(serviceAccountsService, "ldapTemplate", ldapTemplate); when(ldapTemplate.search(Mockito.anyString(), Mockito.anyString(), Mockito.any(AttributesMapper.class))).thenReturn(list); ReflectionTestUtils.setField(serviceAccountsService, "adUserLdapTemplate", ldapTemplate); when(ldapTemplate.search(Mockito.anyString(), Mockito.eq(encodedFilter), Mockito.any(AttributesMapper.class))).thenReturn(allServiceAccounts); Response response = getMockResponse(HttpStatus.NOT_FOUND, true, "{\"keys\":[]}"); when(reqProcessor.process("/ad/serviceaccount/onboardedlist","{}",token)).thenReturn(response); ResponseEntity<ADServiceAccountObjects> responseEntity = serviceAccountsService.getADServiceAccounts(token, userDetails, userPrincipalName, excludeOnboarded); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(3, responseEntity.getBody().getData().getValues().length); }
@Test public void test_getADServiceAccounts_all_successfully_NoAccounts() { UserDetails userDetails = getMockUser(true); String token = userDetails.getClientToken(); String userPrincipalName = "test"; boolean excludeOnboarded = false; List<ADServiceAccount> allServiceAccounts = null; ResponseEntity<ADServiceAccountObjects> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body(generateADServiceAccountObjects(allServiceAccounts)); List<ADUserAccount> list = new ArrayList<>(); ADUserAccount adUserAccount = new ADUserAccount(); adUserAccount.setUserId("user.user11"); adUserAccount.setUserName("user11"); adUserAccount.setDisplayName("user user11"); adUserAccount.setGivenName("user11"); adUserAccount.setUserEmail("user11@abc.com"); list.add(adUserAccount); String encodedFilter = "(&(userPrincipalName=test*)(objectClass=user)(!(CN=null)))"; ReflectionTestUtils.setField(serviceAccountsService, "ldapTemplate", ldapTemplate); when(ldapTemplate.search(Mockito.anyString(), Mockito.anyString(), Mockito.any(AttributesMapper.class))).thenReturn(list); ReflectionTestUtils.setField(serviceAccountsService, "adUserLdapTemplate", ldapTemplate); when(ldapTemplate.search(Mockito.anyString(), Mockito.eq(encodedFilter), Mockito.any(AttributesMapper.class))).thenReturn(null); ResponseEntity<ADServiceAccountObjects> responseEntity = serviceAccountsService.getADServiceAccounts(token, userDetails, userPrincipalName, excludeOnboarded); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(0, responseEntity.getBody().getData().getValues().length); }
@Test public void test_getADServiceAccounts_all_success() { UserDetails userDetails = getMockUser(true); String token = userDetails.getClientToken(); String userPrincipalName = "test"; boolean excludeOnboarded = false; List<ADServiceAccount> allServiceAccounts = generateADSerivceAccounts(); ResponseEntity<ADServiceAccountObjects> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body(generateADServiceAccountObjects(allServiceAccounts)); List<ADUserAccount> list = new ArrayList<>(); ADUserAccount adUserAccount = new ADUserAccount(); adUserAccount.setUserId("user.user11"); adUserAccount.setUserName("user11"); adUserAccount.setDisplayName("user user11"); adUserAccount.setGivenName("user11"); adUserAccount.setUserEmail("user11@abc.com"); list.add(adUserAccount); String encodedFilter = "(&(userPrincipalName=test*)(objectClass=user)(!(CN=null)))"; ReflectionTestUtils.setField(serviceAccountsService, "ldapTemplate", ldapTemplate); when(ldapTemplate.search(Mockito.anyString(), Mockito.anyString(), Mockito.any(AttributesMapper.class))).thenReturn(list); ReflectionTestUtils.setField(serviceAccountsService, "adUserLdapTemplate", ldapTemplate); when(ldapTemplate.search(Mockito.anyString(), Mockito.eq(encodedFilter), Mockito.any(AttributesMapper.class))).thenReturn(allServiceAccounts); ResponseEntity<ADServiceAccountObjects> responseEntity = serviceAccountsService.getADServiceAccounts(token, userDetails, userPrincipalName, excludeOnboarded); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(responseEntityExpected.getBody().getData().getValues()[0].toString(), responseEntity.getBody().getData().getValues()[0].toString()); }