001    /*
002     * Cumulus4j - Securing your data in the cloud - http://cumulus4j.org
003     * Copyright (C) 2011 NightLabs Consulting GmbH
004     *
005     * This program is free software: you can redistribute it and/or modify
006     * it under the terms of the GNU Affero General Public License as
007     * published by the Free Software Foundation, either version 3 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU Affero General Public License for more details.
014     *
015     * You should have received a copy of the GNU Affero General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    package org.cumulus4j.keymanager.front.webapp;
019    
020    import java.util.Collections;
021    import java.util.HashSet;
022    import java.util.Set;
023    
024    import javax.ws.rs.ApplicationPath;
025    import javax.ws.rs.core.Application;
026    
027    import org.slf4j.Logger;
028    import org.slf4j.LoggerFactory;
029    
030    /**
031     * <p>
032     * REST application for the key-server.
033     * <p></p>
034     * This class is the entry point for Jersey where all REST services and their environment is declared.
035     * </p>
036     *
037     * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
038     */
039    @ApplicationPath("/")
040    public class KeyManagerFrontWebApp
041    extends Application
042    {
043            private static final Logger logger = LoggerFactory.getLogger(KeyManagerFrontWebApp.class);
044    
045    //      /**
046    //       * <p>
047    //       * System property to control which key file is to be used. If not specified,
048    //       * the file "&#36;{user.home}/.cumulus4j/cumulus4j.keystore" will be used.
049    //       * </p>
050    //       * <p>
051    //       * You can use system properties in this system-property's value. For example
052    //       * passing "-Dorg.cumulus4j.keymanager.front.webapp.App.keyStoreFile=&#36;{java.io.tmpdir}/test.keystore"
053    //       * to the java command will be resolved to "/tmp/test.keystore" on GNU+Linux.
054    //       * </p>
055    //       * @deprecated to be removed due to introduction of keyStoreID
056    //       */
057    //      @Deprecated
058    //      public static final String SYSTEM_PROPERTY_KEY_STORE_FILE = "cumulus4j.KeyManagerFrontWebApp.keyStoreFile";
059    
060    //      /**
061    //       * <p>
062    //       * System property to control whether to delete the key-store-file on startup.
063    //       * Possible values are "true" and "false".
064    //       * </p>
065    //       * <p>
066    //       * <b>Important:</b> This feature is for debugging and test reasons only! Never use it
067    //       * on a productive system or you will loose all your keys (and thus your complete database)!!!
068    //       * </p>
069    //       * @deprecated TODO rename this after introduction of keyStoreID as they are not deleted on startup anymore but when first accessed.
070    //       */
071    //      @Deprecated
072    //      public static final String SYSTEM_PROPERTY_DELETE_KEY_STORE_FILE_ON_STARTUP = "cumulus4j.KeyManagerFrontWebApp.deleteKeyStoreFileOnStartup";
073    
074            private static final Class<?>[] serviceClassesArray = {
075                    AppServerService.class,
076                    DateDependentKeyStrategyService.class,
077                    CryptoSessionService.class,
078                    UserService.class
079            };
080    
081            private static final Set<Class<?>> serviceClassesSet;
082            static {
083                    Set<Class<?>> s = new HashSet<Class<?>>(serviceClassesArray.length);
084                    for (Class<?> c : serviceClassesArray)
085                            s.add(c);
086    
087                    serviceClassesSet = Collections.unmodifiableSet(s);
088    
089                    if (logger.isDebugEnabled()) {
090                            logger.debug("<init>: Service classes:");
091                            for (Class<?> c : serviceClassesSet)
092                                    logger.debug("<init>: {}", c == null ? null : c.getName());
093                    }
094            }
095    
096            @Override
097            public Set<Class<?>> getClasses() {
098                    return serviceClassesSet;
099            }
100    
101            private Set<Object> singletons;
102    
103    //      private File keyStoreFile;
104    //      private KeyStore keyStore;
105    
106    //      private void initKeyStoreFile()
107    //      {
108    //              String keyStoreFileSysPropVal = System.getProperty(SYSTEM_PROPERTY_KEY_STORE_FILE);
109    //              if (keyStoreFileSysPropVal == null || keyStoreFileSysPropVal.trim().isEmpty()) {
110    //                      keyStoreFile = new File(new File(getUserHome(), ".cumulus4j"), "cumulus4j.keystore");
111    //                      logger.info(
112    //                                      "getSingletons: System property '{}' is empty or not specified. Using default keyStoreFile '{}'.",
113    //                                      SYSTEM_PROPERTY_KEY_STORE_FILE, keyStoreFile.getAbsolutePath()
114    //                      );
115    //              }
116    //              else {
117    //                      String keyStoreFileSysPropValResolved = SystemPropertyUtil.resolveSystemProperties(keyStoreFileSysPropVal);
118    //                      keyStoreFile = new File(keyStoreFileSysPropValResolved);
119    //                      logger.info(
120    //                                      "getSingletons: System property '{}' was set to '{}'. Using keyStoreFile '{}'.",
121    //                                      new Object[] { SYSTEM_PROPERTY_KEY_STORE_FILE, keyStoreFileSysPropVal, keyStoreFile.getAbsolutePath() }
122    //                      );
123    //              }
124    //      }
125    //
126    //      private void deleteKeyStoreIfSysPropRequested() throws IOException {
127    //              String deleteKS = System.getProperty(SYSTEM_PROPERTY_DELETE_KEY_STORE_FILE_ON_STARTUP);
128    //              if (Boolean.TRUE.toString().equalsIgnoreCase(deleteKS)) {
129    //                      if (keyStoreFile.exists()) {
130    //                              logger.warn(
131    //                                              "getSingletons: System property '{}' was set to 'true'. DELETING keyStoreFile '{}'!!!",
132    //                                              SYSTEM_PROPERTY_DELETE_KEY_STORE_FILE_ON_STARTUP, keyStoreFile.getAbsolutePath()
133    //                              );
134    //                              if (!keyStoreFile.delete())
135    //                                      throw new IOException("Could not delete keyStoreFile '" + keyStoreFile.getAbsolutePath() + "'!");
136    //                      }
137    //                      else {
138    //                              logger.warn(
139    //                                              "getSingletons: System property '{}' was set to 'true', but keyStoreFile '{}' does NOT exist, hence not deleting it!",
140    //                                              SYSTEM_PROPERTY_DELETE_KEY_STORE_FILE_ON_STARTUP, keyStoreFile.getAbsolutePath()
141    //                              );
142    //                      }
143    //              }
144    //      }
145    
146    //      private void checkForDeprecatedSystemProperties()
147    //      {
148    //              checkForDeprecatedSystemProperty(SYSTEM_PROPERTY_KEY_STORE_FILE);
149    //              checkForDeprecatedSystemProperty(SYSTEM_PROPERTY_DELETE_KEY_STORE_FILE_ON_STARTUP);
150    //      }
151    //
152    //      private void checkForDeprecatedSystemProperty(String sysPropName)
153    //      {
154    //              if (System.getProperty(sysPropName) != null) {
155    //                      logger.error("**************************************************************************");
156    //                      logger.error("**************************************************************************");
157    //                      logger.error("**************************************************************************");
158    //
159    //                      logger.error("*** deprecated system property present (and ignored): " + sysPropName);
160    //
161    //                      logger.error("**************************************************************************");
162    //                      logger.error("**************************************************************************");
163    //                      logger.error("**************************************************************************");
164    //              }
165    //      }
166    
167            @Override
168            public Set<Object> getSingletons()
169            {
170                    if (singletons == null) {
171    //                      checkForDeprecatedSystemProperties();
172    //                      initKeyStoreFile();
173    //
174    //                      try {
175    //                              deleteKeyStoreIfSysPropRequested();
176    //
177    //                              if (!keyStoreFile.getParentFile().isDirectory()) {
178    //                                      keyStoreFile.getParentFile().mkdirs();
179    //                                      if (!keyStoreFile.getParentFile().isDirectory())
180    //                                              throw new IOException("Directory does not exist and could not be created: " + keyStoreFile.getParentFile().getAbsolutePath());
181    //                              }
182    //
183    //                              logger.info("Opening keyStoreFile: {}", keyStoreFile.getAbsolutePath());
184    //                              keyStore = new KeyStore(keyStoreFile);
185    //                      } catch (IOException x) {
186    //                              throw new RuntimeException(x);
187    //                      }
188    
189                            Set<Object> s = new HashSet<Object>();
190    //                      s.add(new KeyStoreProvider(keyStore));
191    //                      s.add(new AppServerManagerProvider(new AppServerManager(keyStore)));
192                            s.add(new KeyStoreManagerProvider(new KeyStoreManager()));
193                            singletons = Collections.unmodifiableSet(s);
194                    }
195    
196                    return singletons;
197            }
198    }