Search in sources :

Example 6 with TrustBundleAnchor

use of org.nhindirect.config.store.TrustBundleAnchor in project nhin-d by DirectProject.

the class TrustBundleDaoImpl method getTrustBundles.

/**
	 * {@inheritDoc}
	 */
@SuppressWarnings("unchecked")
@Override
@Transactional(readOnly = true)
public Collection<TrustBundle> getTrustBundles() throws ConfigurationStoreException {
    validateState();
    Collection<TrustBundle> rs;
    try {
        Query select = entityManager.createQuery("SELECT tb from TrustBundle tb");
        rs = select.getResultList();
        if (rs.size() == 0)
            return Collections.emptyList();
    } catch (Exception e) {
        throw new ConfigurationStoreException("Failed to execute trust bundle DAO query.", e);
    }
    // make sure the anchors are loaded
    for (TrustBundle bundle : rs) {
        if (!bundle.getTrustBundleAnchors().isEmpty())
            for (TrustBundleAnchor anchor : bundle.getTrustBundleAnchors()) anchor.getData();
    }
    return rs;
}
Also used : Query(javax.persistence.Query) TrustBundle(org.nhindirect.config.store.TrustBundle) ConfigurationStoreException(org.nhindirect.config.store.ConfigurationStoreException) TrustBundleAnchor(org.nhindirect.config.store.TrustBundleAnchor) NoResultException(javax.persistence.NoResultException) ConfigurationStoreException(org.nhindirect.config.store.ConfigurationStoreException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with TrustBundleAnchor

use of org.nhindirect.config.store.TrustBundleAnchor in project nhin-d by DirectProject.

the class DefaultBundleRefreshProcessorImpl method refreshBundle.

/**
	 * {@inheritDoc}
	 */
@Handler
public void refreshBundle(TrustBundle bundle) {
    // track when the process started
    final Calendar processAttempStart = Calendar.getInstance(Locale.getDefault());
    // get the bundle from the URL
    final byte[] rawBundle = downloadBundleToByteArray(bundle, processAttempStart);
    if (rawBundle == null)
        return;
    // check to see if there is a difference in the anchor sets
    // use a checksum 
    boolean update = false;
    String checkSum = "";
    if (bundle.getCheckSum() == null)
        // never got a check sum... 
        update = true;
    else {
        try {
            checkSum = BundleThumbprint.toThumbprint(rawBundle).toString();
            update = !bundle.getCheckSum().equals(BundleThumbprint.toThumbprint(rawBundle).toString());
        }///CLOVER:OFF
         catch (NoSuchAlgorithmException ex) {
            dao.updateLastUpdateError(bundle.getId(), processAttempStart, BundleRefreshError.INVALID_BUNDLE_FORMAT);
            log.error("Failed to generate downloaded bundle thumbprint ", ex);
        }
    ///CLOVER:ON
    }
    if (!update) {
        dao.updateLastUpdateError(bundle.getId(), processAttempStart, BundleRefreshError.SUCCESS);
        return;
    }
    final Collection<X509Certificate> bundleCerts = convertRawBundleToAnchorCollection(rawBundle, bundle, processAttempStart);
    if (bundleCerts == null)
        return;
    final HashSet<X509Certificate> downloadedSet = new HashSet<X509Certificate>((Collection<X509Certificate>) bundleCerts);
    try {
        final Collection<TrustBundleAnchor> newAnchors = new ArrayList<TrustBundleAnchor>();
        for (X509Certificate downloadedAnchor : downloadedSet) {
            try {
                final TrustBundleAnchor anchorToAdd = new TrustBundleAnchor();
                anchorToAdd.setData(downloadedAnchor.getEncoded());
                anchorToAdd.setTrustBundle(bundle);
                newAnchors.add(anchorToAdd);
            }///CLOVER:OFF
             catch (Exception e) {
                log.warn("Failed to convert downloaded anchor to byte array. ", e);
            }
        ///CLOVER:ON
        }
        bundle.setTrustBundleAnchors(newAnchors);
        dao.updateTrustBundleAnchors(bundle.getId(), processAttempStart, newAnchors, checkSum);
        dao.updateLastUpdateError(bundle.getId(), processAttempStart, BundleRefreshError.SUCCESS);
    } catch (ConfigurationStoreException e) {
        dao.updateLastUpdateError(bundle.getId(), processAttempStart, BundleRefreshError.INVALID_BUNDLE_FORMAT);
        log.error("Failed to write updated bundle anchors to data store ", e);
    }
}
Also used : Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ConfigurationStoreException(org.nhindirect.config.store.ConfigurationStoreException) X509Certificate(java.security.cert.X509Certificate) SocketTimeoutException(java.net.SocketTimeoutException) ConfigurationStoreException(org.nhindirect.config.store.ConfigurationStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) TrustBundleAnchor(org.nhindirect.config.store.TrustBundleAnchor) HashSet(java.util.HashSet) Handler(org.apache.camel.Handler)

Example 8 with TrustBundleAnchor

use of org.nhindirect.config.store.TrustBundleAnchor in project nhin-d by DirectProject.

the class DefaultBundleRefreshProcessorImpl_refreshBundleTest method testRefreshBundle_invalidBundle_assertUpdateNotCalled.

@SuppressWarnings("unchecked")
public void testRefreshBundle_invalidBundle_assertUpdateNotCalled() throws Exception {
    DefaultBundleRefreshProcessorImpl processor = new DefaultBundleRefreshProcessorImpl();
    processor.setDao(dao);
    final TrustBundle bundle = new TrustBundle();
    bundle.setBundleName("Junit Bundle");
    File fl = new File("src/test/resources/bundles/invalidBundle.der");
    bundle.setBundleURL(filePrefix + fl.getAbsolutePath());
    processor.refreshBundle(bundle);
    verify(dao, times(0)).updateTrustBundleAnchors(eq(bundle.getId()), (Calendar) any(), (Collection<TrustBundleAnchor>) any(), (String) any());
}
Also used : TrustBundle(org.nhindirect.config.store.TrustBundle) File(java.io.File) TrustBundleAnchor(org.nhindirect.config.store.TrustBundleAnchor)

Example 9 with TrustBundleAnchor

use of org.nhindirect.config.store.TrustBundleAnchor in project nhin-d by DirectProject.

the class DefaultBundleRefreshProcessorImpl_refreshBundleTest method testRefreshBundle_bundleNotFound_assertUpdateNotCalled.

@SuppressWarnings("unchecked")
public void testRefreshBundle_bundleNotFound_assertUpdateNotCalled() throws Exception {
    DefaultBundleRefreshProcessorImpl processor = new DefaultBundleRefreshProcessorImpl();
    processor.setDao(dao);
    final TrustBundle bundle = new TrustBundle();
    bundle.setBundleName("Junit Bundle");
    File fl = new File("src/test/resources/bundles/signedbundle.p7b2122");
    bundle.setBundleURL(filePrefix + fl.getAbsolutePath());
    processor.refreshBundle(bundle);
    verify(dao, times(0)).updateTrustBundleAnchors(eq(bundle.getId()), (Calendar) any(), (Collection<TrustBundleAnchor>) any(), (String) any());
}
Also used : TrustBundle(org.nhindirect.config.store.TrustBundle) File(java.io.File) TrustBundleAnchor(org.nhindirect.config.store.TrustBundleAnchor)

Example 10 with TrustBundleAnchor

use of org.nhindirect.config.store.TrustBundleAnchor in project nhin-d by DirectProject.

the class DefaultBundleRefreshProcessorImpl_refreshBundleTest method testRefreshBundle_validBundle_noCheckSum_needsRefreshed_assertUpdateCalled.

@SuppressWarnings("unchecked")
public void testRefreshBundle_validBundle_noCheckSum_needsRefreshed_assertUpdateCalled() throws Exception {
    DefaultBundleRefreshProcessorImpl processor = new DefaultBundleRefreshProcessorImpl();
    processor.setDao(dao);
    final TrustBundle bundle = new TrustBundle();
    bundle.setBundleName("Junit Bundle");
    File fl = new File("src/test/resources/bundles/signedbundle.p7b");
    bundle.setBundleURL(filePrefix + fl.getAbsolutePath());
    processor.refreshBundle(bundle);
    verify(dao, times(1)).updateTrustBundleAnchors(eq(bundle.getId()), (Calendar) any(), (Collection<TrustBundleAnchor>) any(), (String) any());
}
Also used : TrustBundle(org.nhindirect.config.store.TrustBundle) File(java.io.File) TrustBundleAnchor(org.nhindirect.config.store.TrustBundleAnchor)

Aggregations

TrustBundleAnchor (org.nhindirect.config.store.TrustBundleAnchor)12 TrustBundle (org.nhindirect.config.store.TrustBundle)10 File (java.io.File)7 ConfigurationStoreException (org.nhindirect.config.store.ConfigurationStoreException)6 NoResultException (javax.persistence.NoResultException)4 Query (javax.persistence.Query)4 Transactional (org.springframework.transaction.annotation.Transactional)4 SocketTimeoutException (java.net.SocketTimeoutException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 X509Certificate (java.security.cert.X509Certificate)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 HashSet (java.util.HashSet)1 Handler (org.apache.camel.Handler)1 TrustBundleService (org.nhindirect.config.service.TrustBundleService)1 Domain (org.nhindirect.config.store.Domain)1 TrustBundleDomainReltn (org.nhindirect.config.store.TrustBundleDomainReltn)1 ApplicationContext (org.springframework.context.ApplicationContext)1