CAS Unit Tests
Clover coverage report - CAS Unit Tests
Coverage timestamp: Mon Jul 4 2005 18:13:17 CDT
file stats: LOC: 330   Methods: 4
NCLOC: 267   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CasProxyInit.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.client;
 12   
 13    import java.util.Vector;
 14   
 15    import java.io.FileReader;
 16    import java.io.IOException;
 17    import java.io.BufferedReader;
 18    import java.io.FileNotFoundException;
 19   
 20    import org.globus.wsrf.impl.security.authentication.Constants;
 21   
 22    import org.globus.cas.impl.CasConstants;
 23   
 24    /**
 25    * Command line client to create a proxy with CAS Assertions embedded
 26    */
 27    public class CasProxyInit {
 28   
 29    static String mesg =
 30    " Usage: cas-proxy-init [<options>] [ -t tag | -p proxyFile]\n"
 31    + " Options are -debug Runs with debug trace \n"
 32    + " -c <instanceURL> Specify URL for CAS server\n"
 33    + " -help Prints this message\n"
 34    + " -v Prints version number\n"
 35    + " -l Requested CAS credential lifetime in hours\n"
 36    + " By defualt, lifetime of 24 hours is used\n"
 37    + " -f Requested policies that need to be included in\n"
 38    + " CAS credential. By defualt, all applicable \n"
 39    + " policies are included.\n"
 40    + " -s Expected server identity. If not specified, \n"
 41    + " a identity with <fqdn>/cas is expected, where\n"
 42    + " <fqdn> is DN of the hostname of CAS server\n"
 43    + " -m security mechanism. 'msg' for secure message\n"
 44    + " or 'conv' for secure conversation If unspecified\n"
 45    + " If instance url is 'https', then trasport \n"
 46    + " security is used else mechanism defaults to\n"
 47    + " secure message\n"
 48    + " -n protection type, 'sig' signature and 'enc' \n"
 49    + " encryption, defaults to signature\n"
 50    + " tag : Generated credential is placed in file with default proxy\n"
 51    + " filename with the tag appended to it\n"
 52    + " proxyFile : File to place generated credential\n"
 53    + " If neither a tag not a proxyFile is specified, tag with value \n"
 54    + " \"cas\" is assumed. If both are specifies, -p takes precedence.";
 55   
 56    static String newProxyFileName = null;
 57    static int assertionLifetime = 24 * 60 * 60;
 58    static String policyFileName = null;
 59    static String instanceURL = null;
 60    static String serverIdentity = null;
 61    static boolean debug = false;
 62    static String casTag = CasConstants.CAS_PROXY_TAG;
 63   
 64    static String securityType = null;
 65    static Object protectionType = null;
 66   
 67    static String resourceSpec = "Resource:";
 68   
 69  0 public static void main(String args[]) throws Exception {
 70   
 71    // parse aguments
 72  0 parseArgs(args);
 73  0 printMessage("CAS client to generate restrictd proxy with CAS"
 74    + " assertions.");
 75   
 76    // Option not set, look for property CAS_SERVER_URL
 77  0 if (instanceURL == null) {
 78  0 printMessage("Instance URL not set using -c");
 79  0 instanceURL = System.getProperty(CasClientSetup.casServerURL);
 80    }
 81   
 82  0 if ((instanceURL == null) || (instanceURL.trim().equals(""))) {
 83  0 System.err.println("Error: CAS server URL must be set using -c "
 84    + " or environment variable CAS_SERVER_URL "
 85    + " should be set.");
 86  0 System.err.println(mesg);
 87  0 System.exit(-1);
 88    }
 89   
 90    // If debug mode, print arguments
 91  0 printArgs();
 92   
 93  0 CasProxyHelper casProxyHelper =
 94    new CasProxyHelper(instanceURL, serverIdentity, debug);
 95   
 96    // Always picks up default credential
 97  0 ClientParams clientParams = new ClientParams();
 98  0 clientParams.setSecurityType(securityType);
 99  0 clientParams.setProtectionType(protectionType);
 100   
 101    // Read from a file for policies requested as assertion.
 102  0 if (policyFileName != null) {
 103    // Vector of ResourceActionsMap object
 104  0 Vector resActionsVector = null;
 105  0 printMessage("filename " + policyFileName);
 106  0 BufferedReader reader = null;
 107  0 try {
 108  0 reader = new BufferedReader(new FileReader(policyFileName));
 109    } catch (FileNotFoundException fnfe) {
 110  0 System.err.println("Error reading policyFileName "
 111    + policyFileName + "\n"
 112    + fnfe.getMessage());
 113  0 if (debug) {
 114  0 System.err.println(fnfe.toString());
 115    }
 116  0 System.exit(-1);
 117    }
 118  0 String resLine = null;
 119  0 String resource = null;
 120  0 int index = -1;
 121  0 try {
 122   
 123  0 if (((resLine = reader.readLine()) != null)
 124    && ((index = resLine.indexOf(resourceSpec)) != -1)) {
 125  0 resource = resLine.substring(index
 126    + resourceSpec.length());
 127  0 Vector actions = null;
 128  0 index = -1;
 129  0 while ((resLine = reader.readLine()) != null) {
 130  0 printMessage("line: " + resLine);
 131  0 if ((index = resLine.indexOf(resourceSpec)) == -1) {
 132  0 printMessage("Action");
 133  0 if (actions == null) {
 134  0 actions = new Vector();
 135    }
 136  0 printMessage("adding action " + resLine);
 137  0 actions.add(resLine);
 138    } else {
 139  0 printMessage("Resource " + resource);
 140  0 ResourceActionsMap resActions =
 141    new ResourceActionsMap(resource, actions);
 142  0 if (resActionsVector == null) {
 143  0 resActionsVector = new Vector();
 144    }
 145  0 printMessage("adding for resource " + resource);
 146  0 resActionsVector.add(resActions);
 147  0 actions = null;
 148  0 resource = resLine.substring(index +
 149    resourceSpec.length());
 150  0 printMessage("Next Resource " + resource);
 151    }
 152    }
 153   
 154  0 ResourceActionsMap resActions =
 155    new ResourceActionsMap(resource, actions);
 156  0 if (resActionsVector == null) {
 157  0 resActionsVector = new Vector();
 158    }
 159  0 printMessage("adding for resource " + resource);
 160  0 resActionsVector.add(resActions);
 161   
 162    // Convert to array
 163  0 ResourceActionsMap[] resActionsArray =
 164    new ResourceActionsMap[resActionsVector.size()];
 165  0 resActionsVector.toArray(resActionsArray);
 166  0 clientParams.setResourceActionsMap(resActionsArray);
 167    }
 168    else {
 169  0 printMessage("Policy file has no data. "
 170    + " getMaximalAssertion");
 171    }
 172    } catch (IOException ioe) {
 173  0 System.err.println("Error reading from policy file "
 174    + policyFileName + "\n" + ioe.getMessage());
 175  0 if (debug) {
 176  0 System.err.println(ioe.toString());
 177    }
 178  0 System.exit(-1);
 179    }
 180    }
 181   
 182    // assertion lifetime
 183  0 clientParams.setAssertionLifetime(assertionLifetime);
 184   
 185    // tag
 186  0 clientParams.setCasProxyTag(casTag);
 187   
 188    // Name of proxy file with CAS assertion embedded.
 189  0 clientParams.setCasProxyFileName(newProxyFileName);
 190   
 191  0 printMessage("Client params: " + clientParams.toString());
 192   
 193  0 String casProxyFileName = casProxyHelper.getCasProxy(clientParams);
 194  0 System.out.println("Completed successfully. Proxy written to "
 195    + casProxyFileName);
 196    }
 197   
 198  0 private static void parseArgs(String[] args) {
 199   
 200  0 for (int i=0; i<args.length; i++) {
 201   
 202  0 if (args[i].equals("-debug")) {
 203  0 debug = true;
 204    }
 205   
 206  0 if (args[i].equals("-help")) {
 207  0 System.out.println(mesg);
 208  0 System.exit(0);
 209    }
 210   
 211  0 if (args[i].equals("-v")) {
 212  0 System.out.println("CAS Version number: "
 213    + CasConstants.versionNumber);
 214  0 System.exit(0);
 215    }
 216   
 217  0 if (args[i].equals("-c")) {
 218  0 if (i+1 == args.length) {
 219  0 System.err.println("Error: -c requires an argument.");
 220  0 System.out.println(mesg);
 221  0 System.exit(-1);
 222    }
 223  0 instanceURL = args[i+1];
 224    }
 225   
 226  0 if (args[i].equals("-l")) {
 227  0 if (i+1 == args.length) {
 228  0 System.err.println("Error: -l requires an argument.");
 229  0 System.out.println(mesg);
 230  0 System.exit(-1);
 231    }
 232  0 try {
 233  0 assertionLifetime = Integer.parseInt(args[i+1]) * 60 * 60;
 234    } catch (NumberFormatException exp) {
 235  0 System.err.println("-l should have an integer argument.\n "
 236    + exp.getMessage());
 237  0 if (debug) {
 238  0 System.err.println(exp.toString());
 239    }
 240  0 System.exit(-1);
 241    }
 242    }
 243   
 244  0 if (args[i].equals("-f")) {
 245  0 if (i+1 == args.length) {
 246  0 System.err.println("Error: -f requires an argument.");
 247  0 System.exit(-1);
 248    }
 249  0 policyFileName = args[i+1];
 250    }
 251   
 252  0 if (args[i].equals("-t")) {
 253  0 if (i+1 == args.length) {
 254  0 System.err.println("Error: -t requires an argument.");
 255  0 System.exit(-1);
 256    }
 257  0 casTag = args[i+1];
 258    }
 259   
 260  0 if (args[i].equals("-p")) {
 261  0 if (i+1 == args.length) {
 262  0 System.err.println("Error: -p requires an argument.");
 263  0 System.exit(-1);
 264    }
 265  0 newProxyFileName = args[i+1];
 266    }
 267   
 268  0 if (args[i].equals("-s")) {
 269  0 if (i+1 == args.length) {
 270  0 System.err.println("Error: -s requires an argument.");
 271  0 System.exit(-1);
 272    }
 273  0 serverIdentity = args[i+1];
 274    }
 275   
 276  0 if (args[i].equals("-m")) {
 277  0 if (args.length == i+1) {
 278  0 System.err.println("-m option should have 'msg' or "
 279    + "'conv'");
 280  0 System.exit(-1);
 281    }
 282  0 String val = args[i+1];
 283  0 if (val.equals("msg")) {
 284  0 securityType = Constants.GSI_SEC_MSG;
 285  0 } else if (val.equals("conv")) {
 286  0 securityType = Constants.GSI_SEC_CONV;
 287    } else {
 288  0 System.err.println("-m option should have 'msg' or "
 289    + "'conv'");
 290  0 System.exit(-1);
 291    }
 292    }
 293   
 294  0 if (args[i].equals("-n")) {
 295  0 if (args.length == i+1) {
 296  0 System.err.println("-n option should have 'sig' or "
 297    + "'enc'");
 298  0 System.exit(-1);
 299    }
 300  0 String val = args[i+1];
 301  0 if (val.equals("sig")) {
 302  0 protectionType = Constants.SIGNATURE;
 303  0 } else if (val.equals("enc")) {
 304  0 protectionType = Constants.ENCRYPTION;
 305    } else {
 306  0 System.err.println("-n option should have 'sig' or "
 307    + "'enc'");
 308  0 System.exit(-1);
 309    }
 310    }
 311    }
 312    }
 313   
 314  0 private static void printArgs() {
 315  0 if (!debug)
 316  0 return;
 317  0 System.out.println("instanceURL " + instanceURL);
 318  0 System.out.println("lifetime " + assertionLifetime);
 319  0 System.out.println("tag " + casTag);
 320  0 System.out.println("new proxyFilename " + newProxyFileName);
 321  0 System.out.println("policyFileName " + policyFileName);
 322  0 System.out.println("Server identity " + serverIdentity);
 323    }
 324   
 325  0 private static void printMessage(String msg) {
 326  0 if (debug) {
 327  0 System.out.println(msg);
 328    }
 329    }
 330    }