AuthorizationUtils { public boolean isAuthorized(UserDetails userDetails, Safe safeMetaData, String[] latestPolicies, ArrayList<String> policiesTobeChecked, boolean forceCapabilityCheck) { boolean authorized = false; String powerToken = userDetails.getSelfSupportToken(); if (userDetails.isAdmin()) { return true; } else { if (userDetails.getUsername() != null && userDetails.getUsername().equalsIgnoreCase(safeMetaData.getSafeBasicDetails().getOwnerid())) { if (!forceCapabilityCheck) { return true; } } } String safeType = getSafeTypeFromPath(safeMetaData.getPath()); for (String policyTobeChecked: policiesTobeChecked) { String policyKeyTobeChecked = new StringBuffer().append(safeType).toString(); authorized = isAuthorized(latestPolicies, policyTobeChecked, policyKeyTobeChecked, powerToken); if (authorized) { break; } } return authorized; } AuthorizationUtils(); boolean isAuthorized(UserDetails userDetails, Safe safeMetaData, String[] latestPolicies, ArrayList<String> policiesTobeChecked, boolean forceCapabilityCheck); boolean containsAdminPolicies(List<String> policies, List<String> adminPolicies); boolean isAuthorizedCert(UserDetails userDetails, SSLCertificateMetadataDetails sslMetaData, String[] latestPolicies, ArrayList<String> policiesTobeChecked, boolean forceCapabilityCheck);  }
@Test public void test_isAuthorized_adminuser_success() { String username = "testuser1"; String powerToken = "self_support_token"; String userToken = "ordinary_client_token"; boolean admin = true; String safename = "mysafe01"; String safeType = "shared"; String path = "shared/mysafe01"; String latestPoliciesStr = "s_shared_mysafe01, safeadmin"; String policiesTobeCheckedStr = "s_shared_mysafe01"; String safeOwner = "owner@someorg.com"; String safeOwnerId = "normaluser"; UserDetails userDetails = createUserDetails(username, powerToken, userToken, admin); Safe safeMetaData = createSafe(safename, safeType, path, safeOwner, safeOwnerId); String[] latestPolicies = createLatestPolicies(latestPoliciesStr); ArrayList<String> policiesTobeChecked = createPoliciesTobeChecked(policiesTobeCheckedStr); boolean forceCapabilityCheck = true; boolean actual = authorizationUtils.isAuthorized(userDetails, safeMetaData, latestPolicies, policiesTobeChecked, forceCapabilityCheck); assertEquals(actual, true); }
@Test public void test_isAuthorized_safeOwner_nocapabilitycheck_success() { String username = "normaluser"; String powerToken = "self_support_token"; String userToken = "ordinary_client_token"; boolean admin = false; String safename = "mysafe01"; String safeType = "shared"; String safeOwner = "owner@someorg.com"; String safeOwnerId = "normaluser"; String path = "shared/mysafe01"; String latestPoliciesStr = "s_shared_mysafe01, safeadmin"; String policiesTobeCheckedStr = "s_shared_mysafe01"; UserDetails userDetails = createUserDetails(username, powerToken, userToken, admin); Safe safeMetaData = createSafe(safename, safeType, path, safeOwner, safeOwnerId); String[] latestPolicies = createLatestPolicies(latestPoliciesStr); ArrayList<String> policiesTobeChecked = createPoliciesTobeChecked(policiesTobeCheckedStr); boolean forceCapabilityCheck = false; boolean actual = authorizationUtils.isAuthorized(userDetails, safeMetaData, latestPolicies, policiesTobeChecked, forceCapabilityCheck); assertEquals(actual, true); }
@Test public void test_isAuthorized_safeOwner_withcapabilitycheck_success() throws Exception { String username = "normaluser"; String powerToken = "self_support_token"; String userToken = "ordinary_client_token"; boolean admin = false; String safename = "mysafe01"; String safeType = "shared"; String safeOwner = "owner@someorg.com"; String safeOwnerId = "normaluser"; String path = "shared/mysafe01"; String latestPoliciesStr = "s_shared_mysafe01, safeadmin"; String policiesTobeCheckedStr = "s_shared_mysafe01"; boolean forceCapabilityCheck = true; UserDetails userDetails = createUserDetails(username, powerToken, userToken, admin); Safe safeMetaData = createSafe(safename, safeType, path, safeOwner, safeOwnerId); String[] latestPolicies = createLatestPolicies(latestPoliciesStr); ArrayList<String> policiesTobeChecked = createPoliciesTobeChecked(policiesTobeCheckedStr); LinkedHashMap<String, Object> capRes = new LinkedHashMap<String, Object>(); LinkedHashMap<String, Object> pathsMap = new LinkedHashMap<String, Object>(); LinkedHashMap<String, Object> capPathMap = new LinkedHashMap<String, Object>(); LinkedHashMap<String, Object> capMap = new LinkedHashMap<String, Object>(); capMap.put("policy", "sudo"); capPathMap.put("metadata/shared/s1/*", capMap); capPathMap.put("shared/s1/*", capMap); pathsMap.put("path", capPathMap); capRes.put("name", "s_shared_s1"); capRes.put("rules", getJSON(pathsMap)); String expectedBody = getJSON(capRes); Response capabilitiesResponse = getMockResponse(HttpStatus.OK, true, expectedBody); when(reqProcessor.process("/access","{\"accessid\":\""+"s_shared_mysafe01"+"\"}",powerToken)).thenReturn(capabilitiesResponse); boolean actual = authorizationUtils.isAuthorized(userDetails, safeMetaData, latestPolicies, policiesTobeChecked, forceCapabilityCheck); assertEquals(actual, true); }
@Test public void test_isAuthorized_safeOwner_withcapabilitycheck_success_list_policies() throws Exception { String username = "normaluser"; String powerToken = "self_support_token"; String userToken = "ordinary_client_token"; boolean admin = false; String safename = "mysafe01"; String safeType = "shared"; String safeOwner = "owner@someorg.com"; String safeOwnerId = "normaluser"; String path = "shared/mysafe01"; String latestPoliciesStr = "s_shared_mysafe01, safeadmin"; String policiesTobeCheckedStr = "s_shared_mysafe01"; boolean forceCapabilityCheck = true; UserDetails userDetails = createUserDetails(username, powerToken, userToken, admin); Safe safeMetaData = createSafe(safename, safeType, path, safeOwner, safeOwnerId); String[] latestPolicies = createLatestPolicies(latestPoliciesStr); ArrayList<String> policiesTobeChecked = createPoliciesTobeChecked(policiesTobeCheckedStr); LinkedHashMap<String, Object> capRes = new LinkedHashMap<String, Object>(); LinkedHashMap<String, Object> pathsMap = new LinkedHashMap<String, Object>(); LinkedHashMap<String, Object> capPathMap = new LinkedHashMap<String, Object>(); LinkedHashMap<String, Object> capMap = new LinkedHashMap<String, Object>(); List<String> policies = new ArrayList<>(); policies.add("create"); capMap.put("policy", policies); capPathMap.put("metadata/shared/s1/*", capMap); capPathMap.put("shared/s1/*", capMap); pathsMap.put("path", capPathMap); capRes.put("name", "s_shared_s1"); capRes.put("rules", getJSON(pathsMap)); String expectedBody = getJSON(capRes); Response capabilitiesResponse = getMockResponse(HttpStatus.OK, true, expectedBody); when(reqProcessor.process("/access","{\"accessid\":\""+"s_shared_mysafe01"+"\"}",powerToken)).thenReturn(capabilitiesResponse); boolean actual = authorizationUtils.isAuthorized(userDetails, safeMetaData, latestPolicies, policiesTobeChecked, forceCapabilityCheck); assertEquals(actual, true); }