CK权限管理


CK主要通过users.xml文件做用户权限控制,主要通过用户角色、数据库账号定义、权限管理等来进行控制。

一、用户角色profile

1、重要参数解释

  • profile : 每个profile相当于一个数据库用户角色,方便后续数据库账号授权时进行复用,默认为default,可自定义创建
  • max_memory_usage : 内存使用大小,可通过min、max设置其上下限。
  • distributed_product_mode : 分布式查询模式,默认开启。
  • readonly :读写权限限制。0:不进行任何限制(默认值); 1:只拥有读权限(只能执行 SELECT、 EXISTS、 SHOW 和 DESCRIBE) ; 2:拥有读权限和设置权限(在读权限基础上,增加了 SET查询)。
  • allow ddl : DDL权限。0:不允许 DDL查询; 1: 允许 DDL查询(默认值)。

2、示例

    <!-- Profiles of settings. -->
    <profiles>
        <!-- Default settings. -->
        <default>           <!--默认default角色-->
            <max_memory_usage>10000000000</max_memory_usage>
            <use_uncompressed_cache>0</use_uncompressed_cache>
            <load_balancing>random</load_balancing>
        </default>

    	<rw_role>           <!--超级读写权限角色-->  
	        <distributed_product_mode>allow</distributed_product_mode>
	        <constraints>
		        <max_memory_usage>
		            <min>5000000000</min>
		            <max>20000000000</max>
		        </max_memory_usage>
	        </constraints>

            <readonly>0</readonly>
            <allow_ddl>1</allow_ddl>
	    </rw_role>

        <ro_role>       <!--拥有set权限的读权限角色-->
            <distributed_product_mode>allow</distributed_product_mode>
            <constraints>
                <max_memory_usage>
                    <min>5000000000</min>
                    <max>10000000000</max>
                </max_memory_usage>
            </constraints>

            <readonly>2</readonly>
            <allow_ddl>1</allow_ddl>
        </ro_role>

    </profiles>

二、数据库账号配置

1、重要参数解释

  • username : 每个user标签即为一个用户信息
  • password : 数据库账号密码可免密、明文设置、加密设置
  • network : 对客户端开放的端口
  • profile : 引用上述的数据库角色
  • quota : 资源限额

关于密码的设置:

1)无密码

<password></password>

2)明文密码

<password>123</password>            //设置明文密码为123

3)加密设置

可使用多种加码方式进行密码加密,如:SHA256、double SHA1等

-- SHA256为例
# 查看对应加密方式加密后文本
# echo -n 123 | openssl dgst -sha256
(stdin)= a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3

<password_sha256_hex>a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3</password_sha256_hex>        //密码设置

2、示例

    <!-- Users and ACL. -->
    <users>
        <!-- If user name was not specified, 'default' user is used. -->
        <default>               <!--默认default账号-->
            <password></password>
            <networks incl="networks" replace="replace">
                <ip>::/0</ip>
            </networks>

            <!-- Settings profile for user. -->
            <profile>default</profile>

            <!-- Quota for user. -->
            <quota>default</quota>

            <!-- User can create other users and grant rights to them. -->
            <!-- <access_management>1</access_management> -->
        </default>

	<sansi_rw>                  <!--超级读写权限账号-->
	    <!--echo -n 123 | openssl dgst -sha256-->
	    <password_sha256_hex>a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3</password_sha256_hex>
            <networks incl="networks" replace="replace">
                <ip>::/0</ip>
            </networks>
	    <profile>rw_role</profile>
	    <quota>default</quota>
	</sansi_rw>


	<sansi_ro>                  <!--拥有set权限的只读账号-->
	    <!--echo -n 123 | openssl dgst -sha256-->
	    <password_sha256_hex>a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3</password_sha256_hex>
            <networks incl="networks" replace="replace">
                <ip>::/0</ip>
            </networks>
	    <profile>ro_role</profile>
	    <quota>default</quota>
	</sansi_ro>

    </users>

