@Test public void test_getServiceAccounts_successfully() { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; UserDetails userDetails = getMockUser(false); String [] policies = {"r_users_s1", "w_users_s2", "r_shared_s3", "w_shared_s4", "r_apps_s5", "w_apps_s6", "d_apps_s7", "w_svcacct_test"}; ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body("{\"shared\":[{\"s3\":\"read\"},{\"s4\":\"write\"}],\"users\":[{\"s1\":\"read\"},{\"s2\":\"write\"}],\"svcacct\":[{\"test\":\"read\"}],\"apps\":[{\"s5\":\"read\"},{\"s6\":\"write\"},{\"s7\":\"deny\"}]}"); when(policyUtils.getCurrentPolicies(token, userDetails.getUsername(), userDetails)).thenReturn(policies); when(JSONUtil.getJSON(Mockito.any())).thenReturn("{\"shared\":[{\"s3\":\"read\"},{\"s4\":\"write\"}],\"users\":[{\"s1\":\"read\"},{\"s2\":\"write\"}],\"svcacct\":[{\"test\":\"read\"}],\"apps\":[{\"s5\":\"read\"},{\"s6\":\"write\"},{\"s7\":\"deny\"}]}"); ResponseEntity<String> responseEntity = serviceAccountsService.getServiceAccounts(userDetails, token); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
public ResponseEntity<String> getServiceAccounts(UserDetails userDetails, String userToken) { oidcUtil.renewUserToken(userDetails.getClientToken()); String token = userDetails.getClientToken(); if (!userDetails.isAdmin()) { token = userDetails.getSelfSupportToken(); } String[] policies = policyUtils.getCurrentPolicies(token, userDetails.getUsername(), userDetails); policies = filterPoliciesBasedOnPrecedence(Arrays.asList(policies)); List<Map<String, String>> svcListUsers = new ArrayList<>(); Map<String, List<Map<String, String>>> safeList = new HashMap<>(); if (policies != null) { for (String policy : policies) { Map<String, String> safePolicy = new HashMap<>(); String[] _policies = policy.split("_", -1); if (_policies.length >= 3) { String[] policyName = Arrays.copyOfRange(_policies, 2, _policies.length); String safeName = String.join("_", policyName); String safeType = _policies[1]; if (policy.startsWith("r_")) { safePolicy.put(safeName, "read"); } else if (policy.startsWith("w_")) { safePolicy.put(safeName, "write"); } else if (policy.startsWith("d_")) { safePolicy.put(safeName, "deny"); } if (!safePolicy.isEmpty()) { if (safeType.equals(TVaultConstants.SVC_ACC_PATH_PREFIX)) { svcListUsers.add(safePolicy); } } } } safeList.put(TVaultConstants.SVC_ACC_PATH_PREFIX, svcListUsers); } return ResponseEntity.status(HttpStatus.OK).body(JSONUtil.getJSON(safeList)); }
ServiceAccountsService { public ResponseEntity<String> getServiceAccounts(UserDetails userDetails, String userToken) { oidcUtil.renewUserToken(userDetails.getClientToken()); String token = userDetails.getClientToken(); if (!userDetails.isAdmin()) { token = userDetails.getSelfSupportToken(); } String[] policies = policyUtils.getCurrentPolicies(token, userDetails.getUsername(), userDetails); policies = filterPoliciesBasedOnPrecedence(Arrays.asList(policies)); List<Map<String, String>> svcListUsers = new ArrayList<>(); Map<String, List<Map<String, String>>> safeList = new HashMap<>(); if (policies != null) { for (String policy : policies) { Map<String, String> safePolicy = new HashMap<>(); String[] _policies = policy.split("_", -1); if (_policies.length >= 3) { String[] policyName = Arrays.copyOfRange(_policies, 2, _policies.length); String safeName = String.join("_", policyName); String safeType = _policies[1]; if (policy.startsWith("r_")) { safePolicy.put(safeName, "read"); } else if (policy.startsWith("w_")) { safePolicy.put(safeName, "write"); } else if (policy.startsWith("d_")) { safePolicy.put(safeName, "deny"); } if (!safePolicy.isEmpty()) { if (safeType.equals(TVaultConstants.SVC_ACC_PATH_PREFIX)) { svcListUsers.add(safePolicy); } } } } safeList.put(TVaultConstants.SVC_ACC_PATH_PREFIX, svcListUsers); } return ResponseEntity.status(HttpStatus.OK).body(JSONUtil.getJSON(safeList)); } }
ServiceAccountsService { public ResponseEntity<String> getServiceAccounts(UserDetails userDetails, String userToken) { oidcUtil.renewUserToken(userDetails.getClientToken()); String token = userDetails.getClientToken(); if (!userDetails.isAdmin()) { token = userDetails.getSelfSupportToken(); } String[] policies = policyUtils.getCurrentPolicies(token, userDetails.getUsername(), userDetails); policies = filterPoliciesBasedOnPrecedence(Arrays.asList(policies)); List<Map<String, String>> svcListUsers = new ArrayList<>(); Map<String, List<Map<String, String>>> safeList = new HashMap<>(); if (policies != null) { for (String policy : policies) { Map<String, String> safePolicy = new HashMap<>(); String[] _policies = policy.split("_", -1); if (_policies.length >= 3) { String[] policyName = Arrays.copyOfRange(_policies, 2, _policies.length); String safeName = String.join("_", policyName); String safeType = _policies[1]; if (policy.startsWith("r_")) { safePolicy.put(safeName, "read"); } else if (policy.startsWith("w_")) { safePolicy.put(safeName, "write"); } else if (policy.startsWith("d_")) { safePolicy.put(safeName, "deny"); } if (!safePolicy.isEmpty()) { if (safeType.equals(TVaultConstants.SVC_ACC_PATH_PREFIX)) { svcListUsers.add(safePolicy); } } } } safeList.put(TVaultConstants.SVC_ACC_PATH_PREFIX, svcListUsers); } return ResponseEntity.status(HttpStatus.OK).body(JSONUtil.getJSON(safeList)); }  }
ServiceAccountsService { public ResponseEntity<String> getServiceAccounts(UserDetails userDetails, String userToken) { oidcUtil.renewUserToken(userDetails.getClientToken()); String token = userDetails.getClientToken(); if (!userDetails.isAdmin()) { token = userDetails.getSelfSupportToken(); } String[] policies = policyUtils.getCurrentPolicies(token, userDetails.getUsername(), userDetails); policies = filterPoliciesBasedOnPrecedence(Arrays.asList(policies)); List<Map<String, String>> svcListUsers = new ArrayList<>(); Map<String, List<Map<String, String>>> safeList = new HashMap<>(); if (policies != null) { for (String policy : policies) { Map<String, String> safePolicy = new HashMap<>(); String[] _policies = policy.split("_", -1); if (_policies.length >= 3) { String[] policyName = Arrays.copyOfRange(_policies, 2, _policies.length); String safeName = String.join("_", policyName); String safeType = _policies[1]; if (policy.startsWith("r_")) { safePolicy.put(safeName, "read"); } else if (policy.startsWith("w_")) { safePolicy.put(safeName, "write"); } else if (policy.startsWith("d_")) { safePolicy.put(safeName, "deny"); } if (!safePolicy.isEmpty()) { if (safeType.equals(TVaultConstants.SVC_ACC_PATH_PREFIX)) { svcListUsers.add(safePolicy); } } } } safeList.put(TVaultConstants.SVC_ACC_PATH_PREFIX, svcListUsers); } return ResponseEntity.status(HttpStatus.OK).body(JSONUtil.getJSON(safeList)); }  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); }
ServiceAccountsService { public ResponseEntity<String> getServiceAccounts(UserDetails userDetails, String userToken) { oidcUtil.renewUserToken(userDetails.getClientToken()); String token = userDetails.getClientToken(); if (!userDetails.isAdmin()) { token = userDetails.getSelfSupportToken(); } String[] policies = policyUtils.getCurrentPolicies(token, userDetails.getUsername(), userDetails); policies = filterPoliciesBasedOnPrecedence(Arrays.asList(policies)); List<Map<String, String>> svcListUsers = new ArrayList<>(); Map<String, List<Map<String, String>>> safeList = new HashMap<>(); if (policies != null) { for (String policy : policies) { Map<String, String> safePolicy = new HashMap<>(); String[] _policies = policy.split("_", -1); if (_policies.length >= 3) { String[] policyName = Arrays.copyOfRange(_policies, 2, _policies.length); String safeName = String.join("_", policyName); String safeType = _policies[1]; if (policy.startsWith("r_")) { safePolicy.put(safeName, "read"); } else if (policy.startsWith("w_")) { safePolicy.put(safeName, "write"); } else if (policy.startsWith("d_")) { safePolicy.put(safeName, "deny"); } if (!safePolicy.isEmpty()) { if (safeType.equals(TVaultConstants.SVC_ACC_PATH_PREFIX)) { svcListUsers.add(safePolicy); } } } } safeList.put(TVaultConstants.SVC_ACC_PATH_PREFIX, svcListUsers); } return ResponseEntity.status(HttpStatus.OK).body(JSONUtil.getJSON(safeList)); }  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);  }