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


##########
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:
   Added the upgrade guidance above. The default accepts a file directly under 
the table directory. Operators staging in a nested or external directory can 
configure that tree for a named table with 
`controller.segment.upload.allowedSources` before upgrading.



##########
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:
   Updated the HTTP 400 message to name 
`controller.segment.upload.allowedSources` and added DEBUG logging for 
validation exceptions. The controller endpoint test checks the returned setting 
name and confirms no segment is registered. Both focused classes passed in 
621a1e7.



-- 
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