在 Django 中禁用 SSL 验证 Sentry Raven

Disable SSL verification Sentry Raven in Django

我想添加一个具有自签名证书的测试哨兵实例。

应用有默认值RAVEN_CONFIG

RAVEN_CONFIG = {
    'dsn': 'https://xxxx@sentry.tst2.server.com/2',
    # If you are using git, you can also automatically configure the
    # release based on the git info.
    'release': raven.fetch_git_sha(os.path.dirname(os.pardir)),
}

我尝试将 'verify_ssl':0 添加到配置字典中,但无济于事。

这是我遇到的错误:

Traceback (most recent call last):
  File "/opt/apps/.virtualenvs/palantir/local/lib/python2.7/site-packages/raven/transport/threaded.py", line 162, in send_sync
    super(ThreadedHTTPTransport, self).send(data, headers)
  File "/opt/apps/.virtualenvs/palantir/local/lib/python2.7/site-packages/raven/transport/http.py", line 47, in send
    ca_certs=self.ca_certs,
  File "/opt/apps/.virtualenvs/palantir/local/lib/python2.7/site-packages/raven/utils/http.py", line 62, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 431, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 449, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/opt/apps/.virtualenvs/palantir/local/lib/python2.7/site-packages/raven/utils/http.py", line 46, in https_open
    return self.do_open(ValidHTTPSConnection, req)
  File "/usr/lib/python2.7/urllib2.py", line 1197, in do_open
    raise URLError(err)
URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)>

您能否尝试添加 verify_ssl=0 to your DSN,如文档所示:

https://xxxx@sentry.tst2.server.com/2?verify_ssl=0

覆盖哨兵-python的功能

def disable_sentry_ssl_check():
    # disable sni warnings
    import urllib3
    urllib3.disable_warnings()

    def _get_pool_options(self, ca_certs):
        return {
            "num_pools": 2,
            "cert_reqs": "CERT_NONE",
        }

    # disable ssl check
    from sentry_sdk.transport import HttpTransport
    HttpTransport._get_pool_options = _get_pool_options

这对我有用:

from raven.transport.requests import RequestsHTTPTransport
RAVEN_CONFIG = {
'transport': RequestsHTTPTransport, 
'dsn': DSN 
}

注意:是同步调用。

settings.py中输入以下代码:

# Sentry
# https://docs.sentry.io/platforms/python/guides/django/

def _get_pool_options(self, ca_certs):
    return {
        'num_pools': 2,
        'cert_reqs': 'CERT_NONE'
    }

if sentry_dsn := os.getenv('SENTRY_DSN'):
    sentry_sdk.transport.HttpTransport._get_pool_options = _get_pool_options
    sentry_sdk.init(dsn=sentry_dsn, integrations=[DjangoIntegration()], send_default_pii=True)

requirements.txt 文件:

Django==3.1.1
sentry-sdk==0.19.4