Search in sources :

Example 11 with ConfigurationServiceException

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

the class DNSController method removeAnchors.

@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping(value = "/removesettings", method = RequestMethod.POST)
public ModelAndView removeAnchors(@RequestHeader(value = "X-Requested-With", required = false) String requestedWith, @ModelAttribute("AdnsForm") DNSEntryForm AdnsForm, @ModelAttribute("NSdnsForm") DNSEntryForm NSdnsForm, @ModelAttribute("AAdnsForm") DNSEntryForm AAdnsForm, @ModelAttribute("CdnsForm") DNSEntryForm CdnsForm, @ModelAttribute("CertdnsForm") DNSEntryForm CertdnsForm, @ModelAttribute("SrvdnsForm") DNSEntryForm SrvdnsForm, @ModelAttribute("SoadnsForm") DNSEntryForm SoadnsForm, @ModelAttribute("MXdnsForm") DNSEntryForm MXdnsForm, HttpSession session, Model model, @RequestParam(value = "submitType") String actionPath) {
    if (log.isDebugEnabled())
        log.debug("Enter domain/removesettings");
    // A records
    try {
        Collection<DNSRecord> arecords = null;
        if (dnsService != null && AdnsForm != null && actionPath != null && (actionPath.equalsIgnoreCase("deleteADnsEntries") || actionPath.equalsIgnoreCase("Remove Selected As")) && AdnsForm.getRemove() != null) {
            int cnt = AdnsForm.getRemove().size();
            arecords = dnsService.getDNSRecord(DNSType.A.getValue(), "");
            for (int x = 0; x < cnt; x++) {
                String removeid = AdnsForm.getRemove().get(x);
                Long remid = Long.parseLong(removeid);
                for (Iterator<DNSRecord> iter = arecords.iterator(); iter.hasNext(); ) {
                    DNSRecord t = (DNSRecord) iter.next();
                    if (t.getId() == remid) {
                        dnsService.deleteDNSRecordsByIds(Arrays.asList(remid));
                    }
                }
            }
        }
    } catch (ServiceException e1) {
    }
    // A4 records
    try {
        Collection<DNSRecord> a4records = null;
        if (dnsService != null && AAdnsForm != null && actionPath != null && (actionPath.equalsIgnoreCase("deleteA4DnsEntries") || actionPath.equalsIgnoreCase("Remove Selected A4s")) && AAdnsForm.getRemove() != null) {
            int cnt = AAdnsForm.getRemove().size();
            a4records = dnsService.getDNSRecord(DNSType.AAAA.getValue(), "");
            for (int x = 0; x < cnt; x++) {
                String removeid = AAdnsForm.getRemove().get(x);
                Long remid = Long.parseLong(removeid);
                for (Iterator<DNSRecord> iter = a4records.iterator(); iter.hasNext(); ) {
                    DNSRecord t = (DNSRecord) iter.next();
                    if (t.getId() == remid) {
                        dnsService.deleteDNSRecordsByIds(Arrays.asList(remid));
                    }
                }
            }
        }
    } catch (ServiceException e1) {
    }
    // CNAME records
    try {
        Collection<DNSRecord> a4records = null;
        if (dnsService != null && CdnsForm != null && actionPath != null && (actionPath.equalsIgnoreCase("deleteCNAMEDnsEntries") || actionPath.equalsIgnoreCase("Remove Selected CNAMEs")) && CdnsForm.getRemove() != null) {
            int cnt = AAdnsForm.getRemove().size();
            a4records = dnsService.getDNSRecord(DNSType.CNAME.getValue(), "");
            for (int x = 0; x < cnt; x++) {
                String removeid = CdnsForm.getRemove().get(x);
                Long remid = Long.parseLong(removeid);
                for (Iterator<DNSRecord> iter = a4records.iterator(); iter.hasNext(); ) {
                    DNSRecord t = (DNSRecord) iter.next();
                    if (t.getId() == remid) {
                        dnsService.deleteDNSRecordsByIds(Arrays.asList(remid));
                    }
                }
            }
        }
    } catch (ServiceException e1) {
    }
    // MX records
    try {
        Collection<DNSRecord> a4records = null;
        if (dnsService != null && MXdnsForm != null && actionPath != null && (actionPath.equalsIgnoreCase("deleteMXDnsEntries") || actionPath.equalsIgnoreCase("Remove Selected MXs")) && MXdnsForm.getRemove() != null) {
            int cnt = MXdnsForm.getRemove().size();
            a4records = dnsService.getDNSRecord(DNSType.MX.getValue(), "");
            for (int x = 0; x < cnt; x++) {
                String removeid = MXdnsForm.getRemove().get(x);
                Long remid = Long.parseLong(removeid);
                for (Iterator<DNSRecord> iter = a4records.iterator(); iter.hasNext(); ) {
                    DNSRecord t = (DNSRecord) iter.next();
                    if (t.getId() == remid) {
                        dnsService.deleteDNSRecordsByIds(Arrays.asList(remid));
                    }
                }
            }
        }
    } catch (ServiceException e1) {
    }
    // CERT records
    try {
        Collection<DNSRecord> a4records = null;
        if (dnsService != null && CertdnsForm != null && actionPath != null && (actionPath.equalsIgnoreCase("deleteCERTDnsEntries") || actionPath.equalsIgnoreCase("Remove Selected CERTs")) && CertdnsForm.getRemove() != null) {
            int cnt = CertdnsForm.getRemove().size();
            a4records = dnsService.getDNSRecord(DNSType.CERT.getValue(), "");
            for (int x = 0; x < cnt; x++) {
                String removeid = CertdnsForm.getRemove().get(x);
                Long remid = Long.parseLong(removeid);
                for (Iterator<DNSRecord> iter = a4records.iterator(); iter.hasNext(); ) {
                    DNSRecord t = (DNSRecord) iter.next();
                    if (t.getId() == remid) {
                        dnsService.deleteDNSRecordsByIds(Arrays.asList(remid));
                    }
                }
            }
        }
    } catch (ServiceException e1) {
    }
    // SRV records
    try {
        Collection<DNSRecord> a4records = null;
        if (dnsService != null && SrvdnsForm != null && actionPath != null && (actionPath.equalsIgnoreCase("deleteSRVDnsEntries") || actionPath.equalsIgnoreCase("Remove Selected SRVs")) && SrvdnsForm.getRemove() != null) {
            int cnt = SrvdnsForm.getRemove().size();
            a4records = dnsService.getDNSRecord(DNSType.SRV.getValue(), "");
            for (int x = 0; x < cnt; x++) {
                String removeid = SrvdnsForm.getRemove().get(x);
                Long remid = Long.parseLong(removeid);
                for (Iterator<DNSRecord> iter = a4records.iterator(); iter.hasNext(); ) {
                    DNSRecord t = (DNSRecord) iter.next();
                    if (t.getId() == remid) {
                        dnsService.deleteDNSRecordsByIds(Arrays.asList(remid));
                    }
                }
            }
        }
    } catch (ServiceException e1) {
    }
    // SOA records
    try {
        Collection<DNSRecord> soarecords = null;
        if (dnsService != null && SoadnsForm != null && actionPath != null && (actionPath.equalsIgnoreCase("deleteSOADnsEntries") || actionPath.equalsIgnoreCase("Remove Selected SOAs")) && SoadnsForm.getRemove() != null) {
            int cnt = SoadnsForm.getRemove().size();
            soarecords = dnsService.getDNSRecord(DNSType.SOA.getValue(), "");
            for (int x = 0; x < cnt; x++) {
                String removeid = SoadnsForm.getRemove().get(x);
                Long remid = Long.parseLong(removeid);
                for (Iterator<DNSRecord> iter = soarecords.iterator(); iter.hasNext(); ) {
                    DNSRecord t = (DNSRecord) iter.next();
                    if (t.getId() == remid) {
                        dnsService.deleteDNSRecordsByIds(Arrays.asList(remid));
                    }
                }
            }
        }
    } catch (ServiceException e1) {
    }
    // NS records
    try {
        Collection<DNSRecord> nsrecords = null;
        if (dnsService != null && NSdnsForm != null && actionPath != null && (actionPath.equalsIgnoreCase("deleteNSDnsEntries") || actionPath.equalsIgnoreCase("Remove Selected NSs")) && NSdnsForm.getRemove() != null) {
            int cnt = NSdnsForm.getRemove().size();
            nsrecords = dnsService.getDNSRecord(DNSType.NS.getValue(), "");
            for (int x = 0; x < cnt; x++) {
                String removeid = NSdnsForm.getRemove().get(x);
                Long remid = Long.parseLong(removeid);
                for (Iterator<DNSRecord> iter = nsrecords.iterator(); iter.hasNext(); ) {
                    DNSRecord t = (DNSRecord) iter.next();
                    if (t.getId() == remid) {
                        dnsService.deleteDNSRecordsByIds(Arrays.asList(remid));
                    }
                }
            }
        }
    } catch (ServiceException e1) {
    }
    // additional post clean up to redisplay
    ModelAndView mav = new ModelAndView("dns");
    if (AdnsForm.getRemove() != null) {
        if (log.isDebugEnabled())
            log.debug("the list of checkboxes checked or not is: " + AdnsForm.getRemove().toString());
    }
    /*
		 * if (configSvc != null && simpleForm != null && actionPath != null &&
		 * actionPath.equalsIgnoreCase("delete") && simpleForm.getRemove() !=
		 * null) { int cnt = simpleForm.getRemove().size(); try{
		 * Collection<String> settingstoberemovedlist = simpleForm.getRemove();
		 * if (log.isDebugEnabled())
		 * log.debug(" Trying to remove settings from database");
		 * configSvc.deleteSetting(settingstoberemovedlist); if
		 * (log.isDebugEnabled())
		 * log.debug(" SUCCESS Trying to remove settings"); } catch
		 * (ConfigurationServiceException e) { if (log.isDebugEnabled())
		 * log.error(e); } }
		 */
    refreshModelFromService(model);
    model.addAttribute("dnsEntryForm", new DNSEntryForm());
    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());
    return mav;
}
Also used : DNSRecord(org.nhindirect.config.model.DNSRecord) ServiceException(org.nhindirect.common.rest.exceptions.ServiceException) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException) ModelAndView(org.springframework.web.servlet.ModelAndView) DNSEntryForm(org.nhindirect.config.ui.form.DNSEntryForm) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 12 with ConfigurationServiceException

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

