From d4fa3bd9dd48f4d5e26850a23f5ba48a9c10ad64 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Thu, 5 Jun 2014 15:02:07 +0200 Subject: LDAP refactored behind interfaces. All functions of LDAP now hidden behind interfaces: Authenticator, Lookup and NextKeyManager (LDAP property storage was already a ClusterStorage). Gateway holds additional objects, and Fixes #26 #191. Refs #27 (needs additional work for read perms and auth tokens) --- .../c2kernel/lookup/ldap/LDAPNextKeyManager.java | 92 ++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/main/java/com/c2kernel/lookup/ldap/LDAPNextKeyManager.java (limited to 'src/main/java/com/c2kernel/lookup/ldap/LDAPNextKeyManager.java') diff --git a/src/main/java/com/c2kernel/lookup/ldap/LDAPNextKeyManager.java b/src/main/java/com/c2kernel/lookup/ldap/LDAPNextKeyManager.java new file mode 100644 index 0000000..4db8a49 --- /dev/null +++ b/src/main/java/com/c2kernel/lookup/ldap/LDAPNextKeyManager.java @@ -0,0 +1,92 @@ +package com.c2kernel.lookup.ldap; + +import com.c2kernel.common.ObjectCannotBeUpdated; +import com.c2kernel.common.ObjectNotFoundException; +import com.c2kernel.lookup.AgentPath; +import com.c2kernel.lookup.InvalidItemPathException; +import com.c2kernel.lookup.ItemPath; +import com.c2kernel.persistency.ClusterStorageException; +import com.c2kernel.persistency.NextKeyManager; +import com.c2kernel.process.Gateway; +import com.c2kernel.utils.Logger; +import com.novell.ldap.LDAPEntry; + +/************************************************************************** + * + * $Revision: 1.2 $ + * $Date: 2005/04/27 13:47:24 $ + * + * Copyright (C) 2003 CERN - European Organization for Nuclear Research + * All rights reserved. + **************************************************************************/ + +// public static final String codeRevision = "$Revision: 1.2 $ $Date: 2005/04/27 13:47:24 $ $Author: abranson $"; +public class LDAPNextKeyManager implements NextKeyManager { + + LDAPAuthManager ldap; + String lastKeyPath; + + public LDAPNextKeyManager(LDAPAuthManager ldap, String lastKeyPath) { + super(); + this.ldap = ldap; + this.lastKeyPath = lastKeyPath; + } + + @Override + public synchronized ItemPath generateNextEntityKey() + throws ObjectCannotBeUpdated, ObjectNotFoundException + { + ItemPath lastKey = getLastEntityPath(); + + try { + lastKey.setSysKey(lastKey.getSysKey()+1); + } catch (InvalidItemPathException ex) { + throw new ObjectCannotBeUpdated("Invalid syskey "+(lastKey.getSysKey()+1)+". Maybe centre is full."); + } + //test that storage is empty for that key + try { + if (Gateway.getStorage().getClusterContents(lastKey.getSysKey(), "").length > 0) + throw new ObjectCannotBeUpdated("NextKeyManager: Storage already contains data for syskey "+lastKey.getSysKey()+ + ". Storage is out of sync with nextkey. Please contact an administrator", ""); + } catch (ClusterStorageException e) { + Logger.error(e); + throw new ObjectCannotBeUpdated("Could not check storage for prior data for the next generated systemKey: "+e.getMessage()); + } + + //set the last key + writeLastEntityKey(lastKey.getSysKey()); + + return lastKey; + } + + @Override + public synchronized AgentPath generateNextAgentKey() + throws ObjectCannotBeUpdated, ObjectNotFoundException { + ItemPath newEntity = generateNextEntityKey(); + return new AgentPath(newEntity); + } + + @Override + public void writeLastEntityKey(int sysKey) throws ObjectCannotBeUpdated, ObjectNotFoundException { + LDAPEntry lastKeyEntry = LDAPLookupUtils.getEntry(ldap.getAuthObject(),lastKeyPath); + LDAPLookupUtils.setAttributeValue(ldap.getAuthObject(), lastKeyEntry,"intsyskey",Integer.toString(sysKey)); + } + + @Override + public ItemPath getLastEntityPath() throws ObjectNotFoundException + { + LDAPEntry lastKeyEntry = LDAPLookupUtils.getEntry(ldap.getAuthObject(),lastKeyPath); + String lastKey = LDAPLookupUtils.getFirstAttributeValue(lastKeyEntry,"intsyskey"); + try { + int sysKey = Integer.parseInt(lastKey); + ItemPath sysPath = new ItemPath(sysKey); + return sysPath; + } catch (InvalidItemPathException ex) { + throw new ObjectNotFoundException("Invalid syskey. Maybe centre is full."); + } catch (NumberFormatException ex) { + throw new ObjectNotFoundException("Invalid syskey in lastkey."); + } + + } + +} -- cgit v1.2.3 From 8dbf947eb6e84ec6a3c6e9ba421c682d847a8b00 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Thu, 5 Jun 2014 16:13:23 +0200 Subject: Make LDAPNextKeyManager independent of the LDAPLookup. The authenticator is the only required common component. --- src/main/java/com/c2kernel/lookup/ldap/LDAPLookup.java | 12 ------------ .../java/com/c2kernel/lookup/ldap/LDAPNextKeyManager.java | 11 ++++++++--- 2 files changed, 8 insertions(+), 15 deletions(-) (limited to 'src/main/java/com/c2kernel/lookup/ldap/LDAPNextKeyManager.java') diff --git a/src/main/java/com/c2kernel/lookup/ldap/LDAPLookup.java b/src/main/java/com/c2kernel/lookup/ldap/LDAPLookup.java index a96a46b..10c1830 100644 --- a/src/main/java/com/c2kernel/lookup/ldap/LDAPLookup.java +++ b/src/main/java/com/c2kernel/lookup/ldap/LDAPLookup.java @@ -55,7 +55,6 @@ public class LDAPLookup implements Lookup { private LDAPAuthManager mLDAPAuth; - private LDAPNextKeyManager mNextKeyManager; private LDAPPropertyManager mPropManager; final String mItemTypeRoot, mDomainTypeRoot, mGlobalPath, mRootPath, mLocalPath, mRolePath; @@ -87,21 +86,10 @@ public class LDAPLookup implements Lookup @Override public void open(Authenticator auth) { mLDAPAuth = (LDAPAuthManager)auth; - mNextKeyManager = new LDAPNextKeyManager(mLDAPAuth, "cn=last,"+mItemTypeRoot); - Gateway.getProperties().setProperty("NextKeyManager", mNextKeyManager); Logger.msg(7, "LDAP.useOldProps="+Gateway.getProperties().getBoolean("LDAP.useOldProps", false)); mPropManager = new LDAPPropertyManager(this, mLDAPAuth); } - /** - * Gets the entity key generator, used to get a unique system key for new entities. - * @return the global NextKeyManager - */ - public LDAPNextKeyManager getNextKeyManager() - { - return mNextKeyManager; - } - /** * Gets the property manager, that is used to read and write cristal properties to the LDAP store. * @return Returns the global LDAPPropertyManager. diff --git a/src/main/java/com/c2kernel/lookup/ldap/LDAPNextKeyManager.java b/src/main/java/com/c2kernel/lookup/ldap/LDAPNextKeyManager.java index 4db8a49..48f938e 100644 --- a/src/main/java/com/c2kernel/lookup/ldap/LDAPNextKeyManager.java +++ b/src/main/java/com/c2kernel/lookup/ldap/LDAPNextKeyManager.java @@ -8,6 +8,7 @@ import com.c2kernel.lookup.ItemPath; import com.c2kernel.persistency.ClusterStorageException; import com.c2kernel.persistency.NextKeyManager; import com.c2kernel.process.Gateway; +import com.c2kernel.process.auth.Authenticator; import com.c2kernel.utils.Logger; import com.novell.ldap.LDAPEntry; @@ -26,10 +27,14 @@ public class LDAPNextKeyManager implements NextKeyManager { LDAPAuthManager ldap; String lastKeyPath; - public LDAPNextKeyManager(LDAPAuthManager ldap, String lastKeyPath) { + public LDAPNextKeyManager() { super(); - this.ldap = ldap; - this.lastKeyPath = lastKeyPath; + LDAPProperties props = new LDAPProperties(Gateway.getProperties()); + this.lastKeyPath = "cn=last,cn=entity,"+props.mLocalPath; + } + + public void open(Authenticator auth) { + this.ldap = (LDAPAuthManager)auth; } @Override -- cgit v1.2.3