Added support for post metadata encrypted password

This commit is contained in:
Alessandro Pilotti
2013-02-03 22:00:04 +02:00
parent 11e183212f
commit e94d732d2b
5 changed files with 321 additions and 29 deletions

View File

@@ -14,6 +14,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import abc
import json
import os
import posixpath
@@ -23,16 +24,21 @@ from cloudbaseinit.openstack.common import log as logging
LOG = logging.getLogger(__name__)
class NotExistingMetadataException(Exception):
pass
class BaseMetadataService(object):
def load(self):
self._cache = {}
@abc.abstractmethod
def _get_data(self, path):
pass
def _get_cache_data(self, path):
if path in self._cache:
LOG.debug('Using cached copy of metadata: \'%s\'' % path)
LOG.debug("Using cached copy of metadata: '%s'" % path)
return self._cache[path]
else:
data = self._get_data(path)
@@ -57,5 +63,16 @@ class BaseMetadataService(object):
return json.loads(self._get_cache_data(path))
else:
return data
def _post_data(self, path, data):
raise NotImplementedError()
def post_password(self, enc_password_b64, version='latest'):
path = posixpath.normpath(posixpath.join(path,
'openstack',
version,
'password'))
return self._post_data(path, enc_password_b64)
def cleanup(self):
pass

View File

@@ -15,6 +15,7 @@
# under the License.
import posixpath
import urllib
import urllib2
from cloudbaseinit.metadata.services.base import *
@@ -42,9 +43,39 @@ class HttpService(BaseMetadataService):
CONF.metadata_base_url)
return False
def _get_response(self, req):
try:
return urllib2.urlopen(req)
except urllib2.HTTPError as ex:
if ex.code == 404:
raise NotExistingMetadataException()
else:
raise ex
def _get_data(self, path):
norm_path = posixpath.join(CONF.metadata_base_url, path)
LOG.debug('Getting metadata from: %(norm_path)s' % locals())
req = urllib2.Request(norm_path)
response = urllib2.urlopen(req)
response = self._get_response(req)
return response.read()
def _post_data(self, path, data):
norm_path = posixpath.join(CONF.metadata_base_url, path)
LOG.debug('Posting metadata to: %(norm_path)s' % locals())
req = urllib2.Request(norm_path, data=urllib.urlencode(data))
self._get_response(req)
return True
def post_password(self, enc_password_b64, version='latest'):
try:
return super(HttpService, self).post_password(enc_password_b64,
version)
except urllib2.HTTPError as ex:
if ex.code == 409:
# Password already set
return False
else:
raise ex