Browse Source

fix: Local resource error in some entrypoint (#14826)

Fix createResource, queryResourceListPaging, onlineCreateResource
resource error in local resource
3.2.1-prepare
Jay Chung 1 year ago committed by GitHub
parent
commit
9e60632201
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 41
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java

41
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java

@ -24,6 +24,7 @@ import static org.apache.dolphinscheduler.common.constants.Constants.FORMAT_SS;
import static org.apache.dolphinscheduler.common.constants.Constants.FORMAT_S_S; import static org.apache.dolphinscheduler.common.constants.Constants.FORMAT_S_S;
import static org.apache.dolphinscheduler.common.constants.Constants.JAR; import static org.apache.dolphinscheduler.common.constants.Constants.JAR;
import static org.apache.dolphinscheduler.common.constants.Constants.PERIOD; import static org.apache.dolphinscheduler.common.constants.Constants.PERIOD;
import static org.apache.dolphinscheduler.common.enums.ResUploadType.HDFS;
import org.apache.dolphinscheduler.api.dto.resources.DeleteDataTransferResponse; import org.apache.dolphinscheduler.api.dto.resources.DeleteDataTransferResponse;
import org.apache.dolphinscheduler.api.dto.resources.ResourceComponent; import org.apache.dolphinscheduler.api.dto.resources.ResourceComponent;
@ -241,7 +242,7 @@ public class ResourcesServiceImpl extends BaseServiceImpl implements ResourcesSe
// check resource name exists // check resource name exists
String userResRootPath = ResourceType.UDF.equals(type) ? storageOperate.getUdfDir(tenantCode) String userResRootPath = ResourceType.UDF.equals(type) ? storageOperate.getUdfDir(tenantCode)
: storageOperate.getResDir(tenantCode); : storageOperate.getResDir(tenantCode);
String currDirNFileName = !currentDir.contains(userResRootPath) ? userResRootPath + name : currentDir + name; String currDirNFileName = getOnlineCreatePath(currentDir, userResRootPath) + name;
try { try {
if (checkResourceExists(currDirNFileName)) { if (checkResourceExists(currDirNFileName)) {
@ -561,7 +562,7 @@ public class ResourcesServiceImpl extends BaseServiceImpl implements ResourcesSe
String baseDir = isAdmin(loginUser) ? storageOperate.getDir(ResourceType.ALL, tenantCode) String baseDir = isAdmin(loginUser) ? storageOperate.getDir(ResourceType.ALL, tenantCode)
: storageOperate.getDir(type, tenantCode); : storageOperate.getDir(type, tenantCode);
if (!isUserTenantValid(isAdmin(loginUser), tenantCode, resTenantCode) if (!isUserTenantValid(isAdmin(loginUser), tenantCode, resTenantCode)
|| (StringUtils.isNotBlank(fullName) && !StringUtils.startsWith(fullName, baseDir))) { || isMatchBaseDir(fullName, baseDir)) {
log.error("current user does not have permission"); log.error("current user does not have permission");
putMsg(result, Status.NO_CURRENT_OPERATING_PERMISSION); putMsg(result, Status.NO_CURRENT_OPERATING_PERMISSION);
return result; return result;
@ -1206,13 +1207,8 @@ public class ResourcesServiceImpl extends BaseServiceImpl implements ResourcesSe
String name = fileName.trim() + "." + nameSuffix; String name = fileName.trim() + "." + nameSuffix;
String fullName = "";
String userResRootPath = storageOperate.getResDir(tenantCode); String userResRootPath = storageOperate.getResDir(tenantCode);
if (!currentDir.contains(userResRootPath)) { String fullName = getOnlineCreatePath(currentDir, userResRootPath) + name;
fullName = userResRootPath + name;
} else {
fullName = currentDir + name;
}
result = verifyResourceName(fullName, type, loginUser); result = verifyResourceName(fullName, type, loginUser);
if (!result.getCode().equals(Status.SUCCESS.getCode())) { if (!result.getCode().equals(Status.SUCCESS.getCode())) {
@ -1822,6 +1818,35 @@ public class ResourcesServiceImpl extends BaseServiceImpl implements ResourcesSe
return true; return true;
} }
private boolean isLocal(String baseDir) {
return storageOperate.returnStorageType() == HDFS && baseDir.startsWith("file:///");
}
/**
* Check whether the full name is in the correct base directory. Local storage full path with value `file:/path/to/file`
* instead of of `file:///path/to/file`
*/
private boolean isMatchBaseDir(String fullName,
String baseDir) {
if (isLocal(baseDir)) {
String midBaseDir = baseDir.replace("file:///", "file:/");
return (StringUtils.isNotBlank(fullName) && !StringUtils.startsWith(fullName, midBaseDir));
}
return (StringUtils.isNotBlank(fullName) && !StringUtils.startsWith(fullName, baseDir));
}
/**
* Get online create path. Local storage full path with value `file:/path/to/file` instead of `file:///path/to/file`
*/
private String getOnlineCreatePath(String currentDir,
String userResRootPath) {
if (isLocal(userResRootPath)) {
String midUserResRootPath = userResRootPath.replace("file:///", "file:/");
return currentDir.contains(midUserResRootPath) ? currentDir : userResRootPath;
}
return currentDir.contains(userResRootPath) ? currentDir : userResRootPath;
}
private String getTenantCode(User user) { private String getTenantCode(User user) {
Tenant tenant = tenantMapper.queryById(user.getTenantId()); Tenant tenant = tenantMapper.queryById(user.getTenantId());
if (tenant == null) { if (tenant == null) {

Loading…
Cancel
Save