Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

key-auth vs basic-auth plugin behaviour #12577

Closed
1 task done
zpevma opened this issue Feb 17, 2024 · 1 comment · Fixed by #11794
Closed
1 task done

key-auth vs basic-auth plugin behaviour #12577

zpevma opened this issue Feb 17, 2024 · 1 comment · Fixed by #11794

Comments

@zpevma
Copy link

zpevma commented Feb 17, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Kong version ($ kong version)

3.6.0

Current Behavior

key-auth and basic-auth plugins behave differently when setting www-authenticate header with anonymous config option:

  • key-auth sets www-authenticate: Key realm="kong" header with anonymous option set
  • basic-auth doesn't set www-authenticate: Basic realm="service" header with anonymous option set

Expected Behavior

I'm trying to achieve the following behaviour:

The idea is to use key-auth and basic-auth globally, both configured with anonymous consumer. Based on plugin ordering documentation, key-auth is evaluated first, sets www-authenticate: Key realm="kong" header, then basic-auth plugin is evaluated and overwrites www-authenticate header with Basic realm="service" value. The last step fails as we can see below.

Steps To Reproduce

Following declarative configuration

_format_version: "3.0"

services:
- name: test.service
  url: https://upstream.internal
  routes:
    - name: basic.route
      paths:
        - /basic
      plugins:
        - name: basic-auth

    - name: key.route
      paths:
        - /key
      plugins:
        - name: key-auth
    
    - name: basic-anon.route
      paths:
        - /basic-anon
      plugins:
        - name: basic-auth
          config:
            anonymous: anonymous
    
    - name: key-anon.route
      paths:
        - /key-anon
      plugins:
        - name: key-auth
          config:
            anonymous: anonymous
            

consumers:
- username: anonymous
  plugins:
  - name: request-termination
    config:
      status_code: 401
      message: Unauthorised

Produces following behaviour:

test~ % curl -s -D - -o /dev/null -k https://kong/basic   
HTTP/2 401 
date: Sat, 17 Feb 2024 17:53:13 GMT
content-type: application/json; charset=utf-8
www-authenticate: Basic realm="service"
content-length: 81
x-kong-response-latency: 1

test~ % curl -s -D - -o /dev/null -k https://kong/key  
HTTP/2 401 
date: Sat, 17 Feb 2024 17:53:19 GMT
content-type: application/json; charset=utf-8
www-authenticate: Key realm="kong"
content-length: 96
x-kong-response-latency: 1

test~ % curl -s -D - -o /dev/null -k https://kong/basic-anon
HTTP/2 401 
date: Sat, 17 Feb 2024 17:53:28 GMT
content-type: application/json; charset=utf-8
content-length: 26
x-kong-response-latency: 1

test~ % curl -s -D - -o /dev/null -k https://kong/key-anon  
HTTP/2 401 
date: Sat, 17 Feb 2024 17:53:34 GMT
content-type: application/json; charset=utf-8
www-authenticate: Key realm="kong"
content-length: 26
x-kong-response-latency: 

test~ %

As we can see, route with basic-auth plugin together with anonymous config doesn't return any www-authenticate header.

I'm not sure what correct behaviour should be (returning www-authenticate header in all cases would solve my issue 😉 ), but I would expect consistency between key-auth and basic-auth plugins.

Anything else?

No response

@nowNick
Copy link
Contributor

nowNick commented Feb 19, 2024

Hi @zpevma !

Thank you for bringing that up :) ! There's an ongoing effort to keep auth plugins behaviour consistent and compliant with RFC7235

A change for basic-auth was introduced with 3.6 and there's an open PR that consolidates this behaviour between key-auth and basic-auth: #11794

Maybe to sum it up - all auth plugins are planned to return www-authenticate header with 401 response. In that header there's also optional realm that can be included if the plugin was configured with it. The only exception will be basic-auth where realm is required and by default it's set to service.

@nowNick nowNick linked a pull request Feb 19, 2024 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants