Skip to content

Conversation

@anshuksi282-ksolves
Copy link
Contributor

Description

This PR resolves an issue where the airflow connections test CLI command would fail with "Connection not found" or "conn_id isn't defined", even when the connection was clearly visible using airflow connections list.

The Issue:
Previously, connections_test relied solely on Connection.get_connection_from_secrets(args.conn_id). If the secrets backend failed to retrieve the connection (e.g., due to environment issues or Hook initialization failures), the CLI would exit with an error, ignoring the fact that the connection might exist in the Metastore Database.
In contrast, connections_list queries the database directly, creating a discrepancy between the two commands.

The Fix:
I have updated airflow/cli/commands/connection_command.py to implement a fallback mechanism:

  1. It first attempts to retrieve the connection via the Secrets Backend (existing behavior).
  2. If that fails (throws AirflowNotFoundException or returns None), it now explicitly checks the Database using create_session.
  3. This ensures that if a connection exists in the DB, the CLI can still attempt to test it.

Related Issue

Closes #58567


^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named {pr_number}.significant.rst or {issue_number}.significant.rst, in airflow-core/newsfragments.

Test a connection

Positional Arguments:
  conn_id        Connection id, required to get/add/delete/test a connection

Options:
  -h, --help     show this help message and exit
  -v, --verbose  Make logging output more verbose fails to resolve connection by conn_id
Copy link
Member

@potiuk potiuk left a comment

Choose a reason for hiding this comment

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

That's not the right way of solving it.

Currently get_connection_from_secrets (and it's likely a misleading name but we have to keep it for compatibility) - will retrieve connection from configured secret backends and it will do it in the sequence the backends are configured.

Currently, the backends are always in predefined order but we have a change in progress #45931 that will allow to configure the order.

And connection list is different. connection list will only ever list connections that you have in database and nothing else, because there is no "enumeration" possibility when using the secrets backends.

So - I am not sure if there is anything to fix.

If you have backend configured and run connection.get and it fails with anything else than "NOT FOUND" - it should indeed fail, because it's possible that connection.get should return the connection from secret - not the one from the database.

So I think we should just close that issue.

@potiuk
Copy link
Member

potiuk commented Dec 6, 2025

I am not even sure if that is the actual problem that was described in #58567

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test connection not working

2 participants