- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个简单的 Web 服务器,它通过 HTTPS 提供内容:
sslContext = ssl.DefaultOpenSSLContextFactory(
'/home/user/certs/letsencrypt-privkey.pem',
'/home/user/certs/letsencrypt-fullchain.pem',
)
reactor.listenSSL(
port=https_server_port,
factory=website_factory,
contextFactory=sslContext,
interface=https_server_interface
)
do_print(bcolors.YELLOW + 'server.py | running https server on ' + https_server_interface + ':' + str(https_server_port) + bcolors.END)
是否可以即时重新加载证书(例如通过调用类似 https://example.com/server/reload-certificates 的路径并让它执行一些代码)或者我需要做什么才能完成它?
我想避免重启 Python 进程。
最佳答案
可以通过多种方式实现。 Daniel F 的回答非常好,展示了一种用于动态重新配置服务器的良好通用技术。
这里有一些更具体的 Twisted 中的 TLS 支持技术。
首先,您可以从 DefaultOpenSSLContextFactory
实例重新加载 OpenSSL“上下文”对象。当需要重新加载证书时,运行:
sslContext._context = None
sslContext.cacheContext()
cacheContext
调用将创建一个新的 OpenSSL 上下文,并在此过程中重新读取证书文件。这确实有依赖私有(private)接口(interface) (_context
) 及其与非真正公共(public)接口(interface) (cacheContext
) 交互的缺点。
您还可以实现您自己的 DefaultOpenSSLContextFactory
版本,这样您就不必依赖这些东西。 DefaultOpenSSLContextFactory
并没有做太多事情。这是完全删除缓存行为的复制/粘贴/编辑:
class DefaultOpenSSLContextFactory(ContextFactory):
"""
L{DefaultOpenSSLContextFactory} is a factory for server-side SSL context
objects. These objects define certain parameters related to SSL
handshakes and the subsequent connection.
"""
_context = None
def __init__(self, privateKeyFileName, certificateFileName,
sslmethod=SSL.SSLv23_METHOD, _contextFactory=SSL.Context):
"""
@param privateKeyFileName: Name of a file containing a private key
@param certificateFileName: Name of a file containing a certificate
@param sslmethod: The SSL method to use
"""
self.privateKeyFileName = privateKeyFileName
self.certificateFileName = certificateFileName
self.sslmethod = sslmethod
self._contextFactory = _contextFactory
def getContext(self):
"""
Return an SSL context.
"""
ctx = self._contextFactory(self.sslmethod)
# Disallow SSLv2! It's insecure! SSLv3 has been around since
# 1996. It's time to move on.
ctx.set_options(SSL.OP_NO_SSLv2)
ctx.use_certificate_file(self.certificateFileName)
ctx.use_privatekey_file(self.privateKeyFileName)
当然,这会为每个可能不需要的连接重新加载证书文件。您可以添加自己的缓存逻辑,并使用适合您的证书刷新系统的控制界面。这也有一个缺点,即 DefaultOpenSSLContextFactory
并不是一个非常好的 SSL 上下文工厂。它不遵循 TLS 配置的当前最佳实践。
所以您可能真的想改用 twisted.internet.ssl.CertificateOptions
。这有一个类似的 _context
缓存,您可以清除它:
sslContext = CertificateOptions(...) # Or PrivateCertificate(...).options(...)
...
sslContext._context = None
当它发现它是 None
时,它会自动重新生成上下文,所以至少你不必以这种方式调用 cacheContext
。但是您又依赖于私有(private)接口(interface)。
另一种与 Daniel F 的建议更相似的技术是为已经在监听 的套接字提供一个新工厂。这避免了 stopListening
和 listenSSL
之间的短暂服务中断。这将是这样的:
from twisted.protocols.tls import TLSMemoryBIOFactory
# DefaultOpenSSLContextFactory or CertificateOptions or whatever
newContextFactory = ...
tlsWebsiteFactory = TLSMemoryBIOFactory(
newContextFactory,
isClient=False,
websiteFactory,
)
listeningPortFileno = sslPort.fileno()
websiteFactory.sslPort.stopReading()
websiteFactory.sslPort = reactor.adoptStreamPort(
listeningPortFileno,
AF_INET,
tlsWebsiteFactory,
)
这基本上只是让 react 堆停止为具有过时配置的旧 sslPort
提供服务,并告诉它开始为新工厂上该端口的底层套接字提供服务事件。在这种方法中,您必须下降到级别稍低的 TLS 接口(interface),因为您不能采用“TLS 端口”,因为没有这样的东西。相反,您采用 TCP 端口并应用必要的 TLS 包装自己(这正是 listenSSL 在幕后为您所做的)。
请注意,此方法比其他方法有一些限制,因为并非所有反应器都提供 fileno
或 adoptStreamPort
方法。如果您想在支持它的地方使用它并在其他地方优雅地降级,您可以测试各种对象提供的接口(interface)。
另请注意,由于 TLSMemoryBIOFactory
是它始终在幕后工作的方式,您还可以调整它的私有(private)接口(interface),如果您有对它的引用:
tlsMemoryBIOFactory._connectionCreator = IOpenSSLServerConnectionCreator(
newContextFactory,
)
它将开始使用它来建立新的连接。但是,再次,私有(private)...
关于 python 扭曲 : Is it possible to reload certificates on the fly?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57812501/
我们想在应用商店中发布一个应用。为我们构建它的第 3 方需要我们通过苹果开发门户创建的证书和配置文件。根据文档,创建证书的方法是使用 mac 的钥匙串(keychain)应用程序,然后选择“从证书颁发
我正在尝试使用 Java Http 客户端连接到服务器以进行 Web 服务调用。如果我用下面的代码打开网络调试.. System.setProperty("javax.net.debug", "all
我在尝试推送我的更改时才开始收到此错误。我不知道我的系统发生了什么变化,并且在这方面不应该有任何自签名证书。 Git 已卸载并重新安装。 Git 似乎使用了正确的包: http.sslcainfo=C
除非我设置以下内容,否则我会收到上述错误: curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 这是不安全的,违背了 SSL 的目的。 我已经从 http
我有一个基于 Linux 的 Docker 容器,如果我这样做: curl https://google.com ...然后我得到一个错误: curl: (60) SSL certificate pr
我想在我的 Linux 计算机上安装 nvm。(我的 Debian 版本是 10,Git 版本是 2.27。OPENSSL 版本是 1.1.1d 2019 年 9 月 10 日) 我阅读了这份文件 h
我正在使用 Symfony 5,我需要使用 composer 安装 'knplabs/knp-snappy-bundle' 但我有这个消息: [Composer\Downloader\Transpor
我已经通过 VSTS 部署我的应用程序一段时间了,然后突然出现此错误: 无法访问 SSL 证书问题:(url) 证书链中的自签名证书。 为什么突然发生这种情况,我该如何解决? 更新:我注意到我有一个新
我正在尝试使用安全性配置 WCF 服务。我生成了 2 个存储在 LocalComputer\Personal Certificates 中的证书(用于服务器和客户端)。我的配置是: 服务器:
我正在创建连接到的 Azure 函数来执行 PnP 命令。我已经创建了 docs 中提到的证书。我总是收到Cannot findcertificate with this指纹在证书存储中。Except
美好的一天,我是服务器设置的新手。我目前正在使用 laravel 5.4 来集成我的 quickbooks app在我的实时服务器上 http://qb.dagnum.com/connect但我继续收
我正在尝试从 Github 克隆一个项目但我无法克隆它,因为我有这个错误 无法访问:SSL 证书问题:证书链中的自签名证书 我可以从我的网络访问并且我有证书。 我的Android Studio有什么问
我正在尝试使用 AWS-CLI 检索 aws elasticbeanstalk 详细信息,但出现以下错误。 错误信息: C:\abdul>aws elasticbeanstalk describe-e
我实际上正在阅读有关证书和证书链的内容。我了解证书是由实体的私钥签名的一段数据,只能使用给定实体(例如根 CA)的公钥解密。 但是,我在几个地方看到“证书签署另一个证书”(例如:Microsoft I
我想默认“接受”新证书。我尝试了以下方法。 $ dpkg-reconfigure -f noninteractive ca-certificates 它运行,但没有添加 CA。 如果不行,直接修改/e
有人通过AWS Certificate Manager为他们的域名购买了通配符证书,我需要将其转移到Heroku,以获取使用域名子域名的应用程序。 无论是通过AWS控制台还是通过其CLI,我都找不到如
我可以通过重新启动我的 Windows 7 来运行 selenium 服务器。但是,如果我终止服务器并再次启动它,我将收到此错误。有时执行 webdriver-manager update--igno
我在 IE9 中遇到安全证书问题。 然后我去我得到的特定地址 There is a problem with this website's security certificate. 如何避免出现此窗
应用签名证书和上传证书有什么区别? 我在将 Google Play 游戏与我的应用程序集成时遇到了问题(我将此作为另一个问题发布),我注意到用于在 Google API 控制台上自动生成的客户端 ID
我正在尝试在 Mac 上使用 Heroku CLI。 当我尝试使用 Heroku login 登录 Heroku 并提供我的凭据时,出现以下错误: Error: self signed certifi
我是一名优秀的程序员,十分优秀!