[java-opensaml] branch main updated: OSJ-325: Invalid backingFile causes MetadataResolver initialization ...

Brent Putman putmanb at georgetown.edu
Fri Oct 23 00:58:33 UTC 2020


This is an automated email from the git hooks/post-receive script.

putmanb pushed a commit to branch main
in repository java-opensaml.

View the commit online:
http://git.shibboleth.net/view/?p=java-opensaml.git;a=commit;h=dc3a40825a23e45442394d078836825265127f80

The following commit(s) were added to refs/heads/main by this push:
       new  dc3a40825 OSJ-325: Invalid backingFile causes MetadataResolver initialization ...
dc3a40825 is described below

commit dc3a40825a23e45442394d078836825265127f80
Author: Brent Putman <putmanb at georgetown.edu>
AuthorDate: Thu Oct 22 16:37:37 2020 -0400

    OSJ-325: Invalid backingFile causes MetadataResolver initialization ...
    
    Invalid backingFile causes MetadataResolver initialization to fail.
    
    This change attempts to minimize the changes of writing a bad backing
    file by always writing to a staging file and then moving it into place.
---
 .../impl/FileBackedHTTPMetadataResolver.java       | 24 ++++++++++++++++------
 .../impl/FileBackedHTTPMetadataResolverTest.java   | 12 +++++------
 2 files changed, 24 insertions(+), 12 deletions(-)

diff --git a/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/FileBackedHTTPMetadataResolver.java b/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/FileBackedHTTPMetadataResolver.java
index 94683d64b..ff215b2aa 100644
--- a/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/FileBackedHTTPMetadataResolver.java
+++ b/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/FileBackedHTTPMetadataResolver.java
@@ -20,6 +20,7 @@ package org.opensaml.saml.metadata.resolver.impl;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.nio.file.StandardCopyOption;
 import java.time.Duration;
 import java.time.Instant;
 import java.util.Timer;
