~openstack-charmers-next/charms/xenial/swift-storage/trunk

« back to all changes in this revision

Viewing changes to charmhelpers/contrib/hardening/host/checks/login.py

  • Committer: Edward Hope-Morley
  • Date: 2016-03-24 11:11:58 UTC
  • Revision ID: edward.hope-morley@canonical.com-20160324111158-4whr78jd2v9yihw2
Add hardening support

Add charmhelpers.contrib.hardening and calls to install,
config-changed, upgrade-charm and update-status hooks.
Also add new config option to allow one or more hardening
modules to be applied at runtime.

Change-Id: If0d1e10b58ed506e0aca659f30120b8d5c96c04f

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2016 Canonical Limited.
 
2
#
 
3
# This file is part of charm-helpers.
 
4
#
 
5
# charm-helpers is free software: you can redistribute it and/or modify
 
6
# it under the terms of the GNU Lesser General Public License version 3 as
 
7
# published by the Free Software Foundation.
 
8
#
 
9
# charm-helpers is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU Lesser General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU Lesser General Public License
 
15
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
from six import string_types
 
18
 
 
19
from charmhelpers.contrib.hardening.audits.file import TemplatedFile
 
20
from charmhelpers.contrib.hardening.host import TEMPLATES_DIR
 
21
from charmhelpers.contrib.hardening import utils
 
22
 
 
23
 
 
24
def get_audits():
 
25
    """Get OS hardening login.defs audits.
 
26
 
 
27
    :returns:  dictionary of audits
 
28
    """
 
29
    audits = [TemplatedFile('/etc/login.defs', LoginContext(),
 
30
                            template_dir=TEMPLATES_DIR,
 
31
                            user='root', group='root', mode=0o0444)]
 
32
    return audits
 
33
 
 
34
 
 
35
class LoginContext(object):
 
36
 
 
37
    def __call__(self):
 
38
        settings = utils.get_settings('os')
 
39
 
 
40
        # Octal numbers in yaml end up being turned into decimal,
 
41
        # so check if the umask is entered as a string (e.g. '027')
 
42
        # or as an octal umask as we know it (e.g. 002). If its not
 
43
        # a string assume it to be octal and turn it into an octal
 
44
        # string.
 
45
        umask = settings['environment']['umask']
 
46
        if not isinstance(umask, string_types):
 
47
            umask = '%s' % oct(umask)
 
48
 
 
49
        ctxt = {
 
50
            'additional_user_paths':
 
51
            settings['environment']['extra_user_paths'],
 
52
            'umask': umask,
 
53
            'pwd_max_age': settings['auth']['pw_max_age'],
 
54
            'pwd_min_age': settings['auth']['pw_min_age'],
 
55
            'uid_min': settings['auth']['uid_min'],
 
56
            'sys_uid_min': settings['auth']['sys_uid_min'],
 
57
            'sys_uid_max': settings['auth']['sys_uid_max'],
 
58
            'gid_min': settings['auth']['gid_min'],
 
59
            'sys_gid_min': settings['auth']['sys_gid_min'],
 
60
            'sys_gid_max': settings['auth']['sys_gid_max'],
 
61
            'login_retries': settings['auth']['retries'],
 
62
            'login_timeout': settings['auth']['timeout'],
 
63
            'chfn_restrict': settings['auth']['chfn_restrict'],
 
64
            'allow_login_without_home': settings['auth']['allow_homeless']
 
65
        }
 
66
 
 
67
        return ctxt