问题描述
我在/ healthcheck中可以访问python37标准的env应用程序引擎URL。
我已经有一个服务帐户密钥已生成并保存为json。
具有所有预期字段:
"project_id": "","private_key_id": "","private_key": "","client_email": "project@appspot.gserviceaccount.com","client_id": "","auth_uri": "https://accounts.google.com/o/oauth2/auth","token_uri": "https://oauth2.googleapis.com/token","auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs","client_x509_cert_url": ""
我想用两种不同的方式调用网址:
- 手动请求我的URL,并以某种方式将服务帐户字段用作身份验证请求中的标头。
- 安排要运行的cron作业调用相同的URL。
我在Google Cloud上找到的所有文档仅讨论App Engine网址请求下游的身份验证。是否有一种标准方法将App Engine网址请求限制为仅一个服务帐户?
解决方法
不是使用Google Cloud提供的任何服务来确保安全。我只是用一个已编好的api密钥创建了一个参数,并将所有连接强制为https。
在main.py中,我使用了烧瓶功能@ app.before_request来检查cron执行或变量。
@app.before_request
def do_something_whenever_a_request_comes_in():
if 'X-Appengine-Cron' in request.headers:
if not request.headers['X-Appengine-Cron']:
return 'Not Authorized',401
elif 'apikey' in request.args:
print (request.args['apikey'])
if (request.args['apikey'] != config.apikey):
return 'Not Authorized',401
else:
return 'Not Authorized',401
print('Passed authorization')