-
Notifications
You must be signed in to change notification settings - Fork 17.4k
Validate connection port is a valid network port number #70052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dj-wise-ronin
wants to merge
2
commits into
apache:main
Choose a base branch
from
dj-wise-ronin:fix-connection-port-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
shared/configuration/src/airflow_shared/configuration/connection.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| from __future__ import annotations | ||
|
|
||
|
|
||
| def parse_and_validate_port(port: int | str | None) -> int | None: | ||
| """Parse and validate connection port range.""" | ||
| if port is None: | ||
| return None | ||
| if isinstance(port, str) and not port.strip(): | ||
| return None | ||
| try: | ||
| port_val = int(port) | ||
| except (ValueError, TypeError): | ||
| raise ValueError(f"Expected integer value for `port`, but got {port!r} instead.") | ||
| if not (1 <= port_val <= 65535): | ||
| raise ValueError( | ||
| f"The `port` field must be a value between 1 and 65535, but got {port_val!r} instead." | ||
| ) | ||
| return port_val |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -421,3 +421,41 @@ def test_from_uri_roundtrip(self): | |
| original_extra = json.loads(conn_from_original.extra) | ||
| roundtrip_extra = json.loads(conn_from_roundtrip.extra) | ||
| assert original_extra == roundtrip_extra | ||
|
|
||
|
|
||
| class TestConnectionPortValidation: | ||
| """Test connection port validation in the task-sdk.""" | ||
|
|
||
| def test_port_validation(self): | ||
| """Test that Connection model validates the port field correctly.""" | ||
| # Valid ports | ||
| assert Connection(conn_id="test_port_1", port=80).port == 80 | ||
| assert Connection(conn_id="test_port_2", port="8080").port == 8080 | ||
| assert Connection(conn_id="test_port_3", port=None).port is None | ||
|
|
||
| # Invalid ports - out of range | ||
| with pytest.raises(ValueError, match="The `port` field must be a value between 1 and 65535"): | ||
| Connection(conn_id="test_port_fail_1", port=70000) | ||
|
|
||
| with pytest.raises(ValueError, match="The `port` field must be a value between 1 and 65535"): | ||
| Connection(conn_id="test_port_fail_2", port=0) | ||
|
|
||
| with pytest.raises(ValueError, match="The `port` field must be a value between 1 and 65535"): | ||
| Connection(conn_id="test_port_fail_3", port=-80) | ||
|
|
||
| # Invalid ports - type errors | ||
| with pytest.raises(ValueError, match="Expected integer value for `port`"): | ||
| Connection(conn_id="test_port_fail_4", port="invalid_port") | ||
|
|
||
| def test_from_uri_port_validation(self): | ||
| """Test that Connection.from_uri validates the port field correctly.""" | ||
| # Valid port from URI | ||
| assert Connection.from_uri("postgres://host:5432/db", conn_id="test").port == 5432 | ||
|
|
||
| # Invalid port from URI - out of range | ||
| with pytest.raises(ValueError, match="Port out of range 0-65535"): | ||
| Connection.from_uri("postgres://host:70000/db", conn_id="test") | ||
|
|
||
| # Invalid port from URI - type/invalid format | ||
| with pytest.raises(ValueError, match="Port could not be cast to integer value"): | ||
| Connection.from_uri("postgres://host:abc/db", conn_id="test") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tests could be better. Boundary and whitespace coverage is missing amongst other things. I would suggest the below: |
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test could be better as well. Please see the below: