Skip to content
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

PYTHON-3800 Add lower() to node when validate hosts of srv records #1289

Merged
merged 1 commit into from
Jul 5, 2023

Conversation

qkrwjdan
Copy link
Contributor

@qkrwjdan qkrwjdan commented Jul 4, 2023

Background

When using mongodb+srv DNS Seed List Connection String, I had no problems with the other drivers, but only when using the pymongo driver.

The error is as follows

python3 main.py        
Traceback (most recent call last):  
  File "/Users/user/Workspace/tmp/pymongo-test/main.py", line 11, in <module>  
    client = pymongo.MongoClient(uri)  
             ^^^^^^^^^^^^^^^^^^^^^^^^  
  File "/Users/user/Workspace/tmp/pymongo-test/venv/lib/python3.11/site-packages/pymongo/mongo_client.py", line 748, in __init__  
    res = uri_parser.parse_uri(  
          ^^^^^^^^^^^^^^^^^^^^^  
  File "/Users/user/Workspace/tmp/pymongo-test/venv/lib/python3.11/site-packages/pymongo/uri_parser.py", line 539, in parse_uri  
    nodes = dns_resolver.get_hosts()  
            ^^^^^^^^^^^^^^^^^^^^^^^^  
  File "/Users/user/Workspace/tmp/pymongo-test/venv/lib/python3.11/site-packages/pymongo/srv_resolver.py", line 121, in get_hosts  
    _, nodes = self._get_srv_response_and_hosts(True)  
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
  File "/Users/user/Workspace/tmp/pymongo-test/venv/lib/python3.11/site-packages/pymongo/srv_resolver.py", line 115, in _get_srv_response_and_hosts  
    raise ConfigurationError(f"Invalid SRV host: {node[0]}")  
pymongo.errors.ConfigurationError: Invalid SRV host: mongodb-r.jeongmu-rs.RegionOne.mg.db.***.com

It was weird that it only happened with the python driver.

Cause

First, the uri in the connection string that I was using contained uppercase letters, and the SRV Records also contained uppercase letters.
For example,

  • connection string : mongodb+srv://username:password@mongodb-jeongmu-rs.RegionOne.mg.db.***.com
  • SRV records :
nslookup -type=SRV _mongodb._tcp.mongodb-jeongmu-rs.RegionOne.mg.db.***.com
Server:         10.28.4.20
Address:        10.28.4.20#53

_mongodb._tcp.mongodb-jeongmu-rs.RegionOne.mg.db.***.com service = 1 1 27017 mongodb-r.jeongmu-rs.RegionOne.mg.db.***.com.
_mongodb._tcp.mongodb-jeongmu-rs.RegionOne.mg.db.***.com service = 1 1 27017 mongodb-r.jeongmu-rs.RegionOne.mg.db.***.com.
_mongodb._tcp.mongodb-jeongmu-rs.RegionOne.mg.db.***.com service = 1 1 27017 mongodb-r.jeongmu-rs.RegionOne.mg.db.***.com.

If you look at line 144 of uri_parser.py, inside the parse_host function, the hostname is converted to lowercase via lower(), but the imported SRV records still contain uppercase letters.

# Normalize hostname to lowercase, since DNS is case-insensitive:
# http://tools.ietf.org/html/rfc4343
# This prevents useless rediscovery if "foo.com" is in the seed list but
# "FOO.com" is in the hello response.
return host.lower(), port

Therefore, when comparing the fqdn and node at _get_srv_response_and_hosts, the fqdn has been changed to lowercase and the node still contains uppercase letters.
It cause pymongo.errors.ConfigurationError: Invalid SRV host.

Copy link
Member

@blink1073 blink1073 left a comment

Choose a reason for hiding this comment

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

LGTM, thank you!

@blink1073 blink1073 merged commit 7d7118b into mongodb:master Jul 5, 2023
ShaneHarvey pushed a commit that referenced this pull request Jul 6, 2023
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.

2 participants