Skip to content

Feat: credential and auth model (tokens, delagation) - #30

Merged
aldbr merged 79 commits into
DIRACGrid:mainfrom
Alexander-Auer:stack/credential-models
Dec 1, 2026
Merged

aldbr merged 79 commits into
DIRACGrid:mainfrom
Alexander-Auer:stack/credential-models

Conversation

@Alexander-Auer

@Alexander-Auer Alexander-Auer commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

feat: credentials (models, provider, freshness)

Part of #17. Follows #29 (merged).

Backends get their credentials from a provider that the consumer owns. Configuration never holds a secret.

A backend authenticates with exactly one credential. X.509 proxies are not supported, so nothing needs a token and a proxy together.

How a consumer uses it

The consumer supplies the credentials: it creates a CredentialProvider, passes it to get_backend, and closes it when done. interCEde only decides when to ask, never where credentials come from, and never closes the provider.

A provider is any object with an async get method and async with support. get returns one credential, of the kind the backend asks for:

from intercede import registry
from intercede.auth import BearerToken, CredentialRequirements

class MyTokenProvider:
    async def get(self, requirements: CredentialRequirements) -> BearerToken:
        # requirements.resource is the endpoint asking; requirements.scopes the operation's scopes
        token = await my_token_service.mint(audience=requirements.resource)
        return BearerToken(value=token.value, expires_at=token.expires_at)

    async def __aenter__(self): return self
    async def __aexit__(self, *exc): ...

async with MyTokenProvider() as credentials:
    async with registry.get_backend(config, credentials) as ce:
        await ce.submit(spec, copies)

For fixed credentials, for example a key read from a file, the consumer can use StaticCredentials instead of writing a provider:

from intercede.auth import SSHPrivateKey, StaticCredentials

async with StaticCredentials([SSHPrivateKey(pem=key_bytes)]) as credentials:
    ...

One provider can be shared by every backend the consumer drives.

What's added

  • intercede.auth (public):
    • Credential types: BearerToken, SSHPrivateKey, SSHPassword. Secrets never appear in repr, JSON or error messages.
    • CredentialRequirements, which names the one kind a backend needs, and the CredentialProvider protocol.
    • StaticCredentials. It holds at most one credential of each kind, so it can serve several backends, and each backend gets only the one it asks for. Asking for a kind it does not hold raises an AuthenticationError.
  • Requirements (internal): each backend works out the one credential it needs from its config:
    • ARC and HTCondor-CE ask for a token for their endpoint. Their auth field only accepts "token".
    • SSH asks for a key or a password for user@host:port, as its auth field says.
    • Local, cloud and SSH through an agent ask for nothing.
  • Freshness (internal): before each operation, a backend asks the provider again only when it has no credential yet, or when its credential expires within 5 minutes. A provider that fails, returns anything other than the requested kind, or returns an expired credential causes an AuthenticationError for the whole operation, never a per-job failure.
  • Registry: get_backend(config, credentials) passes the provider to backends and transports. Anything that isn't a provider raises a ConfigurationError. Building a backend never calls the provider.

Breaking change

registry.get_backend(config) now takes a second argument, credentials.

Open points

  • There is no credential type for cloud API keys yet.
  • Should a provider be able to change the 5-minute margin?

Tests

155 tests pass on Python 3.11 and 3.13, with 99% coverage. test_credentials.py imports intercede._backends.credentials directly, because no real backend uses it yet.

Alexander-Auer and others added 30 commits October 2, 2026 16:54
* feat: Models the core contract needs

* Feat: Protocols and Exceptions added for the core contract

* Feat: Private protocols for the BatchBackend

* feat: a fake backend to run unit tests against

* fix: some code cleanup in the helper functions

* feat: setup for the unit test suite

* chore: style fix

* chore: styling, unit test fixture

* chore: single letter var `h` refering to hanlde changed to `handle`

* fix: unused parameter in `test_injected_failure_is_reported_per_job()` removed

* feat: Models the core contract needs

* Feat: Protocols and Exceptions added for the core contract

* Feat: Private protocols for the BatchBackend

* feat: a fake backend to run unit tests against

* fix: some code cleanup in the helper functions

* feat: setup for the unit test suite

* chore: style fix

