1
2
3
4
5
6 package gov.bnl.gums.admin;
7
8
9 import gov.bnl.gums.command.Configuration;
10 import org.apache.axis.AxisFault;
11
12 import org.apache.commons.cli.*;
13
14 import java.net.ConnectException;
15 import java.net.InetAddress;
16 import java.net.URL;
17 import gov.bnl.gums.command.AbstractCommand;
18 import org.opensciencegrid.authz.client.GRIDIdentityMappingServiceClient;
19 import org.opensciencegrid.authz.common.GridId;
20
21
22 /***
23 * @author carcassi
24 */
25 public class MapUser extends RemoteCommand {
26 static {
27 command = new MapUser();
28 }
29
30 /***
31 * Creates a new MapUser object.
32 */
33 public MapUser() {
34 syntax = "[-s SERVICEDN] [-n TIMES] [-t NREQUESTS] [-b] [-f FQAN] [-i FQANISSUER] USERDN1 [USERDN2] ... ";
35 description = "Maps the grid identity to the local user.";
36 }
37
38 protected org.apache.commons.cli.Options buildOptions() {
39 Options options = new Options();
40 Option host = new Option("s", "service", true,
41 "DN of the service. When using gums-host, it defaults to the host credential DN.");
42
43 options.addOption(host);
44
45 Option number = new Option("n", "ntimes", true,
46 "number of times the request will be repeated");
47
48 options.addOption(number);
49
50 Option timing = new Option("t", "timing", true,
51 "enables timing, grouping the requests. For example, \"-t 100\" will give you timing information on 100 requests at a time");
52
53 options.addOption(timing);
54
55 Option bypass = new Option("b", "bypassCallout", false,
56 "connects directly to GUMS instead of using the callout");
57
58 options.addOption(bypass);
59
60 Option fqan = new Option("f", "fqan", true,
61 "Fully Qualified Attribute " +
62 "Name, as it would be selected using voms-proxy-init; no extended information by default");
63
64 options.addOption(fqan);
65
66 Option issuer = new Option("i", "issuer", true,
67 "Fully Qualified Attribute " +
68 "Name Issuer, that is the DN of the VOMS service that issued the attribute certificate");
69
70 options.addOption(issuer);
71
72 return options;
73 }
74
75 protected void execute(org.apache.commons.cli.CommandLine cmd)
76 throws Exception {
77 if (cmd.getArgs().length < 1) {
78 failForWrongParameters("Missing parameters...");
79 }
80
81 String hostname = (cmd.getOptionValue("s", null));
82
83 if (hostname == null) {
84 if (isUsingProxy()) {
85 failForWrongParameters("No service specified: please use the -s option followed by the DN of the service.");
86 }
87
88 try {
89 hostname = getClientDN();
90 } catch (Exception e) {
91 System.err.print("Couldn't retrieve the DN of the service/host");
92 System.exit(-1);
93 }
94 }
95
96 String fqan = cmd.getOptionValue("f", null);
97 String[] userDN = (cmd.getArgs());
98
99 String times = (cmd.getOptionValue("n", "1"));
100 int nTimes = 0;
101 try {
102 nTimes = Integer.parseInt(times);
103 } catch (NumberFormatException e) {
104 System.err.println("-n argument should be an integer, and was '" + times + "'");
105 System.exit(-1);
106 }
107
108 String timing = (cmd.getOptionValue("t", null));
109 int requestInGroup = 0;
110 if (timing != null) {
111 try {
112 requestInGroup = Integer.parseInt(timing);
113 } catch (NumberFormatException e) {
114 System.err.println("-t argument should be an integer, and was '" + timing + "'");
115 System.exit(-1);
116 }
117 }
118
119 boolean bypass = cmd.hasOption("b");
120
121 long overall = System.currentTimeMillis();
122 long start = System.currentTimeMillis();
123 GRIDIdentityMappingServiceClient client = new GRIDIdentityMappingServiceClient(Configuration.getInstance().getGUMSAuthZLocation());
124 GridId id = new GridId();
125 id.setHostDN(hostname);
126 id.setUserFQAN(cmd.getOptionValue("f", null));
127 id.setUserFQANIssuer(cmd.getOptionValue("i", null));
128 long end = System.currentTimeMillis();
129 if ((timing != null) && (!bypass)) {
130 System.out.println("Initialization time: " + (end - start) + "ms");
131 }
132
133 start = System.currentTimeMillis();
134 for (int n = 0; n < nTimes; n++) {
135 for (int i = 0; i < userDN.length; i++) {
136 if (!bypass) {
137 id.setUserDN(userDN[i]);
138 System.out.println(client.mapCredentials(id));
139 } else {
140 System.out.println(getGums().mapUser(hostname, userDN[i], id.getUserFQAN()));
141 }
142 }
143 if ((timing != null) && (((n+1) % requestInGroup) == 0)) {
144 end = System.currentTimeMillis();
145 double delta = end - start;
146 double freq = (delta * (double) userDN.length) / (double) requestInGroup;
147 System.out.println("Requests [" + (n - requestInGroup + 1) + ", " + n + "]: " + delta + "ms, " + freq + "ms/req");
148 start = System.currentTimeMillis();
149 }
150 }
151
152 end = System.currentTimeMillis();
153 double delta = end - overall;
154 double freq = (delta * (double) userDN.length) / (double) nTimes;
155
156 if (timing != null) {
157 System.out.println("Overall: " + delta + "ms, " + freq + "ms/req");
158 }
159 }
160 }