Use Ec2Signer from keystoneclient
Also refactor some tests for less confusing monkeypatching.changes/54/35854/1
parent
d78808a9a8
commit
ffb503ed04
|
@ -14,7 +14,9 @@
|
|||
# limitations under the License.
|
||||
|
||||
import json
|
||||
from keystoneclient.contrib.ec2 import utils as ec2_utils
|
||||
from oslo.config import cfg
|
||||
import urlparse
|
||||
|
||||
from openstack.common import log
|
||||
from os_collect_config import common
|
||||
|
@ -31,6 +33,10 @@ opts = [
|
|||
help='Stack name to describe'),
|
||||
cfg.MultiStrOpt('path',
|
||||
help='Path to Metadata'),
|
||||
cfg.StrOpt('secret-access-key',
|
||||
help='Secret Access Key'),
|
||||
cfg.StrOpt('access-key-id',
|
||||
help='Access Key ID'),
|
||||
]
|
||||
name = 'cfn'
|
||||
|
||||
|
@ -44,6 +50,12 @@ class Collector(object):
|
|||
if CONF.cfn.metadata_url is None:
|
||||
logger.warn('No metadata_url configured.')
|
||||
raise exc.CfnMetadataNotConfigured
|
||||
if CONF.cfn.access_key_id is None:
|
||||
logger.warn('No Access Key ID configured.')
|
||||
raise exc.CfnMetadataNotConfigured
|
||||
if CONF.cfn.secret_access_key is None:
|
||||
logger.warn('No Secret Access Key configured.')
|
||||
raise exc.CfnMetadataNotConfigured
|
||||
url = CONF.cfn.metadata_url
|
||||
stack_name = CONF.cfn.stack_name
|
||||
headers = {'Content-Type': 'application/json'}
|
||||
|
@ -52,6 +64,7 @@ class Collector(object):
|
|||
logger.warn('No path configured')
|
||||
raise exc.CfnMetadataNotConfigured
|
||||
|
||||
signer = ec2_utils.Ec2Signer(secret_key=CONF.cfn.secret_access_key)
|
||||
for path in CONF.cfn.path:
|
||||
if '.' not in path:
|
||||
logger.error('Path not in format resource.field[.x.y] (%s)' %
|
||||
|
@ -64,7 +77,15 @@ class Collector(object):
|
|||
sub_path = ''
|
||||
params = {'Action': 'DescribeStackResource',
|
||||
'Stackname': stack_name,
|
||||
'LogicalResourceId': resource}
|
||||
'LogicalResourceId': resource,
|
||||
'AWSAccessKeyId': CONF.cfn.access_key_id,
|
||||
'SignatureVersion': '2'}
|
||||
parsed_url = urlparse.urlparse(url)
|
||||
credentials = {'params': params,
|
||||
'verb': 'GET',
|
||||
'host': parsed_url.netloc,
|
||||
'path': parsed_url.path}
|
||||
params['Authorization'] = signer.generate(credentials)
|
||||
try:
|
||||
content = self._session.get(
|
||||
url, params=params, headers=headers)
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from openstack.common import log
|
||||
from os_collect_config import cache
|
||||
|
@ -85,9 +86,9 @@ def collect_all(collectors, store=False, requests_impl_map=None):
|
|||
return (any_changed, paths_or_content)
|
||||
|
||||
|
||||
def __main__(requests_impl_map=None):
|
||||
def __main__(args=sys.argv, requests_impl_map=None):
|
||||
setup_conf()
|
||||
CONF(prog="os-collect-config")
|
||||
CONF(args=args[1:], prog="os-collect-config")
|
||||
log.setup("os-collect-config")
|
||||
|
||||
(any_changed, content) = collect_all(COLLECTORS, store=bool(CONF.command),
|
||||
|
|
|
@ -58,7 +58,9 @@ class FakeRequests(object):
|
|||
self._test.assertEquals('/', url.path)
|
||||
self._test.assertEquals('application/json',
|
||||
headers['Content-Type'])
|
||||
# TODO(clint-fewbar) Refactor usage of requests to a factory
|
||||
self._test.assertIn('SignatureVersion', params)
|
||||
self._test.assertEquals('2', params['SignatureVersion'])
|
||||
self._test.assertIn('Authorization', params)
|
||||
self._test.assertIn('Action', params)
|
||||
self._test.assertEquals('DescribeStackResource',
|
||||
params['Action'])
|
||||
|
@ -83,6 +85,8 @@ class TestCfn(testtools.TestCase):
|
|||
collect.setup_conf()
|
||||
cfg.CONF.cfn.metadata_url = 'http://127.0.0.1:8000/'
|
||||
cfg.CONF.cfn.path = ['foo.Metadata']
|
||||
cfg.CONF.cfn.access_key_id = '0123456789ABCDEF'
|
||||
cfg.CONF.cfn.secret_access_key = 'FEDCBA9876543210'
|
||||
|
||||
def test_collect_cfn(self):
|
||||
cfn_md = cfn.Collector(requests_impl=FakeRequests(self)).collect()
|
||||
|
|
|
@ -33,12 +33,9 @@ class TestCollect(testtools.TestCase):
|
|||
cfg.CONF.reset()
|
||||
|
||||
def _call_main(self, fake_args):
|
||||
self.useFixture(
|
||||
fixtures.MonkeyPatch('sys.argv', fake_args))
|
||||
|
||||
requests_impl_map = {'ec2': test_ec2.FakeRequests,
|
||||
'cfn': test_cfn.FakeRequests(self)}
|
||||
collect.__main__(requests_impl_map=requests_impl_map)
|
||||
collect.__main__(args=fake_args, requests_impl_map=requests_impl_map)
|
||||
|
||||
def test_main(self):
|
||||
expected_cmd = self.getUniqueString()
|
||||
|
@ -56,7 +53,12 @@ class TestCollect(testtools.TestCase):
|
|||
'--cfn-stack-name',
|
||||
'foo',
|
||||
'--cfn-path',
|
||||
'foo.Metadata'
|
||||
'foo.Metadata',
|
||||
'--cfn-access-key-id',
|
||||
'0123456789ABCDEF',
|
||||
'--cfn-secret-access-key',
|
||||
'FEDCBA9876543210',
|
||||
|
||||
]
|
||||
self.called_fake_call = False
|
||||
|
||||
|
@ -94,7 +96,11 @@ class TestCollect(testtools.TestCase):
|
|||
'--cfn-stack-name',
|
||||
'foo',
|
||||
'--cfn-path',
|
||||
'foo.Metadata'
|
||||
'foo.Metadata',
|
||||
'--cfn-access-key-id',
|
||||
'0123456789ABCDEF',
|
||||
'--cfn-secret-access-key',
|
||||
'FEDCBA9876543210',
|
||||
]
|
||||
output = self.useFixture(fixtures.ByteStream('stdout'))
|
||||
self.useFixture(
|
||||
|
@ -122,6 +128,8 @@ class TestCollectAll(testtools.TestCase):
|
|||
cfg.CONF.cfn.metadata_url = 'http://127.0.0.1:8000/'
|
||||
cfg.CONF.cfn.stack_name = 'foo'
|
||||
cfg.CONF.cfn.path = ['foo.Metadata']
|
||||
cfg.CONF.cfn.access_key_id = '0123456789ABCDEF'
|
||||
cfg.CONF.cfn.secret_access_key = 'FEDCBA9876543210'
|
||||
|
||||
def _call_collect_all(self, store, requests_impl_map=None):
|
||||
if requests_impl_map is None:
|
||||
|
|
|
@ -2,6 +2,7 @@ anyjson
|
|||
argparse
|
||||
d2to1
|
||||
eventlet>=0.12.0
|
||||
python-keystoneclient
|
||||
requests
|
||||
iso8601>=0.1.4
|
||||
oslo.config>=1.1.0
|
||||
|
|
Loading…
Reference in New Issue