auth kv动态化

1.功能描述

该模块有三个指令:

auth_basic: 是否auth认证开关,取值 [string|off], 默认值off。 取值可以为字符串(也可以为变量) 非off, 则为开启auth

auth_basic_user_file: 后面跟一个文件路径,使用密码文件进行auth认证

auth_basic_kv: 后面跟一个固定字符串前缀或者变量前缀,密码保存在kv数据库,可以通过 http api接口动态修改密码.

2.依赖模块

njet.conf:

load_module modules/njt_http_dyn_auth_module.so;  

3.配置样例

njet.conf 配置如下:

  ...
  load_module modules/njt_http_dyn_auth_module.so;  #加载动态auth模块
  ...
  
  
  server {
        listen       443 ssl;
        server_name www.test.com;
        proxy_cache_valid any 1d;
        expires      1d;
        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
        ssl_ciphers         AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5;
        ssl_certificate     /etc/vsftpd/.sslkey/vsftpd.pem;
        ssl_certificate_key     /etc/vsftpd/.sslkey/vsftpd.pem;

        set $passwd_file /root/bug/njet1.0/htpasswd;    #后面测试使用变量
        
        location / {
            auth_basic "test auth no var";
            auth_basic_user_file  /root/bug/njet1.0/htpasswd;
            
            root html;
            index index.html;
        }

        location /test1 {
            alias /root/bug/njet1.0/html;
            index index.html;
            
            auth_basic off;
            auth_basic_kv test_prefix;
        }

        location /test_off {
            alias /root/bug/njet1.0/html;
            index index.html;

            auth_basic "test aaaa";
        }

         #配置为off, 但是auth_basic_user_file指令也配置了
        location /test_off_config_file {
            alias /root/bug/njet1.0/html;
            index index.html;
           
            auth_basic off;
            auth_basic_user_file /root/bug/njet1.0/htpasswd;
        }

        #配置为off, 但是auth_basic_kv指令也配置了
        location /test_off_config_kv {
            alias /root/bug/njet1.0/html;
            index index.html;

            auth_basic off;
            auth_basic_kv test_auth_prefix;
        }

        #配置为使用密码文件方式(原生自带功能)
        location /test_file_on {
            alias /root/bug/njet1.0/html;
            index index.html;

            auth_basic "auth use file";
            auth_basic_user_file /root/bug/njet1.0/htpasswd;
        }

        #配置为使用kv 内存数据库固定字符串配置
        location /test_kv_str_on {
            alias /root/bug/njet1.0/html;
            index index.html;
        
            auth_basic "auth use kv";
            auth_basic_kv test_auth_prefix; #固定字符串
        }

        #配置为使用kv 内存数据库变量配置
        location /test_kv_var_on {
            alias /root/bug/njet1.0/html;
            index index.html;

            auth_basic "auth use kv";
            auth_basic_kv $server_name; #变量
        }
   }

4.API

4.1 api 列表

查询接口

GET  http://IP+port/api/v1/config/http_dyn_auth

修改接口

PUT  http://IP+port/api/v1/config/http_dyn_auth

参数说明:

指令 类型 取值 默认值 说明
auth_basic string [string|off] off off表示关闭auth认证其他字符串,开启auth认证, auth提示字符串
auth_type string [file|kv] - file: 密码文件认证,auth_param为密码文件路径kv: kv内存数据库密码认证,auth_param为kv前缀
auth_param string - - 根据auth_type 分别取值密码文件路径或者kv 前缀

4.2调用样例

通过PUT接口添加动态auth

curl -X 'PUT' \
  'http://192.168.40.156:8088/api/v1/config/http_dyn_auth' \
  -H 'accept: */*' \
  -H 'Content-Type: application/json' \
  -d '{
  "servers": [
    {
      "listens": [
        "0.0.0.0:8001"
      ],
      "serverNames": [
        "localhost"
      ],
      "locations": [
        {
          "location": "/test_kv",
          "auth_basic": "on",
          "auth_type": "kv",
          "auth_param": "$server_name"
        }
      ]
    }
  ]
}'

通过GET接口查看动态auth

curl -X 'GET' \
  'http://192.168.40.156:8088/api/v1/config/http_dyn_auth' \