xiangfu0 commented on code in PR #20428:
URL: https://github.com/apache/pinot/pull/20428#discussion_r4795200965


##########
pinot-controller/src/main/java/org/apache/pinot/controller/api/upload/SegmentValidationUtils.java:
##########
@@ -49,6 +64,153 @@ private SegmentValidationUtils() {
 
   private static final Logger LOGGER = 
LoggerFactory.getLogger(SegmentValidationUtils.class);
 
+  /// Validates a source before probing its size or copying it with the 
controller's storage credentials.
+  /// Built-in staging uses files directly in the destination table directory. 
Controller configuration can grant
+  /// access to separate staging directories for that table. Those directories 
must contain only trusted staging data.
+  public static String validateMetadataCopySource(String source, URI dataDir, 
String rawTableName,
+      List<String> stagingDirs) {
+    try {
+      URI sourceUri = new URI(source);
+      if (dataDir.getScheme().equals(sourceUri.getScheme()) && 
isAbsoluteStoragePath(sourceUri)
+          && !rawTableName.isEmpty() && !rawTableName.equals(".") && 
!rawTableName.equals("..")
+          && rawTableName.indexOf('/') < 0 && rawTableName.indexOf('\\') < 0 
&& rawTableName.indexOf('%') < 0
+          && rawTableName.indexOf('?') < 0 && rawTableName.indexOf('#') < 0) {
+        String scheme = sourceUri.getScheme();
+        boolean localFileSystem = 
PinotFSFactory.isSchemeRegisteredWith(scheme, LocalPinotFS.class);
+        if (CommonConstants.Segment.LOCAL_SEGMENT_SCHEME.equals(scheme)) {
+          if (localFileSystem && sourceUri.getRawAuthority() == null) {
+            Path sourcePath = resolveLocalStoragePath(sourceUri);
+            Path tableDir = 
localStoragePath(dataDir).toRealPath().resolve(URIUtils.decode(rawTableName));
+            if (Files.isRegularFile(sourcePath, LinkOption.NOFOLLOW_LINKS)
+                || Files.notExists(sourcePath, LinkOption.NOFOLLOW_LINKS)) {
+              if (tableDir.equals(sourcePath.getParent())) {

Review Comment:
   By default only a file directly inside the table's directory passes. 
Anything in a sub-directory or in a separate staging location needs 
`controller.segment.metadataPush.sourceDirs`. That changes behaviour for 
existing metadata-push jobs, so the description should carry an upgrade note 
that names the key.



##########
pinot-controller/src/main/java/org/apache/pinot/controller/api/upload/SegmentValidationUtils.java:
##########
@@ -49,6 +64,153 @@ private SegmentValidationUtils() {
 
   private static final Logger LOGGER = 
LoggerFactory.getLogger(SegmentValidationUtils.class);
 
+  /// Validates a source before probing its size or copying it with the 
controller's storage credentials.
+  /// Built-in staging uses files directly in the destination table directory. 
Controller configuration can grant
+  /// access to separate staging directories for that table. Those directories 
must contain only trusted staging data.
+  public static String validateMetadataCopySource(String source, URI dataDir, 
String rawTableName,
+      List<String> stagingDirs) {
+    try {
+      URI sourceUri = new URI(source);
+      if (dataDir.getScheme().equals(sourceUri.getScheme()) && 
isAbsoluteStoragePath(sourceUri)
+          && !rawTableName.isEmpty() && !rawTableName.equals(".") && 
!rawTableName.equals("..")
+          && rawTableName.indexOf('/') < 0 && rawTableName.indexOf('\\') < 0 
&& rawTableName.indexOf('%') < 0
+          && rawTableName.indexOf('?') < 0 && rawTableName.indexOf('#') < 0) {
+        String scheme = sourceUri.getScheme();
+        boolean localFileSystem = 
PinotFSFactory.isSchemeRegisteredWith(scheme, LocalPinotFS.class);
+        if (CommonConstants.Segment.LOCAL_SEGMENT_SCHEME.equals(scheme)) {
+          if (localFileSystem && sourceUri.getRawAuthority() == null) {
+            Path sourcePath = resolveLocalStoragePath(sourceUri);
+            Path tableDir = 
localStoragePath(dataDir).toRealPath().resolve(URIUtils.decode(rawTableName));
+            if (Files.isRegularFile(sourcePath, LinkOption.NOFOLLOW_LINKS)
+                || Files.notExists(sourcePath, LinkOption.NOFOLLOW_LINKS)) {
+              if (tableDir.equals(sourcePath.getParent())) {
+                // Use the controller's spelling of the directory to preserve 
in-place pushes and sidecar checks.
+                String fileName = 
URIUtils.encode(sourcePath.getFileName().toString());
+                return URIUtils.getUri(dataDir.toString(), rawTableName, 
fileName).toString();
+              }
+              if (stagingDirs.stream().anyMatch(dir -> 
isLocalStagingSource(sourcePath, dir))) {
+                return sourcePath.toUri().toASCIIString().replace("+", "%2B");
+              }
+            }
+          }
+        } else if (!localFileSystem) {
+          URI tableDir = URIUtils.getUri(dataDir.toString(), rawTableName);
+          boolean bucketFileSystem = usesUriBucket(scheme);
+          boolean sameAuthority = Objects.equals(sourceUri.getRawAuthority(), 
dataDir.getRawAuthority());
+          if (isRemoteStagingSource(sourceUri, tableDir, true, 
bucketFileSystem)
+              || ((sameAuthority || bucketFileSystem)
+                  && stagingDirs.stream().anyMatch(dir -> 
isRemoteStagingSource(sourceUri, dir, bucketFileSystem)))) {
+            return source;
+          }
+        }
+      }
+    } catch (URISyntaxException | IOException | IllegalArgumentException e) {
+      // Report invalid and inaccessible sources the same way.
+    }
+    throw new ControllerApplicationException(LOGGER, "Metadata copy source 
must be in an approved staging directory",

Review Comment:
   Could this message name the config key 
(`controller.segment.metadataPush.sourceDirs`), and could the exception caught 
at line 107 be logged at debug? As it stands an operator who hits this 400 has 
nothing pointing at the setting or at the underlying error.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to