Search in sources :

Example 6 with ConfigurationServiceException

use of org.nhindirect.config.service.ConfigurationServiceException in project nhin-d by DirectProject.

the class MainController method toCertContainer.

public CertContainer toCertContainer(byte[] data) throws Exception {
    CertContainer certContainer = null;
    try {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        // lets try this a as a PKCS12 data stream first
        try {
            KeyStore localKeyStore = KeyStore.getInstance("PKCS12", Certificate.getJCEProviderName());
            localKeyStore.load(bais, "".toCharArray());
            Enumeration<String> aliases = localKeyStore.aliases();
            // we are really expecting only one alias
            if (aliases.hasMoreElements()) {
                String alias = aliases.nextElement();
                X509Certificate cert = (X509Certificate) localKeyStore.getCertificate(alias);
                // check if there is private key
                Key key = localKeyStore.getKey(alias, "".toCharArray());
                if (key != null && key instanceof PrivateKey) {
                    certContainer = new CertContainer(cert, key);
                }
            }
        } catch (Exception e) {
        // must not be a PKCS12 stream, go on to next step
        }
        if (certContainer == null) {
            //try X509 certificate factory next
            bais.reset();
            bais = new ByteArrayInputStream(data);
            X509Certificate cert = (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(bais);
            certContainer = new CertContainer(cert, null);
        }
        bais.close();
    } catch (Exception e) {
        throw new ConfigurationServiceException("Data cannot be converted to a valid X.509 Certificate", e);
    }
    return certContainer;
}
Also used : PrivateKey(java.security.PrivateKey) ByteArrayInputStream(java.io.ByteArrayInputStream) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException) KeyStore(java.security.KeyStore) CertContainer(org.nhindirect.config.ui.DNSController.CertContainer) X509Certificate(java.security.cert.X509Certificate) Key(java.security.Key) PrivateKey(java.security.PrivateKey) CertificateEncodingException(javax.security.cert.CertificateEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) TextParseException(org.xbill.DNS.TextParseException) IOException(java.io.IOException) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException)

Example 7 with ConfigurationServiceException

use of org.nhindirect.config.service.ConfigurationServiceException in project nhin-d by DirectProject.

the class MainController method search.

/**
	 * Execute the search and return the results
	 */
