~junaidali/charms/trusty/plumgrid-gateway/analyst_opsvm

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/hardening/audits/apt.py

  • Committer: bbaqar at plumgrid
  • Date: 2016-04-25 09:21:09 UTC
  • mfrom: (26.1.2 plumgrid-gateway)
  • Revision ID: bbaqar@plumgrid.com-20160425092109-kweey25bx97pmj80
Merge: Liberty/Mitaka support

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 __future__ import absolute_import  # required for external apt import
 
18
from apt import apt_pkg
 
19
from six import string_types
 
20
 
 
21
from charmhelpers.fetch import (
 
22
    apt_cache,
 
23
    apt_purge
 
24
)
 
25
from charmhelpers.core.hookenv import (
 
26
    log,
 
27
    DEBUG,
 
28
    WARNING,
 
29
)
 
30
from charmhelpers.contrib.hardening.audits import BaseAudit
 
31
 
 
32
 
 
33
class AptConfig(BaseAudit):
 
34
 
 
35
    def __init__(self, config, **kwargs):
 
36
        self.config = config
 
37
 
 
38
    def verify_config(self):
 
39
        apt_pkg.init()
 
40
        for cfg in self.config:
 
41
            value = apt_pkg.config.get(cfg['key'], cfg.get('default', ''))
 
42
            if value and value != cfg['expected']:
 
43
                log("APT config '%s' has unexpected value '%s' "
 
44
                    "(expected='%s')" %
 
45
                    (cfg['key'], value, cfg['expected']), level=WARNING)
 
46
 
 
47
    def ensure_compliance(self):
 
48
        self.verify_config()
 
49
 
 
50
 
 
51
class RestrictedPackages(BaseAudit):
 
52
    """Class used to audit restricted packages on the system."""
 
53
 
 
54
    def __init__(self, pkgs, **kwargs):
 
55
        super(RestrictedPackages, self).__init__(**kwargs)
 
56
        if isinstance(pkgs, string_types) or not hasattr(pkgs, '__iter__'):
 
57
            self.pkgs = [pkgs]
 
58
        else:
 
59
            self.pkgs = pkgs
 
60
 
 
61
    def ensure_compliance(self):
 
62
        cache = apt_cache()
 
63
 
 
64
        for p in self.pkgs:
 
65
            if p not in cache:
 
66
                continue
 
67
 
 
68
            pkg = cache[p]
 
69
            if not self.is_virtual_package(pkg):
 
70
                if not pkg.current_ver:
 
71
                    log("Package '%s' is not installed." % pkg.name,
 
72
                        level=DEBUG)
 
73
                    continue
 
74
                else:
 
75
                    log("Restricted package '%s' is installed" % pkg.name,
 
76
                        level=WARNING)
 
77
                    self.delete_package(cache, pkg)
 
78
            else:
 
79
                log("Checking restricted virtual package '%s' provides" %
 
80
                    pkg.name, level=DEBUG)
 
81
                self.delete_package(cache, pkg)
 
82
 
 
83
    def delete_package(self, cache, pkg):
 
84
        """Deletes the package from the system.
 
85
 
 
86
        Deletes the package form the system, properly handling virtual
 
87
        packages.
 
88
 
 
89
        :param cache: the apt cache
 
90
        :param pkg: the package to remove
 
91
        """
 
92
        if self.is_virtual_package(pkg):
 
93
            log("Package '%s' appears to be virtual - purging provides" %
 
94
                pkg.name, level=DEBUG)
 
95
            for _p in pkg.provides_list:
 
96
                self.delete_package(cache, _p[2].parent_pkg)
 
97
        elif not pkg.current_ver:
 
98
            log("Package '%s' not installed" % pkg.name, level=DEBUG)
 
99
            return
 
100
        else:
 
101
            log("Purging package '%s'" % pkg.name, level=DEBUG)
 
102
            apt_purge(pkg.name)
 
103
 
 
104
    def is_virtual_package(self, pkg):
 
105
        return pkg.has_provides and not pkg.has_versions