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