@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping(value = "/search", method = RequestMethod.GET)
public ModelAndView search(@RequestHeader(value = "X-Requested-With", required = false) String requestedWith, HttpSession session, @ModelAttribute SimpleForm simpleForm, Model model, @RequestParam(value = "submitType") String actionPath) {
    if (log.isDebugEnabled())
        log.debug("Enter search");
    String message = "Search complete";
    ModelAndView mav = new ModelAndView();
    // check to see if new domain requested
    if (actionPath.equalsIgnoreCase("gotosettings")) {
        if (log.isDebugEnabled())
            log.debug("trying to go to the settings page");
        String action = "add";
        model.addAttribute("action", action);
        mav.setViewName("settings");
        mav.addObject("actionPath", actionPath);
        SettingsForm form = (SettingsForm) session.getAttribute("settingsForm");
        if (form == null) {
            form = new SettingsForm();
        }
        model.addAttribute("settingsForm", form);
        // retrieve list of settings for settingsResults
        List<Setting> results = null;
        if (configSvc != null) {
            try {
                Collection<Setting> settings = configSvc.getAllSettings();
                if (settings != null)
                    results = new ArrayList<Setting>(settings);
                else
                    results = new ArrayList<Setting>();
            } catch (ConfigurationServiceException e) {
                e.printStackTrace();
            }
        }
        model.addAttribute("simpleForm", new SimpleForm());
        model.addAttribute("settingsResults", results);
    } else if (actionPath.equalsIgnoreCase("gotocertificates")) {
        if (log.isDebugEnabled())
            log.debug("trying to go to the certificates page");
        String action = "Update";
        model.addAttribute("action", action);
        mav.setViewName("certificates");
        mav.addObject("actionPath", actionPath);
        CertificateForm form = (CertificateForm) session.getAttribute("certificateForm");
        if (form == null) {
            form = new CertificateForm();
        }
        model.addAttribute("certificateForm", form);
        // retrieve list of settings for settingsResults
        List<Certificate> results = null;
        if (configSvc != null) {
            try {
                Collection<Certificate> certs = configSvc.listCertificates(1, 10000, CertificateGetOptions.DEFAULT);
                if (certs != null)
                    results = new ArrayList<Certificate>(certs);
                else
                    results = new ArrayList<Certificate>();
            } catch (ConfigurationServiceException e) {
                e.printStackTrace();
            }
        }
        model.addAttribute("simpleForm", new SimpleForm());
        model.addAttribute("certificatesResults", results);
    } else if (actionPath.equalsIgnoreCase("newdomain")) {
        if (log.isDebugEnabled())
            log.debug("trying to go to the new domain page");
        HashMap<String, String> msgs = new HashMap<String, String>();
        mav.addObject("msgs", msgs);
        model.addAttribute("simpleForm", new SimpleForm());
        AddressForm addrform = new AddressForm();
        addrform.setId(0L);
        model.addAttribute("addressForm", addrform);
        // TODO: once certificates and anchors are available change code accordingly
        CertificateForm cform = new CertificateForm();
        cform.setId(0L);
        AnchorForm aform = new AnchorForm();
        aform.setId(0L);
        model.addAttribute("certificateForm", cform);
        model.addAttribute("anchorForm", aform);
        String action = "Add";
        DomainForm form = (DomainForm) session.getAttribute("domainForm");
        if (form == null) {
            form = new DomainForm();
        }
        model.addAttribute("domainForm", form);
        model.addAttribute("action", action);
        mav.setViewName("domain");
        mav.addObject("actionPath", actionPath);
        mav.addObject("statusList", EntityStatus.getEntityStatusList());
    } else if (actionPath.equalsIgnoreCase("gotodns")) {
        if (log.isDebugEnabled())
            log.debug("trying to go to the DNS page");
        HashMap<String, String> msgs = new HashMap<String, String>();
        mav.addObject("msgs", msgs);
        String action = "Update";
        model.addAttribute("action", action);
        // get all DNSType.A.getValue() records
        // GET A RECORDS
        Collection<DNSRecord> arecords = null;
        arecords = getDnsRecords(DNSType.A.getValue());
        model.addAttribute("dnsARecordResults", arecords);
        // GET A4 RECORDS
        Collection<DNSRecord> a4records = null;
        a4records = getDnsRecords(DNSType.AAAA.getValue());
        model.addAttribute("dnsA4RecordResults", a4records);
        // GET C RECORDS
        Collection<DNSRecord> crecords = null;
        crecords = getDnsRecords(DNSType.CNAME.getValue());
        model.addAttribute("dnsCnameRecordResults", crecords);
        // GET Cert RECORDS
        Collection<DNSRecord> certrecords = null;
        certrecords = getDnsRecords(DNSType.CERT.getValue());
        model.addAttribute("dnsCertRecordResults", certrecords);
        // GET MX RECORDS
        Collection<DNSRecord> mxrecords = null;
        mxrecords = getDnsRecords(DNSType.MX.getValue());
        model.addAttribute("dnsMxRecordResults", mxrecords);
        // GET SRV RECORDS
        Collection<DNSRecord> srvrecords = null;
        srvrecords = getDnsRecords(DNSType.SRV.getValue());
        model.addAttribute("dnsSrvRecordResults", srvrecords);
        mav.setViewName("dns");
        mav.addObject("actionPath", actionPath);
        model.addAttribute("AdnsForm", new DNSEntryForm());
        model.addAttribute("AAdnsForm", new DNSEntryForm());
        model.addAttribute("CdnsForm", new DNSEntryForm());
        model.addAttribute("CertdnsForm", new DNSEntryForm());
        model.addAttribute("MXdnsForm", new DNSEntryForm());
        model.addAttribute("SrvdnsForm", new DNSEntryForm());
        refreshModelFromService(model);
        model.addAttribute("simpleForm", new SimpleForm());
    } else {
        SearchDomainForm form = (SearchDomainForm) session.getAttribute("searchDomainForm");
        if (form == null) {
            form = new SearchDomainForm();
        }
        model.addAttribute(form);
        model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(requestedWith));
        String domain = form.getDomainName();
        EntityStatus status = form.getStatus();
        List<Domain> results = null;
        if (configSvc != null) {
            Collection<Domain> domains = configSvc.searchDomain(domain, status);
            if (domains != null) {
                results = new ArrayList<Domain>(domains);
            } else {
                results = new ArrayList<Domain>();
            }
        }
        if (AjaxUtils.isAjaxRequest(requestedWith)) {
            // prepare model for rendering success message in this request
            model.addAttribute("message", new Message(MessageType.success, message));
            model.addAttribute("ajaxRequest", true);
            model.addAttribute("searchResults", results);
            return null;
        }
        mav.setViewName("main");
        mav.addObject("statusList", EntityStatus.getEntityStatusList());
        mav.addObject("searchResults", results);
    }
    if (log.isDebugEnabled())
        log.debug("Exit");
    return mav;
}
Also used : SimpleForm(org.nhindirect.config.ui.form.SimpleForm) CertificateForm(org.nhindirect.config.ui.form.CertificateForm) AnchorForm(org.nhindirect.config.ui.form.AnchorForm) Message(org.nhindirect.config.ui.flash.FlashMap.Message) HashMap(java.util.HashMap) Setting(org.nhindirect.config.store.Setting) ModelAndView(org.springframework.web.servlet.ModelAndView) ArrayList(java.util.ArrayList) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException) DNSEntryForm(org.nhindirect.config.ui.form.DNSEntryForm) DomainForm(org.nhindirect.config.ui.form.DomainForm) SearchDomainForm(org.nhindirect.config.ui.form.SearchDomainForm) SearchDomainForm(org.nhindirect.config.ui.form.SearchDomainForm) AddressForm(org.nhindirect.config.ui.form.AddressForm) SettingsForm(org.nhindirect.config.ui.form.SettingsForm) Collection(java.util.Collection) EntityStatus(org.nhindirect.config.store.EntityStatus) List(java.util.List) ArrayList(java.util.ArrayList) X509Certificate(java.security.cert.X509Certificate) Certificate(org.nhindirect.config.store.Certificate) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with ConfigurationServiceException

