【问题标题】:How to use TLS 1.2 with appengine/urlfetch from a Go AppEngine app如何将 TLS 1.2 与来自 Go AppEngine 应用程序的 appengine/urlfetch 一起使用
【发布时间】:2016-12-20 21:31:20
【问题描述】:

我正在编写一个 go appengine 应用程序,它需要从 Stripe 获取页面。

基本上我使用的是官方 Stripe API 附带的these instructions。但是,当我使用 dev_appserver.py 运行它时,我得到:

2016/08/14 12:03:15 Requesting POST api.stripe.com/v1/customers
2016/08/14 12:03:18 Error encountered from Stripe: {"type":"invalid_request_error","message":"Stripe no longer supports API requests made with TLS 1.0. Please initiate HTTPS connections with TLS 1.2 or later. You can learn more about this at https://stripe.com/blog/upgrading-tls.","request_id":"req_90O6reF1Mwi9yZ","status":401}

我发现 Python AppEngine 应用程序可以在我的 app.yaml 中指定要使用的 SSL 库(请参阅 SSL support)。但是,如果我在 app.yaml 文件中添加 libraries 部分,我会得到:

$ (go_appengine/dev_appserver.py app)
Traceback (most recent call last):
  File "go_appengine/dev_appserver.py", line 89, in <module>
    _run_file(__file__, globals())
  File "go_appengine/dev_appserver.py", line 85, in _run_file
    execfile(_PATHS.script_file(script_name), globals_)
  File "/Users/kchodorow/gitroot/tt/go_appengine/google/appengine/tools/devappserver2/devappserver2.py", line 1040, in <module>
    main()
  File "/Users/kchodorow/gitroot/tt/go_appengine/google/appengine/tools/devappserver2/devappserver2.py", line 1033, in main
    dev_server.start(options)
  File "/Users/kchodorow/gitroot/tt/go_appengine/google/appengine/tools/devappserver2/devappserver2.py", line 758, in start
    options.config_paths, options.app_id)
  File "/Users/kchodorow/gitroot/tt/go_appengine/google/appengine/tools/devappserver2/application_configuration.py", line 831, in __init__
    module_configuration = ModuleConfiguration(config_path, app_id)
  File "/Users/kchodorow/gitroot/tt/go_appengine/google/appengine/tools/devappserver2/application_configuration.py", line 127, in __init__
    self._config_path)
  File "/Users/kchodorow/gitroot/tt/go_appengine/google/appengine/tools/devappserver2/application_configuration.py", line 424, in _parse_configuration
    config, files = appinfo_includes.ParseAndReturnIncludePaths(f)
  File "/Users/kchodorow/gitroot/tt/go_appengine/google/appengine/api/appinfo_includes.py", line 82, in ParseAndReturnIncludePaths
    appyaml = appinfo.LoadSingleAppInfo(appinfo_file)
  File "/Users/kchodorow/gitroot/tt/go_appengine/google/appengine/api/appinfo.py", line 2191, in LoadSingleAppInfo
    listener.Parse(app_info)
  File "/Users/kchodorow/gitroot/tt/go_appengine/google/appengine/api/yaml_listener.py", line 227, in Parse
    self._HandleEvents(self._GenerateEventParameters(stream, loader_class))
  File "/Users/kchodorow/gitroot/tt/go_appengine/google/appengine/api/yaml_listener.py", line 178, in _HandleEvents
    raise yaml_errors.EventError(e, event_object)
google.appengine.api.yaml_errors.EventError: libraries entries are only supported by the "python27" runtime
  in "app/app.yaml", line 25, column 1

这是有道理的,因为我没有使用 Python。我真的需要一种方法来为 Go 设置它。

我的 app.yaml 文件如下所示:

application: app-name
version: alpha-001
runtime: go
api_version: go1

handlers:
...

runtime 更改为 python27 可以消除库错误,但显然我的 go 代码不起作用。

知道如何通过开发应用服务器和生产启用 TLS 1.2?

【问题讨论】:

  • 您不能在 appengine 上创建传输吗?像tr := &amp;http.Transport{ TLSClientConfig: &amp;tls.Config{...}, } client := &amp;http.Client{Transport: tr} 可能在 flex 环境中?
  • 不幸的是,AppEngine 要求您使用它的传输,urlfetch.Transport (AFAICT)。可能有一些方法可以覆盖这个传输的RoundTrip 正在做的事情,这就是我正在寻找的建议。
  • 我认为可以在flex environment 上完成 - 请参阅here
  • 啊哈!根据您对传输的建议,我深入研究了 RoundTrip 代码调用的内容,最终发现我可以使用普通的 http.Client,我只需要在传输中设置 Proxy: http.ProxyFromEnvironment。如果你想写一个答案,很高兴奖励你。
  • 是的,现在只使用&amp;http.Client{Transport: &amp;http.Transport{TLSClientConfig: &amp;tls.Config{MinVersion: tls.VersionTLS12,},},},不需要弄乱 urlfetch。我不知道为什么 http.Client 最初是不允许的。

标签: google-app-engine ssl go


【解决方案1】:

请看这个oracle blog post。 Appengine运行JDK7,它支持TLSv1.2,但默认协议是TLSV1

所以它首先尝试在 v1 中建立连接,如果它不支持尝试其他协议 v1.1 然后是 v1.2

据我所知,您无法将 {-Dhttps.protocols=TLSv1.2} 传递给 GAE,但您可以通过代码进行设置 {System.setProperty("https.protocols", "TLSv1.2");}

【讨论】:

  • 我不认为我可以从 Go 开始。 dev_appserver.py 调用python backend,所以我想知道如何在 Python/Go 中设置它,而不是 Java。
【解决方案2】:

来自 Go net/http 文档:

为了控制代理、TLS 配置、keep-alives、压缩和其他设置,创建一个传输:

tr := &http.Transport{
    TLSClientConfig:    &tls.Config{...},
    DisableCompression: true,
}
client := &http.Client{Transport: tr}

appengine documentation 看来,您可以像往常一样使用 net/http 包,只需稍加调整即可。

【讨论】:

    【解决方案3】:

    您应该创建并设置具有 TLS 配置并使用 AppEngine 服务的新传输。可以通过将默认拨号功能替换为 AppEngine SDK(带上下文)定义的功能来完成。

    // NewClient sets up an HTTP/2 client for a certificate and context
    func NewClient(ctx context.Context) (*http.Client, error) {
        config := &tls.Config{}
        transport := &http.Transport{
            TLSClientConfig: config,
            Dial: func(network, addr string) (net.Conn, error) {
                // this uses the appengine service to create the actual client
                return socket.Dial(ctx, network, addr)
            },
        }
        return &http.Client{Transport: transport}, nil
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-22
      • 1970-01-01
      • 2015-09-01
      • 2012-03-23
      • 1970-01-01
      • 2012-10-09
      • 2023-03-03
      • 1970-01-01
      • 2023-03-12
      相关资源
      最近更新 更多