You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2014/09/08 08:55:38 UTC

git commit: [KARAF-3006] Align with KARAF-3207

Repository: karaf
Updated Branches:
  refs/heads/master a67ce95c5 -> f57ac3ddf


[KARAF-3006] Align with KARAF-3207

Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/f57ac3dd
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/f57ac3dd
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/f57ac3dd

Branch: refs/heads/master
Commit: f57ac3ddfc05be948f3edece41445bddb60f63bc
Parents: a67ce95
Author: Guillaume Nodet <gn...@gmail.com>
Authored: Mon Sep 8 08:55:28 2014 +0200
Committer: Guillaume Nodet <gn...@gmail.com>
Committed: Mon Sep 8 08:55:28 2014 +0200

----------------------------------------------------------------------
 .../modules/audit/AbstractAuditLoginModule.java |  91 +++++++++++++
 .../audit/EventAdminAuditLoginModule.java       |  83 ++++++++++++
 .../modules/audit/FileAuditLoginModule.java     |  72 ++++++++++
 .../eventadmin/EventAdminLoginModule.java       | 131 -------------------
 .../karaf/jaas/modules/impl/Activator.java      |   4 +
 .../karaf/jaas/modules/impl/KarafRealm.java     |  18 ++-
 6 files changed, 263 insertions(+), 136 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/f57ac3dd/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/audit/AbstractAuditLoginModule.java