@@ -349,19 +350,30 @@ public class FileBackedHTTPMetadataResolver extends HTTPMetadataResolver {
     protected void postProcessMetadata(final byte[] metadataBytes, 
                                        final Document metadataDom, final XMLObject originalMetadata, 
             final XMLObject filteredMetadata) throws ResolverException {
+
+        final File staging = new File(metadataBackupFile.getAbsolutePath() + ".staging");
         try {
+            validateBackupFile(staging);
             validateBackupFile(metadataBackupFile);
-            try (final FileOutputStream out = new FileOutputStream(metadataBackupFile)) {
+            try (final FileOutputStream out = new FileOutputStream(staging)) {
                 out.write(metadataBytes);
                 out.flush();
             }
-        } catch (final ResolverException e) {
-            log.error("{} Unable to write metadata to backup file: {}", 
-                    getLogPrefix(), metadataBackupFile.getAbsoluteFile(), e);
-        } catch (final IOException e) {
-            log.error("{} Unable to write metadata to backup file: {}", 
+
+            try {
+                java.nio.file.Files.move(staging.toPath(), metadataBackupFile.toPath(),
+                        StandardCopyOption.REPLACE_EXISTING);
+            } catch (final IOException e) {
+                log.warn("{} Error moving metadata backup staging file into place: {}",
+                        getLogPrefix(), staging.getAbsolutePath(), e);
+            }
+        } catch (final ResolverException|IOException e) {
+            log.warn("{} Unable to write metadata to backup file: {}", 
                     getLogPrefix(), metadataBackupFile.getAbsoluteFile(), e);
         } finally {
+            if (staging.exists()) {
+                staging.delete();
+            }
             super.postProcessMetadata(metadataBytes, metadataDom, originalMetadata, filteredMetadata);
         }
     }
diff --git a/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/FileBackedHTTPMetadataResolverTest.java b/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/FileBackedHTTPMetadataResolverTest.java
index 14cfbed39..a9b965c28 100644
--- a/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/FileBackedHTTPMetadataResolverTest.java
+++ b/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/FileBackedHTTPMetadataResolverTest.java
@@ -22,7 +22,6 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
-import java.nio.file.Paths;
 import java.time.Duration;
 import java.time.Instant;
 
@@ -63,6 +62,7 @@ public class FileBackedHTTPMetadataResolverTest extends XMLObjectBaseTestCase {
     private String relativeMDResourceBad;
     private String badMDURL;
     private String backupFilePath;
+    private File backupFile;
     private FileBackedHTTPMetadataResolver metadataProvider;
     private String entityID;
     private CriteriaSet criteriaSet;
@@ -81,13 +81,14 @@ public class FileBackedHTTPMetadataResolverTest extends XMLObjectBaseTestCase {
         badMDURL = "http://www.opensaml.org/foo/bar/baz/samlmd";
         backupFilePath = System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") 
                 + "filebacked-http-metadata.xml";
+        backupFile = new File(backupFilePath);
         
         criteriaSet = new CriteriaSet(new EntityIdCriterion(entityID));
     }
 
     @AfterMethod
     protected void tearDown() throws IOException {
-        Path nioBackupFilePath = Paths.get(backupFilePath);
+        Path nioBackupFilePath = backupFile.toPath();
         Files.deleteIfExists(nioBackupFilePath);
     }
     
@@ -98,6 +99,8 @@ public class FileBackedHTTPMetadataResolverTest extends XMLObjectBaseTestCase {
      */
     @Test
     public void testGetEntityDescriptor() throws Exception {
+        Assert.assertFalse(backupFile.exists());
+        
         metadataProvider = new FileBackedHTTPMetadataResolver(httpClientBuilder.buildClient(), metadataURLHttp, backupFilePath);
         metadataProvider.setParserPool(parserPool);
         metadataProvider.setId("test");
@@ -108,6 +111,7 @@ public class FileBackedHTTPMetadataResolverTest extends XMLObjectBaseTestCase {
         Assert.assertNull(metadataProvider.getLastFailureCause());
         
         Assert.assertFalse(metadataProvider.isInitializedFromBackupFile());
+        Assert.assertTrue(backupFile.exists());
         
         EntityDescriptor descriptor = metadataProvider.resolveSingle(criteriaSet);
         Assert.assertNotNull(descriptor, "Retrieved entity descriptor was null");
@@ -233,7 +237,6 @@ public class FileBackedHTTPMetadataResolverTest extends XMLObjectBaseTestCase {
      */
     @Test
     public void testInitFromBackupFile() throws Exception {
-        File backupFile = new File(backupFilePath);
         try (FileOutputStream backupFileOutputStream = new FileOutputStream(backupFile)) {
             Resources.copy(Resources.getResource(relativeMDResource), backupFileOutputStream);
         }
@@ -288,7 +291,6 @@ public class FileBackedHTTPMetadataResolverTest extends XMLObjectBaseTestCase {
      */
     @Test
     public void testInitFromExpiredBackupFile() throws Exception {
-        File backupFile = new File(backupFilePath);
         try (FileOutputStream backupFileOutputStream = new FileOutputStream(backupFile)) {
             Resources.copy(Resources.getResource(relativeMDResourceExpired), backupFileOutputStream);
         }
@@ -343,7 +345,6 @@ public class FileBackedHTTPMetadataResolverTest extends XMLObjectBaseTestCase {
      */
     @Test
     public void testInitFromBadBackupFileNonFailFast() throws Exception {
-        File backupFile = new File(backupFilePath);
         try (FileOutputStream backupFileOutputStream = new FileOutputStream(backupFile)) {
             Resources.copy(Resources.getResource(relativeMDResourceBad), backupFileOutputStream);
         }
@@ -395,7 +396,6 @@ public class FileBackedHTTPMetadataResolverTest extends XMLObjectBaseTestCase {
      */
     @Test
     public void testNoBackupFileLoadWhenMetadataCached() throws Exception {
-        File backupFile = new File(backupFilePath);
         try (FileOutputStream backupFileOutputStream = new FileOutputStream(backupFile)) {
             Resources.copy(Resources.getResource(relativeMDResource), backupFileOutputStream);
         }

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list