|
64 | 64 | if DATABASES["default"]["ENGINE"].endswith("mysql"): # noqa F405 |
65 | 65 | DATABASES["default"]["OPTIONS"] = {"charset": "utf8mb4"} # noqa F405 |
66 | 66 |
|
| 67 | +# SSL/mTLS options for PostgreSQL connections. |
| 68 | +# When NAUTOBOT_DB_SSLMODE is set to "verify-ca" or "verify-full", the client |
| 69 | +# certificate, key, and CA root cert must be present at the configured paths. |
| 70 | +_db_sslcert = os.getenv("NAUTOBOT_DB_SSLCERT", "/etc/nautobot/mtls/tls.crt") |
| 71 | +_db_sslkey = os.getenv("NAUTOBOT_DB_SSLKEY", "/etc/nautobot/mtls/tls.key") |
| 72 | +_db_sslrootcert = os.getenv("NAUTOBOT_DB_SSLROOTCERT", "/etc/nautobot/mtls/ca.crt") |
| 73 | +_db_sslmode = os.getenv("NAUTOBOT_DB_SSLMODE", "") |
| 74 | + |
| 75 | +if _db_sslmode in ("verify-ca", "verify-full"): |
| 76 | + for _path, _label in [ |
| 77 | + (_db_sslcert, "NAUTOBOT_DB_SSLCERT"), |
| 78 | + (_db_sslkey, "NAUTOBOT_DB_SSLKEY"), |
| 79 | + (_db_sslrootcert, "NAUTOBOT_DB_SSLROOTCERT"), |
| 80 | + ]: |
| 81 | + if not os.path.isfile(_path): |
| 82 | + raise FileNotFoundError( |
| 83 | + f"SSL certificate file required by {_label} not found: {_path}" |
| 84 | + ) |
| 85 | + DATABASES["default"]["OPTIONS"] = { # noqa F405 |
| 86 | + "sslmode": _db_sslmode, |
| 87 | + "sslcert": _db_sslcert, |
| 88 | + "sslkey": _db_sslkey, |
| 89 | + "sslrootcert": _db_sslrootcert, |
| 90 | + } |
| 91 | + |
| 92 | +# SSL/mTLS options for Redis connections. |
| 93 | +# When NAUTOBOT_REDIS_SSL env var is "true" (set by Helm `nautobot.redis.ssl`), |
| 94 | +# the Helm chart switches the URL scheme to rediss://. We still need to tell |
| 95 | +# the Python redis client *which* certs to use for mutual TLS. |
| 96 | +import ssl as _ssl # noqa: E402 |
| 97 | + |
| 98 | +_redis_ca = os.getenv("NAUTOBOT_REDIS_SSL_CA_CERTS", "/etc/nautobot/mtls/ca.crt") |
| 99 | +_redis_cert = os.getenv("NAUTOBOT_REDIS_SSL_CERTFILE", "/etc/nautobot/mtls/tls.crt") |
| 100 | +_redis_key = os.getenv("NAUTOBOT_REDIS_SSL_KEYFILE", "/etc/nautobot/mtls/tls.key") |
| 101 | + |
| 102 | +if os.path.isfile(_redis_ca): |
| 103 | + _redis_ssl_kwargs = { |
| 104 | + "ssl_cert_reqs": _ssl.CERT_REQUIRED, |
| 105 | + "ssl_ca_certs": _redis_ca, |
| 106 | + "ssl_certfile": _redis_cert, |
| 107 | + "ssl_keyfile": _redis_key, |
| 108 | + } |
| 109 | + CACHES["default"].setdefault("OPTIONS", {}) # noqa F405 |
| 110 | + CACHES["default"]["OPTIONS"].setdefault("CONNECTION_POOL_KWARGS", {}) # noqa F405 |
| 111 | + CACHES["default"]["OPTIONS"]["CONNECTION_POOL_KWARGS"].update(_redis_ssl_kwargs) # noqa F405 |
| 112 | + CELERY_BROKER_USE_SSL = _redis_ssl_kwargs # noqa F405 |
| 113 | + CELERY_REDIS_BACKEND_USE_SSL = _redis_ssl_kwargs # noqa F405 |
| 114 | + CELERY_BROKER_TRANSPORT_OPTIONS = {"ssl": _redis_ssl_kwargs} # noqa F405 |
| 115 | + |
67 | 116 | # This key is used for secure generation of random numbers and strings. It must never be exposed outside of this file. |
68 | 117 | # For optimal security, SECRET_KEY should be at least 50 characters in length and contain a mix of letters, numbers, and |
69 | 118 | # symbols. Nautobot will not run without this defined. For more information, see |
|
0 commit comments