首页 > 解决方案 > 应用引擎无法通过云 nat 静态 IP 地址重定向流量

问题描述

我正在尝试使用客户端的本地 SMTP 服务器使用应用引擎标准发送电子邮件。为此,我们在默认网络中创建了无服务器 VPC 访问连接器,并使用静态 IP 地址创建了 Cloud NAT 以发送出口流量。客户端已将静态 IP 地址和端口列入白名单。以下是应用引擎中的代码片段

    msg.set_content('This is a HTML email')

    msg.add_alternative(cleared_html_content, subtype='html')
    try:
        context = ssl._create_unverified_context()
        print("starting conectn")
        with smtplib.SMTP('xx.xxxx.edu', 2525) as server:
            server.starttls(context=context)
            server.send_message(msg)
        print("sent almost")
    except Exception as e:
        print('Error: ', e)

以下是 app.yaml

runtime: python37
entrypoint: gunicorn -t 120 -b :$PORT main:app
vpc_access_connector:
  name: projects/xxxxxxxxx/locations/us-central1/connectors/yyyyyyyyy

当我使用应用程序引擎 url 运行我的应用程序时,我在日志查看器中收到以下错误

Error: (554, b"xxx.xxxxx.edu\nYour access to this mail system has been rejected due to the sending MTA's poor reputation. If you believe that this failure is in error, please contact the intended recipient via alternate means."

此外,我还使用与应用程序引擎中相同的代码创建了云功能来进行测试,并且令人惊讶的是,电子邮件被发送给了预期的收件人,没有任何问题。当我检查云 NAT 日志时,它具有通过云功能触发时的所有详细信息(简而言之,它使用静态 IP 地址)但没有与应用引擎触发器相关的日志。所以我认为我的应用引擎流量不是通过静态 IP 地址进行的,并且不知道如何在 app.yaml 中提及它

电子邮件功能中也可能存在代码问题,但由于它在云功能中工作,我真的怀疑我的 app.yaml 而不是电子邮件 python 代码。非常感谢任何帮助

标签: google-app-enginegoogle-cloud-platformgoogle-cloud-functions

解决方案


我了解到您的 SMTP IP 是公开的。使用无服务器 VPC 连接器需要注意一点。

借助Cloud FunctionCloud Run,您可以选择是仅通过无服务器 VPC 连接器路由私有 IP 还是公共和私有 IP

使用应用程序引擎,我没有找到对出口控制的明确描述,但我猜只有私有 IP(RFC1918)通过 VPC 路由,而不是公共 IP。因此,您的 Cloud Nat 未被使用,因此您在学校的 SMTP 服务器上未获得授权。


编辑1:

你有 3 个解决方案来解决这个问题

  • 您可以创建 App Engine 在需要发送电子邮件时调用的 Cloud Functions(或 Cloud Run 服务)。
  • 您可以从 App Engine 切换到 Cloud Run(使用新的 beta 命令gcloud beta run deploy --source=. --region=<REGION> --platform=managed <Service Name>)。像这样,您可以像使用 App Engine 一样进行部署。使用与 App Engine 相同的容器引擎构建器 (Buildpack)。您必须调整app.yaml文件的内容(如果需要帮助,请分享)。但是,到目前为止,IAP 不兼容 Cloud Run。如果您想使用它,请稍候!
  • 在您的 VPC 和学校网络之间创建一个 VPN。像这样,您将使用私有 IP 调用您的 SMTP 服务器。在 smtp 服务器上,仅授予无服务器 VPC 连接器范围以访问它。而且您不再需要 Cloud NAT 配置。

推荐阅读