Split meter publishing from the global config obj
The meter signing and publishing code was tightly coupled to the global configuration object, making it more difficult to reuse in an outside project. This change adds arguments for the configuration settings actually being used in those libraries and moves use of the global configuration object higher up the stack to the manager. It also organizes the definition of the options related to signing and publishing metering messages so they can more easily by registered on other configuration objects. Change-Id: I8b1df0f228d0c2658841c4b7f7f4527414efc9a6 Signed-off-by: Doug Hellmann <doug.hellmann@dreamhost.com>
This commit is contained in:
@@ -20,6 +20,7 @@ import pkg_resources
|
||||
|
||||
from nova import manager
|
||||
|
||||
from ceilometer.openstack.common import cfg
|
||||
from ceilometer.openstack.common import log
|
||||
from ceilometer import publish
|
||||
|
||||
@@ -65,7 +66,10 @@ class AgentManager(manager.Manager):
|
||||
LOG.info('polling %s', name)
|
||||
for c in pollster.get_counters(self, context):
|
||||
LOG.info('COUNTER: %s', c)
|
||||
publish.publish_counter(context, c)
|
||||
publish.publish_counter(context, c,
|
||||
cfg.CONF.metering_topic,
|
||||
cfg.CONF.metering_secret,
|
||||
)
|
||||
except Exception as err:
|
||||
LOG.warning('Continuing after error from %s: %s', name, err)
|
||||
LOG.exception(err)
|
||||
|
||||
@@ -132,7 +132,7 @@ class CollectorManager(manager.Manager):
|
||||
data['resource_id'],
|
||||
data.get('timestamp', 'NO TIMESTAMP'),
|
||||
data['counter_volume'])
|
||||
if not meter.verify_signature(data):
|
||||
if not meter.verify_signature(data, cfg.CONF.metering_secret):
|
||||
LOG.warning('message signature invalid, discarding message: %r',
|
||||
data)
|
||||
else:
|
||||
|
||||
@@ -29,13 +29,16 @@ METER_OPTS = [
|
||||
default='change this or be hacked',
|
||||
help='Secret value for signing metering messages',
|
||||
),
|
||||
cfg.StrOpt('metering_topic',
|
||||
default='metering',
|
||||
help='the topic ceilometer uses for metering messages',
|
||||
),
|
||||
]
|
||||
|
||||
cfg.CONF.register_opts(METER_OPTS)
|
||||
|
||||
def register_opts(config):
|
||||
"""Register the options for signing metering messages.
|
||||
"""
|
||||
config.register_opts(METER_OPTS)
|
||||
|
||||
|
||||
register_opts(cfg.CONF)
|
||||
|
||||
|
||||
def recursive_keypairs(d):
|
||||
@@ -49,10 +52,10 @@ def recursive_keypairs(d):
|
||||
yield name, value
|
||||
|
||||
|
||||
def compute_signature(message):
|
||||
def compute_signature(message, secret):
|
||||
"""Return the signature for a message dictionary.
|
||||
"""
|
||||
digest_maker = hmac.new(cfg.CONF.metering_secret, '', hashlib.sha256)
|
||||
digest_maker = hmac.new(secret, '', hashlib.sha256)
|
||||
for name, value in recursive_keypairs(message):
|
||||
if name == 'message_signature':
|
||||
# Skip any existing signature value, which would not have
|
||||
@@ -63,16 +66,16 @@ def compute_signature(message):
|
||||
return digest_maker.hexdigest()
|
||||
|
||||
|
||||
def verify_signature(message):
|
||||
def verify_signature(message, secret):
|
||||
"""Check the signature in the message against the value computed
|
||||
from the rest of the contents.
|
||||
"""
|
||||
old_sig = message.get('message_signature')
|
||||
new_sig = compute_signature(message)
|
||||
new_sig = compute_signature(message, secret)
|
||||
return new_sig == old_sig
|
||||
|
||||
|
||||
def meter_message_from_counter(counter):
|
||||
def meter_message_from_counter(counter, secret):
|
||||
"""Make a metering message ready to be published or stored.
|
||||
|
||||
Returns a dictionary containing a metering message
|
||||
@@ -90,5 +93,5 @@ def meter_message_from_counter(counter):
|
||||
'resource_metadata': counter.resource_metadata,
|
||||
'message_id': str(uuid.uuid1()),
|
||||
}
|
||||
msg['message_signature'] = compute_signature(msg)
|
||||
msg['message_signature'] = compute_signature(msg, secret)
|
||||
return msg
|
||||
|
||||
@@ -18,15 +18,31 @@
|
||||
"""Publish a counter using the preferred RPC mechanism.
|
||||
"""
|
||||
|
||||
from ceilometer.openstack.common import cfg
|
||||
from ceilometer.openstack.common import log
|
||||
from ceilometer.openstack.common import rpc
|
||||
from ceilometer.openstack.common import cfg
|
||||
from ceilometer import meter
|
||||
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
PUBLISH_OPTS = [
|
||||
cfg.StrOpt('metering_topic',
|
||||
default='metering',
|
||||
help='the topic ceilometer uses for metering messages',
|
||||
),
|
||||
]
|
||||
|
||||
def publish_counter(context, counter):
|
||||
|
||||
def register_opts(config):
|
||||
"""Register the options for publishing metering messages.
|
||||
"""
|
||||
config.register_opts(PUBLISH_OPTS)
|
||||
|
||||
|
||||
register_opts(cfg.CONF)
|
||||
|
||||
|
||||
def publish_counter(context, counter, topic, secret):
|
||||
"""Send a metering message for the data represented by the counter.
|
||||
|
||||
:param context: Execution context from the service or RPC call
|
||||
@@ -35,11 +51,9 @@ def publish_counter(context, counter):
|
||||
msg = {
|
||||
'method': 'record_metering_data',
|
||||
'version': '1.0',
|
||||
'args': {'data': meter.meter_message_from_counter(counter),
|
||||
'args': {'data': meter.meter_message_from_counter(counter, secret),
|
||||
},
|
||||
}
|
||||
LOG.debug('PUBLISH: %s', str(msg))
|
||||
rpc.cast(context, cfg.CONF.metering_topic, msg)
|
||||
rpc.cast(context,
|
||||
cfg.CONF.metering_topic + '.' + counter.name,
|
||||
msg)
|
||||
rpc.cast(context, topic, msg)
|
||||
rpc.cast(context, topic + '.' + counter.name, msg)
|
||||
|
||||
Reference in New Issue
Block a user