You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2019/05/28 03:41:31 UTC

[camel] branch master updated: CAMEL-13483 File Component: Add option to fail startup when not having read permission on folder

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 2f744d6  CAMEL-13483 File Component: Add option to fail startup when not having read permission on folder
2f744d6 is described below

commit 2f744d6c78672c386f7248b2ac547ed421cc9759
Author: Roberto Flores <be...@gmail.com>
AuthorDate: Fri May 24 14:23:41 2019 -0500

    CAMEL-13483 File Component: Add option to fail startup when not having read permission on folder
    
    -add option startingDirectoryMustHaveAccess
    -add a new test
---
 .../camel-file/src/main/docs/file-component.adoc   |  3 +-
 .../apache/camel/component/file/FileEndpoint.java  |  9 ++-
 .../camel/component/file/GenericFileEndpoint.java  | 16 ++++++
 ...onsumerStartingDirectoryMustHaveAccessTest.java | 64 ++++++++++++++++++++++
 4 files changed, 90 insertions(+), 2 deletions(-)

diff --git a/components/camel-file/src/main/docs/file-component.adoc b/components/camel-file/src/main/docs/file-component.adoc
index e822f50..54d4b1b 100644
--- a/components/camel-file/src/main/docs/file-component.adoc
+++ b/components/camel-file/src/main/docs/file-component.adoc
@@ -85,7 +85,7 @@ with the following path and query parameters:
 |===
 
 
-==== Query Parameters (88 parameters):
+==== Query Parameters (89 parameters):
 
 
 [width="100%",cols="2,5,^1,2",options="header"]
@@ -113,6 +113,7 @@ with the following path and query parameters:
 | *probeContentType* (consumer) | Whether to enable probing of the content type. If enable then the consumer uses Files#probeContentType(java.nio.file.Path) to determine the content-type of the file, and store that as a header with key Exchange#FILE_CONTENT_TYPE on the Message. | false | boolean
 | *processStrategy* (consumer) | A pluggable org.apache.camel.component.file.GenericFileProcessStrategy allowing you to implement your own readLock option or similar. Can also be used when special conditions must be met before a file can be consumed, such as a special ready file exists. If this option is set then the readLock option does not apply. |  | GenericFileProcess Strategy
 | *startingDirectoryMustExist* (consumer) | Whether the starting directory must exist. Mind that the autoCreate option is default enabled, which means the starting directory is normally auto created if it doesn't exist. You can disable autoCreate and enable this to ensure the starting directory must exist. Will thrown an exception if the directory doesn't exist. | false | boolean
+| *startingDirectoryMustHave Access* (consumer) | Whether the starting directory has access permissions. Mind that the startingDirectoryMustExist parameter must be set to true in order to verify that the directory exists. Will thrown an exception if the directory doesn't have read and write permissions. | false | boolean
 | *fileExist* (producer) | What to do if a file already exists with the same name. Override, which is the default, replaces the existing file. Append - adds content to the existing file. Fail - throws a GenericFileOperationException, indicating that there is already an existing file. Ignore - silently ignores the problem and does not override the existing file, but assumes everything is okay. Move - option requires to use the moveExisting option to be configured as well. The option eager [...]
 | *flatten* (producer) | Flatten is used to flatten the file name path to strip any leading paths, so it's just the file name. This allows you to consume recursively into sub-directories, but when you eg write the files to another directory they will be written in a single directory. Setting this to true on the producer enforces that any file name in CamelFileName header will be stripped for any leading paths. | false | boolean
 | *jailStartingDirectory* (producer) | Used for jailing (restricting) writing files to the starting directory (and sub) only. This is enabled by default to not allow Camel to write files to outside directories (to be more secured out of the box). You can turn this off to allow writing files to directories outside the starting directory, such as parent or root folders. | true | boolean
diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/FileEndpoint.java b/components/camel-file/src/main/java/org/apache/camel/component/file/FileEndpoint.java
index f23fc2b..23e5eda 100644
--- a/components/camel-file/src/main/java/org/apache/camel/component/file/FileEndpoint.java
+++ b/components/camel-file/src/main/java/org/apache/camel/component/file/FileEndpoint.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.file;
 
 import java.io.File;
 import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.attribute.PosixFilePermission;
 import java.util.Arrays;
