brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [1/3] incubator-brooklyn git commit: Brooklyn properties files may be symlinks
Date Thu, 17 Dec 2015 12:01:56 GMT
Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master 085e1cc5b -> 08b814117


Brooklyn properties files may be symlinks


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/443fa4be
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/443fa4be
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/443fa4be

Branch: refs/heads/master
Commit: 443fa4be57fd6c0b1ff381c530b71537bc0ec194
Parents: 4ed9e97
Author: Sam Corbett <sam.corbett@cloudsoftcorp.com>
Authored: Fri Dec 11 17:31:52 2015 +0000
Committer: Sam Corbett <sam.corbett@cloudsoftcorp.com>
Committed: Fri Dec 11 17:31:52 2015 +0000

----------------------------------------------------------------------
 .../entity/webapp/JavaWebAppSshDriver.java      |  2 +-
 .../brooklyn/launcher/BrooklynLauncher.java     | 65 +++++++++++++-------
 .../brooklyn/launcher/BrooklynLauncherTest.java | 32 ++++++++--
 3 files changed, 71 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/443fa4be/software/webapp/src/main/java/org/apache/brooklyn/entity/webapp/JavaWebAppSshDriver.java
----------------------------------------------------------------------
diff --git a/software/webapp/src/main/java/org/apache/brooklyn/entity/webapp/JavaWebAppSshDriver.java
b/software/webapp/src/main/java/org/apache/brooklyn/entity/webapp/JavaWebAppSshDriver.java
index 7791418..e1e30c3 100644
--- a/software/webapp/src/main/java/org/apache/brooklyn/entity/webapp/JavaWebAppSshDriver.java
+++ b/software/webapp/src/main/java/org/apache/brooklyn/entity/webapp/JavaWebAppSshDriver.java
@@ -169,7 +169,7 @@ public abstract class JavaWebAppSshDriver extends JavaSoftwareProcessSshDriver
i
                 public void run() {
                     int result = copyResource(url, tmpDest);
                     if (result != 0) {
-                        throw new IllegalStateException("Invalud result " + result + " while
" + copyTaskMsg);
+                        throw new IllegalStateException("Invalid result " + result + " while
" + copyTaskMsg);
                     }
                 }
             });

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/443fa4be/usage/launcher/src/main/java/org/apache/brooklyn/launcher/BrooklynLauncher.java
----------------------------------------------------------------------
diff --git a/usage/launcher/src/main/java/org/apache/brooklyn/launcher/BrooklynLauncher.java
b/usage/launcher/src/main/java/org/apache/brooklyn/launcher/BrooklynLauncher.java
index ca5cda4..5fad573 100644
--- a/usage/launcher/src/main/java/org/apache/brooklyn/launcher/BrooklynLauncher.java
+++ b/usage/launcher/src/main/java/org/apache/brooklyn/launcher/BrooklynLauncher.java
@@ -22,14 +22,15 @@ import static com.google.common.base.Preconditions.checkNotNull;
 
 import java.io.Closeable;
 import java.io.File;
+import java.io.IOException;
 import java.net.InetAddress;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.TimeoutException;
-
 import javax.annotation.Nullable;
 
 import org.apache.brooklyn.api.entity.Application;
