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

Adding certificate validation for ssl in mongo hook #37214

Merged
merged 6 commits into from
Feb 8, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions airflow/providers/mongo/hooks/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from __future__ import annotations

import warnings
from ssl import CERT_NONE
from typing import TYPE_CHECKING, Any, overload
from urllib.parse import quote_plus, urlunsplit

Expand Down Expand Up @@ -75,6 +74,10 @@ def __init__(self, mongo_conn_id: str = default_conn_name, *args, **kwargs) -> N
self.client: MongoClient | None = None
self.uri = self._create_uri()

self.allow_insecure = True
potiuk marked this conversation as resolved.
Show resolved Hide resolved
if self.extras.get("ssl", False):
potiuk marked this conversation as resolved.
Show resolved Hide resolved
self.allow_insecure = False

def __enter__(self):
return self

Expand All @@ -96,14 +99,8 @@ def get_conn(self) -> MongoClient:
# Mongo Connection Options dict that is unpacked when passed to MongoClient
options = self.extras

# If we are using SSL disable requiring certs from specific hostname
if options.get("ssl", False):
if pymongo.__version__ >= "4.0.0":
# In pymongo 4.0.0+ `tlsAllowInvalidCertificates=True`
# replaces `ssl_cert_reqs=CERT_NONE`
options.update({"tlsAllowInvalidCertificates": True})
else:
options.update({"ssl_cert_reqs": CERT_NONE})
# set the tlsAllowInvalidCertificates based on allow_insecure
options["tlsAllowInvalidCertificates"] = self.allow_insecure

self.client = MongoClient(self.uri, **options)
return self.client
Expand Down