禁用SSL验证以在IronPython中发布数据

问题描述 投票:1回答:1

我们最近迁移到了一个新的开发平台。我们仍在为整个工作流程设置一些部分,以便无问题地工作。我们有一个问题,我们需要将项目提升为生产,但我们不断在服务器中收到错误。我们的团队不熟悉IronPython以轻松解决这个问题。

我正在编辑有关此脚本的脚本。原始脚本如下:(请注意,我已编辑/删除机密和不必要的部分)

def callWebService(URI, setProjectState): 
    job = jobs[0]
    job.AddNote(0, job.CurrentVersion, ('%s.' % (job.Id)))

    PARAMETERS='{"id": "%s", "someData": "%s"}' % (job.Id, setProjectState)

    from System.Net import WebRequest
    request = WebRequest.Create(URI)
    request.ContentType = "application/json"
    request.Method = "POST"

    from System.Text import Encoding
    bytes = Encoding.ASCII.GetBytes(PARAMETERS)
    request.ContentLength = bytes.Length
    reqStream = request.GetRequestStream()
    reqStream.Write(bytes, 0, bytes.Length)
    reqStream.Close()

    response = request.GetResponse()

    from System.IO import StreamReader
    result = StreamReader(response.GetResponseStream()).ReadToEnd()
    print result
    return; 

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12   

callWebService('https://somesite.com/needtoposthere', 'Production')

新平台的支持告诉我们,为了解决这个问题,我们需要绕过ssl验证部分,因为只有在我们的工作流程的这一部分中,我们才会将数据发布到HTTPS网址,因为它是生产的。

我尝试了很多方法,比如添加ff代码:

import ssl

try:
    _create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
    pass
else:
    ssl._create_default_https_context = _create_unverified_https_context

还试图按照支持的建议插入这个:

from System.Net import ServicePointManager
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

from System.Net import ServicePointManager
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072

我也尝试过使用ssl库的verify = false,但仍然会遇到错误。

对于第一个解决方案,我得到的错误是ssl模块似乎无法导入。日志显示错误module named "ssl" cannot be found".我尝试声明导入像其他导入声明一样导入:from System.Net import ssl但仍然得到相同的错误。

对于第二个解决方案,即使已成功导入ServicePointManager,脚本也无法识别SecurityProtocolType

我不明白为什么我似乎无法导入甚至Python的内置库(ssl)。请理解我发布的脚本是我们唯一可以修改的脚本,因为我们根本无法访问其他脚本。

.net ssl ironpython servicepointmanager aaa-security-protocol
1个回答
0
投票

当使用.NET的WebRequest时,您绕过了可能在标准python中的所有SSL / TLS基础结构,并且您需要在.NET端更改SSL / TLS设置。

鉴于未找到模块ssl,它不在模块查找路径上或与IronPython不兼容(因为它可能是原生的)。

警告:以下示例禁用所有证书验证,并且不适合生产此外,它仅将SSL / TLS版本限制为1.2。

from System.Net import ServicePointManager, SecurityProtocolType
ServicePointManager.ServerCertificateValidationCallback = lambda sender, certificate, chain, sslPolicyErrors: True
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

如果可能,您应该避免完全禁用证书验证,并至少进行某种指纹检查,这意味着您固定并且只允许一个预期的,不完全有效的开发证书。这可能看起来像

def certificateCheck(sender, certificate, chain, sslPolicyErrors):
    # check for certificate whitelist, specific root certificate etc.
    # print certificate
    return certificate.Thumbprint == "..."

ServicePointManager.ServerCertificateValidationCallback = certificateCheck
© www.soinside.com 2019 - 2024. All rights reserved.