use of org.nhindirect.config.service.ConfigurationServiceException in project nhin-d by DirectProject.

the class DomainController method toCertContainer.

public CertContainer toCertContainer(byte[] data) throws Exception {
    CertContainer certContainer = null;
    try {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        // lets try this a as a PKCS12 data stream first
        try {
            final KeyStore localKeyStore = KeyStore.getInstance("PKCS12", DNSController.getJCEProviderName());
            localKeyStore.load(bais, "".toCharArray());
            Enumeration<String> aliases = localKeyStore.aliases();
            // we are really expecting only one alias
            if (aliases.hasMoreElements()) {
                String alias = aliases.nextElement();
                X509Certificate cert = (X509Certificate) localKeyStore.getCertificate(alias);
                // check if there is private key
                Key key = localKeyStore.getKey(alias, "".toCharArray());
                if (key != null && key instanceof PrivateKey) {
                    certContainer = new CertContainer(cert, key);
                }
            }
        } catch (Exception e) {
        // must not be a PKCS12 stream, go on to next step
        }
        if (certContainer == null) {
            //try X509 certificate factory next
            bais.reset();
            bais = new ByteArrayInputStream(data);
            X509Certificate cert = (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(bais);
            certContainer = new CertContainer(cert, null);
        }
        bais.close();
    } catch (Exception e) {
        throw new ConfigurationServiceException("Data cannot be converted to a valid X.509 Certificate", e);
    }
    return certContainer;
}
Also used : PrivateKey(java.security.PrivateKey) ByteArrayInputStream(java.io.ByteArrayInputStream) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) Key(java.security.Key) PrivateKey(java.security.PrivateKey) ServiceException(org.nhindirect.common.rest.exceptions.ServiceException) IOException(java.io.IOException) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException)

Example 9 with ConfigurationServiceException

use of org.nhindirect.config.service.ConfigurationServiceException in project nhin-d by DirectProject.

the class DomainController method addAnchor.

