Updated test to work with new config strategy.
[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 jargs.gnu.CmdLineParser;
53
54 import java.io.ByteArrayOutputStream;
55 import java.io.IOException;
56 import java.io.PrintStream;
57
58 import org.apache.log4j.BasicConfigurator;
59 import org.apache.log4j.Level;
60 import org.apache.log4j.Logger;
61 import org.apache.xml.serialize.OutputFormat;
62 import org.apache.xml.serialize.XMLSerializer;
63 import org.opensaml.SAMLException;
64 import org.w3c.dom.Element;
65 import org.w3c.dom.Node;
66
67 import edu.internet2.middleware.shibboleth.aa.AAAttribute;
68 import edu.internet2.middleware.shibboleth.aa.AAAttributeSet;
69 import edu.internet2.middleware.shibboleth.aa.AAAttributeSet.ShibAttributeIterator;
70 import edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolver;
71 import edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolverException;
72 import edu.internet2.middleware.shibboleth.common.AuthNPrincipal;
73
74 /**
75  * Utility for testing an Attribute Resolver configuration.
76  * 
77  * @author Walter Hoehn
78  */
79 public class ResolverTest {
80
81         private static boolean debug = false;
82         private static String file = null;
83         private static String requester = null;
84         private static String user = null;
85
86         public static void main(String[] args) {
87
88                 CmdLineParser parser = new CmdLineParser();
89                 CmdLineParser.Option helpOption = parser.addBooleanOption('h', "help");
90                 CmdLineParser.Option debugOption = parser.addBooleanOption('d', "debug");
91                 CmdLineParser.Option fileOption = parser.addStringOption('f', "file");
92                 CmdLineParser.Option userOption = parser.addStringOption('u', "user");
93                 CmdLineParser.Option requesterOption = parser.addStringOption('r', "requester");
94
95                 try {
96                         parser.parse(args);
97                 } catch (CmdLineParser.OptionException e) {
98                         System.err.println(e.getMessage());
99                         try {
100                                 Thread.sleep(100); //silliness to get error to print first
101                         } catch (InterruptedException ie) {
102                                 //doesn't matter
103                         }
104                         printUsage(System.out);
105                         System.exit(1);
106                 }
107
108                 Boolean helpEnabled = (Boolean) parser.getOptionValue(helpOption);
109                 if (helpEnabled != null && helpEnabled.booleanValue()) {
110                         printUsage(System.out);
111                         System.exit(0);
112                 }
113
114                 Boolean debugEnabled = ((Boolean) parser.getOptionValue(debugOption));
115                 if (debugEnabled != null) {
116                         debug = debugEnabled.booleanValue();
117                 }
118
119                 file = (String) parser.getOptionValue(fileOption);
120                 user = (String) parser.getOptionValue(userOption);
121                 requester = (String) parser.getOptionValue(requesterOption);
122
123                 configureLogging(debug);
124                 checkRequired();
125
126                 try {
127                         AttributeResolver resolver = new AttributeResolver(file);
128                         String[] attributes = resolver.listRegisteredAttributeDefinitionPlugIns();
129
130                         AAAttributeSet attributeSet = new AAAttributeSet();
131                         for (int i = 0; i < attributes.length; i++) {
132                                 attributeSet.add(new AAAttribute(attributes[i]));
133                         }
134
135                         resolver.resolveAttributes(new AuthNPrincipal(user), requester, attributeSet);
136
137                         System.out.println(
138                                 "Received the following back from the Attribute Resolver:" + System.getProperty("line.separator"));
139
140                         for (ShibAttributeIterator iterator = attributeSet.shibAttributeIterator(); iterator.hasNext();) {
141                                 AAAttribute attribute = iterator.nextShibAttribute();
142                                 Node node = attribute.toDOM();
143                                 ByteArrayOutputStream xml = new ByteArrayOutputStream();
144                                 if (!(node instanceof Element)) {
145                                         throw new IOException("Received bad Element data from SAML library.");
146                                 }
147                                 OutputFormat format = new OutputFormat();
148                                 format.setIndenting(true);
149                                 format.setIndent(4);
150                                 new XMLSerializer(xml, format).serialize((Element) node);
151                                 System.out.println(xml.toString() + System.getProperty("line.separator"));
152                         }
153                 
154                 } catch (AttributeResolverException e) {
155                         System.err.println("Error initializing the Attribute Resolver: " + e.getMessage());
156                 } catch (SAMLException e) {
157                         System.err.println("Error creating SAML attribute: " + e.getMessage());
158                 } catch (IOException e) {
159                         System.err.println("Error serializing output from Resolver: " + e.getMessage());
160                 }
161         }
162
163         /**
164          * Ensures that all required parameters were specified and successfully parsed.
165          *
166          */
167         private static void checkRequired() {
168                 if (file == null || user == null) {
169                         System.err.println("Missing required parameter(s).");
170                         try {
171                                 Thread.sleep(100); //silliness to get error to print first
172                         } catch (InterruptedException e) {
173                                 //doesn't matter
174                         }
175                         printUsage(System.out);
176                         System.exit(1);
177                 }
178         }
179
180         private static void configureLogging(boolean debugEnabled) {
181
182                 BasicConfigurator.configure();
183                 if (debugEnabled) {
184                         Logger.getRootLogger().setLevel(Level.DEBUG);
185                 } else {
186                         Logger.getRootLogger().setLevel(Level.INFO);
187                         Logger.getLogger("edu.internet2.middleware.shibboleth.aa.attrresolv").setLevel(Level.WARN);
188                 }
189                 Logger.getLogger("org.apache.xml.security").setLevel(Level.OFF);
190         }
191
192         private static void printUsage(PrintStream out) {
193
194                 out.println("Usage: resolvertest [options]..." + System.getProperty("line.separator"));
195                 out.println("Tests an AA Attribute Resolver configuration." + System.getProperty("line.separator"));
196
197                 out.println("-d, --debug                   run in debug mode");
198                 out.println("-h, --help                    print usage information");
199                 out.println("-f, --file=FILEURL            the URL of the resolver configuration");
200                 out.println("                                file (resolver.xml)");
201                 out.println("-u, --user=USER               the user for which attributes should");
202                 out.println("                                be resolved");
203                 out.println("-r, --requester=REQUESTER     the name of the requester (SHAR),");
204                 out.println("                                emulates unauthenticated requester if");
205                 out.println("                                not specified" + System.getProperty("line.separator"));
206
207         }
208 }