updated to read LDAP Credentials from RunningInstance

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portal/social-mail-servlet@118999 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2015-09-23 10:22:49 +00:00
parent d179921e63
commit d2d0e2efe7
2 changed files with 87 additions and 30 deletions

View File

@ -18,6 +18,8 @@ import org.gcube.common.encryption.StringEncrypter;
import org.gcube.common.portal.PortalContext; import org.gcube.common.portal.PortalContext;
import org.gcube.common.resources.gcore.ServiceEndpoint; import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
import org.gcube.common.resources.gcore.utils.Group;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.portal.databook.server.DBCassandraAstyanaxImpl; import org.gcube.portal.databook.server.DBCassandraAstyanaxImpl;
import org.gcube.portal.databook.server.DatabookStore; import org.gcube.portal.databook.server.DatabookStore;
@ -33,15 +35,28 @@ import org.slf4j.LoggerFactory;
public class PortalSchedulerService extends HttpServlet { public class PortalSchedulerService extends HttpServlet {
private static final Logger _log = LoggerFactory.getLogger(PortalSchedulerService.class); private static final Logger _log = LoggerFactory.getLogger(PortalSchedulerService.class);
private static final String POP3_SERVER_NAME = "Pop3MailServer";
private static final String LDAP_SERVER_NAME = "LDAPServer";
private static final String LDAP_SERVER_FILTER_NAME = "filter";
private static final String LDAP_SERVER_PRINCPAL_NAME = "ldapPrincipal";
private static final int POP3_MINUTES_DELAY = 1; private static final int POP3_MINUTES_DELAY = 1;
private static final int LDAP_MINUTES_DELAY = 15; private static final int LDAP_MINUTES_DELAY = 15;
private static DatabookStore store; private static DatabookStore store;
private static String portalName; private String portalName;
private static String host; private String pop3Server;
private static String user; private String pop3user;
private static String password; private String pop3password;
private String ldapUrl;
private String filter;
private String principal;
private String ldapPassword;
public void init() { public void init() {
store = new DBCassandraAstyanaxImpl(); store = new DBCassandraAstyanaxImpl();
@ -66,16 +81,60 @@ public class PortalSchedulerService extends HttpServlet {
} }
else { else {
for (ServiceEndpoint res : list) { for (ServiceEndpoint res : list) {
AccessPoint found = res.profile().accessPoints().iterator().next(); Group<AccessPoint> apGroup = res.profile().accessPoints();
host = found.address(); AccessPoint[] accessPoints = (AccessPoint[]) apGroup.toArray(new AccessPoint[apGroup.size()]);
user = found.username(); for (int i = 0; i < accessPoints.length; i++) {
String encrPassword = found.password(); if (accessPoints[i].name().compareTo(POP3_SERVER_NAME) == 0) {
_log.info("Found credentials for " + POP3_SERVER_NAME);
AccessPoint found = accessPoints[i];
pop3Server = found.address();
pop3user = found.username();
String encrPassword = found.password();
try {
pop3password = StringEncrypter.getEncrypter().decrypt( encrPassword);
} catch (Exception e) {
_log.error("Something went wrong while decrypting password for " + POP3_SERVER_NAME);
e.printStackTrace();
}
}
else if (accessPoints[i].name().compareTo(LDAP_SERVER_NAME) == 0) {
_log.info("Found credentials for " + LDAP_SERVER_NAME);
AccessPoint found = accessPoints[i];
ldapUrl = found.address();
String encrPassword = found.password();
try {
ldapPassword = StringEncrypter.getEncrypter().decrypt( encrPassword);
} catch (Exception e) {
_log.error("Something went wrong while decrypting password for " + LDAP_SERVER_NAME);
e.printStackTrace();
}
Group<Property> propGroup = found.properties();
Property[] props = (Property[]) propGroup.toArray(new Property[propGroup.size()]);
for (int j = 0; j < props.length; j++) {
_log.info("\tFound properties of " + LDAP_SERVER_NAME);
if (props[i].name().compareTo(LDAP_SERVER_FILTER_NAME) == 0) {
String encrValue = props[i].value();
try {
filter = StringEncrypter.getEncrypter().decrypt( encrValue);
} catch (Exception e) {
_log.error("Something went wrong while decrypting value for " + LDAP_SERVER_FILTER_NAME);
e.printStackTrace();
}
}
else if (props[i].name().compareTo(LDAP_SERVER_PRINCPAL_NAME) == 0) {
String encrValue = props[i].value();
try {
principal = StringEncrypter.getEncrypter().decrypt( encrValue);
} catch (Exception e) {
_log.error("Something went wrong while decrypting value for " + LDAP_SERVER_PRINCPAL_NAME);
e.printStackTrace();
}
}
}
try { }
password = StringEncrypter.getEncrypter().decrypt( encrPassword);
} catch (Exception e) {
e.printStackTrace();
} }
} }
} }
} }
@ -83,10 +142,10 @@ public class PortalSchedulerService extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ScheduledExecutorService ldapScheduler = Executors.newScheduledThreadPool(1); ScheduledExecutorService ldapScheduler = Executors.newScheduledThreadPool(1);
ldapScheduler.scheduleAtFixedRate(new LDAPSync(), 0, LDAP_MINUTES_DELAY, TimeUnit.MINUTES); ldapScheduler.scheduleAtFixedRate(new LDAPSync(ldapUrl, filter, principal, ldapPassword), 0, LDAP_MINUTES_DELAY, TimeUnit.MINUTES);
ScheduledExecutorService pop3Scheduler = Executors.newScheduledThreadPool(1); ScheduledExecutorService pop3Scheduler = Executors.newScheduledThreadPool(1);
pop3Scheduler.scheduleAtFixedRate(new PeriodicTask(store, portalName, host, user, password), 0, POP3_MINUTES_DELAY, TimeUnit.MINUTES); pop3Scheduler.scheduleAtFixedRate(new PeriodicTask(store, portalName, pop3Server, pop3user, pop3password), 0, POP3_MINUTES_DELAY, TimeUnit.MINUTES);
String toReturn = "<DIV>Check Notification Email Started ... </DIV>"; String toReturn = "<DIV>Check Notification Email Started ... </DIV>";

View File

@ -22,29 +22,28 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.liferay.portal.kernel.cache.CacheRegistryUtil; import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.exception.PortalException; import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.Organization; import com.liferay.portal.model.Organization;
import com.liferay.portal.model.User; import com.liferay.portal.model.User;
import com.liferay.portal.service.OrganizationLocalServiceUtil; import com.liferay.portal.service.OrganizationLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil; import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.service.UserServiceUtil;
public class LDAPSync implements Runnable { public class LDAPSync implements Runnable {
private static final Logger _log = LoggerFactory.getLogger(LDAPSync.class); private static final Logger _log = LoggerFactory.getLogger(LDAPSync.class);
//TODO Move it to Runtime Resource
private static final String ldapUrl = "ldap://ldap-liferay.d4science.org";
private static final String filter = "(objectClass=inetOrgPerson)";
private static final String ldapPrincipal = "cn=admin,dc=d4science,dc=org";
private static final String ldapPwd = "poiwefhaewfkhj";
// private static final String ldapPrincipal = "";
// private static final String ldapPwd = "";
private String ldapUrl;
public LDAPSync() { private String filter;
super(); private String principal;
_log.debug("LDAPSync()"); private String pwd;
public LDAPSync(String ldapUrl, String filter, String principal, String pwd) {
this.ldapUrl = ldapUrl;
this.filter = filter;
this.principal = principal;
this.pwd = pwd;
_log.info("Starting LDAPSync over " + ldapUrl);
} }
@Override @Override
@ -60,12 +59,11 @@ public class LDAPSync implements Runnable {
_log.debug("Initializing LDAP exporter ..."); _log.debug("Initializing LDAP exporter ...");
Properties env = new Properties(); Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, ldapUrl); env.put(Context.PROVIDER_URL, ldapUrl);
env.put(Context.SECURITY_PRINCIPAL, ldapPrincipal); env.put(Context.SECURITY_PRINCIPAL, principal);
env.put(Context.SECURITY_CREDENTIALS, ldapPwd); env.put(Context.SECURITY_CREDENTIALS, pwd);
try { try {
DirContext ctx = new InitialDirContext(env); DirContext ctx = new InitialDirContext(env);