@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping(value = "/addanchor", method = RequestMethod.POST)
public ModelAndView addAnchor(@RequestHeader(value = "X-Requested-With", required = false) String requestedWith, HttpSession session, @ModelAttribute AnchorForm anchorForm, Model model, @RequestParam(value = "submitType") String actionPath, @RequestParam(value = "id") String id) {
    final String domAttr = (String) session.getAttribute("currentDomainName");
    ModelAndView mav = new ModelAndView();
    String strid = "";
    //anchorForm.getId();
    strid = "" + domAttr;
    Domain dom = null;
    try {
        dom = domainService.getDomain(strid);
    } catch (ServiceException e) {
        e.printStackTrace();
    }
    if (log.isDebugEnabled())
        log.debug("Enter domain/addanchor");
    if (actionPath.equalsIgnoreCase("newanchor") || actionPath.equalsIgnoreCase("add anchor")) {
        strid = "" + anchorForm.getId();
        String owner = "";
        if (dom != null) {
            owner = dom.getDomainName();
        }
        // insert the new address into the Domain list of Addresses
        if (log.isDebugEnabled())
            log.debug("beginning to evaluate filedata");
        try {
            if (!anchorForm.getFileData().isEmpty()) {
                final byte[] bytes = anchorForm.getFileData().getBytes();
                String theUser = "";
                if (bytes != null) {
                    // get the owner from the certificate information
                    // first transform into a certificate
                    CertContainer cont = toCertContainer(bytes);
                    if (cont != null && cont.getCert() != null) {
                        // now get the owner info from the cert
                        theUser = getTrustedEntityName(cont.getCert().getSubjectX500Principal());
                        anchorForm.setTrusteddomainoruser(theUser);
                    }
                }
                // store the bytes somewhere
                final Anchor ank = new Anchor();
                ank.setCertificateData(bytes);
                if (log.isDebugEnabled())
                    log.debug("incoming is: " + anchorForm.isIncoming() + " and outgoing is: " + anchorForm.isOutgoing());
                ank.setIncoming(anchorForm.isIncoming());
                ank.setOutgoing(anchorForm.isOutgoing());
                ank.setOwner(owner);
                ank.setStatus(anchorForm.getStatus());
                anchorService.addAnchor(ank);
                if (log.isDebugEnabled())
                    log.debug("store the anchor certificate into database");
            } else {
                if (log.isDebugEnabled())
                    log.debug("DO NOT store the anchor certificate into database BECAUSE THERE IS NO FILE");
            }
        } catch (ConfigurationServiceException ed) {
            if (log.isDebugEnabled())
                log.error(ed);
        } catch (Exception e) {
            if (log.isDebugEnabled())
                log.error(e.getMessage());
            e.printStackTrace();
        }
        // certificate and anchor forms and results
        try {
            final Collection<Certificate> certs = certService.getCertificatesByOwner(owner);
            model.addAttribute("certificatesResults", certs);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        try {
            final Collection<Anchor> anchors = anchorService.getAnchorsForOwner(owner, false, false, "");
            final Collection<AnchorForm> convertedanchors = convertAnchors(anchors);
            // now set anchorsResults
            model.addAttribute("anchorsResults", convertedanchors);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        try {
            CertificateForm cform = new CertificateForm();
            cform.setId(dom.getId());
            model.addAttribute("certificateForm", cform);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        try {
            AnchorForm aform = new AnchorForm();
            aform.setId(dom.getId());
            model.addAttribute("anchorForm", aform);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(requestedWith));
        SimpleForm simple = new SimpleForm();
        simple.setId(dom.getId());
        simple.setDomainName(dom.getDomainName());
        model.addAttribute("simpleForm", simple);
        model.addAttribute("addressesResults", dom.getAddresses());
        mav.setViewName("domain");
        // the Form's default button action
        String action = "Update";
        DomainForm form = (DomainForm) session.getAttribute("domainForm");
        if (form == null) {
            form = new DomainForm();
            form.populate(dom);
        }
        model.addAttribute("domainForm", form);
        model.addAttribute("action", action);
        model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(requestedWith));
        mav.addObject("privKeyTypeList", PrivateKeyType.getPrivKeyTypeList());
        mav.addObject("statusList", EntityStatus.getEntityStatusList());
    }
    AddressForm addressForm2 = new AddressForm();
    addressForm2.setDisplayName("");
    addressForm2.setEndpoint("");
    addressForm2.setEmailAddress("");
    addressForm2.setType("");
    addressForm2.setId(Long.parseLong(strid));
    model.addAttribute("addressForm", addressForm2);
    return new ModelAndView("redirect:/config/domain?id=" + dom.getDomainName() + "#tab2");
//return mav;
}
Also used : CertificateForm(org.nhindirect.config.ui.form.CertificateForm) SimpleForm(org.nhindirect.config.ui.form.SimpleForm) AnchorForm(org.nhindirect.config.ui.form.AnchorForm) ModelAndView(org.springframework.web.servlet.ModelAndView) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException) ServiceException(org.nhindirect.common.rest.exceptions.ServiceException) IOException(java.io.IOException) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException) DomainForm(org.nhindirect.config.ui.form.DomainForm) SearchDomainForm(org.nhindirect.config.ui.form.SearchDomainForm) Anchor(org.nhindirect.config.model.Anchor) TrustBundleAnchor(org.nhindirect.config.model.TrustBundleAnchor) AddressForm(org.nhindirect.config.ui.form.AddressForm) ServiceException(org.nhindirect.common.rest.exceptions.ServiceException) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException) Domain(org.nhindirect.config.model.Domain) X509Certificate(java.security.cert.X509Certificate) Certificate(org.nhindirect.config.model.Certificate) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with ConfigurationServiceException

