我正在使用 Google App Engine 和 PyCrypto 进行一些加密。我得到的错误如下所示,仅发生在运行 Linux Mint Maya (13)的本地开发服务器上。我将相同的代码部署到 GAE 云中,它运行时没有错误。
ERROR 2012-06-29 16:04:20,717 webapp2.py:1553] [Errno 13] file not accessible: '/dev/urandom'
Traceback (most recent call last):
File "/home/eric/google_appengine/lib/webapp2/webapp2.py", line 1536, in __call__
rv = self.handle_exception(request, response, e)
File "/home/eric/google_appengine/lib/webapp2/webapp2.py", line 1530, in __call__
rv = self.router.dispatch(request, response)
File "/home/eric/google_appengine/lib/webapp2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/home/eric/google_appengine/lib/webapp2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/home/eric/google_appengine/lib/webapp2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/home/eric/google_appengine/lib/webapp2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/home/eric/workspace/commentbox/src/controller/api.py", line 55, in get
self.response.out.write(encrypt(json.dumps(to_json)))
File "/home/eric/workspace/commentbox/src/controller/api.py", line 27, in encrypt
iv = Random.new().read(AES.block_size)
File "/usr/lib/python2.7/dist-packages/Crypto/Random/__init__.py", line 33, in new
return _UserFriendlyRNG.new(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/Crypto/Random/_UserFriendlyRNG.py", line 206, in new
return RNGFile(_get_singleton())
File "/usr/lib/python2.7/dist-packages/Crypto/Random/_UserFriendlyRNG.py", line 200, in _get_singleton
_singleton = _LockingUserFriendlyRNG()
File "/usr/lib/python2.7/dist-packages/Crypto/Random/_UserFriendlyRNG.py", line 144, in __init__
_UserFriendlyRNG.__init__(self)
File "/usr/lib/python2.7/dist-packages/Crypto/Random/_UserFriendlyRNG.py", line 86, in __init__
self._ec = _EntropyCollector(self._fa)
File "/usr/lib/python2.7/dist-packages/Crypto/Random/_UserFriendlyRNG.py", line 53, in __init__
self._osrng = OSRNG.new()
File "/usr/lib/python2.7/dist-packages/Crypto/Random/OSRNG/posix.py", line 60, in new
return DevURandomRNG(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/Crypto/Random/OSRNG/posix.py", line 42, in __init__
f = open(self.name, "rb", 0)
File "/home/eric/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 592, in __init__
raise IOError(errno.EACCES, 'file not accessible', filename)
IOError: [Errno 13] file not accessible: '/dev/urandom'
ERROR 2012-06-29 16:04:20,721 webapp2.py:1549] Exception
ERROR 2012-06-29 16:04:20,721 webapp2.py:1549] AttributeError
ERROR 2012-06-29 16:04:20,721 webapp2.py:1549] :
ERROR 2012-06-29 16:04:20,721 webapp2.py:1549] "'DevURandomRNG' object has no attribute 'closed'"
ERROR 2012-06-29 16:04:20,721 webapp2.py:1549] in
ERROR 2012-06-29 16:04:20,721 webapp2.py:1549] <bound method DevURandomRNG.__del__ of <Crypto.Random.OSRNG.posix.DevURandomRNG object at 0x52707d0>>
ERROR 2012-06-29 16:04:20,721 webapp2.py:1549] ignored
引发错误的 python 代码是此块中的第二行:
from Crypto.Cipher import AES
from Crypto import Random
key = b'Sixteen byte key'
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CBC, iv)
return iv + cipher.encrypt(plaintext)
看到这个错误后,我意识到这可能是一个权限错误。于是我快速检查了 /dev/urandom 的权限:
eric@eric-Latitude-E5400 ~ $ dpkg -L udev | xargs grep urandom
/lib/udev/rules.d/50-udev-default.rules:KERNEL=="null|zero|full|random|urandom", MODE="0666"
eric@eric-Latitude-E5400 ~ $ ls -lart /dev/*random
crw-rw-rw- 1 root root 1, 9 Jun 29 10:53 /dev/urandom
crw-rw-rw- 1 root root 1, 8 Jun 29 10:53 /dev/random
所以看起来我的权限很好。我也尝试过以 root 身份运行开发服务器,但我得到了同样的错误。由于某种原因,这只发生在开发服务器上,而不是部署到谷歌的云上。关于下一步尝试什么的任何想法?
谢谢!