应用引擎无法通过云 nat 静态 IP 地址重定向流量
app engine unable to redirect traffic via cloud nat static ip address
我正在尝试使用应用引擎标准使用客户端的本地 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
当我 运行 我的应用程序使用 App Engine 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."
我还使用与 App Engine 中相同的代码创建了云函数来进行测试,令人惊讶的是,电子邮件已毫无问题地发送给了预期的收件人。当我检查云 NAT 日志时,它包含通过云功能触发时的所有详细信息(简而言之,它使用静态 ip 地址),但没有与应用引擎触发器相关的日志。所以我认为我的应用程序引擎流量不是通过静态 IP 地址传输的,并且不确定如何在 app.yaml
中提及它
电子邮件功能也可能存在代码问题,但由于它在云功能中工作,我真的怀疑我的 app.yaml 而不是电子邮件 python 代码。非常感谢任何帮助
我了解到您的 SMTP IP 是 public。有一个关于无服务器 VPC 连接器的注意事项。
使用 Cloud Function, and Cloud Run,您可以选择是仅私有 IP 还是 Public 和私有 IP 通过无服务器 VPC 连接器路由
对于 App Engine,我没有找到出口控制的明确描述,但我猜只有私有 IP (RFC1918) 通过 VPC 路由,而不是 public。因此,您的 Cloud Nat 未被使用,因此您在学校的 SMTP 服务器上未获得授权。
编辑 1:
你有 3 个解决方案来解决这个问题
- 您可以创建 App Engine 在您需要发送电子邮件时调用的 Cloud Functions(或 Cloud 运行 服务)。
- 您可以从 App Engine 切换到 Cloud 运行(使用新的测试命令
gcloud beta run deploy --source=. --region=<REGION> --platform=managed <Service Name>
)。像这样,您可以像使用 App Engine 一样进行部署。使用与 App Engine 相同的容器引擎构建器 (Buildpack)。您必须调整 app.yaml
文件的内容(如果您需要帮助,请分享)。但是,到目前为止,IAP 与 Cloud 运行 不兼容。如果你想使用它,请稍等!
- 在您的 VPC 和学校网络之间创建一个 VPN。像这样,您将使用私有 IP 调用您的 SMTP 服务器。在 smtp 服务器上,仅授予无服务器 VPC 连接器范围以访问它。而且您不再需要 Cloud NAT 配置。
我正在尝试使用应用引擎标准使用客户端的本地 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
当我 运行 我的应用程序使用 App Engine 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."
我还使用与 App Engine 中相同的代码创建了云函数来进行测试,令人惊讶的是,电子邮件已毫无问题地发送给了预期的收件人。当我检查云 NAT 日志时,它包含通过云功能触发时的所有详细信息(简而言之,它使用静态 ip 地址),但没有与应用引擎触发器相关的日志。所以我认为我的应用程序引擎流量不是通过静态 IP 地址传输的,并且不确定如何在 app.yaml
中提及它电子邮件功能也可能存在代码问题,但由于它在云功能中工作,我真的怀疑我的 app.yaml 而不是电子邮件 python 代码。非常感谢任何帮助
我了解到您的 SMTP IP 是 public。有一个关于无服务器 VPC 连接器的注意事项。
使用 Cloud Function, and Cloud Run,您可以选择是仅私有 IP 还是 Public 和私有 IP 通过无服务器 VPC 连接器路由
对于 App Engine,我没有找到出口控制的明确描述,但我猜只有私有 IP (RFC1918) 通过 VPC 路由,而不是 public。因此,您的 Cloud Nat 未被使用,因此您在学校的 SMTP 服务器上未获得授权。
编辑 1:
你有 3 个解决方案来解决这个问题
- 您可以创建 App Engine 在您需要发送电子邮件时调用的 Cloud Functions(或 Cloud 运行 服务)。
- 您可以从 App Engine 切换到 Cloud 运行(使用新的测试命令
gcloud beta run deploy --source=. --region=<REGION> --platform=managed <Service Name>
)。像这样,您可以像使用 App Engine 一样进行部署。使用与 App Engine 相同的容器引擎构建器 (Buildpack)。您必须调整app.yaml
文件的内容(如果您需要帮助,请分享)。但是,到目前为止,IAP 与 Cloud 运行 不兼容。如果你想使用它,请稍等! - 在您的 VPC 和学校网络之间创建一个 VPN。像这样,您将使用私有 IP 调用您的 SMTP 服务器。在 smtp 服务器上,仅授予无服务器 VPC 连接器范围以访问它。而且您不再需要 Cloud NAT 配置。