@@ -91,7 +92,13 @@ public class FileEndpoint extends GenericFileEndpoint<File> {
                 throw new FileNotFoundException("Starting directory does not exist: " + file);
             }
         }
-
+        if (!isStartingDirectoryMustExist() && isStartingDirectoryMustHaveAccess()) {
+          throw new IllegalArgumentException("You cannot set startingDirectoryMustHaveAccess=true without setting startingDirectoryMustExist=true");
+        } else if (isStartingDirectoryMustExist() && isStartingDirectoryMustHaveAccess()) {
+          if (!file.canRead() || !file.canWrite()) {
+            throw new IOException("Starting directory permission denied: " + file);
+          }
+        }
         FileConsumer result = newFileConsumer(processor, operations);
 
         if (isDelete() && getMove() != null) {
diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
index 01cd8f9..7f19cfd 100644
--- a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
+++ b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
@@ -111,6 +111,8 @@ public abstract class GenericFileEndpoint<T> extends ScheduledPollEndpoint imple
     @UriParam(label = "consumer,advanced")
     protected boolean startingDirectoryMustExist;
     @UriParam(label = "consumer,advanced")
+    protected boolean startingDirectoryMustHaveAccess;
+    @UriParam(label = "consumer,advanced")
     protected boolean directoryMustExist;
     @UriParam(label = "consumer")
     protected boolean noop;
@@ -1122,6 +1124,20 @@ public abstract class GenericFileEndpoint<T> extends ScheduledPollEndpoint imple
         this.startingDirectoryMustExist = startingDirectoryMustExist;
     }
 
+    public boolean isStartingDirectoryMustHaveAccess() {
+      return startingDirectoryMustHaveAccess;
+    }
+
+  /**
+   * Whether the starting directory has access permissions. Mind that the
+   * startingDirectoryMustExist parameter must be set to true in order to verify that the
+   * directory exists. Will thrown an exception if the directory doesn't have
+   * read and write permissions.
+   */
+    public void setStartingDirectoryMustHaveAccess(boolean startingDirectoryMustHaveAccess) {
+      this.startingDirectoryMustHaveAccess = startingDirectoryMustHaveAccess;
+    }
+
     public boolean isDirectoryMustExist() {
         return directoryMustExist;
     }
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerStartingDirectoryMustHaveAccessTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerStartingDirectoryMustHaveAccessTest.java
new file mode 100644
index 0000000..3e66a6f
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerStartingDirectoryMustHaveAccessTest.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.file;
+
+import java.io.File;
+import java.io.IOException;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.junit.Test;
+
+public class FileConsumerStartingDirectoryMustHaveAccessTest extends ContextTestSupport {
+
+  @Override
+  public void setUp() throws Exception {
+    File file1 = new File("./target/noAccess");
+    if (file1.exists()) {
+      file1.setReadable(true);
+    }
+    deleteDirectory("target/noAccess");
+    file1.mkdirs();
+    file1.setReadable(false);
+    super.setUp();
+  }
+
+  @Override
+  public void tearDown() throws Exception {
+    File file1 = new File("./target/noAccess");
+    if (file1.exists()) {
+      file1.setReadable(true);
+    }
+    super.tearDown();
+  }
+
+  @Test
+  public void testStartingDirectoryMustHaveAccess() throws Exception {
+    Endpoint endpoint = context.getEndpoint("file://target/noAccess?autoCreate=false&startingDirectoryMustExist=true&startingDirectoryMustHaveAccess=true");
+    try {
+      endpoint.createConsumer(new Processor() {
+        public void process(Exchange exchange) throws Exception {
+          // noop
+        }
+      });
+      fail("Should have thrown an exception");
+    } catch (IOException e) {
+      assertTrue(e.getMessage().startsWith("Starting directory permission denied"));
+    }
+  }
+}