https://bugs.internet2.edu/jira/browse/SIDP-281
[java-idp.git] / src / main / java / edu / internet2 / middleware / shibboleth / idp / StatusServlet.java
1 /*
2  * Copyright 2009 University Corporation for Advanced Internet Development, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package edu.internet2.middleware.shibboleth.idp;
18
19 import java.io.IOException;
20 import java.io.PrintWriter;
21
22 import javax.servlet.ServletConfig;
23 import javax.servlet.ServletException;
24 import javax.servlet.http.HttpServlet;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.joda.time.DateTime;
29 import org.joda.time.chrono.ISOChronology;
30 import org.joda.time.format.DateTimeFormatter;
31 import org.joda.time.format.ISODateTimeFormat;
32 import org.opensaml.xml.security.x509.X509Credential;
33 import org.opensaml.xml.util.Base64;
34
35 import edu.internet2.middleware.shibboleth.common.attribute.resolver.AttributeResolutionException;
36 import edu.internet2.middleware.shibboleth.common.attribute.resolver.AttributeResolver;
37 import edu.internet2.middleware.shibboleth.common.relyingparty.RelyingPartyConfiguration;
38 import edu.internet2.middleware.shibboleth.common.relyingparty.RelyingPartyConfigurationManager;
39 import edu.internet2.middleware.shibboleth.idp.util.HttpServletHelper;
40
41 /** A Servlet for displaying the status of the IdP. */
42 public class StatusServlet extends HttpServlet {
43
44     /** Serial version UID. */
45     private static final long serialVersionUID = 7917509317276109266L;
46
47     /** Formatter used when print date/times. */
48     private DateTimeFormatter dateFormat;
49
50     /** Time the IdP started up. */
51     private DateTime startTime;
52
53     /** Attribute resolver used by the IdP. */
54     private AttributeResolver<?> attributeResolver;
55
56     /** Relying party configuration manager used by the IdP. */
57     private RelyingPartyConfigurationManager rpConfigManager;
58
59     /** {@inheritDoc} */
60     public void init(ServletConfig config) throws ServletException {
61         super.init(config);
62
63         dateFormat = ISODateTimeFormat.dateTimeNoMillis();
64         startTime = new DateTime(ISOChronology.getInstanceUTC());
65         attributeResolver = HttpServletHelper.getAttributeResolver(config.getServletContext());
66         rpConfigManager = HttpServletHelper.getRelyingPartyConfirmationManager(config.getServletContext());
67     }
68
69     /** {@inheritDoc} */
70     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
71         resp.setContentType("text/plain");
72         PrintWriter output = resp.getWriter();
73
74         printOperatingEnvironmentInformation(output);
75         output.println();
76         printIdPInformation(output);
77         output.println();
78         printRelyingPartyConfigurationsInformation(output, req.getParameter("relyingParty"));
79
80         output.flush();
81     }
82
83     /**
84      * Prints out information about the operating environment. This includes the operating system name, version and
85      * architecture, the JDK version, available CPU cores, memory currently used by the JVM process, the maximum amount
86      * of memory that may be used by the JVM, and the current time in UTC.
87      * 
88      * @param out output writer to which information will be written
89      */
90     protected void printOperatingEnvironmentInformation(PrintWriter out) {
91         Runtime runtime = Runtime.getRuntime();
92
93         out.println("### Operating Environment Information");
94         out.println("operating_system: " + System.getProperty("os.name"));
95         out.println("operating_system_version: " + System.getProperty("os.version"));
96         out.println("operating_system_architecture: " + System.getProperty("os.arch"));
97         out.println("jdk_version: " + System.getProperty("java.version"));
98         out.println("available_cores: " + runtime.availableProcessors());
99         out.println("used_memory: " + runtime.totalMemory() / 1048576 + "MB");
100         out.println("maximum_memory: " + runtime.maxMemory() / 1048576 + "MB");
101         out.println("current_time: " + new DateTime(ISOChronology.getInstanceUTC()).toString(dateFormat));
102     }
103
104     /**
105      * Prints out general IdP information. This includes IdP version, start up time, and whether the attribute resolver
106      * is currently operational.
107      * 
108      * @param out output writer to which information will be written
109      */
110     protected void printIdPInformation(PrintWriter out) {
111         Package pkg = Version.class.getPackage();
112
113         out.println("### Identity Provider Information");
114         out.println("idp_version: " + pkg.getImplementationVersion());
115         out.println("idp_start_time: " + startTime.toString(dateFormat));
116         try {
117             attributeResolver.validate();
118             out.println("attribute_resolver_valid: " + Boolean.TRUE);
119         } catch (AttributeResolutionException e) {
120             out.println("attribute_resolver_valid: " + Boolean.FALSE);
121         }
122     }
123
124     /**
125      * Prints information about relying party configurations. If the given relying party is null then the configuration
126      * for all relying parties is printed. If the relying party ID is not null then the relying party configurations for
127      * that entity is printed.
128      * 
129      * @param out output writer to which information will be written
130      * @param relyingPartyId entity ID of the relying party whose configuration should be printed
131      */
132     protected void printRelyingPartyConfigurationsInformation(PrintWriter out, String relyingPartyId) {
133         out.println("### Relying Party Configurations");
134
135         if (relyingPartyId == null) {
136             for (RelyingPartyConfiguration config : rpConfigManager.getRelyingPartyConfigurations().values()) {
137                 printRelyingPartyConfigurationInformation(out, config);
138                 out.println();
139             }
140         } else {
141             RelyingPartyConfiguration config = rpConfigManager.getRelyingPartyConfiguration(relyingPartyId);
142             printRelyingPartyConfigurationInformation(out, config);
143             out.println();
144         }
145     }
146
147     /**
148      * Prints out the information for a specific relying party configuration. This information includes the relying
149      * party or relying party group ID, the entity ID of the IdP when it responds when using this configuration, the
150      * default authentication method used for this config, and configured communication profiles.
151      * 
152      * @param out output writer to which information will be written
153      * @param config the relying party configuration
154      */
155     protected void printRelyingPartyConfigurationInformation(PrintWriter out, RelyingPartyConfiguration config) {
156         out.println("relying_party_id: " + config.getRelyingPartyId());
157         out.println("idp_entity_id: " + config.getProviderId());
158         
159         if (config.getDefaultAuthenticationMethod() != null) {
160             out.println("default_authentication_method: " + config.getDefaultAuthenticationMethod());
161         } else {
162             out.println("default_authentication_method: none");
163         }
164
165         try{
166             X509Credential signingCredential = (X509Credential) config.getDefaultSigningCredential();
167             out.println("default_signing_tls_key: " + Base64.encodeBytes(signingCredential.getEntityCertificate().getEncoded(), Base64.DONT_BREAK_LINES));
168         }catch(Throwable t){
169             // swallow error
170         }
171         
172         for (String profileId : config.getProfileConfigurations().keySet()) {
173             out.println("configured_communication_profile: " + profileId);
174         }
175     }
176 }