@@ -556,7 +557,7 @@ public class BrooklynLauncher {
     /**
      * @param memento The state to copy
      * @param destinationDir Directory for state to be copied to
-     * @param destinationLocation Optional location if target for copied state is a blob
store.
+     * @param destinationLocationSpec Optional location if target for copied state is a blob
store.
      * @deprecated since 0.7.0 use {@link #copyPersistedState} instead
      */
     // Make private after deprecation
@@ -662,28 +663,35 @@ public class BrooklynLauncher {
         if (managementContext == null) {
             if (brooklynProperties == null) {
                 BrooklynProperties.Factory.Builder builder = BrooklynProperties.Factory.builderDefault();
+
                 if (globalBrooklynPropertiesFile != null) {
-                    if (fileExists(globalBrooklynPropertiesFile)) {
-                        LOG.debug("Using global properties file "+globalBrooklynPropertiesFile);
-                        // brooklyn.properties stores passwords (web-console and cloud credentials),

+                    File globalProperties = new File(Os.tidyPath(globalBrooklynPropertiesFile));
+                    if (globalProperties.exists()) {
+                        globalProperties = resolveSymbolicLink(globalProperties);
+                        checkFileReadable(globalProperties);
+                        // brooklyn.properties stores passwords (web-console and cloud credentials),
                         // so ensure it has sensible permissions
-                        checkFileReadable(globalBrooklynPropertiesFile);
-                        checkFilePermissionsX00(globalBrooklynPropertiesFile);
+                        checkFilePermissionsX00(globalProperties);
+                        LOG.debug("Using global properties file " + globalProperties);
                     } else {
-                        LOG.debug("Global properties file "+globalBrooklynPropertiesFile+"
does not exist, will ignore");
+                        LOG.debug("Global properties file " + globalProperties + " does not
exist, will ignore");
                     }
-                    builder.globalPropertiesFile(globalBrooklynPropertiesFile);
+                    builder.globalPropertiesFile(globalProperties.getAbsolutePath());
                 } else {
                     LOG.debug("Global properties file disabled");
                     builder.globalPropertiesFile(null);
                 }
                 
                 if (localBrooklynPropertiesFile != null) {
-                    checkFileReadable(localBrooklynPropertiesFile);
-                    checkFilePermissionsX00(localBrooklynPropertiesFile);
-                    builder.localPropertiesFile(localBrooklynPropertiesFile);
+                    File localProperties = new File(Os.tidyPath(localBrooklynPropertiesFile));
+                    localProperties = resolveSymbolicLink(localProperties);
+                    checkFileReadable(localProperties);
+                    checkFilePermissionsX00(localProperties);
+                    builder.localPropertiesFile(localProperties.getAbsolutePath());
                 }
+
                 managementContext = new LocalManagementContext(builder, brooklynAdditionalProperties);
+
             } else {
                 if (globalBrooklynPropertiesFile != null)
                     LOG.warn("Ignoring globalBrooklynPropertiesFile "+globalBrooklynPropertiesFile+"
because explicit brooklynProperties supplied");
@@ -691,6 +699,7 @@ public class BrooklynLauncher {
                     LOG.warn("Ignoring localBrooklynPropertiesFile "+localBrooklynPropertiesFile+"
because explicit brooklynProperties supplied");
                 managementContext = new LocalManagementContext(brooklynProperties, brooklynAdditionalProperties);
             }
+
             brooklynProperties = ((ManagementContextInternal)managementContext).getBrooklynProperties();
             
             // We created the management context, so we are responsible for terminating it
@@ -710,32 +719,42 @@ public class BrooklynLauncher {
         }
     }
 
-    private boolean fileExists(String file) {
-        return new File(Os.tidyPath(file)).exists();
+    /**
+     * @return The canonical path of the argument.
+     */
+    private File resolveSymbolicLink(File f) {
+        File f2 = f;
+        try {
+            f2 = f.getCanonicalFile();
+            if (Files.isSymbolicLink(f.toPath())) {
+                LOG.debug("Resolved symbolic link: {} -> {}", f, f2);
+            }
+        } catch (IOException e) {
+            LOG.warn("Could not determine canonical name of global properties file", e);
+        }
+        return f2;
     }
 
-    private void checkFileReadable(String file) {
-        File f = new File(Os.tidyPath(file));
+    private void checkFileReadable(File f) {
         if (!f.exists()) {
-            throw new FatalRuntimeException("File "+file+" does not exist");
+            throw new FatalRuntimeException("File " + f + " does not exist");
         }
         if (!f.isFile()) {
-            throw new FatalRuntimeException(file+" is not a file");
+            throw new FatalRuntimeException(f + " is not a file");
         }
         if (!f.canRead()) {
-            throw new FatalRuntimeException(file+" is not readable");
+            throw new FatalRuntimeException(f + " is not readable");
         }
     }
     
-    private void checkFilePermissionsX00(String file) {
-        File f = new File(Os.tidyPath(file));
-        
+    private void checkFilePermissionsX00(File f) {
+
         Maybe<String> permission = FileUtil.getFilePermissions(f);
         if (permission.isAbsent()) {
             LOG.debug("Could not determine permissions of file; assuming ok: "+f);
         } else {
             if (!permission.get().subSequence(4, 10).equals("------")) {
-                throw new FatalRuntimeException("Invalid permissions for file "+file+"; expected
?00 but was "+permission.get());
+                throw new FatalRuntimeException("Invalid permissions for file " + f + ";
expected ?00 but was " + permission.get());
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/443fa4be/usage/launcher/src/test/java/org/apache/brooklyn/launcher/BrooklynLauncherTest.java
----------------------------------------------------------------------
diff --git a/usage/launcher/src/test/java/org/apache/brooklyn/launcher/BrooklynLauncherTest.java
b/usage/launcher/src/test/java/org/apache/brooklyn/launcher/BrooklynLauncherTest.java
index ff7e0d4..2e0e023 100644
--- a/usage/launcher/src/test/java/org/apache/brooklyn/launcher/BrooklynLauncherTest.java
+++ b/usage/launcher/src/test/java/org/apache/brooklyn/launcher/BrooklynLauncherTest.java
@@ -33,7 +33,6 @@ import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests;
 import org.apache.brooklyn.core.test.entity.TestApplication;
 import org.apache.brooklyn.core.test.entity.TestApplicationImpl;
 import org.apache.brooklyn.core.test.entity.TestEntity;
-import org.apache.brooklyn.launcher.BrooklynLauncher;
 
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNotNull;
@@ -47,6 +46,8 @@ import java.io.StringWriter;
 import java.io.Writer;
 import java.net.URI;
 import java.nio.charset.Charset;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.Properties;
 
 import org.apache.brooklyn.test.HttpTestUtils;
@@ -73,12 +74,10 @@ import com.google.common.io.Files;
 public class BrooklynLauncherTest {
     
     private BrooklynLauncher launcher;
-    private File persistenceDir;
-    
+
     @AfterMethod(alwaysRun=true)
     public void tearDown() throws Exception {
         if (launcher != null) launcher.terminate();
-        if (persistenceDir != null) RebindTestUtils.deleteMementoDir(persistenceDir);
         launcher = null;
     }
     
@@ -294,6 +293,31 @@ public class BrooklynLauncherTest {
     }
 
     @Test(groups="Integration")
+    public void testStartsWithSymlinkedBrooklynPropertiesPermissionsX00() throws Exception
{
+        File dir = Files.createTempDir();
+        Path globalPropsFile = java.nio.file.Files.createFile(Paths.get(dir.toString(), "globalProps.properties"));
+        Path globalSymlink = java.nio.file.Files.createSymbolicLink(Paths.get(dir.toString(),
"globalLink"), globalPropsFile);
+        Path localPropsFile = java.nio.file.Files.createFile(Paths.get(dir.toString(), "localPropsFile.properties"));
+        Path localSymlink = java.nio.file.Files.createSymbolicLink(Paths.get(dir.toString(),
"localLink"), localPropsFile);
+
+        Files.write(getMinimalLauncherPropertiesString() + "key_in_global=1", globalPropsFile.toFile(),
Charset.defaultCharset());
+        Files.write("key_in_local=2", localPropsFile.toFile(), Charset.defaultCharset());
+        FileUtil.setFilePermissionsTo600(globalPropsFile.toFile());
+        FileUtil.setFilePermissionsTo600(localPropsFile.toFile());
+        try {
+            launcher = newLauncherForTests(false)
+                    .webconsole(false)
+                    .localBrooklynPropertiesFile(localSymlink.toAbsolutePath().toString())
+                    .globalBrooklynPropertiesFile(globalSymlink.toAbsolutePath().toString())
+                    .start();
+            assertEquals(launcher.getServerDetails().getManagementContext().getConfig().getFirst("key_in_global"),
"1");
+            assertEquals(launcher.getServerDetails().getManagementContext().getConfig().getFirst("key_in_local"),
"2");
+        } finally {
+            Os.deleteRecursively(dir);
+        }
+    }
+
+    @Test(groups="Integration")
     public void testStartsWithBrooklynPropertiesPermissionsX00() throws Exception {
         File globalPropsFile = File.createTempFile("testChecksLocalBrooklynPropertiesPermissionsX00_global",
".properties");
         Files.write(getMinimalLauncherPropertiesString()+"key_in_global=1", globalPropsFile,
Charset.defaultCharset());


Mime
View raw message