从GCE实例使用时,Google App Engine Admin API无法正常工作?

时间:2015-09-07 11:11:36

标签: python google-app-engine google-cloud-platform

我正在尝试通过Python客户端库和普通cURL调用使用Google App Engine Admin API

这在本地机器上运行良好,但在GCE机器上没有那么多。

我正在尝试的cURL命令是:

$ curl -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" https://appengine.googleapis.com/v1beta4/apps/myapp
{
  "name": "apps/myapp",
  "id": "myapp"
}

这是在我的本地机器上成功执行,其中TOKEN是我个人帐户的oauth2舞蹈的结果。

GCE机器上的相同命令:

$ curl -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" https://appengine.googleapis.com/v1beta4/apps/myapp
curl: (7) Failed to connect to appengine.googleapis.com port 443: Connection timed out

无论我使用的是基于个人oauth2的令牌,还是使用从运行中获取的基于服务帐户的令牌,都会出现此行为

curl http://metadata/computeMetadata/v1/instance/service-accounts/default/token -H "Metadata-Flavor: Google"

同样在尝试使用Python客户端库时。

以下是我在Python中所做的部分片段:

gae = discovery.build('appengine', 'v1beta4')
method = gae.apps().get(appsId='myapp')
method.execute(http=my_authorized_http_instance)

my_authorized_http_instance是使用httplib2.Http()范围的授权https://www.googleapis.com/auth/cloud-platform实例。

在本地计算机上,这很好用,我得到一个描述myapp的响应。

在GCE实例上,最后一行需要很长时间才能执行,最终会引发此异常:

ResponseNotReady                          Traceback (most recent call last)
<ipython-input-9-b5373a41f867> in <module>()
----> 1 meth.execute(http=http)

/usr/local/lib/python2.7/dist-packages/oauth2client/util.pyc in positional_wrapper(*args, **kwargs)
    135         else: # IGNORE
    136           pass
--> 137       return wrapped(*args, **kwargs)
    138     return positional_wrapper
    139

/usr/local/lib/python2.7/dist-packages/googleapiclient/http.pyc in execute(self, http, num_retries)
    720
    721       resp, content = http.request(str(self.uri), method=str(self.method),
--> 722                                    body=self.body, headers=self.headers)
    723       if resp.status < 500:
    724         break

/usr/local/lib/python2.7/dist-packages/oauth2client/util.pyc in positional_wrapper(*args, **kwargs)
    135         else: # IGNORE
    136           pass
--> 137       return wrapped(*args, **kwargs)
    138     return positional_wrapper
    139

/usr/local/lib/python2.7/dist-packages/oauth2client/client.pyc in new_request(uri, method, body, headers, redirections, connection_type)
    549
    550       resp, content = request_orig(uri, method, body, clean_headers(headers),
--> 551                                    redirections, connection_type)
    552
    553       if resp.status in REFRESH_STATUS_CODES:

/usr/local/lib/python2.7/dist-packages/httplib2/__init__.pyc in request(self, uri, method, body, headers, redirections, connection_type)
   1606                     content = ""
   1607                 else:
-> 1608                     (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
   1609         except Exception, e:
   1610             if self.force_exception_to_status_code:

/usr/local/lib/python2.7/dist-packages/httplib2/__init__.pyc in _request(self, conn, host, absolute_uri, request_uri, method, body, headers, redirections, cachekey)
   1348             auth.request(method, request_uri, headers, body)
   1349
-> 1350         (response, content) = self._conn_request(conn, request_uri, method, body, headers)
   1351
   1352         if auth:

/usr/local/lib/python2.7/dist-packages/httplib2/__init__.pyc in _conn_request(self, conn, request_uri, method, body, headers)
   1304                     continue
   1305             try:
-> 1306                 response = conn.getresponse()
   1307             except httplib.BadStatusLine:
   1308                 # If we get a BadStatusLine on the first try then that means

/usr/lib/python2.7/httplib.pyc in getresponse(self, buffering)
   1037         #
   1038         if self.__state != _CS_REQ_SENT or self.__response:
-> 1039             raise ResponseNotReady()
   1040
   1041         args = (self.sock,)

ResponseNotReady:

是的,GCE实例有cloud-platform服务帐户:

$ curl -s http://metadata/computeMetadata/v1/instance/service-accounts/default/scopes -H "Metadata-Flavor: Google" | grep cloud-platform
https://www.googleapis.com/auth/cloud-platform

此外,我想弄清楚服务帐户问题是否有问题,所以我尝试在GCE实例上授权自己,而不是使用服务帐户。我得到了同样的错误。

有什么想法吗?我做错了吗?

1 个答案:

答案 0 :(得分:1)

您正在使用我们正在修复的托管虚拟机的内部错误。

目前,appengine.googleapis.com映射到VM上的内部地址,该地址用于私有VM-to-AppEngine流量,但不会将HTTPS请求转发到公共API。我们正在移动这个内部频道,以免干扰公众频道。

与此同时,您可以将appengine.googleapis.com的手动条目添加到计算机上的/etc/hosts文件中,并重新映射端口10001(用于内部通信)以指向内部传输地址,像这样:

echo '64.233.181.95 appengine.googleapis.com' >> /etc/hosts
sysctl net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -j MASQUERADE
iptables -t nat -A PREROUTING -p tcp -d 64.18.0.0/20,64.233.160.0/19,66.102.0.0/20,66.249.80.0/20,72.14.192.0/18,74.125.0.0/16,108.177.8.0/21,173.194.0.0/16,207.126.144.0/20,209.85.128.0/17,216.58.192.0/19,216.239.32.0/19 --dport 10001 -j DNAT --to-destination 169.254.169.253:10001

(IP地址集来自_spf.google.com记录。)

这仅影响已启用托管VM的项目。我们预计很快就会准备好修复(几周)。

UPD :在上面的命令中修复了appengine.googleapis.com域名