karaf-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Guillaume Nodet <gno...@gmail.com>
Subject Re: svn commit: r1031695 - in /karaf/trunk/jaas: command/ command/src/main/java/org/apache/karaf/jaas/command/ command/src/main/resources/OSGI-INF/blueprint/ modules/src/main/java/org/apache/karaf/jaas/modules/ modules/src/main/java/org/apache/karaf/
Date Fri, 05 Nov 2010 21:13:43 GMT
On Fri, Nov 5, 2010 at 19:20,  <iocanel@apache.org> wrote:
> Author: iocanel
> Date: Fri Nov  5 18:20:56 2010
> New Revision: 1031695
>
> URL: http://svn.apache.org/viewvc?rev=1031695&view=rev
> Log:
> [KARAF-188] Added jaas:list and jaas:userlist commands.
>
> Added:
>    karaf/trunk/jaas/command/src/main/java/org/apache/karaf/jaas/command/ListPendingCommand.java
>      - copied, changed from r1028753, karaf/trunk/jaas/command/src/main/java/org/apache/karaf/jaas/command/ListCommand.java
>    karaf/trunk/jaas/command/src/main/java/org/apache/karaf/jaas/command/ListRealmsCommand.java
>    karaf/trunk/jaas/command/src/main/java/org/apache/karaf/jaas/command/ListUsersCommand.java
> Removed:
>    karaf/trunk/jaas/command/src/main/java/org/apache/karaf/jaas/command/ListCommand.java
> Modified:
>    karaf/trunk/jaas/command/pom.xml
>    karaf/trunk/jaas/command/src/main/resources/OSGI-INF/blueprint/jass-command.xml
>    karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/BackingEngine.java
>    karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/encryption/EncryptionSupport.java
>    karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCBackingEngine.java
>    karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/properties/PropertiesBackingEngine.java
>
> Modified: karaf/trunk/jaas/command/pom.xml
> URL: http://svn.apache.org/viewvc/karaf/trunk/jaas/command/pom.xml?rev=1031695&r1=1031694&r2=1031695&view=diff
> ==============================================================================
> --- karaf/trunk/jaas/command/pom.xml (original)
> +++ karaf/trunk/jaas/command/pom.xml Fri Nov  5 18:20:56 2010
> @@ -45,6 +45,11 @@
>         </dependency>
>         <dependency>
>             <groupId>org.apache.karaf.jaas</groupId>
> +            <artifactId>org.apache.karaf.jaas.boot</artifactId>
> +            <version>${project.version}</version>
> +        </dependency>
> +        <dependency>
> +            <groupId>org.apache.karaf.jaas</groupId>
>             <artifactId>org.apache.karaf.jaas.modules</artifactId>
>             <version>${project.version}</version>
>         </dependency>
>
> Copied: karaf/trunk/jaas/command/src/main/java/org/apache/karaf/jaas/command/ListPendingCommand.java
(from r1028753, karaf/trunk/jaas/command/src/main/java/org/apache/karaf/jaas/command/ListCommand.java)
> URL: http://svn.apache.org/viewvc/karaf/trunk/jaas/command/src/main/java/org/apache/karaf/jaas/command/ListPendingCommand.java?p2=karaf/trunk/jaas/command/src/main/java/org/apache/karaf/jaas/command/ListPendingCommand.java&p1=karaf/trunk/jaas/command/src/main/java/org/apache/karaf/jaas/command/ListCommand.java&r1=1028753&r2=1031695&rev=1031695&view=diff
> ==============================================================================
> --- karaf/trunk/jaas/command/src/main/java/org/apache/karaf/jaas/command/ListCommand.java
(original)
> +++ karaf/trunk/jaas/command/src/main/java/org/apache/karaf/jaas/command/ListPendingCommand.java
Fri Nov  5 18:20:56 2010
> @@ -17,8 +17,8 @@ import java.util.Queue;
>  *
>  * @author iocanel
>  */
> -@Command(scope = "jaas", name = "list", description = "Lists the modification on the
active realm/module.")
> -public class ListCommand extends JaasCommandSupport {
> +@Command(scope = "jaas", name = "pending", description = "Lists the modification on
the active realm/module.")
> +public class ListPendingCommand extends JaasCommandSupport {
>
>     @Override
>     protected Object doExecute() throws Exception {
> @@ -35,7 +35,7 @@ public class ListCommand extends JaasCom
>                     System.out.println(command);
>                 }
>             } else {
> -                System.err.println("No JAAS command in queue.");
> +                System.err.println("No JAAS command¾ in queue.");

Was that done on purpose?


>             }
>         } else {
>             System.err.println("No JAAS Realm / Module has been selected.");
>
> Added: karaf/trunk/jaas/command/src/main/java/org/apache/karaf/jaas/command/ListRealmsCommand.java
> URL: http://svn.apache.org/viewvc/karaf/trunk/jaas/command/src/main/java/org/apache/karaf/jaas/command/ListRealmsCommand.java?rev=1031695&view=auto
> ==============================================================================
> --- karaf/trunk/jaas/command/src/main/java/org/apache/karaf/jaas/command/ListRealmsCommand.java
(added)
> +++ karaf/trunk/jaas/command/src/main/java/org/apache/karaf/jaas/command/ListRealmsCommand.java
Fri Nov  5 18:20:56 2010
> @@ -0,0 +1,47 @@
> +package org.apache.karaf.jaas.command;
> +
> +import org.apache.felix.gogo.commands.Command;
> +import org.apache.karaf.jaas.boot.ProxyLoginModule;
> +import org.apache.karaf.jaas.config.JaasRealm;
> +import org.apache.karaf.jaas.modules.BackingEngine;
> +
> +import javax.security.auth.login.AppConfigurationEntry;
> +import java.util.List;
> +
> +/**
> + * @author iocanel
> + */
> +@Command(scope = "jaas", name = "list", description = "Lists the modification on the
active realm/module.")
> +public class ListRealmsCommand extends JaasCommandSupport {
> +
> +    private static final String REALM_LIST_FORMAT = "%-20s %-80s";
> +
> +    @Override
> +    protected Object doExecute(BackingEngine engine) throws Exception {
> +        return null;
> +    }
> +
> +    protected Object doExecute() throws Exception {
> +        List<JaasRealm> realms = getRealms();
> +        if (realms != null && realms.size() > 0) {
> +            System.out.println(String.format(REALM_LIST_FORMAT, "Realm", "Module
Class"));
> +            for (JaasRealm realm : realms) {
> +                String realmName = realm.getName();
> +                AppConfigurationEntry[] entries = realm.getEntries();
> +
> +                if (entries != null && entries.length > 0) {
> +                    for (int i = 0; i < entries.length; i++) {
> +                        String moduleClass = (String) entries[i].getOptions().get(ProxyLoginModule.PROPERTY_MODULE);
> +                        System.out.println(String.format(REALM_LIST_FORMAT,
realmName, moduleClass));
> +                    }
> +                } else {
> +                    System.out.println(String.format(REALM_LIST_FORMAT, realmName,
"No module found for realm."));
> +                }
> +            }
> +        } else {
> +            System.err.println("No realm found");
> +        }
> +        return null;
> +    }
> +
> +}
>
> Added: karaf/trunk/jaas/command/src/main/java/org/apache/karaf/jaas/command/ListUsersCommand.java
> URL: http://svn.apache.org/viewvc/karaf/trunk/jaas/command/src/main/java/org/apache/karaf/jaas/command/ListUsersCommand.java?rev=1031695&view=auto
> ==============================================================================
> --- karaf/trunk/jaas/command/src/main/java/org/apache/karaf/jaas/command/ListUsersCommand.java
(added)
> +++ karaf/trunk/jaas/command/src/main/java/org/apache/karaf/jaas/command/ListUsersCommand.java
Fri Nov  5 18:20:56 2010
> @@ -0,0 +1,61 @@
> +package org.apache.karaf.jaas.command;
> +
> +import org.apache.felix.gogo.commands.Command;
> +import org.apache.karaf.jaas.config.JaasRealm;
> +import org.apache.karaf.jaas.modules.BackingEngine;
> +import org.apache.karaf.jaas.modules.RolePrincipal;
> +import org.apache.karaf.jaas.modules.UserPrincipal;
> +
> +import javax.security.auth.login.AppConfigurationEntry;
> +import java.util.List;
> +
> +/**
> + * @author iocanel
> + */
> +@Command(scope = "jaas", name = "userlist", description = "Lists the users of the active
realm/module.")
> +public class ListUsersCommand extends JaasCommandSupport {
> +
> +    private static final String OUTPUT_FORMAT = "%-20s %-20s";
> +
> +    @Override
> +    protected Object doExecute() throws Exception {
> +        JaasRealm realm = (JaasRealm) session.get(JAAS_REALM);
> +        AppConfigurationEntry entry = (AppConfigurationEntry) session.get(JAAS_ENTRY);
> +
> +        if (realm == null || entry == null) {
> +            System.err.println("No JAAS Realm / Module has been selected.");
> +            return null;
> +        }
> +
> +        BackingEngine engine = backingEngineService.get(entry);
> +
> +        if (engine == null) {
> +            System.err.println(String.format("Failed to resolve backing engine
for realm:%s and moudle:%s", realm.getName(), entry.getLoginModuleName()));
> +            return null;
> +        }
> +
> +        return doExecute(engine);
> +    }
> +
> +    @Override
> +    protected Object doExecute(BackingEngine engine) throws Exception {
> +        List<UserPrincipal> users = engine.listUsers();
> +        System.out.println(String.format(OUTPUT_FORMAT, "User Name", "Role"));
> +
> +        for (UserPrincipal user : users) {
> +            String userName = user.getName();
> +            List<RolePrincipal> roles = engine.listRoles(user);
> +
> +            if (roles != null && roles.size() >= 1) {
> +                for (RolePrincipal role : roles) {
> +                    String roleName = role.getName();
> +                    System.out.println(String.format(OUTPUT_FORMAT, userName,
roleName));
> +                }
> +            } else {
> +                System.out.println(String.format(OUTPUT_FORMAT, userName, ""));
> +            }
> +
> +        }
> +        return null;
> +    }
> +}
>
> Modified: karaf/trunk/jaas/command/src/main/resources/OSGI-INF/blueprint/jass-command.xml
> URL: http://svn.apache.org/viewvc/karaf/trunk/jaas/command/src/main/resources/OSGI-INF/blueprint/jass-command.xml?rev=1031695&r1=1031694&r2=1031695&view=diff
> ==============================================================================
> --- karaf/trunk/jaas/command/src/main/resources/OSGI-INF/blueprint/jass-command.xml (original)
> +++ karaf/trunk/jaas/command/src/main/resources/OSGI-INF/blueprint/jass-command.xml Fri
Nov  5 18:20:56 2010
> @@ -33,8 +33,18 @@
>         <command name="jaas/cancel">
>             <action class="org.apache.karaf.jaas.command.CancelCommand"/>
>         </command>
> +        <command name="jaas/commandlist">
> +            <action class="org.apache.karaf.jaas.command.ListPendingCommand"/>
> +        </command>
> +        <command name="jaas/userlist">
> +            <action class="org.apache.karaf.jaas.command.ListUsersCommand">
> +                <property name="backingEngineService" ref="engineService"/>
> +            </action>
> +        </command>
>         <command name="jaas/list">
> -            <action class="org.apache.karaf.jaas.command.ListCommand"/>
> +            <action class="org.apache.karaf.jaas.command.ListRealmsCommand">
> +                <property name="realms" ref="realms"/>
> +            </action>
>         </command>
>         <command name="jaas/useradd">
>             <action class="org.apache.karaf.jaas.command.UserAddCommand"/>
>
> Modified: karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/BackingEngine.java
> URL: http://svn.apache.org/viewvc/karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/BackingEngine.java?rev=1031695&r1=1031694&r2=1031695&view=diff
> ==============================================================================
> --- karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/BackingEngine.java
(original)
> +++ karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/BackingEngine.java
Fri Nov  5 18:20:56 2010
> @@ -14,6 +14,8 @@
>  */
>  package org.apache.karaf.jaas.modules;
>
> +import java.util.List;
> +
>  /**
>  * @author iocanel
>  */
> @@ -36,6 +38,19 @@ public interface BackingEngine {
>     void deleteUser(String username);
>
>     /**
> +     * List Users
> +     */
> +    List<UserPrincipal> listUsers();
> +
> +    /**
> +     * List Roles for {@param user}.
> +     *
> +     * @param user
> +     * @return
> +     */
> +    List<RolePrincipal> listRoles(UserPrincipal user);
> +
> +    /**
>      * Add a role to the user
>      *
>      * @param username
>
> Modified: karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/encryption/EncryptionSupport.java
> URL: http://svn.apache.org/viewvc/karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/encryption/EncryptionSupport.java?rev=1031695&r1=1031694&r2=1031695&view=diff
> ==============================================================================
> --- karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/encryption/EncryptionSupport.java
(original)
> +++ karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/encryption/EncryptionSupport.java
Fri Nov  5 18:20:56 2010
> @@ -84,7 +84,7 @@ public class EncryptionSupport {
>                 } catch (InvalidSyntaxException e) {
>                     throw new IllegalStateException("The encryption service
filter is not well formed.", e);
>                 }
> -                if (encryptionServiceReferences.length == 0) {
> +                if (encryptionServiceReferences == null || encryptionServiceReferences.length
== 0) {
>                     if (name != null && name.length() > 0) {
>                         throw new IllegalStateException("Encryption service
" + name + " not found. Please check that the encryption service is correctly set up.");
>                     } else {
>
> Modified: karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCBackingEngine.java
> URL: http://svn.apache.org/viewvc/karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCBackingEngine.java?rev=1031695&r1=1031694&r2=1031695&view=diff
> ==============================================================================
> --- karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCBackingEngine.java
(original)
> +++ karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCBackingEngine.java
Fri Nov  5 18:20:56 2010
> @@ -19,12 +19,17 @@ package org.apache.karaf.jaas.modules.jd
>  import org.apache.commons.logging.Log;
>  import org.apache.commons.logging.LogFactory;
>  import org.apache.karaf.jaas.modules.BackingEngine;
> +import org.apache.karaf.jaas.modules.RolePrincipal;
> +import org.apache.karaf.jaas.modules.UserPrincipal;
>  import org.apache.karaf.jaas.modules.encryption.EncryptionSupport;
>
>  import javax.sql.DataSource;
>  import java.sql.Connection;
>  import java.sql.PreparedStatement;
> +import java.sql.ResultSet;
>  import java.sql.SQLException;
> +import java.util.ArrayList;
> +import java.util.List;
>
>  /**
>  * @author iocanel
> @@ -43,6 +48,8 @@ public class JDBCBackingEngine implement
>     private String deleteRoleStatement = "DELETE FROM ROLES WHERE USERNAME=? AND ROLE=?";
>     private String deleteAllUserRolesStatement = "DELETE FROM ROLES WHERE USERNAME=?";
>     private String deleteUserStatement = "DELETE FROM USERS WHERE USERNAME=?";
> +    private String selectUsersStatement = "SELECT USERNAME FROM USERS";
> +    private String selectRolesStatement = "SELECT ROLE FROM ROLES WHERE USERNAME=?";
>
>
>     /**
> @@ -161,6 +168,103 @@ public class JDBCBackingEngine implement
>     }
>
>     /**
> +     * List all Users
> +     *
> +     * @return
> +     */
> +    public List<UserPrincipal> listUsers() {
> +        List<UserPrincipal> users = new ArrayList<UserPrincipal>();
> +
> +        Connection connection = null;
> +        PreparedStatement listUserStatement = null;
> +        ResultSet usersResultSet = null;
> +
> +
> +        if (dataSource != null) {
> +
> +            try {
> +                connection = dataSource.getConnection();
> +
> +                //Remove from users
> +                listUserStatement = connection.prepareStatement(selectUsersStatement);
> +                usersResultSet = listUserStatement.executeQuery();
> +                while (!usersResultSet.next()) {
> +                    String username = usersResultSet.getString(0);
> +                    users.add(new UserPrincipal(username));
> +                }
> +            } catch (SQLException e) {
> +                LOG.error("Error executiong statement", e);
> +            } finally {
> +                try {
> +                    if (usersResultSet != null) {
> +                        usersResultSet.close();
> +                    }
> +                    if (listUserStatement != null) {
> +                        listUserStatement.close();
> +                    }
> +                    if (connection != null) {
> +                        connection.close();
> +                    }
> +                } catch (SQLException e) {
> +                    LOG.warn(MSG_CONNECTION_CLOSE_FAILED, e);
> +                }
> +            }
> +        }
> +        return users;
> +    }
> +
> +    /**
> +     * List the roles of the {@param user}.
> +     *
> +     * @param user
> +     * @return
> +     */
> +    public List<RolePrincipal> listRoles(UserPrincipal user) {
> +        List<RolePrincipal> roles = new ArrayList<RolePrincipal>();
> +
> +        Connection connection = null;
> +        PreparedStatement listRolesStatement = null;
> +        ResultSet rolesResultSet = null;
> +
> +
> +        if (dataSource != null) {
> +
> +            try {
> +                connection = dataSource.getConnection();
> +
> +                //Remove from roles
> +                listRolesStatement = connection.prepareStatement(selectRolesStatement);
> +                listRolesStatement.setString(1, user.getName());
> +
> +                rolesResultSet = listRolesStatement.executeQuery();
> +
> +                while (!rolesResultSet.next()) {
> +                    String role = rolesResultSet.getString(1);
> +                    roles.add(new RolePrincipal(role));
> +                }
> +
> +            } catch (SQLException e) {
> +                LOG.error("Error executiong statement", e);
> +            } finally {
> +                try {
> +                    if (rolesResultSet != null) {
> +                        rolesResultSet.close();
> +                    }
> +                    if (listRolesStatement != null) {
> +                        listRolesStatement.close();
> +                    }
> +                    if (connection != null) {
> +                        connection.close();
> +                    }
> +                } catch (SQLException e) {
> +                    LOG.warn(MSG_CONNECTION_CLOSE_FAILED, e);
> +                }
> +            }
> +        }
> +        return roles;
> +    }
> +
> +    /**
>      * Add a role to a user.
>      *
>      * @param username
>
> Modified: karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/properties/PropertiesBackingEngine.java
> URL: http://svn.apache.org/viewvc/karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/properties/PropertiesBackingEngine.java?rev=1031695&r1=1031694&r2=1031695&view=diff
> ==============================================================================
> --- karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/properties/PropertiesBackingEngine.java
(original)
> +++ karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/properties/PropertiesBackingEngine.java
Fri Nov  5 18:20:56 2010
> @@ -19,9 +19,14 @@ package org.apache.karaf.jaas.modules.pr
>  import org.apache.commons.logging.Log;
>  import org.apache.commons.logging.LogFactory;
>  import org.apache.karaf.jaas.modules.BackingEngine;
> +import org.apache.karaf.jaas.modules.RolePrincipal;
> +import org.apache.karaf.jaas.modules.UserPrincipal;
>  import org.apache.karaf.jaas.modules.encryption.EncryptionSupport;
>  import org.apache.karaf.util.Properties;
>
> +import java.util.ArrayList;
> +import java.util.List;
> +
>
>  /**
>  * @author iocanel
> @@ -99,6 +104,37 @@ public class PropertiesBackingEngine imp
>     }
>
>     /**
> +     * List Users
> +     *
> +     * @return
> +     */
> +    public List<UserPrincipal> listUsers() {
> +        List<UserPrincipal> result = new ArrayList<UserPrincipal>();
> +
> +        for (String userNames : users.keySet()) {
> +            UserPrincipal userPrincipal = new UserPrincipal(userNames);
> +            result.add(userPrincipal);
> +        }
> +        return result;
> +    }
> +
> +    /**
> +     * List the Roles of the {@param user}
> +     *
> +     * @param user
> +     * @return
> +     */
> +    public List<RolePrincipal> listRoles(UserPrincipal user) {
> +        List<RolePrincipal> result = new ArrayList<RolePrincipal>();
> +        String userInfo = users.get(user.getName());
> +        String[] infos = userInfo.split(",");
> +        for (int i = 1; i < infos.length; i++) {
> +            result.add(new RolePrincipal(infos[i]));
> +        }
> +        return result;
> +    }
> +
> +    /**
>      * Add a role to a User.
>      *
>      * @param username
>
>
>



-- 
Cheers,
Guillaume Nodet
------------------------
Blog: http://gnodet.blogspot.com/
------------------------
Open Source SOA
http://fusesource.com

Mime
View raw message