~ubuntu-branches/ubuntu/saucy/ceilometer/saucy

« back to all changes in this revision

Viewing changes to ceilometer/openstack/common/sslutils.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Chuck Short, Yolanda Robla, Adam Gandelman
  • Date: 2013-09-07 15:58:58 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20130907155858-1yffoiaxi0avgq2k
Tags: 2013.2~b3-0ubuntu1
[ Chuck Short ]
* New upstream release.
* debian/patches/fix-setup-requirements.patch: Refreshed
* debian/patches/skip-database.patch: Refreshed

[ Yolanda Robla ]
* debian/control,debian/tests: Add basic autopkgtests.

[ Adam Gandelman ]
* debian/patches/default-dbconnection-sqlite.patch: Set 'connection' flag
  in [database] section, referencing 'sqlite_db' (LP: #1221956).
* debian/patches/skip-database-tests.patch: Also patch out db2 and hbase
  scenarios in v2 test_statistics_scenarios.py.
* debian/rules: Clean python-pbr egg if it exists, set PYTHONPATH to build
  dir when running tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2013 IBM Corp.
 
4
#
 
5
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
6
#    not use this file except in compliance with the License. You may obtain
 
7
#    a copy of the License at
 
8
#
 
9
#         http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
11
#    Unless required by applicable law or agreed to in writing, software
 
12
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
13
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
14
#    License for the specific language governing permissions and limitations
 
15
#    under the License.
 
16
 
 
17
import os
 
18
import ssl
 
19
 
 
20
from oslo.config import cfg
 
21
 
 
22
from ceilometer.openstack.common.gettextutils import _  # noqa
 
23
 
 
24
 
 
25
ssl_opts = [
 
26
    cfg.StrOpt('ca_file',
 
27
               default=None,
 
28
               help="CA certificate file to use to verify "
 
29
                    "connecting clients"),
 
30
    cfg.StrOpt('cert_file',
 
31
               default=None,
 
32
               help="Certificate file to use when starting "
 
33
                    "the server securely"),
 
34
    cfg.StrOpt('key_file',
 
35
               default=None,
 
36
               help="Private key file to use when starting "
 
37
                    "the server securely"),
 
38
]
 
39
 
 
40
 
 
41
CONF = cfg.CONF
 
42
CONF.register_opts(ssl_opts, "ssl")
 
43
 
 
44
 
 
45
def is_enabled():
 
46
    cert_file = CONF.ssl.cert_file
 
47
    key_file = CONF.ssl.key_file
 
48
    ca_file = CONF.ssl.ca_file
 
49
    use_ssl = cert_file or key_file
 
50
 
 
51
    if cert_file and not os.path.exists(cert_file):
 
52
        raise RuntimeError(_("Unable to find cert_file : %s") % cert_file)
 
53
 
 
54
    if ca_file and not os.path.exists(ca_file):
 
55
        raise RuntimeError(_("Unable to find ca_file : %s") % ca_file)
 
56
 
 
57
    if key_file and not os.path.exists(key_file):
 
58
        raise RuntimeError(_("Unable to find key_file : %s") % key_file)
 
59
 
 
60
    if use_ssl and (not cert_file or not key_file):
 
61
        raise RuntimeError(_("When running server in SSL mode, you must "
 
62
                             "specify both a cert_file and key_file "
 
63
                             "option value in your configuration file"))
 
64
 
 
65
    return use_ssl
 
66
 
 
67
 
 
68
def wrap(sock):
 
69
    ssl_kwargs = {
 
70
        'server_side': True,
 
71
        'certfile': CONF.ssl.cert_file,
 
72
        'keyfile': CONF.ssl.key_file,
 
73
        'cert_reqs': ssl.CERT_NONE,
 
74
    }
 
75
 
 
76
    if CONF.ssl.ca_file:
 
77
        ssl_kwargs['ca_certs'] = CONF.ssl.ca_file
 
78
        ssl_kwargs['cert_reqs'] = ssl.CERT_REQUIRED
 
79
 
 
80
    return ssl.wrap_socket(sock, **ssl_kwargs)
 
81
 
 
82
 
 
83
_SSL_PROTOCOLS = {
 
84
    "tlsv1": ssl.PROTOCOL_TLSv1,
 
85
    "sslv23": ssl.PROTOCOL_SSLv23,
 
86
    "sslv3": ssl.PROTOCOL_SSLv3
 
87
}
 
88
 
 
89
try:
 
90
    _SSL_PROTOCOLS["sslv2"] = ssl.PROTOCOL_SSLv2
 
91
except AttributeError:
 
92
    pass
 
93
 
 
94
 
 
95
def validate_ssl_version(version):
 
96
    key = version.lower()
 
97
    try:
 
98
        return _SSL_PROTOCOLS[key]
 
99
    except KeyError:
 
100
        raise RuntimeError(_("Invalid SSL version : %s") % version)