Skip to content

Unable to authenticate on macOS - how to confirm it's pulling cached credentials? #193

@victorhooi

Description

@victorhooi

I'm trying to use requests-kerberos on macOS, and can't seem to authenticate properly. I'm a Kerberos newbie, so it's very possible I'm missing something basic here.

I use kinit to authenticate myself and populate the credential cache. I then used klist to verify that I have a cached TGT there:

❯ klist -A
Credentials cache: API:F56611A6-CC9E-447F-9438-E264BAF2ECFD
        Principal: john.smith@FOOBAR.COM

  Issued                Expires               Principal
May  8 14:06:10 2025  May  9 00:06:10 2025  krbtgt/FOOBAR.COM@FOOBAR.COM

I did notice that the "Credentials cache" specifies "API:" as the location, rather than say, a temporary file somewhere - but from my research I believe that's normal/expected on macOS as it's no longer file-backed. (Source: jcmturner/gokrb5#412)

When I make my request to the remote server with requests-kerberos, requests.text simply contains "Unauthorised". Sample code:

def main():
    logging.basicConfig(level=logging.DEBUG)
    kerberos_auth = HTTPKerberosAuth(principal="krbtgt/foobar.COM@foobar.COM")
    r = requests.get("http://proxy1.orthrus.byted.org:8080/api/v1/gss/list", auth=kerberos_auth)
    print(r.text)

I turned on verbose logging (logging.basicConfig(level=logging.DEBUG)), and I tried again, full output is below:

However, it's a little unclear to me if requests-kerberos is actually successfully pulling the cached credentials or not.

Is there some way to verify that it's actually pulling them, and presenting them, or is there something else I'm missing here?

DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): proxy1.foobar.org:8080
DEBUG:urllib3.connectionpool:http://proxy1.foobar.org:8080 "GET /api/v1/gss/list HTTP/1.1" 401 15
DEBUG:requests_kerberos.kerberos_:handle_401(): Handling: 401
ERROR:requests_kerberos.kerberos_:generate_request_header(): ctx init failed:
Traceback (most recent call last):
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/spnego/_gss.py", line 363, in __init__
    gssapi_credential = _get_gssapi_credential(
                        ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/spnego/_gss.py", line 120, in _get_gssapi_credential
    gss_cred = gssapi.Credentials(name=principal, usage=usage, mechs=[mech])
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/gssapi/creds.py", line 77, in __new__
    res = cls.acquire(name, lifetime, mechs, usage,
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/gssapi/creds.py", line 163, in acquire
    res = rcreds.acquire_cred(name, lifetime,
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "gssapi/raw/creds.pyx", line 127, in gssapi.raw.creds.acquire_cred
gssapi.raw.exceptions.MissingCredentialsError: Major (458752):  No credentials were supplied, or the credentials were unavailable or inaccessible., Minor (0): unknown mech-code 0 for mech unknown

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/requests_kerberos/kerberos_.py", line 213, in generate_request_header
    self._context[host] = ctx = spnego.client(
                                ^^^^^^^^^^^^^^
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/spnego/auth.py", line 169, in client
    return _new_context(
           ^^^^^^^^^^^^^
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/spnego/auth.py", line 84, in _new_context
    return proxy(
           ^^^^^^
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/spnego/_gss.py", line 370, in __init__
    raise SpnegoError(base_error=gss_err, context_msg="Getting GSSAPI credential") from gss_err
spnego.exceptions.NoCredentialError: SpnegoError (7): Major (458752):  No credentials were supplied, or the credentials were unavailable or inaccessible., Minor (0): unknown mech-code 0 for mech unknown, Context: Getting GSSAPI credential
ERROR:requests_kerberos.kerberos_:SpnegoError (7): Major (458752):  No credentials were supplied, or the credentials were unavailable or inaccessible., Minor (0): unknown mech-code 0 for mech unknown, Context: Getting GSSAPI credential
Traceback (most recent call last):
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/spnego/_gss.py", line 363, in __init__
    gssapi_credential = _get_gssapi_credential(
                        ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/spnego/_gss.py", line 120, in _get_gssapi_credential
    gss_cred = gssapi.Credentials(name=principal, usage=usage, mechs=[mech])
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/gssapi/creds.py", line 77, in __new__
    res = cls.acquire(name, lifetime, mechs, usage,
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/gssapi/creds.py", line 163, in acquire
    res = rcreds.acquire_cred(name, lifetime,
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "gssapi/raw/creds.pyx", line 127, in gssapi.raw.creds.acquire_cred
gssapi.raw.exceptions.MissingCredentialsError: Major (458752):  No credentials were supplied, or the credentials were unavailable or inaccessible., Minor (0): unknown mech-code 0 for mech unknown

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/requests_kerberos/kerberos_.py", line 213, in generate_request_header
    self._context[host] = ctx = spnego.client(
                                ^^^^^^^^^^^^^^
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/spnego/auth.py", line 169, in client
    return _new_context(
           ^^^^^^^^^^^^^
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/spnego/auth.py", line 84, in _new_context
    return proxy(
           ^^^^^^
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/spnego/_gss.py", line 370, in __init__
    raise SpnegoError(base_error=gss_err, context_msg="Getting GSSAPI credential") from gss_err
spnego.exceptions.NoCredentialError: SpnegoError (7): Major (458752):  No credentials were supplied, or the credentials were unavailable or inaccessible., Minor (0): unknown mech-code 0 for mech unknown, Context: Getting GSSAPI credential
DEBUG:requests_kerberos.kerberos_:handle_401(): returning <Response [401]>
DEBUG:requests_kerberos.kerberos_:handle_response(): returning <Response [401]>
DEBUG:requests_kerberos.kerberos_:handle_response() has seen 0 401 responses
DEBUG:requests_kerberos.kerberos_:handle_401(): Handling: 401
ERROR:requests_kerberos.kerberos_:generate_request_header(): ctx init failed:
Traceback (most recent call last):
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/spnego/_gss.py", line 363, in __init__
    gssapi_credential = _get_gssapi_credential(
                        ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/spnego/_gss.py", line 120, in _get_gssapi_credential
    gss_cred = gssapi.Credentials(name=principal, usage=usage, mechs=[mech])
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/gssapi/creds.py", line 77, in __new__
    res = cls.acquire(name, lifetime, mechs, usage,
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/gssapi/creds.py", line 163, in acquire
    res = rcreds.acquire_cred(name, lifetime,
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "gssapi/raw/creds.pyx", line 127, in gssapi.raw.creds.acquire_cred
gssapi.raw.exceptions.MissingCredentialsError: Major (458752):  No credentials were supplied, or the credentials were unavailable or inaccessible., Minor (0): unknown mech-code 0 for mech unknown

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/requests_kerberos/kerberos_.py", line 213, in generate_request_header
    self._context[host] = ctx = spnego.client(
                                ^^^^^^^^^^^^^^
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/spnego/auth.py", line 169, in client
    return _new_context(
           ^^^^^^^^^^^^^
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/spnego/auth.py", line 84, in _new_context
    return proxy(
           ^^^^^^
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/spnego/_gss.py", line 370, in __init__
    raise SpnegoError(base_error=gss_err, context_msg="Getting GSSAPI credential") from gss_err
spnego.exceptions.NoCredentialError: SpnegoError (7): Major (458752):  No credentials were supplied, or the credentials were unavailable or inaccessible., Minor (0): unknown mech-code 0 for mech unknown, Context: Getting GSSAPI credential
ERROR:requests_kerberos.kerberos_:SpnegoError (7): Major (458752):  No credentials were supplied, or the credentials were unavailable or inaccessible., Minor (0): unknown mech-code 0 for mech unknown, Context: Getting GSSAPI credential
Traceback (most recent call last):
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/spnego/_gss.py", line 363, in __init__
    gssapi_credential = _get_gssapi_credential(
                        ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/spnego/_gss.py", line 120, in _get_gssapi_credential
    gss_cred = gssapi.Credentials(name=principal, usage=usage, mechs=[mech])
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/gssapi/creds.py", line 77, in __new__
    res = cls.acquire(name, lifetime, mechs, usage,
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/gssapi/creds.py", line 163, in acquire
    res = rcreds.acquire_cred(name, lifetime,
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "gssapi/raw/creds.pyx", line 127, in gssapi.raw.creds.acquire_cred
gssapi.raw.exceptions.MissingCredentialsError: Major (458752):  No credentials were supplied, or the credentials were unavailable or inaccessible., Minor (0): unknown mech-code 0 for mech unknown

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/requests_kerberos/kerberos_.py", line 213, in generate_request_header
    self._context[host] = ctx = spnego.client(
                                ^^^^^^^^^^^^^^
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/spnego/auth.py", line 169, in client
    return _new_context(
           ^^^^^^^^^^^^^
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/spnego/auth.py", line 84, in _new_context
    return proxy(
           ^^^^^^
  File "/Users/foobar/code/foobar/api_menangerie/.venv/lib/python3.12/site-packages/spnego/_gss.py", line 370, in __init__
    raise SpnegoError(base_error=gss_err, context_msg="Getting GSSAPI credential") from gss_err
spnego.exceptions.NoCredentialError: SpnegoError (7): Major (458752):  No credentials were supplied, or the credentials were unavailable or inaccessible., Minor (0): unknown mech-code 0 for mech unknown, Context: Getting GSSAPI credential
DEBUG:requests_kerberos.kerberos_:handle_401(): returning <Response [401]>
DEBUG:requests_kerberos.kerberos_:handle_response(): returning <Response [401]>
DEBUG:requests_kerberos.kerberos_:handle_response() has seen 1 401 responses
DEBUG:requests_kerberos.kerberos_:handle_response(): returning 401 <Response [401]>
Unauthorised.



Process finished with exit code 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions