简体   繁体   English

适用于Google App Engine的Secured Cron Job无法启动(Node js)

[英]Secured Cron Job for Google App Engine fails to start (Node js)

I am struggling with a cron job with secured URL in google app engine. 我正努力在Google App Engine中使用具有安全URL的Cron作业。 As soon as I add login:admin in the app.yaml , the job fails to start (otherwise it works like a charm). 一旦我在app.yaml中添加login:admin ,该作业便无法启动(否则它将像超级按钮一样工作)。

Here is my code: 这是我的代码:

  • app.js app.js

     const express = require('express'); const app = express(); app.get('/', (req, res) => { res.status(200).send('Hello, world!').end(); }); app.get('/admin', function(req, res){ res.status(200).send('Hello, admin!').end(); }); // Start the server const PORT = process.env.PORT || 8080; app.listen(PORT, () => { console.log(`App listening on port ${PORT}`); console.log('Press Ctrl+C to quit.'); }); 

  • app.yaml app.yaml

     runtime: nodejs env: flex handlers: - url: / script: app.js - url: /admin login: admin script: app.js 

  • cron.yaml cron.yaml

     cron: - description: daily summary job url: /admin schedule: every 12 mins target: default 

And the result: cron failure 结果: cron失败

Any hint on what I am missing? 关于我所缺少的任何提示吗?

Thanks a lot in advance 提前谢谢

The login setting under handlers is now deprecated for the App Engine flexible environment. 现在,不赞成在App Engine灵活环境中使用handlers下的login设置。 https://cloud.google.com/appengine/docs/flexible/nodejs/upgrading https://cloud.google.com/appengine/docs/flexible/nodejs/升级

login: admin should be removed from your app.yaml. login: admin应该从您的app.yaml中删除。

You can still secure the url by checking for the X-Appengine-Cron header in your code. 您仍然可以通过检查代码中的X-Appengine-Cron标头来保护网址。

app.get('/admin', function(req, res){
    if (req.get('X-Appengine-Cron') !== 'true') {
         return res.status(401).end();
    }
    res.status(200).send('Hello, admin!');
});

Per the docs 根据文档

The X-Appengine-Cron header is set internally by Google App Engine. X-Appengine-Cron标头由Google App Engine在内部设置。 If your request handler finds this header it can trust that the request is a cron request. 如果您的请求处理程序找到此标头,则可以相信该请求是cron请求。 If the header is present in an external user request to your app, it is stripped, except for requests from logged in administrators of the application, who are allowed to set the header for testing purposes. 如果标头出现在对您的应用程序的外部用户请求中,则会除去该标头,但已登录应用程序管理员的请求除外,该管理员可以设置标头进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM