summaryrefslogtreecommitdiffstats
path: root/ipalib/config.py
blob: b3155490c8a92cafaeba5fbeadf4e5255ccf899e (plain)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# Authors:
#   Martin Nagy <mnagy@redhat.com>
#
# Copyright (C) 2008  Red Hat
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 only
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

"""
Basic configuration management.

This module handles the reading and representation of basic local settings.
It will also take care of settings that can be discovered by different
methods, such as DNS.
"""

from ConfigParser import SafeConfigParser, ParsingError
import types
import os

from errors import check_isinstance, raise_TypeError

DEFAULT_CONF='/etc/ipa/ipa.conf'


class Environment(object):
    """
    A mapping object used to store the environment variables.
    """

    def __init__(self):
        object.__setattr__(self, '_Environment__map', {})

    def __getattr__(self, name):
        """
        Return the attribute named ``name``.
        """
        return self[name]

    def __setattr__(self, name, value):
        """
        Set the attribute named ``name`` to ``value``.
        """
        self[name] = value

    def __delattr__(self, name):
        """
        Raise AttributeError (deletion is not allowed).
        """
        raise AttributeError('cannot del %s.%s' %
            (self.__class__.__name__, name)
        )

    def __getitem__(self, key):
        """
        Return the value corresponding to ``key``.
        """
        val = self.__map[key]
        if hasattr(val, 'get_value'):
            return val.get_value()
        else:
            return val

    def __setitem__(self, key, value):
        """
        Set the item at ``key`` to ``value``.
        """
        if key in self or hasattr(self, key):
            if hasattr(self.__map[key], 'set_value'):
                self.__map[key].set_value(value)
            else:
                raise AttributeError('cannot overwrite %s.%s' %
                            (self.__class__.__name__, key)
                )
        else:
            self.__map[key] = value

    def __contains__(self, key):
        """
        Return True if instance contains ``key``; otherwise return False.
        """
        return key in self.__map

    def __iter__(self):
        """
        Iterate through keys in ascending order.
        """
        for key in sorted(self.__map):
            yield key

    def update(self, new_vals, ignore_errors = False):
        """
        Update variables using keys and values from ``new_vals``.

        Error will occur if there is an attempt to override variable that was
        already set, unless``ignore_errors`` is True.
        """
        assert type(new_vals) == dict
        for key, value in new_vals.iteritems():
            if ignore_errors:
                try:
                    self[key] = value
                except (AttributeError, KeyError):
                    pass
            else:
                self[key] = value

    def get(self, name, default=None):
        """
        Return the value corresponding to ``key``. Defaults to ``default``.
        """
        if name in self:
            return self[name]
        else:
            return default


def set_default_env(env):
    """
    Set default values for ``env``.
    """
    assert isinstance(env, Environment)

    default = dict(
        basedn = EnvProp(basestring, 'dc=example,dc=com'),
        container_accounts = EnvProp(basestring, 'cn=accounts'),
        container_user = EnvProp(basestring, 'cn=users,cn=accounts'),
        container_group = EnvProp(basestring, 'cn=groups,cn=accounts'),
        container_service = EnvProp(basestring, 'cn=services,cn=accounts'),
        container_host = EnvProp(basestring, 'cn=computers,cn=accounts'),
        domain = LazyProp(basestring, get_domain),
        interactive = EnvProp(bool, True),
        query_dns = EnvProp(bool, True),
        realm = LazyProp(basestring, get_realm),
        server_context = EnvProp(bool, True),
        server = LazyIter(basestring, get_servers),
        verbose = EnvProp(bool, False),
        ldaphost = EnvProp(basestring, 'localhost'),
        ldapport = EnvProp(int, 389),
    )

    env.update(default)


class EnvProp(object):
    """
    Environment set-once property with optional default value.
    """
    def __init__(self, type_, default, multi_value=False):
        """
        :param type_: Type of the property.
        :param default: Default value.
        :param multi_value: Allow multiple values.
        """
        if multi_value:
            if isinstance(default, tuple) and len(default):
                check_isinstance(default[0], type_, allow_none=True)
        self._type = type_
        self._default = default
        self._value = None
        self._multi_value = multi_value

    def get_value(self):
        """
        Return the value if it was set.

        If the value is not set return the default. Otherwise raise an
        exception.
        """
        if self._get() != None:
            return self._get()
        else:
            raise KeyError, 'Value not set'

    def set_value(self, value):
        """
        Set the value.
        """
        if self._value != None:
            raise KeyError, 'Value already set'
        self._value = self._validate(value)

    def _get(self):
        """
        Return value, default, or None.
        """
        if self._value != None:
            return self._value
        elif self._default != None:
            return self._default
        else:
            return None

    def _validate(self, value):
        """
        Make sure ``value`` is of the right type. Do conversions if necessary.

        This will also handle multi value.
        """
        if self._multi_value and isinstance(value, tuple):
            converted = []
            for val in value:
                converted.append(self._validate_value(val))
            return tuple(converted)
        else:
            return self._validate_value(value)

    def _validate_value(self, value):
        """
        Validate and convert a single value.
        """
        bool_true = ('true', 'yes', 'on')
        bool_false = ('false', 'no', 'off')

        if self._type == bool and isinstance(value, basestring):
            if value.lower() in bool_true:
                return True
            elif value.lower() in bool_false:
                return False
            else:
                raise raise_TypeError(value, bool, 'value')
        check_isinstance(value, self._type, 'value')
        return value


class LazyProp(EnvProp):
    def __init__(self, type_, func, default=None, multi_value=False):
        check_isinstance(func, types.FunctionType, 'func')
        self._func = func
        EnvProp.__init__(self, type_, default, multi_value)

    def get_value(self):
        if self._get() != None:
            return self._get()
        else:
            return self._func()


class LazyIter(LazyProp):
    def __init__(self, type_, func, default=None):
        LazyProp.__init__(self, type_, func, default, multi_value=True)

    def get_value(self):
        val = self._get()
        if val != None:
            if type(val) == tuple:
                for item in val:
                    yield item
            else:
                yield val
        for item in self._func():
            if not val or item not in val:
                yield item


# TODO: Make it possible to use var = 'foo, bar' without
#       turning it into ("'foo", "bar'")
def read_config(config_file=None):
    assert config_file == None or isinstance(config_file, (basestring, file))

    parser = SafeConfigParser()
    if config_file == None:
        files = [DEFAULT_CONF, os.path.expanduser('~/.ipa.conf')]
    else:
        files = [config_file]

    for f in files:
        try:
            if isinstance(f, file):
                parser.readfp(f)
            else:
                parser.read(f)
        except ParsingError:
            print "Can't read %s" % f

    ret = {}
    if parser.has_section('defaults'):
        for name, value in parser.items('defaults'):
            value = tuple(elem.strip() for elem in value.split(','))
            if len(value) == 1:
                value = value[0]
            ret[name] = value

    return ret


# these functions are here just to "emulate" dns resolving for now
def get_domain():
    return "ipatest.com"


def get_realm():
    return "IPATEST.COM"


def get_servers():
    yield "server.ipatest.com"
    yield "backup.ipatest.com"
    yield "fake.ipatest.com"