* chore: styling, unit test fixture

* chore: single letter var `h` refering to hanlde changed to `handle`

* fix: unused parameter in `test_injected_failure_is_reported_per_job()` removed

* fix: made the OutputSpec optional as it is not suppoerted by all and a consumer should choose to use it or not

---------

Co-authored-by: alexander <alexander.auer@cern.ch>
A value given by mistake, such as a misspelled field or plain data in a
CredentialSet, may be the secret itself. Validation errors now hide their
input, as the configuration models already do.
The message comes from the consumer's code and may hold a token or a
secret URL. The AuthenticationError names only its type; the exception
itself is still available as __cause__.
The fields were described in comments, which the generated reference does
not show. They now use attribute docstrings, as the configuration models do.
Name every argument and the return value, and drop the blank lines
before closing quotes.
@Alexander-Auer
Alexander-Auer requested a review from aldbr October 27, 2026 16:17
@Alexander-Auer
Alexander-Auer marked this pull request as ready for review October 27, 2026 16:18
Alexander-Auer added a commit to Alexander-Auer/intercede that referenced this pull request Oct 27, 2026
… into feat/HTCondorCEBackend

Replaces the earlier versions this branch was built on with what was merged
upstream (DIRACGrid#27, DIRACGrid#28, DIRACGrid#29) and the credentials under review (DIRACGrid#30). The HTCondor-CE
schedd setting moves onto HTCondorCEConfig.
@Alexander-Auer Alexander-Auer linked an issue Oct 29, 2026 that may be closed by this pull request
4 tasks
@Alexander-Auer

Copy link
Copy Markdown
Contributor Author

@aldbr I removed the x509 proxy, so I think it's ready for a review

Comment thread src/intercede/_backends/credentials.py Resolved
Comment thread src/intercede/config.py Resolved
…n, and removed auth value from config as it was not used
@Alexander-Auer Alexander-Auer changed the title Feat: credential and auth model (tokens, proxy, delagation) Feat: credential and auth model (tokens, delagation) Nov 5, 2026
@Alexander-Auer
Alexander-Auer force-pushed the stack/credential-models branch from 40fcf80 to 11e435b Compare November 16, 2026 08:24
@Alexander-Auer
Alexander-Auer requested a review from aldbr November 16, 2026 08:25
Comment thread tests/unit/test_credentials.py Resolved
Comment thread src/intercede/auth/models.py Resolved
Comment thread src/intercede/auth/models.py Resolved
Comment thread src/intercede/_backends/credentials.py Resolved
Comment thread src/intercede/_backends/credentials.py Resolved
Comment thread src/intercede/_backends/credentials.py Resolved
…ing credentials

A backend keeps no credential between operations: it calls provider.get
once per operation and checks only the kind and that it has not expired.
Whether a token is reused, or one with more scopes is handed out, is the
provider's choice. The operation decides a token's scopes and how long it
must stay valid (valid_for), defined once in TOKEN_NEEDS.
…s unavailable

The audience is the endpoint's host and port in the form each CE checks,
with the default port added when the endpoint names none: host:9619 for an
HTCondor-CE, https://host:443 for ARC, as DIRAC builds it. The resource
stays the endpoint as configured.

A provider that raises something untyped now fails the operation with
ResourceUnavailableError, which is worth retrying, instead of
AuthenticationError, which is kept for a refused or unusable credential.
Submit uploads inputs (compute.modify) and kill and purge read the jobs'
states first (compute.read). An SSH transport logs in without knowing the
operation, so the operation is optional for the kinds it does not change.
Comment thread src/intercede/_backends/credentials.py Resolved
transfer_timeout_s bounds collecting one job's output, so the token for it gets the same lifetime instead of a fixed hour; one setting decides both.
@Alexander-Auer
Alexander-Auer requested a review from aldbr December 1, 2026 15:39
…e credential

A provider that cannot be reached, holds no credential of the kind asked
for, or returns one of another kind or already expired is not the resource
rejecting credentials (AuthenticationError) nor the resource being down
(ResourceUnavailableError). The protocols now list it next to those two.
@aldbr
aldbr merged commit 644bebb into DIRACGrid:main Dec 1, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Credential/auth model (tokens, proxies, delegation)

2 participants