| 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 |
| 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 |
| 17 |
| 18 |
| 19 def is_development_server(): |
| 20 return os.environ['SERVER_SOFTWARE'].startswith('Development') |
| 21 |
| 22 |
| 23 class VMHandler(webapp2.RequestHandler): |
| 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) |
| 28 |
| 29 def post(self): |
| 30 requester_id = self.request.headers.get('X-Appengine-Inbound-Appid', None) |
| 31 task_name = self.request.headers.get('X-AppEngine-TaskName', None) |
| 32 my_id = app_identity.get_application_id() |
| 33 authorized = is_development_server() or task_name or requester_id == my_id |
| 34 if not authorized: |
| 35 self.abort(403) |
| 36 # logging.debug('Received POST /vm: %s', |
| 37 # common.payload_stats(self.request.body)) |
| 38 data = common.get_data() |
| 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']) |
| 48 http_auth = credentials.authorize(http_auth) |
| 49 def callback(_response, _content): |
| 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), |
| 70 ], debug=True) |
| OLD | NEW |