1
# -*- encoding: utf-8 -*-
3
# Copyright © 2012 New Dream Network, LLC (DreamHost)
5
# Author: Doug Hellmann <doug.hellmann@dreamhost.com>
6
# Tyaptin Ilya <ityaptin@mirantis.com>
8
# Licensed under the Apache License, Version 2.0 (the "License"); you may
9
# not use this file except in compliance with the License. You may obtain
10
# a copy of the License at
12
# http://www.apache.org/licenses/LICENSE-2.0
14
# Unless required by applicable law or agreed to in writing, software
15
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17
# License for the specific language governing permissions and limitations
19
"""Utils for publishers
25
from oslo.config import cfg
27
from ceilometer import utils
29
METER_PUBLISH_OPTS = [
30
cfg.StrOpt('metering_secret',
32
default='change this or be hacked',
33
help='Secret value for signing metering messages',
34
deprecated_opts=[cfg.DeprecatedOpt("metering_secret",
36
cfg.DeprecatedOpt("metering_secret",
42
def register_opts(config):
43
"""Register the options for publishing metering messages.
45
config.register_opts(METER_PUBLISH_OPTS, group="publisher")
48
register_opts(cfg.CONF)
51
def compute_signature(message, secret):
52
"""Return the signature for a message dictionary.
54
digest_maker = hmac.new(secret, '', hashlib.sha256)
55
for name, value in utils.recursive_keypairs(message):
56
if name == 'message_signature':
57
# Skip any existing signature value, which would not have
58
# been part of the original message.
60
digest_maker.update(name)
61
digest_maker.update(unicode(value).encode('utf-8'))
62
return digest_maker.hexdigest()
65
def verify_signature(message, secret):
66
"""Check the signature in the message against the value computed
67
from the rest of the contents.
69
old_sig = message.get('message_signature')
70
new_sig = compute_signature(message, secret)
71
return new_sig == old_sig
74
def meter_message_from_counter(sample, secret):
75
"""Make a metering message ready to be published or stored.
77
Returns a dictionary containing a metering message
78
for a notification message and a Sample instance.
80
msg = {'source': sample.source,
81
'counter_name': sample.name,
82
'counter_type': sample.type,
83
'counter_unit': sample.unit,
84
'counter_volume': sample.volume,
85
'user_id': sample.user_id,
86
'project_id': sample.project_id,
87
'resource_id': sample.resource_id,
88
'timestamp': sample.timestamp,
89
'resource_metadata': sample.resource_metadata,
90
'message_id': sample.id,
92
msg['message_signature'] = compute_signature(msg, secret)