三、quota资源限制

1、重要参数解释

quota主要是CK的一个资源限制策略,当使用资源达到阈值后,会进行自我熔断。quota主要通过两种方式来进行限制,一种方式是根据时间周期的累计资源使用进行熔断;另外一种方式是根据单次查询的用量进行熔断。

1)根据时间周期的累计资源进行熔断

  • quota : 每个标签为一个quota限制,全局唯一,可自定义,默认为default
  • duration : 表示累计的时间周期,单位为秒
  • queries : 表示在周期时间内允许的查询次数,0表示不限制
  • errors : 表示在周期时间内允许发生异常的请求次数,0表示不限制
  • result_rows : 表示在周期时间内允许返回的结果集行数,0表示不限制
  • read_rows : 表示在周期时间内,允许的分布式查询从远端读取数据行数,0表示不限制
  • execution_time : 表示在周期时间内,允许查询时间,0表示不限制

2)根据单次查询的用量进行熔断

  • max_memory_usage : 运行一次查询的最大内存使用量,默认为10G
  • max_memory_usage_for_user : 单个用户运行查询时的内存限制,默认为0表示不限制
  • max_memory_usage_for_all_queries : CK服务中,所有运行中的查询累计使用的最大内存,默认为0表示不限制
  • max_partitions_per_insert_block : 单次Insert写入时,限制创建最大分区个数,默认为100个
  • max_rows_to_group_by : 在执行group by查询时,限制去重后聚合key的最大个数,默认为0表示不限制
  • group_by_overflow_mode : 查询被熔断时的措施。throw:表示抛出异常;break:表示立即停止并返回当前数据; any:仅根据当前已存在的聚合 KEY 继续完成聚合查询。
  • max_bytes_before_external_group_by : 在执行 GROUP BY 聚合查询的时候,限制 使用的最大内存量,默认值为 0,表示不限制

2、示例

    <!-- Quotas. -->
    <quotas>
        <!-- Name of quota. -->
        <default>
            <!-- Limits for time interval. You could specify many intervals with different limits. -->
            <interval>
                <!-- Length of interval. -->
                <duration>3600</duration>

                <!-- No limits. Just calculate resource usage for time interval. -->
                <queries>0</queries>
                <errors>0</errors>
                <result_rows>0</result_rows>
                <read_rows>0</read_rows>
                <execution_time>0</execution_time>
            </interval>
        </default>
    </quotas>

四、更细粒度数据库权限限制

1、访问权限

访问限制主要通过network标签进行限制,可有多种设置模式:

1)IP地址

<ip>127.0.0.1</ip>          //仅允许本地登录
<ip>::/0</ip>               //开放所有IP登陆

2)host主机名

<host>sdw1</host>           //允许sdw主机的客户端访问

3)正则匹配

<host>^sdw\d</host>         //允许sdw..所有主机的客户端访问

2、查询权限

1)数据库与数据字典的访问权限

数据库与数据字段的访问权限主要由allow_databases、allow_dictionaries两个标签进行限制,默认所有数据库用户不做限制。

<dict_role>
    <allow_databases> 
        <database>default</database> 
        <database>test_dictionaries</database>
    </allow_databases> 

    <allow_dictionaries>
        <dictionary>test_flat_dict</dictionary> 
    </allow_dictionaries>
</dict_role>

2)数据库读写权限

如上述profile中 readonly、allow_ddl 参数的限制。

3、数据行级别权限

数据行级别的权限主要通过 database 标签进行限制。

<dataline_role>
    <databases>
        <database_name>         //数据库名称
            <table_name>        //表名称
                <filter>${过滤条件}</filter>        //数据行的where过滤条件限制
            </table_name>       //表名称
        </database_name>        //数据库名称
    </databases>   
</dataline_role>

猜你喜欢

转载自blog.csdn.net/weixin_37692493/article/details/110817448