CAS Unit Tests
Clover coverage report - CAS Unit Tests
Coverage timestamp: Mon Jul 4 2005 18:13:17 CDT
file stats: LOC: 488   Methods: 13
NCLOC: 405   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ExternalPolicyEvaluator.java 0% 0% 0% 0%
coverage
 1    /*
 2    * Portions of this file Copyright 1999-2005 University of Chicago
 3    * Portions of this file Copyright 1999-2005 The University of Southern California.
 4    *
 5    * This file or a portion of this file is licensed under the
 6    * terms of the Globus Toolkit Public License, found at
 7    * http://www.globus.org/toolkit/download/license.html.
 8    * If you redistribute this file, with or without
 9    * modifications, you must include this notice in the file.
 10    */
 11    package org.globus.cas.impl.databaseAccess;
 12   
 13    import org.globus.cas.types.UserData;
 14    import org.globus.cas.types.PolicyData;
 15    import org.globus.cas.types.ArrayOfString;
 16   
 17    import org.globus.cas.impl.CasConstants;
 18   
 19    import java.sql.ResultSet;
 20    import java.sql.SQLException;
 21    import java.sql.Connection;
 22    import java.sql.Statement;
 23   
 24    import java.util.Vector;
 25    import java.util.HashMap;
 26    import java.util.StringTokenizer;
 27   
 28    import org.apache.commons.logging.Log;
 29    import org.apache.commons.logging.LogFactory;
 30   
 31    import org.globus.util.I18n;
 32   
 33    /**
 34    * Evaluates and returns relevant external policies i.e policies on objects
 35    * other than the CAS server itself. All methods in this class deal only with
 36    * external objects.
 37    */
 38    public class ExternalPolicyEvaluator {
 39   
 40    static Log logger =
 41    LogFactory.getLog(ExternalPolicyEvaluator.class.getName() );
 42   
 43    private static I18n i18n =
 44    I18n.getI18n("org.globus.cas.impl.databaseAccess.errors",
 45    ExternalPolicyEvaluator.class.getClassLoader());
 46   
 47    // Returns all policies for a given user (any resource, any action)
 48  0 public static Vector getPolicies(String userNick)
 49    throws CasDBException {
 50  0 logger.debug("Method to get policies given user: " + userNick);
 51  0 String userGpQuery = getUserGroupNameQuery(userNick);
 52  0 if (userGpQuery == null)
 53  0 return null;
 54  0 String query = getBaseQuery() + " and " + userGpQuery + " and "
 55    + getBasicObjectQuery();
 56  0 logger.debug("Query is: " + query);
 57  0 Vector policiesVector = getPoliciesForQuery(query);
 58  0 if (policiesVector == null) {
 59  0 logger.debug("Policies vector for query is null");
 60  0 return null;
 61    } else {
 62  0 logger.debug("Policies vector for query " + policiesVector.size());
 63  0 return sortPolicies(policiesVector);
 64    }
 65    }
 66   
 67    // Returns all polcies for a given user with a particular resource.
 68  0 public static Vector getPoliciesForResource(String userNick,
 69    String resource)
 70    throws CasDBException {
 71  0 logger.debug("Method to get policies give user: " + userNick
 72    + " resource is " + resource);
 73  0 String userGpQuery = getUserGroupNameQuery(userNick);
 74  0 if (userGpQuery == null)
 75  0 return null;
 76  0 String query = getBaseQuery() + " and " + userGpQuery;
 77    // Get query for resource
 78  0 String queryForObjects = getResourceQueries(resource);
 79  0 if (!queryForObjects.equals(""))
 80  0 query = query + " and " + queryForObjects;
 81  0 logger.debug("Query is: " + query);
 82  0 Vector policiesVector = getPoliciesForQuery(query);
 83  0 Vector toSortPoliciesVector = null;
 84  0 if (policiesVector == null) {
 85  0 logger.debug("Policies vector for query is null");
 86  0 return null;
 87    } else {
 88  0 logger.debug("Policies vector for query " + policiesVector.size());
 89    // overwrite object group
 90  0 for (int i=0; i<policiesVector.size(); i++) {
 91  0 PolicyData policyData = (PolicyData)policiesVector.get(i);
 92  0 policyData.setObjectSpec(resource);
 93  0 policiesVector.setElementAt(policyData, i);
 94    }
 95  0 if (toSortPoliciesVector == null)
 96  0 toSortPoliciesVector = new Vector();
 97  0 toSortPoliciesVector.addAll(policiesVector);
 98  0 return sortPolicies(toSortPoliciesVector);
 99    }
 100    }
 101   
 102    // Returns all polcies for a given user with a particular service/action
 103  0 public static Vector getPoliciesForActions(String userNick,
 104    Vector actionIds)
 105    throws CasDBException {
 106  0 logger.debug("Method to get policies given user " + userNick +
 107    " and action id ");
 108  0 String userGpQuery = getUserGroupNameQuery(userNick);
 109  0 if (userGpQuery == null)
 110  0 return null;
 111  0 Vector toSortPoliciesVector = null;
 112  0 String preQuery = getBaseQuery() + " and " + userGpQuery + " and (";
 113  0 for (int i=0; i<actionIds.size(); i++) {
 114  0 String actionId = (String)actionIds.get(i);
 115  0 String query = preQuery + getActionQuery(actionId) + ") and "
 116    + getBasicObjectQuery();
 117  0 logger.debug("ActionId " + actionId + "Query is " + query);
 118  0 Vector policiesVector = getPoliciesForQuery(query);
 119  0 if (policiesVector == null) {
 120  0 logger.debug("Policies vector for query is null");
 121    } else {
 122  0 logger.debug("Policies vector for query "
 123    + policiesVector.size());
 124    // overwrite action group
 125  0 for (int j=0; j<policiesVector.size(); j++) {
 126  0 PolicyData policyData = (PolicyData)policiesVector.get(j);
 127  0 policyData.setActionSpec(actionId);
 128  0 policyData.setActionSpecDesc(
 129    CasConstants.SERVICEACTION_SPEC);
 130  0 policiesVector.setElementAt(policyData, j);
 131    }
 132  0 if (toSortPoliciesVector == null)
 133  0 toSortPoliciesVector = new Vector();
 134  0 toSortPoliciesVector.addAll(policiesVector);
 135    }
 136    }
 137  0 if (toSortPoliciesVector == null)
 138  0 return null;
 139    else
 140  0 return sortPolicies(toSortPoliciesVector);
 141    }
 142   
 143  0 public static Vector getPolicies(String userNick, String resource,
 144    Vector actionIds)
 145    throws CasDBException {
 146  0 logger.debug("Method to get policies give user: " + userNick
 147    + " resource is " + resource + " actionIds");
 148  0 String userGpQuery = getUserGroupNameQuery(userNick);
 149  0 if (userGpQuery == null)
 150  0 return null;
 151  0 String query = getBaseQuery() + " and " + userGpQuery;
 152    // Get query for resource
 153  0 String queryForObjects = getResourceQueries(resource);
 154  0 if (!queryForObjects.equals(""))
 155  0 query = query + " and " + queryForObjects;
 156  0 Vector toSortPoliciesVector = null;
 157  0 for (int i=0; i<actionIds.size(); i++) {
 158  0 String actionId = (String)actionIds.get(i);
 159  0 String completeQuery = query + " and (" + getActionQuery(actionId)
 160    + ")";
 161  0 Vector policiesVector = getPoliciesForQuery(completeQuery);
 162  0 if (policiesVector == null) {
 163  0 logger.debug("Policies vector for query is null");
 164    } else {
 165  0 logger.debug("Policies vector for query "
 166    + policiesVector.size());
 167    // overwrite action group and object
 168  0 for (int j=0; j<policiesVector.size(); j++) {
 169  0 PolicyData policyData = (PolicyData)policiesVector.get(j);
 170  0 policyData.setActionSpec(actionId);
 171  0 policyData.setActionSpecDesc(
 172    CasConstants.SERVICEACTION_SPEC);
 173  0 String objSpecs[] = parseResourceString(resource);
 174  0 String objId =
 175    Integer.toString(ObjectDataHandler.getObjectId(
 176    objSpecs[1],
 177    objSpecs[0]));
 178  0 policyData.setObjectSpec(objId);
 179  0 policiesVector.setElementAt(policyData, j);
 180    }
 181  0 if (toSortPoliciesVector == null)
 182  0 toSortPoliciesVector = new Vector();
 183  0 toSortPoliciesVector.addAll(policiesVector);
 184    }
 185    }
 186  0 if (toSortPoliciesVector == null)
 187  0 return null;
 188    else
 189  0 return sortPolicies(toSortPoliciesVector);
 190    }
 191   
 192  0 private static String getResourceQueries(String resource)
 193    throws CasDBException {
 194   
 195  0 String objSpecs[] = parseResourceString(resource);
 196   
 197    // All objects that match (using comp algorithm)
 198  0 Vector matchingObjIds =
 199    ObjectDataHandler.retrieveMatchingObjects(objSpecs[0],
 200    objSpecs[1]);
 201  0 StringBuffer objQueryBuf = null;
 202  0 StringBuffer objGroupQueryBuf = null;
 203  0 if (matchingObjIds != null) {
 204  0 logger.debug("macthing ids found");
 205    // find all groups for those objects
 206  0 for (int i=0; i<matchingObjIds.size(); i++) {
 207  0 String objId = (String)matchingObjIds.get(i);
 208  0 if (objQueryBuf == null) {
 209  0 objQueryBuf = new StringBuffer();
 210  0 objQueryBuf.append("'");
 211    } else {
 212  0 objQueryBuf.append(", '");
 213    }
 214  0 objQueryBuf.append(objId);
 215  0 objQueryBuf.append("'");
 216  0 String[] gpNames =
 217    ObjectGroupDataHandler.getObjectGroupsForMember(
 218    objId, CasConstants.OBJECT_SPEC);
 219  0 if (gpNames != null) {
 220  0 for (int j=0; j<gpNames.length; j++) {
 221  0 if (objGroupQueryBuf == null) {
 222  0 objGroupQueryBuf = new StringBuffer();
 223  0 objGroupQueryBuf.append("'");
 224    } else {
 225  0 objGroupQueryBuf.append(", '");
 226    }
 227  0 objGroupQueryBuf.append(gpNames[j]);
 228  0 objGroupQueryBuf.append("'");
 229    }
 230    }
 231    }
 232    }
 233   
 234    // If object exists, then it would match itself nad hence will
 235    // be automatically included in the above search (including the
 236    // groups the object belongs to)
 237  0 String queryForObjects = "";
 238  0 if (objQueryBuf != null) {
 239  0 String query = objQueryBuf.toString();
 240  0 logger.debug("Matching ids, object query: " + query);
 241  0 queryForObjects = "((" + CasDBConstants.COL_OBJECT_SPEC + " in ("
 242    + query + ")) and ("
 243    + CasDBConstants.COL_OBJECT_SPEC_DESC
 244    + "='" + CasConstants.OBJECT_SPEC + "'))";
 245    }
 246  0 if (objGroupQueryBuf != null) {
 247  0 String query = objGroupQueryBuf.toString();
 248  0 logger.debug("Matching ids, objGp qurey: " + query);
 249  0 queryForObjects = "(" + queryForObjects + "or (("
 250    + CasDBConstants.COL_OBJECT_SPEC + " in (" + query
 251    + ")) and (" + CasDBConstants.COL_OBJECT_SPEC_DESC
 252    + "='" + CasConstants.OBJECTGP_SPEC + "')))";
 253    }
 254   
 255  0 return queryForObjects;
 256    }
 257   
 258  0 private static String[] parseResourceString(String resource)
 259    throws CasDBException {
 260   
 261  0 StringTokenizer strTok =
 262    new StringTokenizer(resource, CasConstants.OBJECTSPEC_DELIMITER);
 263  0 if (strTok.countTokens() != 2) {
 264  0 String errMesg =
 265    i18n.getMessage("malformedObjStr",
 266    CasConstants.OBJECTSPEC_DELIMITER);
 267  0 logger.error(errMesg);
 268  0 throw new CasDBException(errMesg);
 269    }
 270  0 String obj[] = new String[2];
 271  0 obj[0] = strTok.nextToken();
 272  0 obj[1] = strTok.nextToken();
 273  0 return obj;
 274    }
 275   
 276    // Query to return only objects or objectGroups (since external objects
 277    // can be stored in only one of the above)
 278  0 private static String getBasicObjectQuery() {
 279  0 return "((" + CasDBConstants.COL_OBJECT_SPEC_DESC + "='" +
 280    CasConstants.OBJECT_SPEC + "') or ("
 281    + CasDBConstants.COL_OBJECT_SPEC_DESC + "='"
 282    + CasConstants.OBJECTGP_SPEC + "'))";
 283    }
 284   
 285    //Query selects from policy table, excludes cas server object
 286  0 private static String getBaseQuery() throws CasDBException {
 287  0 int selfObjId =
 288    ObjectDataHandler.getObjectId(CasConstants.OBJECT_SELF,
 289    CasConstants.NAMESPACE_SELF);
 290  0 return "select * from" + CasDBConstants.TABLE_POLICY
 291    + " where (not (("
 292    + CasDBConstants.COL_OBJECT_SPEC + " = '"
 293    + Integer.toString(selfObjId) + "') and ("
 294    + CasDBConstants.COL_OBJECT_SPEC_DESC + "='"
 295    + CasConstants.OBJECT_SPEC + "')))";
 296    }
 297   
 298    // Returns the query that checks for all groups that a user belongs to.
 299  0 private static String getUserGroupNameQuery(String userNick)
 300    throws CasDBException {
 301  0 UserData userData = (UserData)UserDataHandler.retrieveObject(userNick);
 302  0 if (userData == null) {
 303  0 String err = i18n.getMessage("doesNotExist", new Object[] {
 304    "User with nickname", userNick });
 305  0 logger.error(err);
 306  0 throw new CasDBException(err);
 307    }
 308  0 ArrayOfString arrayOfGpNames = userData.getGroupNames();
 309  0 if (arrayOfGpNames == null) {
 310  0 logger.debug("The user does not belong to any group and hence"
 311    + " cannot have policies");
 312  0 return null;
 313    } else {
 314  0 String gpNames[] = arrayOfGpNames.getStrings();
 315  0 String gpNamesList = returnGroupMemberList(gpNames);
 316  0 return "(" + CasDBConstants.COL_USERGP_NAME + " in ( "
 317    + gpNamesList + " ))";
 318    }
 319    }
 320   
 321    // Returns action query given service/action id.
 322  0 private static String getActionQuery(String actionId)
 323    throws CasDBException {
 324   
 325  0 String returnString = "((" + CasDBConstants.COL_ACTION_SPEC + "='"
 326    + actionId + "') and (" + CasDBConstants.COL_ACTION_SPEC_DESC +
 327    "='" + CasConstants.SERVICEACTION_SPEC + "'))";
 328   
 329  0 String groups[] =
 330    ServiceTypeActionHandler.retrieveServiceActionGroups(actionId);
 331  0 String groupList = returnGroupMemberList(groups);
 332  0 if (!groupList.equals("")) {
 333  0 returnString = returnString + " or (("
 334    + CasDBConstants.COL_ACTION_SPEC + " in ( " + groupList
 335    + " )) and (" + CasDBConstants.COL_ACTION_SPEC_DESC +
 336    "='" + CasConstants.SERVICEACTIONGP_SPEC + "'))";
 337    }
 338  0 return returnString;
 339    }
 340   
 341  0 private static String returnGroupMemberList(String gpNames[])
 342    throws CasDBException {
 343  0 if ((gpNames == null) || (gpNames.length < 1)) {
 344  0 return "";
 345    } else {
 346  0 StringBuffer buf = new StringBuffer("'");
 347  0 buf.append(gpNames[0]);
 348  0 buf.append("'");
 349   
 350  0 for (int i=1; i<gpNames.length; i++) {
 351  0 buf.append(", '");
 352  0 buf.append(gpNames[i]);
 353  0 buf.append("'");
 354    }
 355  0 return buf.toString();
 356    }
 357    }
 358   
 359    // Returns all policies relevant to the query passed. Invokes
 360    // PolicyData.constructPolicyData, which expands all the action and object
 361    // groups; and in the latter case also ensure that only external object
 362    // members of the group are returned.
 363  0 private static Vector getPoliciesForQuery(String query)
 364    throws CasDBException {
 365   
 366  0 logger.debug("Query is "+ query);
 367   
 368  0 Vector policiesVector = null;
 369  0 Connection connection = CasDBStorage.getDBConnection();
 370  0 Statement statement = null;
 371  0 ResultSet resultSet = null;
 372  0 try {
 373  0 statement = connection.createStatement();
 374  0 resultSet = statement.executeQuery(query);
 375  0 policiesVector =
 376    PolicyDataHandler.constructPolicyData(connection,
 377    resultSet, true);
 378  0 if (logger.isDebugEnabled()) {
 379  0 PolicyDataHandler.printPolicyVector(policiesVector);
 380    }
 381    }
 382    catch (SQLException exp) {
 383  0 logger.error(i18n.getMessage("nonCASRetrieveErr", query), exp);
 384  0 throw new CasDBException(i18n.getMessage("nonCASRetrieveErr",
 385    query) + exp.getMessage(),
 386    exp);
 387    }
 388    catch (CasDBException exp) {
 389  0 logger.error(i18n.getMessage("nonCASCheckErr"), exp);
 390  0 throw new CasDBException(i18n.getMessage("nonCASCheckErr")
 391    + exp.getMessage(),
 392    exp);
 393    }
 394    finally {
 395  0 CasDBStorage.returnDBConnection(connection);
 396  0 try {
 397  0 if (resultSet != null)
 398  0 resultSet.close();
 399  0 if (statement != null)
 400  0 statement.close();
 401    }
 402    catch (SQLException exp) {
 403  0 String err = "Error checking for external policies. "
 404    + " Could not close SQL statement.";
 405  0 logger.warn(err, exp);
 406    }
 407    }
 408  0 if (policiesVector == null) {
 409  0 logger.debug("Null");
 410    } else {
 411  0 logger.debug("Not null");
 412    }
 413  0 return policiesVector;
 414    }
 415   
 416   
 417    // Sorts policies bases on resources, returns a vector of vector;
 418    // a vector per resource (object).
 419  0 public static Vector sortPolicies(Vector policyVector)
 420    throws CasDBException {
 421   
 422  0 if (policyVector == null) {
 423  0 logger.debug("Policies null");
 424  0 return null;
 425    }
 426  0 logger.debug("Policies not null");
 427   
 428    // Sort policies. Returned is Vector of vectors, each internal
 429    // vector having policy for one objectSpec
 430   
 431    //Get a Vector of obejct specification
 432  0 Vector objSpec = new Vector(policyVector.size());
 433  0 for (int i=0; i<policyVector.size(); i++) {
 434  0 objSpec.add(((PolicyData)policyVector.get(i)).getObjectSpec());
 435    }
 436   
 437    // Policies at start
 438  0 if (logger.isDebugEnabled()) {
 439  0 logger.debug("Policies before sort per object");
 440  0 PolicyDataHandler.printPolicyVector(policyVector);
 441    }
 442   
 443  0 logger.debug("objSpec size at start" + objSpec.size());
 444    // Polices vector - vector of vectors!
 445  0 Vector policiesVector = new Vector();
 446  0 while (objSpec.size() >0) {
 447  0 int i=0;
 448  0 HashMap newObjMap = new HashMap();
 449  0 String objSpecString = (String)objSpec.remove(i);
 450  0 logger.debug("Object in consideration is " + objSpecString);
 451  0 PolicyData remPolicyData = (PolicyData)policyVector.remove(i);
 452  0 newObjMap.put(remPolicyData.getActionSpec(), remPolicyData);
 453  0 logger.debug("objSpec size afert a remove " + objSpec.size());
 454  0 while (i < objSpec.size()) {
 455  0 logger.debug("i is " + i + " objSpec size " + objSpec.size());
 456  0 if (objSpecString.equals(objSpec.get(i))) {
 457  0 logger.debug("Equals");
 458  0 remPolicyData = (PolicyData)policyVector.remove(i);
 459  0 if (!newObjMap.containsKey(remPolicyData.getActionSpec())) {
 460  0 newObjMap.put(remPolicyData.getActionSpec(),
 461    remPolicyData);
 462    }
 463  0 objSpec.remove(i);
 464    }
 465    else {
 466  0 logger.debug("not equals, move to next");
 467  0 i++;
 468    }
 469    }
 470  0 policiesVector.add(new Vector(newObjMap.values()));
 471    }
 472   
 473  0 if (logger.isDebugEnabled()) {
 474  0 logger.debug("Final Policies Vector " + policiesVector.size());
 475  0 for (int i=0; i<policiesVector.size(); i++) {
 476  0 logger.debug(" ObjVector " + i);
 477  0 Vector objVec = (Vector)policiesVector.get(i);
 478  0 for (int j=0; j<objVec.size(); j++) {
 479  0 logger.debug("New policy vector");
 480  0 PolicyData policyData = (PolicyData)objVec.get(j);
 481  0 logger.debug("policy is " + policyData.getObjectSpec() + " "
 482    + policyData.getPolicyId());
 483    }
 484    }
 485    }
 486  0 return policiesVector;
 487    }
 488    }