~ubuntu-branches/ubuntu/trusty/ceilometer/trusty-proposed

« back to all changes in this revision

Viewing changes to ceilometer/publisher/utils.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, James Page, Chuck Short
  • Date: 2014-01-23 15:08:11 UTC
  • mfrom: (1.1.11)
  • Revision ID: package-import@ubuntu.com-20140123150811-1zaismsuyh1hcl8y
Tags: 2014.1~b2-0ubuntu1
[ James Page ]
* d/control: Add python-jsonpath-rw to BD's.
* d/p/fix-setup-requirements.patch: Bump WebOb to support < 1.4.
 (LP: #1261101)

[ Chuck Short ]
* New upstream version.
* debian/control, debian/ceilometer-common.install: Split out
  ceilometer-alarm-evaluator and ceilometer-alarm-notifier into their
  own packages. (LP: #1250002)
* debian/ceilometer-agent-central.logrotate,
  debian/ceilometer-agent-compute.logrotate,
  debian/ceilometer-api.logrotate,
  debian/ceilometer-collector.logrotate: Add logrotate files, 
  thanks to Ahmed Rahal. (LP: #1224223)
* Fix typos in upstart files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
#
 
3
# Copyright © 2012 New Dream Network, LLC (DreamHost)
 
4
#
 
5
# Author: Doug Hellmann <doug.hellmann@dreamhost.com>
 
6
#         Tyaptin Ilya <ityaptin@mirantis.com>
 
7
#
 
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
 
11
#
 
12
#      http://www.apache.org/licenses/LICENSE-2.0
 
13
#
 
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
 
18
# under the License.
 
19
"""Utils for publishers
 
20
"""
 
21
 
 
22
import hashlib
 
23
import hmac
 
24
 
 
25
from oslo.config import cfg
 
26
 
 
27
from ceilometer import utils
 
28
 
 
29
METER_PUBLISH_OPTS = [
 
30
    cfg.StrOpt('metering_secret',
 
31
               secret=True,
 
32
               default='change this or be hacked',
 
33
               help='Secret value for signing metering messages',
 
34
               deprecated_opts=[cfg.DeprecatedOpt("metering_secret",
 
35
                                                  "DEFAULT"),
 
36
                                cfg.DeprecatedOpt("metering_secret",
 
37
                                                  "publisher_rpc")]
 
38
               ),
 
39
]
 
40
 
 
41
 
 
42
def register_opts(config):
 
43
    """Register the options for publishing metering messages.
 
44
    """
 
45
    config.register_opts(METER_PUBLISH_OPTS, group="publisher")
 
46
 
 
47
 
 
48
register_opts(cfg.CONF)
 
49
 
 
50
 
 
51
def compute_signature(message, secret):
 
52
    """Return the signature for a message dictionary.
 
53
    """
 
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.
 
59
            continue
 
60
        digest_maker.update(name)
 
61
        digest_maker.update(unicode(value).encode('utf-8'))
 
62
    return digest_maker.hexdigest()
 
63
 
 
64
 
 
65
def verify_signature(message, secret):
 
66
    """Check the signature in the message against the value computed
 
67
    from the rest of the contents.
 
68
    """
 
69
    old_sig = message.get('message_signature')
 
70
    new_sig = compute_signature(message, secret)
 
71
    return new_sig == old_sig
 
72
 
 
73
 
 
74
def meter_message_from_counter(sample, secret):
 
75
    """Make a metering message ready to be published or stored.
 
76
 
 
77
    Returns a dictionary containing a metering message
 
78
    for a notification message and a Sample instance.
 
79
    """
 
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,
 
91
           }
 
92
    msg['message_signature'] = compute_signature(msg, secret)
 
93
    return msg