Skip to content

fix(a2a): require client_id and client_secret for OAuth agents on create - #7673

Open
Shanmukh-l33t wants to merge 2 commits into
IBM:mainfrom
Shanmukh-l33t:fix/7669-require-a2a-oauth-credentials
Open

Shanmukh-l33t wants to merge 2 commits into
IBM:mainfrom
Shanmukh-l33t:fix/7669-require-a2a-oauth-credentials

Conversation

@Shanmukh-l33t

Copy link
Copy Markdown

Fixes #7669

Summary

A2AAgentCreate accepted auth_type: "oauth" with an oauth_config that had no client_id or client_secret. Registration succeeded and the agent read as active, and the failure only appeared at first invocation. A card import suggestion (grant type, token URL and scopes, no credentials) posted unchanged produces this state.

Root cause

_validate_a2a_oauth_config normalised the URLs and rejected grant types other than client_credentials, but never checked that the credentials that grant needs were present.

Fix

_validate_a2a_oauth_config takes a require_credentials flag. A2AAgentCreate passes True, so a missing, empty or blank client_id or client_secret raises a ValueError naming the field, which FastAPI returns as 422. A2AAgentUpdate is unchanged: an update may omit the credentials and keep the stored ones (the masked placeholder flow depends on this).

Three existing admin form tests in test_admin.py built a credential-less config and expected success. They now expect the 422 and that register_agent is not called (one of them gets the same outcome with a config that only lacks client_secret).

Not in scope

auth_type: "oauth" with no oauth_config at all is still accepted on create. The issue is about a config that is present but lacks credentials.

Reviewability

  • This PR has one clear purpose
  • The linked issue is not labeled triage
  • Unrelated bugs or improvements are tracked in separate issues/PRs
  • Tests are included with the code they validate
  • If AI-assisted, I understand and can explain the generated changes

Verification

New tests, run on the unmodified parent commit (red), then with the fix (green):

env -u PYTHONPATH python -m pytest tests/unit/mcpgateway/services/test_a2a_oauth.py tests/unit/mcpgateway/test_main.py -k "credential or oauth_without or oauth_with_cred" -q
before fix: 8 failed (6 schema cases, 2 POST /a2a cases returned 201 instead of 422)
after fix:  16 passed (includes update-may-omit-credentials and POST /a2a with credentials returning 201)

Wider targeted run after the fix:

env -u PYTHONPATH python -m pytest tests/unit/mcpgateway/test_a2a_*.py tests/unit/mcpgateway/services/test_a2a_*.py tests/unit/mcpgateway/test_main.py tests/unit/mcpgateway/test_main_extended.py tests/unit/mcpgateway/test_schemas.py tests/unit/mcpgateway/test_schemas_validators_extra.py tests/unit/mcpgateway/test_schemas_auth_validation.py tests/unit/mcpgateway/test_admin.py -n 2
4206 passed

Not run: make lint as a whole, make coverage, the schemas doctest for the new example (the doctest collection needs a configured .env that this environment lacks), the live gateway suite, sqlite/postgres runs. ruff and black report the same findings on the touched files before and after the change.

AI assistance was used to prepare this change; I reviewed the diff and ran the tests above.

A2AAgentCreate accepted auth_type oauth with an oauth_config that carried no client credentials, so registration succeeded and the agent failed only at first invocation. The create validator now rejects a missing or blank client_id or client_secret with a 422 that names the field. Updates are unchanged because they may keep the stored credentials. Fixes IBM#7669.

Signed-off-by: Shanmukh Dheekonda <Shanmukh-l33t@users.noreply.github.com>

@marekdano marekdano left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Shanmukh-l33t - thanks for your contribution!

Please see our findings below

Comment thread mcpgateway/schemas.py Outdated
grant_type = v.get("grant_type", _A2A_OAUTH_GRANT_TYPE)
if grant_type != _A2A_OAUTH_GRANT_TYPE:
raise ValueError(f"A2A agents support only the {_A2A_OAUTH_GRANT_TYPE} OAuth grant type, got '{grant_type}'")
if require_credentials:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking: The check requires client_secret for every oauth_config. Token requests do not always need it.

_apply_token_endpoint_auth supports token_endpoint_auth_method values none and private_key_jwt. Neither method sends a client_secret. none needs no client_id either.

Create now rejects configs that worked before. The error text says the client_credentials grant requires both fields. That is not correct for these methods.

Fix:

  • Skip the client_secret check when token_endpoint_auth_method is none or private_key_jwt.
  • Skip the client_id check when it is none.
  • Add tests for both methods.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 3f1084f. The credential check now skips client_secret for token_endpoint_auth_method none and private_key_jwt, and skips client_id for none. private_key_jwt still requires client_id (its assertion iss/sub come from it). Tests added for both methods, plus a negative case for private_key_jwt without client_id.

Comment thread mcpgateway/schemas.py Outdated
The validated configuration.
"""
return _validate_a2a_oauth_config(v)
return _validate_a2a_oauth_config(v, require_credentials=True)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking: This mode="before" field validator runs for every non-null oauth_config. It cannot read auth_type.

admin_add_a2a_agent assembles oauth_config from any oauth_* form field. It sends the config with the submitted auth_type. A bearer, basic or none agent that carries a partial oauth_config now gets a 422 for a missing credential.

Fix: move the check to a model_validator(mode="after"). Run it only when auth_type == "oauth".

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 3f1084f. The check moved out of the mode="before" field validator into a model_validator(mode="after") that runs only when auth_type == "oauth". _validate_a2a_oauth_config is back to its original signature. A bearer/basic/none agent with a partial oauth_config no longer gets a 422.

build({**CONFIG, "token_url": "http://10.0.0.5/token"})


class TestA2AOAuthCredentialsRequiredOnCreate:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: Two cases have no test:

  • A non-oauth auth_type with an oauth_config.
  • token_endpoint_auth_method none or private_key_jwt on create.

Add both. They cover the two comments above.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 3f1084f: test_methods_that_send_no_secret_do_not_require_one (none with no credentials, private_key_jwt with only client_id), test_private_key_jwt_still_requires_client_id, and test_non_oauth_auth_type_ignores_partial_oauth_config (bearer/basic/none). Also merged current main (conflicts in schemas.py and test_admin.py resolved keeping both sides). Two upstream tests and one card-import test built an OAuth agent without credentials; they now supply client_id/client_secret.

Move the A2A OAuth credential check to a model_validator(mode=after) that runs
only for auth_type=oauth, and skip client_secret for none/private_key_jwt and
client_id for none.

Signed-off-by: Shanmukh Dheekonda <Shanmukh-l33t@users.noreply.github.com>

This branch has not been deployed

No deployments
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.

[BUG]: An A2A agent can be registered with OAuth and no credentials, failing only at first invocation

2 participants