the class ServiceTest method testDNSService.

//    @Test
//    public void testDomainService()
//    {
//        try
//        {
//            int count = configSvc. getDomainCount();
//            log.info(""+count + " Domains exist in the database");
//        } 
//        catch (ConfigurationServiceException e)
//        {
//            e.printStackTrace();
//            fail(e.getMessage());
//        }  
//    }
//    @Test
public void testDNSService() {
    //        try
    //        {
    //configSvc.getDNSCount();
    int count = 0;
    //            log.info(""+count + " DNS records exist in the database");
    //            System.out.println(""+count + " DNS records exist in the database");
    //            configSvc.removeDomain("pjpassoc.com");
    Collection<DNSRecord> records = new ArrayList<DNSRecord>();
    //            records.add(DNSRecordUtils.createARecord("pjpassoc.com", 3600L, "192.168.1.1"));
    //            records.add(DNSRecordUtils.createMXRecord("pjpassoc.com", "mail.pjpassoc.com", 3600L, 0));
    byte[] certData = null;
    try {
        //				certData = loadPkcs12FromCertAndKey("gm2552.der", "gm2552Key.der");
        certData = loadCertificateData("gm2552.der");
        if (certData != null) {
            // get the owner from the certificate information
            // first transform into a certificate
            CertContainer cont = toCertContainer(certData);
            if (cont != null && cont.getCert() != null) {
                Certificate cert2 = new Certificate();
                cert2.setData(certData);
                System.out.println(getThumbPrint(cont.getCert()));
            }
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
//            records.add(DNSEntryForm.createCertRecord("yahoo.com", 3600L, 0,0,0,certData));
//            configSvc.addDNS(records);
//            assertEquals(2, configSvc.getDNSCount());
//        }
//        catch (ConfigurationServiceException e)
//        {
//            e.printStackTrace();
//            fail(e.getMessage());
//        }  
}
Also used : DNSRecord(org.nhindirect.config.store.DNSRecord) ArrayList(java.util.ArrayList) TextParseException(org.xbill.DNS.TextParseException) CertificateEncodingException(javax.security.cert.CertificateEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException) X509Certificate(java.security.cert.X509Certificate) Certificate(org.nhindirect.config.store.Certificate)

Example 13 with ConfigurationServiceException

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

the class ServiceTest method testSOA.

//  @Test
public void testSOA() {
    DNSEntryForm SoadnsForm = new DNSEntryForm();
    SoadnsForm.setName("savvy");
    SoadnsForm.setTtl(84555L);
    SoadnsForm.setAdmin("ns.savvy.com");
    SoadnsForm.setDomain("ns2.savvy.com");
    SoadnsForm.setSerial(4L);
    SoadnsForm.setRefresh(6L);
    SoadnsForm.setRetry(8L);
    SoadnsForm.setExpire(66L);
    SoadnsForm.setMinimum(22L);
    Collection<DNSRecord> records = new ArrayList<DNSRecord>();
    records.add(DNSRecordUtils.createSOARecord(SoadnsForm.getName(), SoadnsForm.getTtl(), SoadnsForm.getDomain(), SoadnsForm.getAdmin(), (int) SoadnsForm.getSerial(), SoadnsForm.getRefresh(), SoadnsForm.getRetry(), SoadnsForm.getExpire(), SoadnsForm.getMinimum()));
    try {
        configSvc.addDNS(records);
        Collection<DNSRecord> arecords = configSvc.getDNSByType(DNSType.SOA.getValue());
        for (Iterator<DNSRecord> iter = arecords.iterator(); iter.hasNext(); ) {
            DNSRecord arec = iter.next();
            SOARecord newrec = (SOARecord) Record.newRecord(Name.fromString(arec.getName()), arec.getType(), arec.getDclass(), arec.getTtl(), arec.getData());
            System.out.println("A admin: " + newrec.getAdmin());
            System.out.println("A name: " + newrec.getName());
        }
    } catch (ConfigurationServiceException e) {
        e.printStackTrace();
    } catch (TextParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : DNSRecord(org.nhindirect.config.store.DNSRecord) ArrayList(java.util.ArrayList) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException) DNSEntryForm(org.nhindirect.config.ui.form.DNSEntryForm) SOARecord(org.xbill.DNS.SOARecord) TextParseException(org.xbill.DNS.TextParseException)

Example 14 with ConfigurationServiceException

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

the class ServiceTest 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) X509Certificate(java.security.cert.X509Certificate) Key(java.security.Key) PrivateKey(java.security.PrivateKey) TextParseException(org.xbill.DNS.TextParseException) CertificateEncodingException(javax.security.cert.CertificateEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException)

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