incubator-kalumet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbono...@apache.org
Subject svn commit: r1203240 - in /incubator/kalumet/trunk/console/src/main/java/org/apache/kalumet/console: configuration/ConfigurationManager.java configuration/model/KalumetConsole.java configuration/model/Property.java utils/LdapUtils.java
Date Thu, 17 Nov 2011 16:18:32 GMT
Author: jbonofre
Date: Thu Nov 17 16:18:32 2011
New Revision: 1203240

URL: http://svn.apache.org/viewvc?rev=1203240&view=rev
Log:
Add the console configuration and configuration manager

Added:
    incubator/kalumet/trunk/console/src/main/java/org/apache/kalumet/console/configuration/ConfigurationManager.java
    incubator/kalumet/trunk/console/src/main/java/org/apache/kalumet/console/utils/LdapUtils.java
Modified:
    incubator/kalumet/trunk/console/src/main/java/org/apache/kalumet/console/configuration/model/KalumetConsole.java
    incubator/kalumet/trunk/console/src/main/java/org/apache/kalumet/console/configuration/model/Property.java

Added: incubator/kalumet/trunk/console/src/main/java/org/apache/kalumet/console/configuration/ConfigurationManager.java
URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/console/src/main/java/org/apache/kalumet/console/configuration/ConfigurationManager.java?rev=1203240&view=auto
==============================================================================
--- incubator/kalumet/trunk/console/src/main/java/org/apache/kalumet/console/configuration/ConfigurationManager.java
(added)
+++ incubator/kalumet/trunk/console/src/main/java/org/apache/kalumet/console/configuration/ConfigurationManager.java
Thu Nov 17 16:18:32 2011
@@ -0,0 +1,159 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.kalumet.console.configuration;
+
+import java.io.File;
+import java.util.Calendar;
+import java.util.Date;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.kalumet.console.configuration.model.KalumetConsole;
+import org.apache.kalumet.console.configuration.model.Property;
+import org.apache.kalumet.model.Kalumet;
+import org.apache.kalumet.model.log.Journal;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Load and manipulate the Kalumet configuration store.
+ */
+public class ConfigurationManager {
+
+    private final static transient Logger LOGGER = LoggerFactory.getLogger(ConfigurationManager.class);
+
+    private final static String KALUMET_CONSOLE_CONFIGURATION_FILE = "/kalumet-console-config.xml";
+    private final static String ENVIRONMENT_JOURNAL_FILE_EXTENSION = ".log";
+
+    private static KalumetConsole KALUMET_CONSOLE_CACHE = null;
+
+    private static Kalumet KALUMET_CACHE = null;
+    private static int KALUMET_CACHE_TIMEOUT_MINUTES = 5;
+    private static Date KALUMET_CACHE_DEPRECATION_DATE = null;
+
+    /**
+     * Load the Kalumet Console configuration.
+     *
+     * @return the Kalumet configuration object.
+     */
+    public final static KalumetConsole loadConfiguration() throws Exception {
+        if (KALUMET_CONSOLE_CACHE == null) {
+            LOGGER.debug("Loading Apache Kalumet console configuration from {}", KALUMET_CONSOLE_CONFIGURATION_FILE);
+            String configurationFile = null;
+            try {
+                configurationFile = ConfigurationManager.class.getResource(ConfigurationManager.KALUMET_CONSOLE_CONFIGURATION_FILE).toString();
+            } catch (NullPointerException nullPointerException) {
+                LOGGER.error("Apache Kalumet configuration file is not found in the server
classpath");
+                throw new IllegalStateException("Apache Kalumet configuration file is not
found in the server classpath");
+            }
+            KALUMET_CONSOLE_CACHE = KalumetConsole.digeste(configurationFile);
+        }
+        return KALUMET_CONSOLE_CACHE;
+    }
+
+    /**
+     * Get the Kalumet XML configuration location.
+     *
+     * @return the Kalumet XML configuration location.
+     */
+    private final static String getStoreFile() throws Exception {
+        KalumetConsole kalumetConsole = ConfigurationManager.loadConfiguration();
+        Property kalumetConsoleProperty = kalumetConsole.getProperty("ConfigurationLocation");
+        if (kalumetConsoleProperty == null) {
+            throw new IllegalStateException("The property ConfigurationLocation is not found
in the Apache Kalumet Console configuration. This property is required to use Apache Kalumet
Console and must contains the location (file: or http:) to the Kalumet configuration store");
+        }
+        return kalumetConsoleProperty.getValue();
+    }
+
+    /**
+     * Load the Kalumet configuration.
+     *
+     * @return the Kalumet configuration.
+     */
+    public final static Kalumet loadStore() throws Exception {
+        if (KALUMET_CACHE == null || KALUMET_CACHE_DEPRECATION_DATE.after(Calendar.getInstance().getTime()))
{
+            String kalumetConfigurationFile = ConfigurationManager.getStoreFile();
+            KALUMET_CACHE = Kalumet.digeste(kalumetConfigurationFile);
+            // update the deprecation date
+            Calendar timeout = Calendar.getInstance();
+            timeout.set(Calendar.MINUTE, timeout.get(Calendar.MINUTE) + KALUMET_CACHE_TIMEOUT_MINUTES);
+            KALUMET_CACHE_DEPRECATION_DATE = timeout.getTime();
+        }
+        return KALUMET_CACHE;
+    }
+
+    /**
+     * Read the Kalumet configuration store and return the XML raw content.
+     *
+     * @return the Kalumet XML raw content.
+     */
+    public final static String readStore() throws Exception {
+        String content = null;
+        return FileUtils.readFileToString(new File(ConfigurationManager.getStoreFile()),
"ISO-8859-1");
+    }
+
+    /**
+     * Write the Kalumet configuration.
+     *
+     * @param kalumet the Kalumet configuration object to store.
+     */
+    public final static void writeStore(Kalumet kalumet) throws Exception {
+        // get the kalumet configuration store location
+        String kalumetConfigurationLocation = ConfigurationManager.getStoreFile();
+        // write the file
+        kalumet.writeXMLFile(kalumetConfigurationLocation);
+        // update the cache
+        KALUMET_CACHE = kalumet;
+        // update the deprecation date
+        Calendar timeout = Calendar.getInstance();
+        timeout.set(Calendar.MINUTE, timeout.get(Calendar.MINUTE) + KALUMET_CACHE_TIMEOUT_MINUTES);
+        KALUMET_CACHE_DEPRECATION_DATE = timeout.getTime();
+    }
+
+    /**
+     * Get the Environment Journal location.
+     *
+     * @param environment the Environment name.
+     * @return the Environment Journal location.
+     */
+    public final static String getEnvironmentJournalFile(String environment) throws Exception
{
+        KalumetConsole kalumetConsole = ConfigurationManager.loadConfiguration();
+        Property kalumetConsoleProperty = kalumetConsole.getProperty("JournalsLocation");
+        if (kalumetConsoleProperty == null) {
+            throw new IllegalArgumentException("The property JournalsLocation is not found
in the Apache Kalumet Console configuration. This property is required to store the environment
journals and must contain the directory path for the journal files.");
+        }
+        return kalumetConsoleProperty.getValue() + "/" + environment + ConfigurationManager.ENVIRONMENT_JOURNAL_FILE_EXTENSION;
+    }
+
+    /**
+     * Read the environment journal.
+     *
+     * @param environment the <code>Environment</code> name.
+     * @return the environment journal object.
+     */
+    public final static Journal loadEnvironmentJournal(String environment) throws Exception
{
+        KalumetConsole kalumetConsole = ConfigurationManager.loadConfiguration();
+        Property kalumetJournalLocation = kalumetConsole.getProperty("JournalsLocation");
+        if (kalumetJournalLocation == null) {
+            throw new IllegalArgumentException("The property JournalsLocation is not found
in the Apache Kalumet Console configuration. This property is required to store the environment
journals and must contain the directory path for the journal files.");
+        }
+        Journal journal = null;
+        return Journal.digeste(kalumetJournalLocation.getValue() + "/" + environment + ConfigurationManager.ENVIRONMENT_JOURNAL_FILE_EXTENSION);
+    }
+
+}

Modified: incubator/kalumet/trunk/console/src/main/java/org/apache/kalumet/console/configuration/model/KalumetConsole.java
URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/console/src/main/java/org/apache/kalumet/console/configuration/model/KalumetConsole.java?rev=1203240&r1=1203239&r2=1203240&view=diff
==============================================================================
--- incubator/kalumet/trunk/console/src/main/java/org/apache/kalumet/console/configuration/model/KalumetConsole.java
(original)
+++ incubator/kalumet/trunk/console/src/main/java/org/apache/kalumet/console/configuration/model/KalumetConsole.java
Thu Nov 17 16:18:32 2011
@@ -94,7 +94,7 @@ public class KalumetConsole {
 
          kalumetConsole = (KalumetConsole) digester.parse(path);
       } catch (Exception e) {
-         throw new IOException("Can't read the Kalumet Console configuration", e);
+         throw new IOException("Can't read the Apache Kalumet console configuration", e);
       }
       return kalumetConsole;
    }

Modified: incubator/kalumet/trunk/console/src/main/java/org/apache/kalumet/console/configuration/model/Property.java
URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/console/src/main/java/org/apache/kalumet/console/configuration/model/Property.java?rev=1203240&r1=1203239&r2=1203240&view=diff
==============================================================================
--- incubator/kalumet/trunk/console/src/main/java/org/apache/kalumet/console/configuration/model/Property.java
(original)
+++ incubator/kalumet/trunk/console/src/main/java/org/apache/kalumet/console/configuration/model/Property.java
Thu Nov 17 16:18:32 2011
@@ -19,7 +19,7 @@
 package org.apache.kalumet.console.configuration.model;
 
 /**
- * Represents the <code>property</code> tag in the WebAutoDeploy-config.xml
+ * Represents the <code>property</code> tag in the kalumet-console-config.xml
  * configuration file.
  */
 public class Property {

Added: incubator/kalumet/trunk/console/src/main/java/org/apache/kalumet/console/utils/LdapUtils.java
URL: http://svn.apache.org/viewvc/incubator/kalumet/trunk/console/src/main/java/org/apache/kalumet/console/utils/LdapUtils.java?rev=1203240&view=auto
==============================================================================
--- incubator/kalumet/trunk/console/src/main/java/org/apache/kalumet/console/utils/LdapUtils.java
(added)
+++ incubator/kalumet/trunk/console/src/main/java/org/apache/kalumet/console/utils/LdapUtils.java
Thu Nov 17 16:18:32 2011
@@ -0,0 +1,143 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.kalumet.console.utils;
+
+import org.apache.kalumet.console.configuration.ConfigurationManager;
+import org.apache.kalumet.model.Kalumet;
+import org.apache.kalumet.model.User;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Hashtable;
+
+import javax.naming.Context;
+import javax.naming.NamingEnumeration;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.DirContext;
+import javax.naming.directory.InitialDirContext;
+import javax.naming.directory.SearchControls;
+import javax.naming.directory.SearchResult;
+
+
+/**
+ * LDAP utility class.
+ */
+public class LdapUtils {
+
+   private final static transient Logger LOGGER = LoggerFactory.getLogger(LdapUtils.class);
+
+   /**
+    * Try to bind a user id and password in a given LDAP directory.
+    * 
+    * @param user the user to bind.
+    * @param password the password to bind.
+    * @return true if the user is identified.
+    */
+   public static boolean bind(String user, String password) throws Exception {
+      LOGGER.debug("Try to bind the user {}", user);
+      // load Kalumet store
+      Kalumet kalumet;
+      kalumet = ConfigurationManager.loadStore();
+      if (kalumet.getProperty("LdapAuthentication") == null || kalumet.getProperty("LdapServer")
== null || kalumet.getProperty("LdapBaseDN") == null || kalumet.getProperty("LdapUidAttribute")
== null
+            || kalumet.getProperty("LdapMailAttribute") == null || kalumet.getProperty("LdapCnAttribute")
== null) {
+         LOGGER.error("All LDAP required properties are not present in Apache Kalumet configuration.
Check if the properties LdapAuthentication, LdapServer, LdapBaseDN, LdapUidAttribute, LdapMailAttribute,
LdapCnAttribute are presents in Apache Kalumet configuration.");
+         throw new IllegalStateException("All LDAP required properties are not present in
Apache Kalumet configuration. Check if the properties LdapAuthentication, LdapServer, LdapBaseDN,
LdapUidAttribute, LdapMailAttribute, LdapCnAttribute are presents in Apache Kalumet configuration.");
+      }
+      if (kalumet.getProperty("LdapAuthentication").getValue().equals("false")) {
+         LOGGER.error("The LDAP authentication is not active in Apache Kalumet configuration.
Can't bind on LDAP.");
+         throw new IllegalStateException("The LDAP authentication is not active in Apache
Kalumet. Can't bind on LDAP.");
+      }
+      // step 1 : connect to the LDAP server (anonymous) to get the user DN
+      LOGGER.debug("LDAP Authentification Backend Step 1 : grab the user DN");
+      LOGGER.debug("Create the LDAP initial context");
+      Hashtable env = new Hashtable();
+      // TODO use a generic LDAP Context Factory compliant with IBM JDK
+      env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
+      LOGGER.debug("Connect to the LDAP server ldap://" + kalumet.getProperty("LdapServer").getValue());
+      env.put(Context.PROVIDER_URL, "ldap://" + kalumet.getProperty("LdapServer").getValue());
+      String userDN;
+      String userName;
+      String userEmail;
+      try {
+         LOGGER.debug("Init the JNDI LDAP Dir context ...");
+         DirContext context = new InitialDirContext(env);
+         LOGGER.debug("Define the subtree scope search control. ");
+         SearchControls controls = new SearchControls();
+         controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
+         LOGGER.debug("Looking for the user in LDAP ...");
+         LOGGER.debug("  Base DN : " + kalumet.getProperty("LdapBaseDN").getValue());
+         LOGGER.debug("  Filter  : (" + kalumet.getProperty("LdapUidAttribute").getValue()
+ "=" + user + ")");
+         NamingEnumeration namingEnumeration = context.search(kalumet.getProperty("LdapBaseDN").getValue(),
"(" + kalumet.getProperty("LdapUidAttribute").getValue() + "=" + user + ")", controls);
+         if (!namingEnumeration.hasMore()) {
+            LOGGER.warn("User " + user + " not found in LDAP");
+            return false;
+         }
+         LOGGER.debug("Get the user object");
+         SearchResult result = (SearchResult) namingEnumeration.next();
+         LOGGER.debug("Get the attributes set");
+         Attributes attributes = result.getAttributes();
+         LOGGER.debug("Trying to get the DN attribute");
+         userDN = (String) result.getName();
+         LOGGER.debug("Get the LDAP user DN : " + userDN);
+         userName = (String) attributes.get(kalumet.getProperty("LdapCnAttribute").getValue()).get();
+         LOGGER.debug("Get the LDAP user name : " + userName);
+         userEmail = (String) attributes.get(kalumet.getProperty("LdapMailAttribute").getValue()).get();
+         LOGGER.debug("Get the LDAP user e-mail : " + userEmail);
+         context.close();
+      } catch (Exception e) {
+         LOGGER.error("Can't connect to the LDAP server.", e);
+         throw new IllegalStateException("Can't connect to the LDAP server.", e);
+      }
+      // step 2 : I have the DN, try to bind the user
+      LOGGER.debug("LDAP Authentification Backend Step 2 : bind the user with the DN/password");
+      env = new Hashtable();
+      env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
+      LOGGER.debug("Connect to the LDAP server ldap://" + kalumet.getProperty("LdapServer").getValue());
+      env.put(Context.PROVIDER_URL, "ldap://" + kalumet.getProperty("LdapServer").getValue());
+      LOGGER.debug("Define a simple authentication");
+      env.put(Context.SECURITY_AUTHENTICATION, "simple");
+      LOGGER.debug("Define the security principal to " + userDN + "," + kalumet.getProperty("LdapBaseDN").getValue());
+      env.put(Context.SECURITY_PRINCIPAL, userDN + "," + kalumet.getProperty("LdapBaseDN").getValue());
+      env.put(Context.SECURITY_CREDENTIALS, password);
+      LOGGER.debug("Init the JNDI context ...");
+      try {
+         LOGGER.debug("Directory context init");
+         DirContext context = new InitialDirContext(env);
+         LOGGER.debug("LDAP user bind successful");
+         context.close();
+         if (kalumet.getSecurity().getUser(user) == null) {
+            User securityUser = new User();
+            securityUser.setId(user);
+            securityUser.setName(userName);
+            securityUser.setEmail(userEmail);
+            kalumet.getSecurity().addUser(securityUser);
+            try {
+                ConfigurationManager.writeStore(kalumet);
+            } catch (Exception e) {
+               LOGGER.error("Can't write the LDAP user in Apache Kalumet configuration store",
e);
+            }
+         }
+      } catch(Exception e) {
+         LOGGER.error("User authentication failure using LDAP server", e);
+         return false;
+      }
+      return true;
+   }
+
+}



Mime
View raw message