----------------------------------------------------------------------
diff --git a/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/audit/AbstractAuditLoginModule.java b/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/audit/AbstractAuditLoginModule.java
new file mode 100644
index 0000000..5349246
--- /dev/null
+++ b/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/audit/AbstractAuditLoginModule.java
@@ -0,0 +1,91 @@
+/*
+ *  Licensed 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.
+ *  under the License.
+ */
+package org.apache.karaf.jaas.modules.audit;
+
+import java.util.Map;
+
+import javax.security.auth.Subject;
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.NameCallback;
+import javax.security.auth.login.LoginException;
+import javax.security.auth.spi.LoginModule;
+
+public abstract class AbstractAuditLoginModule implements LoginModule {
+
+    enum Action {
+        ATTEMPT,
+        SUCCESS,
+        FAILURE,
+        LOGOUT
+    }
+
+    protected Subject subject;
+    private CallbackHandler handler;
+    private String username;
+    private boolean enabled;
+
+    public void initialize(Subject subject, CallbackHandler callbackHandler,
+                           Map sharedState, Map options) {
+        this.subject = subject;
+        enabled = Boolean.parseBoolean((String) options.get("enabled"));
+        handler = callbackHandler;
+    }
+
+    protected abstract void audit(Action action, String user);
+
+    public boolean login() throws LoginException {
+        NameCallback user = new NameCallback("User name:");
+        Callback[] callbacks = new Callback[]{user};
+        try {
+            handler.handle(callbacks);
+        } catch (Exception e) {
+            throw (LoginException) new LoginException("Unable to process callback: " + e.getMessage()).initCause(e);
+        }
+        if (callbacks.length != 1) {
+            throw new IllegalStateException("Number of callbacks changed by server!");
+        }
+        user = (NameCallback) callbacks[0];
+        username = user.getName();
+        if (enabled && username != null) {
+            audit(Action.ATTEMPT, username);
+        }
+        return false;
+    }
+
+    public boolean commit() throws LoginException {
+        if (enabled && username != null) {
+            audit(Action.SUCCESS, username);
+        }
+        return false;
+    }
+
+    public boolean abort() throws LoginException {
+        if (enabled && username != null) { //work around initial "fake" login
+            audit(Action.FAILURE, username);
+            username = null;
+        }
+        return false;
+    }
+
+    public boolean logout() throws LoginException {
+        if (enabled && username != null) {
+            audit(Action.LOGOUT, username);
+            username = null;
+        }
+        return false;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/karaf/blob/f57ac3dd/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/audit/EventAdminAuditLoginModule.java
----------------------------------------------------------------------
diff --git a/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/audit/EventAdminAuditLoginModule.java b/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/audit/EventAdminAuditLoginModule.java
new file mode 100644
index 0000000..5303e86
--- /dev/null
+++ b/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/audit/EventAdminAuditLoginModule.java
@@ -0,0 +1,83 @@
+/*
+ *  Licensed 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.
+ *  under the License.
+ */
+package org.apache.karaf.jaas.modules.audit;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.security.auth.Subject;
+import javax.security.auth.callback.CallbackHandler;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventAdmin;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class EventAdminAuditLoginModule extends AbstractAuditLoginModule {
+
+    public static final String TOPIC_EVENTS = "org/apache/karaf/login/";
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(EventAdminAuditLoginModule.class);
+    private static boolean errorLogged;
+
+    private BundleContext bundleContext;
+    private String topic;
+
+    public void initialize(Subject subject, CallbackHandler callbackHandler,
+                           Map sharedState, Map options) {
+        super.initialize(subject, callbackHandler, sharedState, options);
+        bundleContext = (BundleContext) options.get(BundleContext.class.getName());
+        topic = (String) options.get("topic");
+        if (topic == null) {
+            topic = TOPIC_EVENTS;
+        } else if (!topic.endsWith("/")) {
+            topic += "/";
+        }
+    }
+
+    @Override
+    protected void audit(Action action, String user) {
+        try {
+            EventAdminAuditor.audit(bundleContext, topic + action.toString().toUpperCase(), user, subject);
+        } catch (Throwable t) {
+            if (!errorLogged) {
+                errorLogged = true;
+                LOGGER.warn("Unable to send security auditing EventAdmin events: " + t);
+            }
+        }
+    }
+
+    static class EventAdminAuditor {
+        public static void audit(BundleContext bundleContext, String topic, String username, Subject subject) {
+            ServiceReference<EventAdmin> ref = bundleContext.getServiceReference(EventAdmin.class);
+            if (ref != null) {
+                EventAdmin eventAdmin = bundleContext.getService(ref);
+                try {
+                    Map<String, Object> props = new HashMap<String, Object>();
+                    Event event = new Event(topic, props);
+                    props.put("type", topic.substring(topic.lastIndexOf("/") + 1).toLowerCase());
+                    props.put("timestamp", System.currentTimeMillis());
+                    props.put("username", username);
+                    props.put("subject", subject);
+                    eventAdmin.postEvent(event);
+                } finally {
+                    bundleContext.ungetService(ref);
+                }
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/karaf/blob/f57ac3dd/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/audit/FileAuditLoginModule.java
----------------------------------------------------------------------
diff --git a/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/audit/FileAuditLoginModule.java b/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/audit/FileAuditLoginModule.java
new file mode 100644
index 0000000..eda7c71
--- /dev/null
+++ b/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/audit/FileAuditLoginModule.java
@@ -0,0 +1,72 @@
+/*
+ *  Licensed 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.
+ *  under the License.
+ */
+package org.apache.karaf.jaas.modules.audit;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.nio.channels.FileChannel;
+import java.nio.channels.FileLock;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Map;
+
+import javax.security.auth.Subject;
+import javax.security.auth.callback.CallbackHandler;
+
+public class FileAuditLoginModule extends AbstractAuditLoginModule {
+
+    public static final String LOG_FILE_OPTION = "file";
+
+    private final static DateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
+    private String logFile;
+
+    public void initialize(Subject subject, CallbackHandler callbackHandler,
+                           Map sharedState, Map options) {
+        super.initialize(subject, callbackHandler, sharedState, options);
+        logFile = (String) options.get(LOG_FILE_OPTION);
+    }
+
+    protected synchronized void audit(Action action, String username) {
+        Date date = new Date();
+        try {
+            File file = new File(logFile);
+            file.getParentFile().mkdirs();
+            FileOutputStream out = new FileOutputStream(file, true);
+            FileChannel channel = out.getChannel();
+            FileLock lock = channel.lock(0, Long.MAX_VALUE, false);
+            PrintWriter writer = new PrintWriter(out, false);
+            String actionStr;
+            switch (action) {
+            case ATTEMPT: actionStr = "Authentication attempt"; break;
+            case SUCCESS: actionStr = "Authentication succeeded"; break;
+            case FAILURE: actionStr = "Authentication failed"; break;
+            case LOGOUT: actionStr = "Explicit logout"; break;
+            default: actionStr = action.toString(); break;
+            }
+            writer.println(DATE_FORMAT.format(date) + " - " + actionStr + " - " + username);
+            writer.flush();
+            writer.close();
+            if (lock.isValid()) {
+                lock.release();
+            }
+        } catch (IOException e) {
+            throw new RuntimeException("Unable to write to authentication log file", e);
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/karaf/blob/f57ac3dd/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/eventadmin/EventAdminLoginModule.java
----------------------------------------------------------------------
diff --git a/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/eventadmin/EventAdminLoginModule.java b/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/eventadmin/EventAdminLoginModule.java
deleted file mode 100644
index e831fa6..0000000
--- a/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/eventadmin/EventAdminLoginModule.java
+++ /dev/null
@@ -1,131 +0,0 @@
-/*
- *  Licensed 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.
- *  under the License.
- */
-package org.apache.karaf.jaas.modules.eventadmin;
-
-import java.util.Dictionary;
-import java.util.Hashtable;
-import java.util.Map;
-
-import javax.security.auth.Subject;
-import javax.security.auth.callback.Callback;
-import javax.security.auth.callback.CallbackHandler;
-import javax.security.auth.callback.NameCallback;
-import javax.security.auth.login.LoginException;
-import javax.security.auth.spi.LoginModule;
-
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceReference;
-import org.osgi.service.event.Event;
-import org.osgi.service.event.EventAdmin;
-
-public class EventAdminLoginModule implements LoginModule {
-
-    public static final String TOPIC_EVENTS = "org/apache/karaf/jaas";
-    public static final String TOPIC_LOGIN = TOPIC_EVENTS + "/LOGIN";
-    public static final String TOPIC_SUCCESS = TOPIC_EVENTS + "/SUCCESS";
-    public static final String TOPIC_FAILURE = TOPIC_EVENTS + "/FAILURE";
-    public static final String TOPIC_LOGOUT = TOPIC_EVENTS + "/LOGOUT";
-
-    private Subject subject;
-    private CallbackHandler handler;
-    private Map<String, ?> options;
-    private String username;
-    private BundleContext bundleContext;
-
-    @Override
-    public void initialize(Subject subject, CallbackHandler handler, Map<String, ?> sharedState, Map<String, ?> options) {
-        this.subject = subject;
-        this.handler = handler;
-        this.options = options;
-        this.bundleContext = (BundleContext) options.get(BundleContext.class.getName());
-    }
-
-    @Override
-    public boolean login() throws LoginException {
-        NameCallback user = new NameCallback("User name:");
-        Callback[] callbacks = new Callback[]{user};
-        try {
-            handler.handle(callbacks);
-        } catch (Exception e) {
-            throw (LoginException) new LoginException("Unable to process callback: " + e.getMessage()).initCause(e);
-        }
-        if (callbacks.length != 1) {
-            throw new IllegalStateException("Number of callbacks changed by server!");
-        }
-        user = (NameCallback) callbacks[0];
-        username = user.getName();
-        sendEvent(TOPIC_LOGIN);
-        return false;
-    }
-
-    @Override
-    public boolean commit() throws LoginException {
-        if (username != null) {
-            sendEvent(TOPIC_SUCCESS);
-        }
-        return false;
-    }
-
-    @Override
-    public boolean abort() throws LoginException {
-        if (username != null) { //work around initial "fake" login
-            sendEvent(TOPIC_FAILURE);
-            username = null;
-        }
-        return false;
-    }
-
-    @Override
-    public boolean logout() throws LoginException {
-        if (username != null) {
-            sendEvent(TOPIC_LOGOUT);
-            username = null;
-        }
-        return false;
-    }
-
-    private void sendEvent(String topic) {
-        if (Boolean.parseBoolean((String) options.get("eventadmin.enabled"))) {
-            Dictionary<String, Object> props = new Hashtable<>();
-            props.put("type", topic.substring(topic.lastIndexOf("/") + 1).toLowerCase());
-            props.put("timestamp", System.currentTimeMillis());
-            props.put("username", username);
-            props.put("subject", subject);
-
-            try {
-                Inner.send(bundleContext, topic, props);
-            } catch (Throwable t) {
-                // Ignore
-            }
-        }
-    }
-
-    static class Inner {
-
-        public static void send(BundleContext bundleContext, String topic, Dictionary<String, Object> props) {
-            ServiceReference<EventAdmin> ref = bundleContext.getServiceReference(EventAdmin.class);
-            if (ref != null) {
-                EventAdmin admin = bundleContext.getService(ref);
-                try {
-                    admin.sendEvent(new Event(topic, props));
-                } finally {
-                    bundleContext.ungetService(ref);
-                }
-            }
-        }
-    }
-
-
-}

http://git-wip-us.apache.org/repos/asf/karaf/blob/f57ac3dd/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/impl/Activator.java
----------------------------------------------------------------------
diff --git a/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/impl/Activator.java b/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/impl/Activator.java
index db1d6de..4532692 100644
--- a/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/impl/Activator.java
+++ b/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/impl/Activator.java
@@ -104,6 +104,10 @@ public class Activator extends BaseActivator implements ManagedService {
         populate(config, ENCRYPTION_ALGORITHM, "MD5");
         populate(config, ENCRYPTION_ENCODING, "hexadecimal");
         populate(config, EVENTADMIN_ENABLED, "true");
+        populate(config, "audit.file.enabled", "true");
+        populate(config, "audit.file.file", System.getProperty("karaf.data") + "/security/audit.log");
+        populate(config, "audit.eventadmin.enabled", "true");
+        populate(config, "audit.eventadmin.topic", "org/apache/karaf/login");
         config.put(BundleContext.class.getName(), bundleContext);
         return config;
     }

http://git-wip-us.apache.org/repos/asf/karaf/blob/f57ac3dd/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/impl/KarafRealm.java
----------------------------------------------------------------------
diff --git a/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/impl/KarafRealm.java b/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/impl/KarafRealm.java
index 4df73f4..f22d978 100644
--- a/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/impl/KarafRealm.java
+++ b/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/impl/KarafRealm.java
@@ -28,9 +28,10 @@ public class KarafRealm implements JaasRealm {
 
     private static final String KARAF_ETC = System.getProperty("karaf.etc");
     private static final String REALM = "karaf";
-    private static final String EVENTADMIN_MODULE = "org.apache.karaf.jaas.modules.eventadmin.EventAdminLoginModule";
     private static final String PROPERTIES_MODULE = "org.apache.karaf.jaas.modules.properties.PropertiesLoginModule";
     private static final String PUBLIC_KEY_MODULE = "org.apache.karaf.jaas.modules.publickey.PublickeyLoginModule";
+    private static final String FILE_AUDIT_MODULE = "org.apache.karaf.jaas.modules.audit.FileAuditLoginModule";
+    private static final String EVENTADMIN_AUDIT_MODULE = "org.apache.karaf.jaas.modules.audit.EventAdminAuditLoginModule";
 
     private static final String MODULE = "org.apache.karaf.jaas.module";
 
@@ -72,16 +73,23 @@ public class KarafRealm implements JaasRealm {
         publicKeyOptions.put(ProxyLoginModule.PROPERTY_BUNDLE, Long.toString(bundleContext.getBundle().getBundleId()));
         publicKeyOptions.put("users", KARAF_ETC + File.separatorChar + "keys.properties");
 
+        Map<String, Object> fileOptions = new HashMap<>();
+        fileOptions.putAll(properties);
+        fileOptions.put(BundleContext.class.getName(), bundleContext);
+        fileOptions.put(ProxyLoginModule.PROPERTY_MODULE, FILE_AUDIT_MODULE);
+        fileOptions.put(ProxyLoginModule.PROPERTY_BUNDLE, Long.toString(bundleContext.getBundle().getBundleId()));
+
         Map<String, Object> eventadminOptions = new HashMap<>();
         eventadminOptions.putAll(properties);
         eventadminOptions.put(BundleContext.class.getName(), bundleContext);
-        eventadminOptions.put(ProxyLoginModule.PROPERTY_MODULE, EVENTADMIN_MODULE);
+        eventadminOptions.put(ProxyLoginModule.PROPERTY_MODULE, EVENTADMIN_AUDIT_MODULE);
         eventadminOptions.put(ProxyLoginModule.PROPERTY_BUNDLE, Long.toString(bundleContext.getBundle().getBundleId()));
 
         return new AppConfigurationEntry[] {
-                new AppConfigurationEntry(ProxyLoginModule.class.getName(), AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL, eventadminOptions),
-                new AppConfigurationEntry(ProxyLoginModule.class.getName(), AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT, propertiesOptions),
-                new AppConfigurationEntry(ProxyLoginModule.class.getName(), AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT, publicKeyOptions)
+                new AppConfigurationEntry(ProxyLoginModule.class.getName(), AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL, propertiesOptions),
+                new AppConfigurationEntry(ProxyLoginModule.class.getName(), AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL, publicKeyOptions),
+                new AppConfigurationEntry(ProxyLoginModule.class.getName(), AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL, fileOptions),
+                new AppConfigurationEntry(ProxyLoginModule.class.getName(), AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL, eventadminOptions)
         };
     }