/usr/share/pyshared/os_collect_config/cfn.py is in python-os-collect-config 0.1.15-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | # Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import os
from keystoneclient.contrib.ec2 import utils as ec2_utils
from lxml import etree
from oslo.config import cfg
import urlparse
from openstack.common import log
from os_collect_config import common
from os_collect_config import exc
CONF = cfg.CONF
logger = log.getLogger(__name__)
opts = [
cfg.StrOpt('metadata-url',
help='URL to query for CloudFormation Metadata'),
cfg.StrOpt('heat-metadata-hint',
default='/var/lib/heat-cfntools/cfn-metadata-server',
help='Local file to read for metadata url if not explicitly '
' specified'),
cfg.StrOpt('stack-name',
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'),
cfg.MultiStrOpt('deployment-key',
default=['deployments'],
help='Key(s) to explode into multiple collected outputs. '
'Parsed according to the expected Metadata created by '
'OS::Heat::StructuredDeployment. Only Exploded if seen at '
'the root of the Metadata.')
]
name = 'cfn'
class Collector(object):
def __init__(self, requests_impl=common.requests):
self._requests_impl = requests_impl
self._session = requests_impl.Session()
def collect(self):
if CONF.cfn.metadata_url is None:
if (CONF.cfn.heat_metadata_hint
and os.path.exists(CONF.cfn.heat_metadata_hint)):
with open(CONF.cfn.heat_metadata_hint) as hint:
CONF.cfn.metadata_url = '%s/v1/' % hint.read().strip()
else:
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'}
final_content = {}
if CONF.cfn.path is None:
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)' %
path)
raise exc.CfnMetadataNotConfigured
resource, field = path.split('.', 1)
if '.' in field:
field, sub_path = field.split('.', 1)
else:
sub_path = ''
params = {'Action': 'DescribeStackResource',
'StackName': stack_name,
'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['Signature'] = signer.generate(credentials)
try:
content = self._session.get(
url, params=params, headers=headers)
content.raise_for_status()
except self._requests_impl.exceptions.RequestException as e:
logger.warn(e)
raise exc.CfnMetadataNotAvailable
map_content = etree.fromstring(content.text)
resource_detail = map_content.find(
'DescribeStackResourceResult').find('StackResourceDetail')
sub_element = resource_detail.find(field)
if sub_element is None:
logger.warn('Path %s does not exist.' % (path))
raise exc.CfnMetadataNotAvailable
try:
value = json.loads(sub_element.text)
except ValueError as e:
logger.warn(
'Path %s failed to parse as json. (%s)' % (path, e))
raise exc.CfnMetadataNotAvailable
if sub_path:
for subkey in sub_path.split('.'):
try:
value = value[subkey]
except KeyError:
logger.warn(
'Sub-key %s does not exist. (%s)' % (subkey, path))
raise exc.CfnMetadataNotAvailable
final_content.update(value)
final_list = []
for depkey in cfg.CONF.cfn.deployment_key:
if depkey in final_content:
deployments = final_content[depkey]
if not isinstance(deployments, list):
logger.warn(
'Deployment-key %s was found but does not contain a '
'list.' % (depkey,))
continue
logger.debug(
'Deployment found for %s' % (depkey,))
for deployment in deployments:
if 'name' not in deployment:
logger.warn(
'No name found for a deployment under %s.' %
(depkey,))
continue
final_list.append((deployment['name'],
deployment['config']))
del final_content[depkey]
final_list.insert(0, ('cfn', final_content))
return final_list
|