Search in sources :

Example 1 with StopableThread

use of com.tremolosecurity.server.StopableThread in project OpenUnison by TremoloSecurity.

the class SendMessageThread method initializeHibernate.

private void initializeHibernate(ApprovalDBType adbt) {
    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
    Configuration config = new Configuration();
    config.setProperty("hibernate.connection.driver_class", adbt.getDriver());
    config.setProperty("hibernate.connection.password", adbt.getPassword());
    config.setProperty("hibernate.connection.url", adbt.getUrl());
    config.setProperty("hibernate.connection.username", adbt.getUser());
    config.setProperty("hibernate.dialect", adbt.getHibernateDialect());
    if (adbt.isHibernateCreateSchema() == null || adbt.isHibernateCreateSchema()) {
        config.setProperty("hibernate.hbm2ddl.auto", "update");
    }
    config.setProperty("show_sql", "true");
    config.setProperty("hibernate.current_session_context_class", "thread");
    config.setProperty("hibernate.c3p0.max_size", Integer.toString(adbt.getMaxConns()));
    config.setProperty("hibernate.c3p0.maxIdleTimeExcessConnections", Integer.toString(adbt.getMaxIdleConns()));
    if (adbt.getValidationQuery() != null && !adbt.getValidationQuery().isEmpty()) {
        config.setProperty("hibernate.c3p0.testConnectionOnCheckout", "true");
    }
    config.setProperty("hibernate.c3p0.autoCommitOnClose", "true");
    if (adbt.getHibernateProperty() != null) {
        for (ParamType pt : adbt.getHibernateProperty()) {
            config.setProperty(pt.getName(), pt.getValue());
        }
    }
    // config.setProperty("hibernate.c3p0.debugUnreturnedConnectionStackTraces", "true");
    // config.setProperty("hibernate.c3p0.unreturnedConnectionTimeout", "30");
    String validationQuery = adbt.getValidationQuery();
    if (validationQuery == null) {
        validationQuery = "SELECT 1";
    }
    config.setProperty("hibernate.c3p0.preferredTestQuery", validationQuery);
    LoadedConfig lc = null;
    if (adbt.getHibernateConfig() == null || adbt.getHibernateConfig().trim().isEmpty()) {
        JaxbCfgHibernateConfiguration jaxbCfg = new JaxbCfgHibernateConfiguration();
        jaxbCfg.setSessionFactory(new JaxbCfgSessionFactory());
        JaxbCfgMappingReferenceType mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(AllowedApprovers.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);
        mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(Approvals.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);
        mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(ApproverAttributes.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);
        mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(Approvers.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);
        mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(AuditLogs.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);
        mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(AuditLogType.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);
        mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(Escalation.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);
        mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(Targets.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);
        mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(UserAttributes.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);
        mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(Users.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);
        mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(WorkflowParameters.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);
        mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(Workflows.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);
        lc = LoadedConfig.consume(jaxbCfg);
    } else {
        lc = LoadedConfig.baseline();
    }
    StandardServiceRegistry registry = builder.configure(lc).applySettings(config.getProperties()).build();
    try {
        sessionFactory = null;
        if (adbt.getHibernateConfig() == null || adbt.getHibernateConfig().trim().isEmpty()) {
            sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        } else {
            sessionFactory = new MetadataSources(registry).addResource(adbt.getHibernateConfig()).buildMetadata().buildSessionFactory();
        }
        this.cfgMgr.addThread(new StopableThread() {

            @Override
            public void run() {
            // TODO Auto-generated method stub
            }

            @Override
            public void stop() {
                logger.info("Stopping hibernate");
                sessionFactory.close();
            }
        });
        org.hibernate.Session session = sessionFactory.openSession();
        this.auditLogTypes = new HashMap<String, AuditLogType>();
        List<AuditLogType> alts = session.createCriteria(AuditLogType.class).list();
        if (alts.size() == 0) {
            session.beginTransaction();
            AuditLogType alt = new AuditLogType();
            alt.setName("Add");
            session.save(alt);
            this.auditLogTypes.put("add", alt);
            alt = new AuditLogType();
            alt.setName("Delete");
            session.save(alt);
            this.auditLogTypes.put("delete", alt);
            alt = new AuditLogType();
            alt.setName("Replace");
            session.save(alt);
            this.auditLogTypes.put("replace", alt);
            session.getTransaction().commit();
        } else {
            for (AuditLogType alt : alts) {
                this.auditLogTypes.put(alt.getName().toLowerCase(), alt);
            }
        }
        session.close();
    } catch (Exception e) {
        e.printStackTrace();
        // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
        // so destroy it manually.
        StandardServiceRegistryBuilder.destroy(registry);
    }
}
Also used : ApproverAttributes(com.tremolosecurity.provisioning.objects.ApproverAttributes) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Configuration(org.hibernate.cfg.Configuration) JaxbCfgHibernateConfiguration(org.hibernate.boot.jaxb.cfg.spi.JaxbCfgHibernateConfiguration) Escalation(com.tremolosecurity.provisioning.objects.Escalation) MetadataSources(org.hibernate.boot.MetadataSources) Approvals(com.tremolosecurity.provisioning.objects.Approvals) Users(com.tremolosecurity.provisioning.objects.Users) UserAttributes(com.tremolosecurity.provisioning.objects.UserAttributes) AllowedApprovers(com.tremolosecurity.provisioning.objects.AllowedApprovers) Approvers(com.tremolosecurity.provisioning.objects.Approvers) AllowedApprovers(com.tremolosecurity.provisioning.objects.AllowedApprovers) StopableThread(com.tremolosecurity.server.StopableThread) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) WorkflowParameters(com.tremolosecurity.provisioning.objects.WorkflowParameters) AuditLogType(com.tremolosecurity.provisioning.objects.AuditLogType) DynamicWorkflows(com.tremolosecurity.provisioning.workflows.DynamicWorkflows) Workflows(com.tremolosecurity.provisioning.objects.Workflows) AuditLogs(com.tremolosecurity.provisioning.objects.AuditLogs) LoadedConfig(org.hibernate.boot.cfgxml.spi.LoadedConfig) DynamicTargets(com.tremolosecurity.provisioning.targets.DynamicTargets) Targets(com.tremolosecurity.provisioning.objects.Targets) JaxbCfgMappingReferenceType(org.hibernate.boot.jaxb.cfg.spi.JaxbCfgMappingReferenceType) ParamType(com.tremolosecurity.config.xml.ParamType) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) LDAPException(com.novell.ldap.LDAPException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SocketException(java.net.SocketException) SQLException(java.sql.SQLException) SchedulerException(org.quartz.SchedulerException) IOException(java.io.IOException) MessagingException(javax.mail.MessagingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) JMSException(javax.jms.JMSException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) BadPaddingException(javax.crypto.BadPaddingException) JaxbCfgHibernateConfiguration(org.hibernate.boot.jaxb.cfg.spi.JaxbCfgHibernateConfiguration) JaxbCfgSessionFactory(org.hibernate.boot.jaxb.cfg.spi.JaxbCfgHibernateConfiguration.JaxbCfgSessionFactory)

Example 2 with StopableThread

use of com.tremolosecurity.server.StopableThread in project OpenUnison by TremoloSecurity.

the class UnisonConfigManagerImpl method clearThreads.

/* (non-Javadoc)
	 * @see com.tremolosecurity.config.util.ConfigManager#clearThreads()
	 */
/* (non-Javadoc)
	 * @see com.tremolosecurity.config.util.UnisonConfigManager#clearThreads()
	 */
@Override
public void clearThreads() {
    for (StopableThread r : this.threads) {
        synchronized (r) {
            r.stop();
            r.notify();
        }
    }
    this.threads.clear();
}
Also used : StopableThread(com.tremolosecurity.server.StopableThread)

Example 3 with StopableThread

use of com.tremolosecurity.server.StopableThread in project OpenUnison by TremoloSecurity.

the class SendMessageThread method initializeHibernate.

private void initializeHibernate(String driver, String user, String password, String url, String dialect, int maxCons, int maxIdleCons, String validationQuery, String mappingFile, String createSchema) {
    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
    Configuration config = new Configuration();
    config.setProperty("hibernate.connection.driver_class", driver);
    config.setProperty("hibernate.connection.password", password);
    config.setProperty("hibernate.connection.url", url);
    config.setProperty("hibernate.connection.username", user);
    config.setProperty("hibernate.dialect", dialect);
    if (createSchema == null || createSchema.equalsIgnoreCase("true")) {
        config.setProperty("hibernate.hbm2ddl.auto", "update");
    }
    config.setProperty("show_sql", "true");
    config.setProperty("hibernate.current_session_context_class", "thread");
    config.setProperty("hibernate.c3p0.max_size", Integer.toString(maxCons));
    config.setProperty("hibernate.c3p0.maxIdleTimeExcessConnections", Integer.toString(maxIdleCons));
    if (validationQuery != null && !validationQuery.isEmpty()) {
        config.setProperty("hibernate.c3p0.testConnectionOnCheckout", "true");
    }
    config.setProperty("hibernate.c3p0.autoCommitOnClose", "true");
    if (validationQuery == null) {
        validationQuery = "SELECT 1";
    }
    config.setProperty("hibernate.c3p0.preferredTestQuery", validationQuery);
    LoadedConfig lc = null;
    if (mappingFile == null || mappingFile.trim().isEmpty()) {
        JaxbCfgHibernateConfiguration jaxbCfg = new JaxbCfgHibernateConfiguration();
        jaxbCfg.setSessionFactory(new JaxbCfgSessionFactory());
        JaxbCfgMappingReferenceType mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(PasswordResetRequest.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);
        lc = LoadedConfig.consume(jaxbCfg);
    } else {
        lc = LoadedConfig.baseline();
    }
    StandardServiceRegistry registry = builder.configure(lc).applySettings(config.getProperties()).build();
    try {
        sessionFactory = null;
        if (mappingFile == null || mappingFile.trim().isEmpty()) {
            sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        } else {
            sessionFactory = new MetadataSources(registry).addResource(mappingFile).buildMetadata().buildSessionFactory();
        }
        this.cfgMgr.addThread(new StopableThread() {

            @Override
            public void run() {
            // TODO Auto-generated method stub
            }

            @Override
            public void stop() {
                logger.info("Stopping hibernate");
                sessionFactory.close();
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
        // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
        // so destroy it manually.
        StandardServiceRegistryBuilder.destroy(registry);
    }
}
Also used : PasswordResetRequest(com.tremolosecurity.proxy.auth.passwordreset.PasswordResetRequest) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Configuration(org.hibernate.cfg.Configuration) JaxbCfgHibernateConfiguration(org.hibernate.boot.jaxb.cfg.spi.JaxbCfgHibernateConfiguration) JaxbCfgHibernateConfiguration(org.hibernate.boot.jaxb.cfg.spi.JaxbCfgHibernateConfiguration) LoadedConfig(org.hibernate.boot.cfgxml.spi.LoadedConfig) MetadataSources(org.hibernate.boot.MetadataSources) JaxbCfgSessionFactory(org.hibernate.boot.jaxb.cfg.spi.JaxbCfgHibernateConfiguration.JaxbCfgSessionFactory) StopableThread(com.tremolosecurity.server.StopableThread) JaxbCfgMappingReferenceType(org.hibernate.boot.jaxb.cfg.spi.JaxbCfgMappingReferenceType) ServletException(javax.servlet.ServletException) MessagingException(javax.mail.MessagingException) LDAPException(com.novell.ldap.LDAPException) SQLException(java.sql.SQLException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry)

Example 4 with StopableThread

use of com.tremolosecurity.server.StopableThread in project OpenUnison by TremoloSecurity.

the class SendMessageThread method init.

@Override
public void init(ServletContext ctx, HashMap<String, Attribute> init) {
    this.cfgMgr = (ConfigManager) ctx.getAttribute(ProxyConstants.TREMOLO_CONFIG);
    this.enabled = Boolean.parseBoolean(init.get("enabled").getValues().get(0));
    if (this.enabled) {
        this.msgQ = new ArrayDeque<SmtpMessage>();
        StopableThread st = new SendMessageThread(this);
        Thread t = new Thread(st);
        t.start();
        this.cfgMgr.addThread(st);
        String driver = init.get("driver").getValues().get(0);
        logger.info("Driver : '" + driver + "'");
        String url = init.get("url").getValues().get(0);
        ;
        logger.info("URL : " + url);
        String user = init.get("user").getValues().get(0);
        ;
        logger.info("User : " + user);
        String pwd = init.get("password").getValues().get(0);
        ;
        logger.info("Password : **********");
        int maxCons = Integer.parseInt(init.get("maxCons").getValues().get(0));
        logger.info("Max Cons : " + maxCons);
        int maxIdleCons = Integer.parseInt(init.get("maxIdleCons").getValues().get(0));
        logger.info("maxIdleCons : " + maxIdleCons);
        String dialect = init.get("dialect").getValues().get(0);
        logger.info("Hibernate Dialect : '" + dialect + "'");
        String validationQuery = init.get("validationQuery").getValues().get(0);
        logger.info("Validation Query : '" + validationQuery + "'");
        String hibernateConfig = init.get("hibernateConfig") != null ? init.get("hibernateConfig").getValues().get(0) : null;
        logger.info("HIbernate mapping file : '" + hibernateConfig + "'");
        String hibernateCreateSchema = init.get("hibernateCreateSchema") != null ? init.get("hibernateCreateSchema").getValues().get(0) : null;
        logger.info("Can create schema : '" + hibernateCreateSchema + "'");
        this.initializeHibernate(driver, user, pwd, url, dialect, maxCons, maxIdleCons, validationQuery, hibernateConfig, hibernateCreateSchema);
        this.passwordResetURL = init.get("passwordResetURI").getValues().get(0);
        this.minValidKey = Integer.parseInt(init.get("minValidKey").getValues().get(0));
        StopableThread tokenClean = new TokenCleanup(this.sessionFactory, this.minValidKey);
        t = new Thread(tokenClean);
        this.cfgMgr.addThread(tokenClean);
        t.start();
        this.smtpServer = init.get("smtpHost").getValues().get(0);
        logger.info("SMTP Server : '" + this.smtpServer + "'");
        this.smtpPort = Integer.parseInt(init.get("smtpPort").getValues().get(0));
        logger.info("SMTP Port : '" + this.smtpPort + "'");
        this.smtpUser = init.get("smtpUser").getValues().get(0);
        logger.info("SMTP User : '" + this.smtpUser + "'");
        this.smtpPassword = init.get("smtpPassword").getValues().get(0);
        logger.info("SMTP Password : '************'");
        this.smtpSubject = init.get("smtpSubject").getValues().get(0);
        logger.info("SMTP Subject : '" + this.smtpSubject + "'");
        this.smtpMsg = init.get("smtpMsg").getValues().get(0);
        this.smtpFrom = init.get("smtpFrom").getValues().get(0);
        logger.info("SMTP From : '" + this.smtpFrom + "'");
        this.smtpTLS = Boolean.parseBoolean(init.get("smtpTLS").getValues().get(0));
        logger.info("SMTP TLS : '" + this.smtpTLS + "'");
        if (init.get("smtpSocksHost") != null && init.get("smtpSocksHost").getValues().size() > 0 && !init.get("smtpSocksHost").getValues().get(0).isEmpty()) {
            logger.info("SMTP SOCKS : 'true'");
            this.useSocks = true;
            this.socksHost = init.get("smtpSocksHost").getValues().get(0);
            logger.info("SMTP SOCKS Host : '" + this.socksHost + "'");
            this.socksPort = Integer.parseInt(init.get("smtpSocksPort").getValues().get(0));
            logger.info("SMTP SOCKS Port : '" + this.socksPort + "'");
        } else {
            logger.info("SMTP SOCKS : 'false'");
            this.useSocks = false;
        }
        if (init.get("smtpLocalhost") != null && init.get("smtpLocalhost").getValues().size() > 0) {
            this.smtpLocalhost = init.get("smtpLocalhost").getValues().get(0);
            logger.info("SMTP Localhost : '" + this.smtpLocalhost + "'");
        } else {
            this.smtpLocalhost = null;
        }
        if (init.get("uidAttributeName") != null) {
            this.lookupAttributeName = init.get("uidAttributeName").getValues().get(0);
        } else {
            this.lookupAttributeName = "mail";
        }
    }
}
Also used : StopableThread(com.tremolosecurity.server.StopableThread) StopableThread(com.tremolosecurity.server.StopableThread)

Example 5 with StopableThread

use of com.tremolosecurity.server.StopableThread in project OpenUnison by TremoloSecurity.

the class CrlChecker method init.

@Override
public void init(ServletContext ctx, HashMap<String, Attribute> init) {
    this.cfgMgr = (ConfigManager) ctx.getAttribute(ProxyConstants.TREMOLO_CONFIG);
    this.crls = new ArrayList<CRLManager>();
    if (init.get("crl.names") != null) {
        for (String crlName : init.get("crl.names").getValues()) {
            if (crlName.isEmpty()) {
                break;
            }
            String type = init.get("crl." + crlName + ".type").getValues().get(0);
            try {
                CRLManager crl = (CRLManager) Class.forName(type).newInstance();
                crl.init(crlName, init, cfgMgr);
                this.crls.add(crl);
            } catch (Exception e) {
                logger.error("could not initialize crl : " + type, e);
            }
        }
        StopableThread crlChecker = new CrlChecker(this.crls);
        Thread t = new Thread(crlChecker);
        this.cfgMgr.addThread(crlChecker);
        t.start();
    }
    this.extracts = new ArrayList<CertificateExtractSubjectAttribute>();
    if (init.get("extracts") != null) {
        Attribute attr = init.get("extracts");
        for (String className : attr.getValues()) {
            try {
                this.extracts.add((CertificateExtractSubjectAttribute) Class.forName(className).newInstance());
            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                logger.warn("Could not load : '" + className + "'", e);
            }
        }
    }
}
Also used : LDAPAttribute(com.novell.ldap.LDAPAttribute) Attribute(com.tremolosecurity.saml.Attribute) LDAPException(com.novell.ldap.LDAPException) ServletException(javax.servlet.ServletException) CertificateParsingException(java.security.cert.CertificateParsingException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) CRLManager(com.tremolosecurity.proxy.auth.ssl.CRLManager) StopableThread(com.tremolosecurity.server.StopableThread) StopableThread(com.tremolosecurity.server.StopableThread)

Aggregations

StopableThread (com.tremolosecurity.server.StopableThread)10 IOException (java.io.IOException)5 LDAPException (com.novell.ldap.LDAPException)4 ProvisioningException (com.tremolosecurity.provisioning.core.ProvisioningException)3 Attribute (com.tremolosecurity.saml.Attribute)3 ServletException (javax.servlet.ServletException)3 MetadataSources (org.hibernate.boot.MetadataSources)3 LoadedConfig (org.hibernate.boot.cfgxml.spi.LoadedConfig)3 JaxbCfgHibernateConfiguration (org.hibernate.boot.jaxb.cfg.spi.JaxbCfgHibernateConfiguration)3 JaxbCfgSessionFactory (org.hibernate.boot.jaxb.cfg.spi.JaxbCfgHibernateConfiguration.JaxbCfgSessionFactory)3 JaxbCfgMappingReferenceType (org.hibernate.boot.jaxb.cfg.spi.JaxbCfgMappingReferenceType)3 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)3 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)3 Configuration (org.hibernate.cfg.Configuration)3 LDAPAttribute (com.novell.ldap.LDAPAttribute)2 ParamType (com.tremolosecurity.config.xml.ParamType)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 MalformedURLException (java.net.MalformedURLException)2 HashMap (java.util.HashMap)2 MongoClient (com.mongodb.MongoClient)1