• [Kong] basic-auth基本认证及ACL鉴权



    在上篇文章《key-auth实现对API请求的密钥认证》,简单学习了如何对API做一个访问认证。

    本文将通过配置basic-auth插件,继续对Kong的学习。

    basic-auth

    Basic插件和key-auth插件的启用基本相同, 都可以安装配置在Service或者Route上。

    所有插件都可以使用 http://kong_ip:8001/plugins/ 进行通用配置。这是与任何服务、路由或消费者都无关的插件,它是“公共的”,并且将在每个请求上运行
    下面是basic-auth插件可配置参数列表:

    形式参数 默认值 描述
    name 插件名称, 这里是 basic-auth
    service_id 插件针对的服务ID
    route_id 插件针对的路由ID
    enabled true 插件是否可用
    api_id 插件针对的API模块
    config.hide_credentials(可选) false 可选的布尔值,告诉插件从上游服务中显示或隐藏凭证. 如果为"true",插件将在代理之前将凭据从请求中剥离(例如:Authorization 头部)
    config.anonymous(可选) 可选字符串,如果身份验证失败,该值(消费者uuid)用作“匿名”使用者。 如果空(默认),请求失败将提示认证失败“4xx”。请注意,这个值必须是指向消费者 id属性, 而不是 它的 custom_id.

    任何具有有效凭证的用户都可以访问Kong API。要限制只有部分经过身份验证的用户,还可以添加ACL插件,并创建白名单或黑名单用户组(下节知识)。

    本文我们将使用 之前添加的route 进行测试,另外现有的认证插件请先禁用,防止影响测试。route及其对应service信息如下:

    {
      "updated_at": 1577246121,
      "created_at": 1577246121,
      "strip_path": false,
      "snis": null,
      "hosts": [
        "example.com"
      ],
      "name": "test-plugin-basic_auth",
      "methods": [
        "GET",
        "POST"
      ],
      "sources": null,
      "preserve_host": false,
      "regex_priority": 0,
      "service": {
        "host": "mockbin.org",
        "created_at": 1575882610,
        "connect_timeout": 60000,
        "id": "faa7761e-8883-4762-a7bf-d8df80768db1",
        "protocol": "http",
        "name": "example-service",
        "read_timeout": 60000,
        "port": 80,
        "path": null,
        "updated_at": 1575882610,
        "retries": 5,
        "write_timeout": 60000,
        "tags": null,
        "extras": {}
      },
      "paths": [
        "/request"
      ],
      "destinations": null,
      "id": "6dc2a722-3e71-4e17-8917-6271f3271d96",
      "protocols": [
        "http",
        "https"
      ],
      "tags": null
    }
    

    1. Route上启用插件

    $ curl -X POST http://localhost:8001/routes/6dc2a722-3e71-4e17-8917-6271f3271d96/plugins 
        --data "name=basic-auth" 
        --data "config.hide_credentials=true"
    

    2. 创建一个Consumer

    $ curl -i -X POST 
      --url http://localhost:8001/consumers/ 
      --data "username=basic-user" 
      --data "custom_id=basic-01"
    

    3. 为Consumer创建凭证

    $ curl -X POST http://localhost:8001/consumers/basic-user/basic-auth 
      --data "username=zhangsan" 
      --data "password=123abc"
    

    4. 验证凭证

    授权头必须时由Base64编码加密的。

    例如,凭证用户为“zhangsan,密码为“123abc”,那么加密凭证就是zhangsan:123abc的Base64编码,即 QWxhZGRpbjpPcGVuU2VzYW1l。

    Curl

    $ curl -i -X GET 
      --url http://localhost:8000/request/ 
      --header "Host: example.com" 
      --header 'Authorization: Basic emhhbmdzYW46MTIzYWJj'
    

    响应请求:

    HTTP/1.1 200 OK
    Content-Type: application/json; charset=utf-8
    Content-Length: 1042
    Connection: keep-alive
    Server: Cowboy
    Access-Control-Allow-Origin: *
    Access-Control-Allow-Methods: GET
    Access-Control-Allow-Headers: host,connection,x-forwarded-for,x-forwarded-proto,x-forwarded-host,x-forwarded-port,x-real-ip,user-agent,accept,x-consumer-id,x-consumer-custom-id,x-consumer-username,x-credential-username,x-request-id,via,connect-time,x-request-start,total-route-time
    Access-Control-Allow-Credentials: true
    X-Powered-By: mockbin
    Vary: Accept, Accept-Encoding
    Etag: W/"412-ENv9agFDtEk++eM3OQT6sXpcdNs"
    Date: Thu, 26 Dec 2019 06:42:49 GMT
    Via: kong/1.1.2
    X-Kong-Upstream-Status: 200
    X-Kong-Upstream-Latency: 505
    X-Kong-Proxy-Latency: 295
    Kong-Cloud-Request-ID: b810a616ec95ee60df8fe690d3575f25
    
    {
      "startedDateTime": "2019-12-26T06:42:49.546Z",
      "clientIPAddress": "127.0.0.1",
      "method": "GET",
      "url": "http://mockbin.org/request/",
      "httpVersion": "HTTP/1.1",
      "cookies": {},
      "headers": {
        "host": "mockbin.org",
        "connection": "close",
        "x-forwarded-for": "127.0.0.1, 146.xxx.xxx.254, 18.xxx.xx.183",
        "x-forwarded-proto": "http",
        "x-forwarded-host": "mockbin.org",
        "x-forwarded-port": "80",
        "x-real-ip": "146.xxx.xxx.254",
        "user-agent": "curl/7.29.0",
        "accept": "*/*",
        "x-consumer-id": "95a87530-1c64-49a2-8e48-cff854356408",
        "x-consumer-custom-id": "basic-01",
        "x-consumer-username": "basic-user",
        "x-credential-username": "zhangsan",
        "x-request-id": "3d8aee46-2bab-43b8-a02c-cee35901d777",
        "via": "1.1 vegur",
        "connect-time": "1",
        "x-request-start": "1577342569543",
        "total-route-time": "0"
      },
      "queryString": {},
      "postData": {
        "mimeType": "application/octet-stream",
        "text": "",
        "params": []
      },
      "headersSize": 573,
      "bodySize": 0
    }
    

    浏览器
    当使用浏览器访问时,需要提供正确的用户凭证,否则将被拒绝访问:401 message "Invalid authentication credentials"

    ACL 用户鉴权

    上节提到,认证插件启用后,任何具有有效凭证的用户都可以访问Kong API。

    但实际环境中,我只想要指定的用户可以访问API,这时候就需要借助ACL插件进行访问控制。
    通过下图,可以直白的了解ACL插件的功能,即:控制哪些Comsumers可以访问API。

    1. 在route上启用ACL鉴权插件

    为了方便测试,我们还是在route上启用插件。(测试route_id 6dc2a722-3e71-4e17-8917-6271f3271d96)

    $ curl -X POST http://localhost:8001/routes/{route_id}/plugins 
        --data "name=acl" 
        --data "config.whitelist=group1" 
        --data "config.hide_groups_header=true"
    

    可以看到,我们在上面的route上添加ACL插件,并设置了"config.whitelist=group1, group2",意思就是说:在启用了ACL插件后,只有在白名单里的group1, group2组的comsuner,才可以访问这个route。

    我们继续往下看,在上节文章中,我们在route上启用了basic-auth,并且已经实现访问认证,但是在同一route上又添加了ACL后,我们不能访问这个服务了,这时就是ACL鉴权白名单起了作用,所有consumers都被禁止访问了,它们都没有再ACL鉴权白名单中。

    下面是acl参数列表:

    形式参数 默认值 描述
    service_id 服务ID
    route_id 路由ID
    enabled true 是否可用.
    api_id API ID
    config.whitelist semi-optional 白名单,逗号分隔的任意组名的列表
    config.blacklist semi-optional 黑名单,逗号分隔的任意组名的列表
    config.hide_groups_header optional false 如果启用了标记(“true”),可以防止 请求将X-Consumer-Groups头部发送到上游服务的请求。

    2. 关联comsumer

    如下图所示,假如,我们希望只让消费者basic-user可以访问Kong API,

    $ curl -X POST http://localhost:8001/consumers/basic-user/acls 
        --data "group=group1"
    

    我们再次访问添加了basic-auth的API,提示我们输入用户凭证,这说明消费者basic-user通过了ACL鉴权。

    输入正确的用户凭证,就可以正常访问Kong API了

    Basic auth 与ACL 的总结

    利用basic-auth以及acl插件,我们能够对API接口访问进行有效控制

    • ACL:首先需要通过ACL鉴权的用户,才有资格进行API访问认证
    • Basic auth:通过了ACL鉴权的用户,需要携带正确的用户凭证,才能正常访问API

     
    [sleepy↓]

     

  • 相关阅读:
    用的fileupload组件实现的大文件上传
    手机刷机
    Bonferroni correction
    FTP 常用命令
    Oracle 10 参数配置说明
    Solaris 10 ftp,telnet,ssh,sendmail
    solaris10下安装oracle10g
    solaris10中安装oracle内核参数的调整
    matlab从txt/csv文件中读取一行
    SUN服务器Solaris10安装步骤
  • 原文地址:https://www.cnblogs.com/sunhongleibibi/p/12103215.html
Copyright © 2020-2023  润新知