Skip to content

Commit

Permalink
PAXUSERADMIN-22 support retrival of keys for credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
laeubi committed Apr 5, 2013
1 parent a8aa8c6 commit babcc9e
Show file tree
Hide file tree
Showing 15 changed files with 198 additions and 127 deletions.
Expand Up @@ -121,7 +121,7 @@ public User doWork(EntityManager manager, EntityTransaction transaction) {
manager.persist(user);
transaction.commit();
map.put(name, user);
return factory.createUser(name, null);
return factory.createUser(name, null, null);
}

@Override
Expand Down Expand Up @@ -161,7 +161,7 @@ public Group doWork(EntityManager manager, EntityTransaction transaction) {
manager.persist(group);
transaction.commit();
map.put(name, group);
return factory.createGroup(name, null);
return factory.createGroup(name, null, null);
}

@Override
Expand Down Expand Up @@ -730,12 +730,19 @@ private Role loadRole(UserAdminFactory factory, String name, Filter filter) thro
}
}
Role role = null;
Set<String> keySet = null;
if (dbRole instanceof DBUser) {
Map<String, DBCredential> credentials = ((DBUser) dbRole).getCredentials();
if (credentials != null) {
keySet = credentials.keySet();
}
}
switch (dbRole.getType()) {
case User.USER:
role = factory.createUser(name, properties);
role = factory.createUser(name, properties, keySet);
break;
case User.GROUP:
role = factory.createGroup(name, properties);
role = factory.createGroup(name, properties, keySet);
break;
default:
throw new StorageException("Invalid role type for role '" + name + "': " + dbRole.getType() + " only USER and GROUP are allowed!");
Expand Down
Expand Up @@ -361,9 +361,9 @@ private Role createRole(UserAdminFactory factory, LDAPEntry entry) throws Storag
}
switch (type) {
case Role.USER:
return factory.createUser(entry.getAttribute(m_userIdAttr).getStringValue(), properties);
return factory.createUser(entry.getAttribute(m_userIdAttr).getStringValue(), properties, credentials.keySet());
case Role.GROUP:
return factory.createGroup(entry.getAttribute(m_groupIdAttr).getStringValue(), properties);
return factory.createGroup(entry.getAttribute(m_groupIdAttr).getStringValue(), properties, credentials.keySet());
default:
// should never happen: getRoleType() throws on this
throw new StorageException("Unexpected role type '" + type + "' (0==Role) detected.");
Expand Down Expand Up @@ -614,7 +614,7 @@ public User createUser(UserAdminFactory factory, String name) throws StorageExce
//
try {
connection.add(entry);
return factory.createUser(name, properties);
return factory.createUser(name, properties, null);
} catch (LDAPException e) {
throw new StorageException("Error creating user '" + name + "' " + entry + ": " + e.getMessage() + " / " + e.getLDAPErrorMessage());
} finally {
Expand Down Expand Up @@ -653,7 +653,7 @@ public Group createGroup(UserAdminFactory factory, String name) throws StorageEx
connection.add(entry);
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(m_groupIdAttr, name);
return factory.createGroup(name, properties);
return factory.createGroup(name, properties, null);
} catch (LDAPException e) {
throw new StorageException("Error creating group '" + name + "' " + entry + ": " + e.getMessage() + " / " + e.getLDAPErrorMessage());
} finally {
Expand Down
Expand Up @@ -23,6 +23,7 @@
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

import org.ops4j.pax.useradmin.provider.preferences.ConfigurationConstants;
import org.ops4j.pax.useradmin.service.PaxUserAdminConstants;
Expand Down Expand Up @@ -145,19 +146,19 @@ private Role loadRole(UserAdminFactory factory, String name, Filter filter) thro
}
}
//
Map<String, Object> credentials = null;
Set<String> credentials = null;
if (node.nodeExists(CREDENTIALS_NODE)) {
credentials = loadAttributes(node.node(CREDENTIALS_NODE));
credentials = loadAttributes(node.node(CREDENTIALS_NODE)).keySet();
}
//
int type = new Integer(node.get(NODE_TYPE, "666"));
Role role = null;
switch (type) {
case User.USER:
role = factory.createUser(name, properties);
role = factory.createUser(name, properties, credentials);
break;
case User.GROUP:
role = factory.createGroup(name, properties);
role = factory.createGroup(name, properties, credentials);
break;
default:
throw new StorageException("Invalid role type for role '" + name + " / " + node.name() + "': " + type);
Expand Down Expand Up @@ -218,7 +219,7 @@ private Preferences getRootNode() throws StorageException {
public User createUser(UserAdminFactory factory, String name) throws StorageException {
Preferences node = getRootNode().node(name);
node.putInt(NODE_TYPE, Role.USER);
User user = factory.createUser(name, null);
User user = factory.createUser(name, null, null);
try {
node.flush();
} catch (BackingStoreException e) {
Expand All @@ -231,7 +232,7 @@ public User createUser(UserAdminFactory factory, String name) throws StorageExce
public Group createGroup(UserAdminFactory factory, String name) throws StorageException {
Preferences node = getRootNode().node(name);
node.putInt(NODE_TYPE, Role.GROUP);
Group group = factory.createGroup(name, null);
Group group = factory.createGroup(name, null, null);
try {
node.flush();
} catch (BackingStoreException e) {
Expand Down
3 changes: 2 additions & 1 deletion pax-useradmin-service/osgi.bnd
Expand Up @@ -12,4 +12,5 @@ Bundle-Activator: \
#Export-Package: \
# !${bundle.namespace}.internal.*,\
# ${bundle.namespace}.*;version="${pom.version}"
Private-Package: org.ops4j.pax.useradmin.service.internal
Private-Package: org.ops4j.pax.useradmin.service.internal,\
org.ops4j.pax.useradmin.service.internal.encryption
Expand Up @@ -17,6 +17,8 @@

package org.ops4j.pax.useradmin.service.internal;

import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;

Expand All @@ -33,19 +35,21 @@
* @author Matthias Kuespert
* @since 02.07.2009
*/
public abstract class AbstractProperties<R extends Role> extends Hashtable<String, Object> {
public abstract class AbstractProperties<R extends Role> extends Dictionary<String, Object> {

private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;

/**
* The role these properties belong to.
*/
private R m_role = null;
private final R m_role;

/**
* The interface used to connect to the UserAdmin service.
*/
private UserAdminUtil m_util = null;
private final UserAdminUtil m_util;

private final Hashtable<String, Object> hashtable;

/**
* @return The role these properties belong to.
Expand Down Expand Up @@ -103,13 +107,10 @@ protected UserAdminUtil getUtil() {
protected AbstractProperties(R role, UserAdminUtil util, Map<String, Object> properties) {
m_role = role;
m_util = util;
//
// initialize from storage
//
if (null != properties) {
for (String key : properties.keySet()) {
super.put(key, properties.get(key));
}
if (properties != null) {
hashtable = new Hashtable<String, Object>(properties);
} else {
hashtable = new Hashtable<String, Object>();
}
}

Expand All @@ -129,7 +130,7 @@ protected AbstractProperties(R role, UserAdminUtil util, Map<String, Object> pro
public synchronized Object get(Object key) {
checkKeyValid(key);
checkGetPermission((String) key);
return super.get(key);
return hashtable.get(key);
}

protected void checkKeyValid(Object key) throws IllegalArgumentException {
Expand Down Expand Up @@ -166,7 +167,7 @@ public synchronized Object put(String key, Object value) {
}

protected Object putInternal(String key, Object storedValue, Object oldValue) {
return super.put(key, storedValue);
return hashtable.put(key, storedValue);
}

@Override
Expand All @@ -176,15 +177,31 @@ public synchronized Object remove(Object key) {
StorageProvider storageProvider = m_util.getStorageProvider();
remove(storageProvider, (String) key);
m_util.fireEvent(UserAdminEvent.ROLE_CHANGED, m_role);
return super.remove(key);
return hashtable.remove(key);
} catch (StorageException e) {
m_util.logMessage(this, LogService.LOG_ERROR, e.getMessage());
}
return null;
}

@Override
public synchronized void clear() {
throw new UnsupportedOperationException();
public boolean isEmpty() {
return hashtable.isEmpty();
}

@Override
public Enumeration<Object> elements() {
return hashtable.elements();
}

@Override
public Enumeration<String> keys() {
return hashtable.keys();
}

@Override
public int size() {
return hashtable.size();
}

}
Expand Up @@ -20,6 +20,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

import org.ops4j.pax.useradmin.service.spi.SPIRole;
import org.ops4j.pax.useradmin.service.spi.StorageException;
Expand All @@ -43,10 +44,11 @@ public class GroupImpl extends UserImpl implements Group {
/**
* Constructor.
*
* @param initialCredentialKeys
* @see UserImpl#UserImpl(String, PaxUserAdmin, Map, Map)
*/
protected GroupImpl(String name, PaxUserAdmin admin, Map<String, Object> properties) {
super(name, admin, properties);
protected GroupImpl(String name, PaxUserAdmin admin, Map<String, Object> properties, Set<String> initialCredentialKeys) {
super(name, admin, properties, initialCredentialKeys);
}

/**
Expand Down
Expand Up @@ -23,6 +23,7 @@
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;

import org.ops4j.pax.useradmin.service.PaxUserAdminConstants;
Expand Down Expand Up @@ -414,16 +415,16 @@ public void checkPermission(String name, String action) {
* @see UserAdminFactory#createUser(String, Map)
*/
@Override
public User createUser(String name, Map<String, Object> properties) {
return new UserImpl(name, this, properties);
public User createUser(String name, Map<String, Object> properties, Set<String> initialCredentialKeys) {
return new UserImpl(name, this, properties, initialCredentialKeys);
}

/**
* @see UserAdminFactory#createGroup(String, Map)
*/
@Override
public Group createGroup(String name, Map<String, Object> properties) {
return new GroupImpl(name, this, properties);
public Group createGroup(String name, Map<String, Object> properties, Set<String> initialCredentialKeys) {
return new GroupImpl(name, this, properties, initialCredentialKeys);
}

/**
Expand Down
Expand Up @@ -17,7 +17,12 @@

package org.ops4j.pax.useradmin.service.internal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.ops4j.pax.useradmin.service.spi.StorageException;
import org.ops4j.pax.useradmin.service.spi.StorageProvider;
Expand All @@ -34,14 +39,21 @@
public class UserCredentials extends AbstractProperties<User> {

private static final long serialVersionUID = 1L;
private final Set<String> credentialKeys;

/**
* Initializing constructor.
*
* @param initialCredentialKeys
* @see AbstractProperties#AbstractProperties(Role, UserAdminUtil, Map)
*/
protected UserCredentials(User user, UserAdminUtil util) {
protected UserCredentials(User user, UserAdminUtil util, Set<String> initialCredentialKeys) {
super(user, util, null);
if (initialCredentialKeys != null) {
this.credentialKeys = new HashSet<String>(initialCredentialKeys);
} else {
this.credentialKeys = new HashSet<String>();
}
}

@Override
Expand All @@ -54,13 +66,15 @@ protected synchronized Object store(StorageProvider storageProvider, String key,
UserAdminUtil util = getUtil();
util.checkPermission(key, UserAdminPermission.CHANGE_CREDENTIAL);
storageProvider.getCredentialProvider().setUserCredential(util.getEncryptor(), getRole(), key, plainValue);
credentialKeys.add(key);
return plainValue;
}

@Override
protected synchronized void remove(StorageProvider storageProvider, String key) throws StorageException {
getUtil().checkPermission(key, UserAdminPermission.CHANGE_CREDENTIAL);
storageProvider.getCredentialProvider().removeUserCredential(getRole(), key);
credentialKeys.remove(key);
}

@Override
Expand All @@ -76,6 +90,31 @@ protected Object putInternal(String key, Object storedValue, Object oldValue) {
return oldValue;
}

@Override
public Enumeration<String> keys() {
return Collections.enumeration(credentialKeys);
}

@Override
public boolean isEmpty() {
return credentialKeys.isEmpty();
}

@Override
public int size() {
return credentialKeys.size();
}

@Override
public Enumeration<Object> elements() {
ArrayList<Object> list = new ArrayList<Object>();
String[] keys = credentialKeys.toArray(new String[0]);
for (String key : keys) {
list.add(get(key));
}
return Collections.enumeration(list);
}

/**
* @param key
* @param value
Expand Down
Expand Up @@ -20,6 +20,7 @@
import java.util.Collection;
import java.util.Dictionary;
import java.util.Map;
import java.util.Set;

import org.ops4j.pax.useradmin.service.spi.SPIRole;
import org.osgi.service.useradmin.Role;
Expand All @@ -43,13 +44,14 @@ public class UserImpl extends RoleImpl implements User {
/**
* Constructor.
*
* @param initialCredentialKeys
* @see RoleImpl#RoleImpl(String, PaxUserAdmin, Map)
* @param credentialKeys
* The credentials of this user.
*/
protected UserImpl(String name, PaxUserAdmin admin, Map<String, Object> properties) {
protected UserImpl(String name, PaxUserAdmin admin, Map<String, Object> properties, Set<String> initialCredentialKeys) {
super(name, admin, properties);
m_credentials = new UserCredentials(this, admin);
m_credentials = new UserCredentials(this, admin, initialCredentialKeys);
}

/**
Expand Down

0 comments on commit babcc9e

Please sign in to comment.