将视图限制为只能由App Engine内部网络访问

时间:2013-02-12 04:38:22

标签: python google-app-engine flask

我想找到一种方法来限制视图(请求处理程序)只能在我的视图中从Google App Engine内部网络中调用,而不是在app.yaml中调用。

例如,我有一个处理我的Flask应用程序中的入站电子邮件的视图

@app.route('/_ah/mail/notifications@example.appspotmail.com', methods=['POST'])
def inbound_notification_email():
    from google.appengine.api import mail
    message = mail.InboundEmailMessage(request.data)
    ...
    return '' # 200 OK

虽然我知道我可以将所有邮件处理程序放在他们自己的文件/ wsgi实例中,如下所示:

handlers:
- url: /_ah/mail/.+
  script: inbound_mail.app
  login: admin

我不想这样做,因为我使用的是Flask而不是Webapp。现在,请求的工作方式与上面的设置相同,但它暴露给世界。

检查我的inbound_notification_email()视图的请求,我看到请求标头中的X-App-Country设置为ZZ,请求的远程地址为0.1.0.20。我知道0.x.x.x IP范围是为本地网络保留的IANA,因此检查request.remote_address是否以“0”开头似乎是合乎逻辑的。可以工作,但我不确定App Engine中的所有内部请求是否总是以这种方式处理(推送队列和xmpp会浮现在脑海中)。

我很惊讶地看到users.is_current_user_admin()inbound_notification_mail()内返回False,即使您在使用Webapp时设置login: admin也是如此。

0 个答案:

没有答案