OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import googleapiclient.http | |
6 import json | |
7 import httplib2 | |
ghost stip (do not use)
2015/04/14 00:36:43
nit: alphabetize
Sergey Berezin (google)
2015/04/16 04:39:07
Done.
| |
8 import logging | |
9 import os | |
10 import traceback | |
11 import webapp2 | |
12 | |
13 from google.appengine.api import app_identity | |
14 from google.appengine.ext import ndb | |
15 | |
16 import common | |
ghost stip (do not use)
2015/04/14 00:36:44
you should rename this to be more specific. common
Sergey Berezin (google)
2015/04/16 04:39:07
I agree it's a lousy name. I just can't think of a
| |
17 | |
18 | |
19 def is_development_server(): | |
20 return os.environ['SERVER_SOFTWARE'].startswith('Development') | |
21 | |
22 | |
23 class VMHandler(webapp2.RequestHandler): | |
ghost stip (do not use)
2015/04/14 00:36:44
it pleases me how small this file is :)
Sergey Berezin (google)
2015/04/16 04:39:08
You should've seen the perl version :-)
| |
24 def get(self): | |
25 msg = 'This endpoint is for internal POST requests only.\n' | |
26 self.response.headers['Content-Type'] = 'text/plain' | |
27 self.response.out.write(msg) | |
ghost stip (do not use)
2015/04/14 00:36:43
self.response.set_status(403) ?
Sergey Berezin (google)
2015/04/16 04:39:08
Actually, I just removed get(). It was there for d
| |
28 | |
29 def post(self): | |
30 requester_id = self.request.headers.get('X-Appengine-Inbound-Appid', None) | |
ghost stip (do not use)
2015/04/14 00:36:44
isn't None default? I think you can just do self.r
Sergey Berezin (google)
2015/04/16 04:39:08
Done.
| |
31 task_name = self.request.headers.get('X-AppEngine-TaskName', None) | |
ghost stip (do not use)
2015/04/14 00:36:44
same
Sergey Berezin (google)
2015/04/16 04:39:08
Done.
| |
32 my_id = app_identity.get_application_id() | |
33 authorized = is_development_server() or task_name or requester_id == my_id | |
ghost stip (do not use)
2015/04/14 00:36:43
nit: might want to make the 'is me' check more obv
Sergey Berezin (google)
2015/04/16 04:39:08
Done.
| |
34 if not authorized: | |
35 self.abort(403) | |
36 # logging.debug('Received POST /vm: %s', | |
ghost stip (do not use)
2015/04/14 00:36:43
nit: remove commented code
Sergey Berezin (google)
2015/04/16 04:39:08
Done.
| |
37 # common.payload_stats(self.request.body)) | |
38 data = common.get_data() | |
ghost stip (do not use)
2015/04/14 00:36:43
can you rename get_data to be more specific? what
Sergey Berezin (google)
2015/04/16 04:39:07
Renamed, and moved the function to vm_module.py.
| |
39 if not data: | |
40 self.abort_admin_error('Endpoint data is not set') | |
41 if not all(f in data for f in ['credentials', 'url']): | |
42 self.abort_admin_error('Missing required fields: credentials, url') | |
43 | |
44 url = data['url'] | |
45 http_auth = httplib2.Http() | |
46 if not is_development_server(): | |
47 credentials = common.get_credentials(data['credentials'], data['scopes']) | |
ghost stip (do not use)
2015/04/14 00:36:43
if data came from common, why do I have to pass i
Sergey Berezin (google)
2015/04/16 04:39:07
This function is now also in vm_module.py. It's no
| |
48 http_auth = credentials.authorize(http_auth) | |
49 def callback(_response, _content): | |
ghost stip (do not use)
2015/04/14 00:36:44
I'm surprised this is needed. can you just set the
Sergey Berezin (google)
2015/04/16 04:39:08
Good idea, added logging for an error in response.
| |
50 pass | |
51 # Important: set content-type to binary, otherwise httplib2 mangles it. | |
52 data.setdefault('headers', {}).update({ | |
53 'content-length': str(len(self.request.body)), | |
54 'content-type': 'application/x-protobuf', | |
55 }) | |
56 request = googleapiclient.http.HttpRequest( | |
57 http_auth, callback, url, method='POST', body=self.request.body, | |
58 headers=data['headers']) | |
59 request.execute() | |
60 | |
61 def abort_admin_error(self, message): | |
62 logging.error('%s; please visit https://%s/admin/', | |
63 message, app_identity.get_default_version_hostname()) | |
64 self.abort(500) | |
65 | |
66 | |
67 logging.basicConfig(level=logging.DEBUG) | |
68 app = webapp2.WSGIApplication([ | |
69 (r'/vm', VMHandler), | |
ghost stip (do not use)
2015/04/14 00:36:44
probably don't need the r here
Sergey Berezin (google)
2015/04/16 04:39:08
Done.
| |
70 ], debug=True) | |
OLD | NEW |