xiangfu0 commented on code in PR #20427: URL: https://github.com/apache/pinot/pull/20427#discussion_r4795817373
########## pinot-controller/src/main/java/org/apache/pinot/controller/api/upload/SegmentUploadSourcePolicy.java: ########## @@ -0,0 +1,183 @@ +/** + * 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.pinot.controller.api.upload; + +import com.google.common.base.Preconditions; +import java.io.IOException; +import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.Locale; +import java.util.Objects; +import java.util.regex.Pattern; +import javax.annotation.Nullable; +import org.apache.pinot.common.utils.URIUtils; +import org.apache.pinot.spi.filesystem.LocalPinotFS; +import org.apache.pinot.spi.filesystem.PinotFSFactory; + + +/// Immutable policy for caller-supplied segment upload sources. The controller's table directory is trusted for +/// native filesystem operations. Additional staging prefixes and HTTP destinations are configured by the operator. +public final class SegmentUploadSourcePolicy { + private static final String NOT_ALLOWED = "Segment upload source URI is not allowed"; + private static final Pattern DOT_SEGMENT = Pattern.compile("(?:^|/)\\.{1,2}(?:/|$)"); + + private final URI _dataDir; + private final List<URI> _allowedSources; + + public SegmentUploadSourcePolicy(URI dataDir, List<String> allowedSources) { + _dataDir = dataDir; + _allowedSources = allowedSources.stream().map(SegmentUploadSourcePolicy::parseAllowedSource).toList(); + } + + /// Returns the approved source for the operation. HTTP(S) callers must also enforce network destination policy + /// when they connect. The returned local URI uses the resolved path that passed validation. + public URI validate(URI source, @Nullable String rawTableName, boolean allowHttp, boolean allowLocal) { + validateShape(source); + if (isHttp(source)) { + Preconditions.checkArgument(allowHttp && _allowedSources.stream().anyMatch(prefix -> matchesHttp(source, prefix)), + NOT_ALLOWED); + return source; + } + + String scheme = source.getScheme(); + Preconditions.checkArgument(source.getRawQuery() == null && source.getPath().startsWith("/") + && PinotFSFactory.isSchemeSupported(scheme), NOT_ALLOWED); + String tableName = rawTableName != null && !rawTableName.isEmpty() && rawTableName.indexOf('/') < 0 + && rawTableName.indexOf('\\') < 0 && !DOT_SEGMENT.matcher(rawTableName).matches() ? rawTableName : null; + if (PinotFSFactory.LOCAL_PINOT_FS_SCHEME.equals(scheme)) { + Preconditions.checkArgument(allowLocal && PinotFSFactory.isSchemeRegisteredWith(scheme, LocalPinotFS.class), + NOT_ALLOWED); + return validateLocal(source, tableName); + } + + Preconditions.checkArgument(!PinotFSFactory.isSchemeRegisteredWith(scheme, LocalPinotFS.class), NOT_ALLOWED); + boolean managedSource = tableName != null && sameFileSystem(source, _dataDir) + && isWithin(source.getPath(), asDirectory(_dataDir.getPath()) + tableName); + Preconditions.checkArgument(managedSource || _allowedSources.stream().anyMatch(prefix -> sameFileSystem(source, Review Comment: The shared integration controller now explicitly registers its per-suite staging directory (90953b4). The upgrade section names the setting and includes a table-specific example for existing staging jobs. ########## pinot-controller/src/main/java/org/apache/pinot/controller/api/resources/PinotSegmentUploadDownloadRestletResource.java: ########## @@ -877,21 +876,49 @@ Pair<String, File> encryptSegmentIfNeeded(File tempDecryptedFile, File tempEncry return out; } - private void downloadSegmentFileFromURI(String currentSegmentLocationURI, File destFile, - @Nullable String tableName) - throws Exception { - if (currentSegmentLocationURI == null || currentSegmentLocationURI.isEmpty()) { - throw new ControllerApplicationException(LOGGER, "Failed to get downloadURI, needed for URI upload", + private SegmentUploadSourcePolicy sourcePolicy(ControllerFilePathProvider provider) { + return new SegmentUploadSourcePolicy(provider.getDataDirURI(), _controllerConf.getSegmentUploadAllowedSources()); + } + + private static URI validateSource(SegmentUploadSourcePolicy policy, @Nullable String source, + @Nullable String rawTableName, boolean allowHttp, boolean allowLocal) { + if (StringUtils.isEmpty(source)) { + throw new ControllerApplicationException(LOGGER, "DOWNLOAD_URI is required", Response.Status.BAD_REQUEST); + } + try { + return policy.validate(new URI(source), rawTableName, allowHttp, allowLocal); + } catch (IllegalArgumentException | URISyntaxException e) { + throw new ControllerApplicationException(LOGGER, "Segment upload source URI is not allowed", Response.Status.BAD_REQUEST); } - LOGGER.info("Downloading segment from {} to {} for table {}", currentSegmentLocationURI, destFile.getAbsolutePath(), - tableName); - URI uri = new URI(currentSegmentLocationURI); - if (uri.getScheme().equalsIgnoreCase("file")) { - throw new ControllerApplicationException(LOGGER, "Unsupported URI: " + currentSegmentLocationURI, + } + + private URI validateMetadataSource(SegmentUploadSourcePolicy policy, ControllerFilePathProvider provider, + @Nullable String source, String rawTableName, String tableNameWithType, String segmentName, boolean copy) { + // A minion can refresh the metadata of a segment that already uses the controller's download URL. + if (!copy && provider.getDataDirURI().getScheme().equals(CommonConstants.Segment.LOCAL_SEGMENT_SCHEME)) { + String controllerUrl = + URIUtils.getPath(provider.getVip(), "segments", rawTableName, URIUtils.encode(segmentName)); + if (controllerUrl.equals(source)) { + SegmentZKMetadata existing = _pinotHelixResourceManager.getSegmentZKMetadata(tableNameWithType, segmentName); + if (existing != null && controllerUrl.equals(existing.getDownloadUrl())) { + return URI.create(controllerUrl); + } + } + } + return validateSource(policy, source, rawTableName, false, true); Review Comment: Yes, HTTP(S) imports use URI mode with a copy into deep storage and a configured prefix. I added that distinction to the upgrade section, including the existing controller-issued URL exception for a metadata refresh. -- 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]