use of org.nhindirect.config.service.ConfigurationServiceException in project nhin-d by DirectProject.

the class DNSController method addCertSetting.

@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping(value = "/addCertDNSRecord", method = RequestMethod.POST)
public ModelAndView addCertSetting(@RequestHeader(value = "X-Requested-With", required = false) String requestedWith, HttpSession session, @ModelAttribute("CertdnsForm") DNSEntryForm CertdnsForm, Model model, @RequestParam(value = "submitType") String actionPath) {
    if (log.isDebugEnabled())
        log.debug("Enter");
    // CERT records
    if (CertdnsForm != null && !CertdnsForm.getName().equalsIgnoreCase("") && CertdnsForm.getTtl() != 0L) {
        X509Certificate tcert = null;
        byte[] certbytes = null;
        try {
            if (!CertdnsForm.getFileData().isEmpty()) {
                byte[] bytes = CertdnsForm.getFileData().getBytes();
                certbytes = bytes;
                if (bytes != null) {
                    // get the owner from the certificate information
                    // first transform into a certificate
                    final CertContainer cont = toCertContainer(bytes);
                    if (cont != null && cont.getCert() != null) {
                        final Certificate cert2 = new Certificate();
                        cert2.setData(bytes);
                        tcert = cont.getCert();
                    }
                }
            }
        } catch (ConfigurationServiceException ed) {
            if (log.isDebugEnabled())
                log.error(ed);
        } catch (Exception e) {
            if (log.isDebugEnabled())
                log.error(e.getMessage());
            e.printStackTrace();
        }
        CertdnsForm.setType("CERT");
        CertdnsForm.setCertificate(tcert);
        CertdnsForm.setCertificateData(certbytes);
        try {
            dnsService.addDNSRecord(DNSEntryForm.createCertRecord(CertdnsForm));
        } catch (ServiceException e) {
            e.printStackTrace();
        }
    }
    model.addAttribute("AdnsForm", new DNSEntryForm());
    model.addAttribute("AAdnsForm", new DNSEntryForm());
    model.addAttribute("CdnsForm", new DNSEntryForm());
    model.addAttribute("MXdnsForm", new DNSEntryForm());
    model.addAttribute("CertdnsForm", new DNSEntryForm());
    model.addAttribute("SrvdnsForm", new DNSEntryForm());
    ModelAndView mav = new ModelAndView("dns");
    refreshModelFromService(model);
    if (log.isDebugEnabled())
        log.debug("Exit");
    return mav;
}
Also used : ServiceException(org.nhindirect.common.rest.exceptions.ServiceException) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException) ModelAndView(org.springframework.web.servlet.ModelAndView) DNSEntryForm(org.nhindirect.config.ui.form.DNSEntryForm) X509Certificate(java.security.cert.X509Certificate) CertificateEncodingException(javax.security.cert.CertificateEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) TextParseException(org.xbill.DNS.TextParseException) ServiceException(org.nhindirect.common.rest.exceptions.ServiceException) IOException(java.io.IOException) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException) X509Certificate(java.security.cert.X509Certificate) Certificate(org.nhindirect.config.model.Certificate) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ConfigurationServiceException (org.nhindirect.config.service.ConfigurationServiceException)14 X509Certificate (java.security.cert.X509Certificate)10 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 ModelAndView (org.springframework.web.servlet.ModelAndView)6 TextParseException (org.xbill.DNS.TextParseException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IOException (java.io.IOException)5 Key (java.security.Key)5 KeyStore (java.security.KeyStore)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 PrivateKey (java.security.PrivateKey)5 CertificateEncodingException (javax.security.cert.CertificateEncodingException)5 ServiceException (org.nhindirect.common.rest.exceptions.ServiceException)5 DNSEntryForm (org.nhindirect.config.ui.form.DNSEntryForm)4 ArrayList (java.util.ArrayList)3 Certificate (org.nhindirect.config.model.Certificate)2 Certificate (org.nhindirect.config.store.Certificate)2 DNSRecord (org.nhindirect.config.store.DNSRecord)2 AddressForm (org.nhindirect.config.ui.form.AddressForm)2