Skip to content

Fix Connection port field range validation (#68382) - #70208

Open
dj-wise-ronin wants to merge 2 commits into
apache:mainfrom
dj-wise-ronin:fix-connection-port-validation
Open

Fix Connection port field range validation (#68382)#70208
dj-wise-ronin wants to merge 2 commits into
apache:mainfrom
dj-wise-ronin:fix-connection-port-validation

Conversation

@dj-wise-ronin

Copy link
Copy Markdown

Related Issue

Closes: #68382

Description

Connection models in Apache Airflow accept integer values for the port field but do not enforce that the value is a valid network port in the range [1, 65535].
This PR adds validation logic to connection ports at three layers:

  1. SQLAlchemy Connection model validation via @validates("port") to ensure port numbers assigned are integers between 1 and 65535.
  2. Task SDK Connection model validation in __attrs_post_init__ to enforce range limits and format validation during standard instantiation and URI parsing.
  3. Pydantic API datamodel validation using Field(ge=1, le=65535) on the REST API gateway connections schema.

Additionally, this PR refactors port validation logic into a shared validator in airflow_shared.configuration.connection and includes unit tests for both SQLAlchemy and Task SDK model implementations.

PR Checklist

Signed-off-by: DeAngelo Jackson-Adams <dj@wiseronin.com>
…dk and core

Signed-off-by: DeAngelo Jackson-Adams <dj@wiseronin.com>

@pierrejeambrun pierrejeambrun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What I'm affraid is that if a user has a wrong connection in their current database, upgrading with this patch mean that the task sdk endpoint will fail at runtime. Basically a task could now fail because of this. (public API though is validating only for 'write' operations, which sounds good).

We should probably do the same on the task sdk, enforce validation on 'write' only and let 'read' as it is today.

I would keep validation from 3, and delete 1 and 2 related code as a safer approach.

Comment on lines +19 to +22

def parse_and_validate_port(port: int | str | None) -> int | None:
"""Parse and validate connection port range."""
if port is None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

'connection' validation isn't related to configuration it probably shouldn't live in that module.

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

Labels

area:API Airflow's REST/HTTP API area:task-sdk

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Connection port field does not validate that the value is a valid port number

2 participants