github-advanced-security[bot] commented on code in PR #16565: URL: https://github.com/apache/dolphinscheduler/pull/16565#discussion_r1746480149
########## dolphinscheduler-storage-plugin/dolphinscheduler-storage-cos/src/main/java/org/apache/dolphinscheduler/plugin/storage/cos/CosStorageOperator.java: ########## @@ -0,0 +1,328 @@ +/* + * 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.dolphinscheduler.plugin.storage.cos; + +import org.apache.dolphinscheduler.common.constants.Constants; +import org.apache.dolphinscheduler.common.utils.FileUtils; +import org.apache.dolphinscheduler.plugin.storage.api.AbstractStorageOperator; +import org.apache.dolphinscheduler.plugin.storage.api.ResourceMetadata; +import org.apache.dolphinscheduler.plugin.storage.api.StorageEntity; +import org.apache.dolphinscheduler.plugin.storage.api.StorageOperator; + +import org.apache.commons.lang3.StringUtils; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.Closeable; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.file.FileAlreadyExistsException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.stream.Collectors; + +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; + +import com.qcloud.cos.COSClient; +import com.qcloud.cos.ClientConfig; +import com.qcloud.cos.auth.BasicCOSCredentials; +import com.qcloud.cos.auth.COSCredentials; +import com.qcloud.cos.exception.CosServiceException; +import com.qcloud.cos.http.HttpProtocol; +import com.qcloud.cos.model.COSObject; +import com.qcloud.cos.model.COSObjectSummary; +import com.qcloud.cos.model.CopyObjectRequest; +import com.qcloud.cos.model.CopyResult; +import com.qcloud.cos.model.GetObjectRequest; +import com.qcloud.cos.model.ListObjectsRequest; +import com.qcloud.cos.model.ObjectListing; +import com.qcloud.cos.model.ObjectMetadata; +import com.qcloud.cos.model.PutObjectRequest; +import com.qcloud.cos.model.UploadResult; +import com.qcloud.cos.region.Region; +import com.qcloud.cos.transfer.Copy; +import com.qcloud.cos.transfer.Download; +import com.qcloud.cos.transfer.TransferManager; +import com.qcloud.cos.transfer.TransferManagerConfiguration; +import com.qcloud.cos.transfer.Upload; + +@Slf4j +public class CosStorageOperator extends AbstractStorageOperator implements Closeable, StorageOperator { + + private static final int TRANSFER_THREAD_POOL_SIZE = 16; + + private static final long MULTIPART_UPLOAD_BYTES_THRESHOLD = 5 * 1024 * 1024; + private static final long MIN_UPLOAD_PART_BYTES = 1024 * 1024; + + private final String bucketName; + + private final COSClient cosClient; + + private final TransferManager cosTransferManager; + + public CosStorageOperator(CosStorageProperties cosStorageProperties) { + super(cosStorageProperties.getResourceUploadPath()); + String secretId = cosStorageProperties.getAccessKeyId(); + String secretKey = cosStorageProperties.getAccessKeySecret(); + COSCredentials cosCredentials = new BasicCOSCredentials(secretId, secretKey); + String regionName = cosStorageProperties.getRegion(); + ClientConfig clientConfig = new ClientConfig(new Region(regionName)); + clientConfig.setHttpProtocol(HttpProtocol.https); + this.cosClient = new COSClient(cosCredentials, clientConfig); + this.bucketName = cosStorageProperties.getBucketName(); + ensureBucketSuccessfullyCreated(bucketName); + this.cosTransferManager = getCosTransferManager(); + } + + @Override + public void close() throws IOException { + this.cosTransferManager.shutdownNow(true); + } + + @Override + public String getStorageBaseDirectory() { + // All directory should end with File.separator + if (resourceBaseAbsolutePath.startsWith("/")) { + log.warn("{} -> {} should not start with / in cos", Constants.RESOURCE_UPLOAD_PATH, + resourceBaseAbsolutePath); + return resourceBaseAbsolutePath.substring(1); + } + return resourceBaseAbsolutePath; + } + + @SneakyThrows + @Override + public void createStorageDir(String directoryAbsolutePath) { + String cosKey = transformAbsolutePathToCOSKey(directoryAbsolutePath); + if (cosClient.doesObjectExist(bucketName, cosKey)) { + throw new FileAlreadyExistsException("directory: " + cosKey + " already exists"); + } + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentLength(0L); + InputStream emptyContent = new ByteArrayInputStream(new byte[0]); + PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, cosKey, emptyContent, metadata); + cosClient.putObject(putObjectRequest); + } + + @SneakyThrows + @Override + public void download(String srcFilePath, String dstFilePath, boolean overwrite) { + String cosKey = transformAbsolutePathToCOSKey(srcFilePath); + + File dstFile = new File(dstFilePath); + if (dstFile.isDirectory()) { Review Comment: ## Uncontrolled data used in path expression This path depends on a [user-provided value](1). [Show more details](https://github.com/apache/dolphinscheduler/security/code-scanning/4858) ########## dolphinscheduler-storage-plugin/dolphinscheduler-storage-cos/src/main/java/org/apache/dolphinscheduler/plugin/storage/cos/CosStorageOperator.java: ########## @@ -0,0 +1,328 @@ +/* + * 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.dolphinscheduler.plugin.storage.cos; + +import org.apache.dolphinscheduler.common.constants.Constants; +import org.apache.dolphinscheduler.common.utils.FileUtils; +import org.apache.dolphinscheduler.plugin.storage.api.AbstractStorageOperator; +import org.apache.dolphinscheduler.plugin.storage.api.ResourceMetadata; +import org.apache.dolphinscheduler.plugin.storage.api.StorageEntity; +import org.apache.dolphinscheduler.plugin.storage.api.StorageOperator; + +import org.apache.commons.lang3.StringUtils; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.Closeable; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.file.FileAlreadyExistsException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.stream.Collectors; + +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; + +import com.qcloud.cos.COSClient; +import com.qcloud.cos.ClientConfig; +import com.qcloud.cos.auth.BasicCOSCredentials; +import com.qcloud.cos.auth.COSCredentials; +import com.qcloud.cos.exception.CosServiceException; +import com.qcloud.cos.http.HttpProtocol; +import com.qcloud.cos.model.COSObject; +import com.qcloud.cos.model.COSObjectSummary; +import com.qcloud.cos.model.CopyObjectRequest; +import com.qcloud.cos.model.CopyResult; +import com.qcloud.cos.model.GetObjectRequest; +import com.qcloud.cos.model.ListObjectsRequest; +import com.qcloud.cos.model.ObjectListing; +import com.qcloud.cos.model.ObjectMetadata; +import com.qcloud.cos.model.PutObjectRequest; +import com.qcloud.cos.model.UploadResult; +import com.qcloud.cos.region.Region; +import com.qcloud.cos.transfer.Copy; +import com.qcloud.cos.transfer.Download; +import com.qcloud.cos.transfer.TransferManager; +import com.qcloud.cos.transfer.TransferManagerConfiguration; +import com.qcloud.cos.transfer.Upload; + +@Slf4j +public class CosStorageOperator extends AbstractStorageOperator implements Closeable, StorageOperator { + + private static final int TRANSFER_THREAD_POOL_SIZE = 16; + + private static final long MULTIPART_UPLOAD_BYTES_THRESHOLD = 5 * 1024 * 1024; + private static final long MIN_UPLOAD_PART_BYTES = 1024 * 1024; + + private final String bucketName; + + private final COSClient cosClient; + + private final TransferManager cosTransferManager; + + public CosStorageOperator(CosStorageProperties cosStorageProperties) { + super(cosStorageProperties.getResourceUploadPath()); + String secretId = cosStorageProperties.getAccessKeyId(); + String secretKey = cosStorageProperties.getAccessKeySecret(); + COSCredentials cosCredentials = new BasicCOSCredentials(secretId, secretKey); + String regionName = cosStorageProperties.getRegion(); + ClientConfig clientConfig = new ClientConfig(new Region(regionName)); + clientConfig.setHttpProtocol(HttpProtocol.https); + this.cosClient = new COSClient(cosCredentials, clientConfig); + this.bucketName = cosStorageProperties.getBucketName(); + ensureBucketSuccessfullyCreated(bucketName); + this.cosTransferManager = getCosTransferManager(); + } + + @Override + public void close() throws IOException { + this.cosTransferManager.shutdownNow(true); + } + + @Override + public String getStorageBaseDirectory() { + // All directory should end with File.separator + if (resourceBaseAbsolutePath.startsWith("/")) { + log.warn("{} -> {} should not start with / in cos", Constants.RESOURCE_UPLOAD_PATH, + resourceBaseAbsolutePath); + return resourceBaseAbsolutePath.substring(1); + } + return resourceBaseAbsolutePath; + } + + @SneakyThrows + @Override + public void createStorageDir(String directoryAbsolutePath) { + String cosKey = transformAbsolutePathToCOSKey(directoryAbsolutePath); + if (cosClient.doesObjectExist(bucketName, cosKey)) { + throw new FileAlreadyExistsException("directory: " + cosKey + " already exists"); + } + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentLength(0L); + InputStream emptyContent = new ByteArrayInputStream(new byte[0]); + PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, cosKey, emptyContent, metadata); + cosClient.putObject(putObjectRequest); + } + + @SneakyThrows + @Override + public void download(String srcFilePath, String dstFilePath, boolean overwrite) { + String cosKey = transformAbsolutePathToCOSKey(srcFilePath); + + File dstFile = new File(dstFilePath); + if (dstFile.isDirectory()) { + Files.delete(dstFile.toPath()); Review Comment: ## Uncontrolled data used in path expression This path depends on a [user-provided value](1). [Show more details](https://github.com/apache/dolphinscheduler/security/code-scanning/4859) -- 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]
