Commit 779672ec authored by lgcareer's avatar lgcareer Committed by qiaozhanwei
Browse files

[Fix issue #1770]check udf and data source in order to fix issue 1770 (#1826)

* check udf and data source in order to fix issue 1770

* check udf and data source in order to fix issue 1770

* update testListAuthorizedUdfFunc
parent 07f96375
Loading
Loading
Loading
Loading
+50 −0
Original line number Diff line number Diff line
/*
 * 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.common.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;

/**
 * Authorization type
 */
public enum AuthorizationType {
    /**
     * 0 RESOURCE_FILE;
     * 1 DATASOURCE;
     * 2 UDF;
     */
    RESOURCE_FILE(0, "resource file"),
    DATASOURCE(1, "data source"),
    UDF(2, "udf function");

    AuthorizationType(int code, String descp){
        this.code = code;
        this.descp = descp;
    }

    @EnumValue
    private final int code;
    private final String descp;

    public int getCode() {
        return code;
    }

    public String getDescp() {
        return descp;
    }
}
+49 −6
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package org.apache.dolphinscheduler.dao;

import com.alibaba.fastjson.JSONObject;
import com.cronutils.model.Cron;
import org.apache.commons.lang.ArrayUtils;
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.enums.*;
import org.apache.dolphinscheduler.common.model.DateInterval;
@@ -25,7 +26,6 @@ import org.apache.dolphinscheduler.common.model.TaskNode;
import org.apache.dolphinscheduler.common.process.Property;
import org.apache.dolphinscheduler.common.queue.ITaskQueue;
import org.apache.dolphinscheduler.common.task.subprocess.SubProcessParameters;
import org.apache.dolphinscheduler.common.utils.ArrayUtils;
import org.apache.dolphinscheduler.common.utils.DateUtils;
import org.apache.dolphinscheduler.common.utils.IpUtils;
import org.apache.dolphinscheduler.common.utils.JSONUtils;
@@ -44,6 +44,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toSet;
import static org.apache.dolphinscheduler.common.Constants.*;

/**
@@ -462,13 +463,10 @@ public class ProcessDao {
            return null;
        }

        if(null == tenant){
        if(tenant == null){
            User user = userMapper.selectById(userId);

            if (null != user) {
            tenant = tenantMapper.queryById(user.getTenantId());
        }
        }
        return tenant;
    }

@@ -974,6 +972,9 @@ public class ProcessDao {
    public Boolean submitTaskToQueue(TaskInstance taskInstance) {

        try{
            if(taskInstance.isSubProcess()){
                return true;
            }
            if(taskInstance.getState().typeIsFinished()){
                logger.info(String.format("submit to task queue, but task [%s] state [%s] is already  finished. ", taskInstance.getName(), taskInstance.getState().toString()));
                return true;
@@ -1770,5 +1771,47 @@ public class ProcessDao {
        return projectIdList;
    }

    /**
     * list unauthorized udf function
     * @param userId    user id
     * @param needChecks  data source id array
     * @return unauthorized udf function list
     */
    public <T> List<T> listUnauthorized(int userId,T[] needChecks,AuthorizationType authorizationType){
        List<T> resultList = new ArrayList<T>();

        if (!ArrayUtils.isEmpty(needChecks)) {
            Set<T> originResSet = new HashSet<T>(Arrays.asList(needChecks));

            switch (authorizationType){
                case RESOURCE_FILE:
                    Set<String> authorizedResources = resourceMapper.listAuthorizedResource(userId, needChecks).stream().map(t -> t.getAlias()).collect(toSet());
                    originResSet.removeAll(authorizedResources);
                    break;
                case DATASOURCE:
                    Set<Integer> authorizedDatasources = dataSourceMapper.listAuthorizedDataSource(userId,needChecks).stream().map(t -> t.getId()).collect(toSet());
                    originResSet.removeAll(authorizedDatasources);
                    break;
                case UDF:
                    Set<Integer> authorizedUdfs = udfFuncMapper.listAuthorizedUdfFunc(userId, needChecks).stream().map(t -> t.getId()).collect(toSet());
                    originResSet.removeAll(authorizedUdfs);
                    break;
            }

            resultList.addAll(originResSet);
        }

        return resultList;
    }

    /**
     * get user by user id
     * @param userId user id
     * @return User
     */
    public User getUserById(int userId){
        return userMapper.queryDetailsById(userId);
    }


}
+9 −0
Original line number Diff line number Diff line
@@ -77,4 +77,13 @@ public interface DataSourceMapper extends BaseMapper<DataSource> {
    List<DataSource> listAllDataSourceByType(@Param("type") Integer type);


    /**
     * list authorized UDF function
     * @param userId userId
     * @param dataSourceIds data source id array
     * @return UDF function list
     */
    <T> List<DataSource> listAuthorizedDataSource(@Param("userId") int userId,@Param("dataSourceIds")T[] dataSourceIds);


}
+8 −0
Original line number Diff line number Diff line
@@ -83,4 +83,12 @@ public interface ResourceMapper extends BaseMapper<Resource> {
     * @return tenant code
     */
    String queryTenantCodeByResourceName(@Param("resName") String resName);

    /**
     * list authorized resource
     * @param userId userId
     * @param resNames resource names
     * @return resource list
     */
    <T> List<Resource> listAuthorizedResource(@Param("userId") int userId,@Param("resNames")T[] resNames);
}
+7 −0
Original line number Diff line number Diff line
@@ -78,5 +78,12 @@ public interface UdfFuncMapper extends BaseMapper<UdfFunc> {
     */
    List<UdfFunc> queryAuthedUdfFunc(@Param("userId") int userId);

    /**
     * list authorized UDF function
     * @param userId userId
     * @param udfIds UDF function id array
     * @return UDF function list
     */
    <T> List<UdfFunc> listAuthorizedUdfFunc (@Param("userId") int userId,@Param("udfIds")T[] udfIds);

}
Loading