Added test program for the Attribute Resolver.
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / utils / ResolverTest.java
1 /* 
2  * The Shibboleth License, Version 1. 
3  * Copyright (c) 2002 
4  * University Corporation for Advanced Internet Development, Inc. 
5  * All rights reserved
6  * 
7  * 
8  * Redistribution and use in source and binary forms, with or without 
9  * modification, are permitted provided that the following conditions are met:
10  * 
11  * Redistributions of source code must retain the above copyright notice, this 
12  * list of conditions and the following disclaimer.
13  * 
14  * Redistributions in binary form must reproduce the above copyright notice, 
15  * this list of conditions and the following disclaimer in the documentation 
16  * and/or other materials provided with the distribution, if any, must include 
17  * the following acknowledgment: "This product includes software developed by 
18  * the University Corporation for Advanced Internet Development 
19  * <http://www.ucaid.edu>Internet2 Project. Alternately, this acknowledegement 
20  * may appear in the software itself, if and wherever such third-party 
21  * acknowledgments normally appear.
22  * 
23  * Neither the name of Shibboleth nor the names of its contributors, nor 
24  * Internet2, nor the University Corporation for Advanced Internet Development, 
25  * Inc., nor UCAID may be used to endorse or promote products derived from this 
26  * software without specific prior written permission. For written permission, 
27  * please contact shibboleth@shibboleth.org
28  * 
29  * Products derived from this software may not be called Shibboleth, Internet2, 
30  * UCAID, or the University Corporation for Advanced Internet Development, nor 
31  * may Shibboleth appear in their name, without prior written permission of the 
32  * University Corporation for Advanced Internet Development.
33  * 
34  * 
35  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
36  * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
37  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
38  * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK 
39  * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE. 
40  * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY 
41  * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT, 
42  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
43  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
44  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
47  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  */
49
50 package edu.internet2.middleware.shibboleth.utils;
51
52 import java.io.ByteArrayOutputStream;
53 import java.io.IOException;
54 import java.io.PrintStream;
55 import java.util.Properties;
56
57 import org.apache.log4j.BasicConfigurator;
58 import org.apache.log4j.Level;
59 import org.apache.log4j.Logger;
60 import org.apache.xml.serialize.OutputFormat;
61 import org.apache.xml.serialize.XMLSerializer;
62 import org.opensaml.SAMLException;
63 import org.w3c.dom.Element;
64 import org.w3c.dom.Node;
65
66 import edu.internet2.middleware.shibboleth.aa.AAAttribute;
67 import edu.internet2.middleware.shibboleth.aa.AAAttributeSet;
68 import edu.internet2.middleware.shibboleth.aa.AAAttributeSet.ShibAttributeIterator;
69 import edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolver;
70 import edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolverException;
71 import edu.internet2.middleware.shibboleth.common.AuthNPrincipal;
72 import gnu.getopt.Getopt;
73 import gnu.getopt.LongOpt;
74
75 /**
76  * Utility for testing an Attribute Resolver configuration.
77  * 
78  * @author Walter Hoehn
79  */
80
81 public class ResolverTest {
82
83         private static Logger log = Logger.getLogger(ResolverTest.class.getName());
84         private static boolean debug = false;
85         private static String file = null;
86         private static String requester = null;
87         private static String user = null;
88
89         public static void main(String[] args) {
90
91                 LongOpt[] longopts =
92                         {
93                                 new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h'),
94                                 new LongOpt("file", LongOpt.REQUIRED_ARGUMENT, null, 'f'),
95                                 new LongOpt("user", LongOpt.REQUIRED_ARGUMENT, null, 'u'),
96                                 new LongOpt("requester", LongOpt.REQUIRED_ARGUMENT, null, 'r'),
97                                 new LongOpt("debug", LongOpt.NO_ARGUMENT, null, 'd')};
98
99                 Getopt getOpt = new Getopt("ResolverTest", args, ":hdf:u:r:", longopts);
100                 getOpt.setOpterr(false);
101
102                 for (int c;((c = getOpt.getopt()) != -1);) {
103                         switch (c) {
104
105                                 case 'h' :
106                                         printUsage(System.out);
107                                         System.exit(0);
108                                         break;
109
110                                 case 'd' :
111                                         debug = true;
112                                         break;
113
114                                 case 'f' :
115                                         file = getOpt.getOptarg();
116                                         break;
117
118                                 case 'u' :
119                                         user = getOpt.getOptarg();
120                                         break;
121
122                                 case 'r' :
123                                         requester = getOpt.getOptarg();
124                                         break;
125
126                                 case ':' :
127                                         System.out.println("You need an argument for option " + (char) getOpt.getOptopt());
128                                         System.exit(1);
129
130                                 case '?' :
131                                         System.out.println("The option '" + (char) getOpt.getOptopt() + "' is not valid");
132                                         System.exit(1);
133                         }
134                 }
135
136                 configureLogging(debug);
137                 checkRequired();
138
139                 Properties configuration = new Properties();
140                 configuration.setProperty(
141                         "edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolver.ResolverConfig",
142                         file);
143                         
144                 try {
145                         AttributeResolver resolver = new AttributeResolver(configuration);
146                         String[] attributes = resolver.listRegisteredAttributeDefinitionPlugIns();
147
148                         AAAttributeSet attributeSet = new AAAttributeSet();
149                         for (int i = 0; i < attributes.length; i++) {
150                                 attributeSet.add(new AAAttribute(attributes[i]));
151                         }
152
153                         resolver.resolveAttributes(new AuthNPrincipal(user), requester, attributeSet);
154
155                         System.out.println(
156                                 "Received the following back from the Attribute Resolver:" + System.getProperty("line.separator"));
157                         
158                         for (ShibAttributeIterator iterator = attributeSet.shibAttributeIterator(); iterator.hasNext();) {
159                                 AAAttribute attribute = iterator.nextShibAttribute();
160                                 Node node = attribute.toDOM();
161                                 ByteArrayOutputStream xml = new ByteArrayOutputStream();
162                                 if (!(node instanceof Element)) {
163                                         throw new IOException("Received bad Element data from SAML library.");
164                                 }
165                                 OutputFormat format = new OutputFormat();
166                                 format.setIndenting(true);
167                                 format.setIndent(4);
168                                 new XMLSerializer(xml, format).serialize((Element) node);
169                                 System.out.println(xml.toString() + System.getProperty("line.separator"));
170                         }
171                 } catch (AttributeResolverException e) {
172                         System.err.println("Error initializing the Attribute Resolver: " + e.getMessage());
173                 } catch (SAMLException e) {
174                         System.err.println("Error creating SAML attribute: " + e.getMessage());
175                 } catch (IOException e) {
176                         System.err.println("Error serializing output from Resolver: " + e.getMessage());
177                 }
178         }
179
180         private static void checkRequired() {
181                 if (file == null || user == null) {
182                         System.err.println("Missing required parameter(s).");
183                         try {
184                                 Thread.sleep(100); //silliness to get error to print first
185                         } catch (InterruptedException e) {
186                                 //doesn't matter
187                         }
188                         printUsage(System.out);
189                         System.exit(1);
190                 }
191         }
192
193         private static void configureLogging(boolean debugOn) {
194
195                 BasicConfigurator.configure();
196                 if (debug) {
197                         Logger.getRootLogger().setLevel(Level.DEBUG);
198                 } else {
199                         Logger.getRootLogger().setLevel(Level.INFO);
200                         Logger.getLogger("edu.internet2.middleware.shibboleth.aa.attrresolv").setLevel(Level.WARN);
201                 }
202                 Logger.getLogger("org.apache.xml.security").setLevel(Level.OFF);
203         }
204
205         private static void printUsage(PrintStream out) {
206
207                 out.println("Usage: resolvertest [options]..." + System.getProperty("line.separator"));
208                 out.println("Tests an AA Attribute Resolver configuration." + System.getProperty("line.separator"));
209
210                 out.println("-d, --debug                   run in debug mode");
211                 out.println("-h, --help                    print usage information");
212                 out.println("-f, --file=FILEURL            the URL of the resolver configuration");
213                 out.println("                                file (resolver.xml)");
214                 out.println("-u, --user=USER               the user for which attributes should");
215                 out.println("                                be resolved");
216                 out.println("-r, --requester=REQUESTER     the name of the requester (SHAR),");
217                 out.println("                                emulates unauthenticated requester if");
218                 out.println("                                not specified" + System.getProperty("line.separator"));
219
220         }
221 }