summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/controllers/settings.py
blob: 5df9ab1c90fdedac1b928c6f54395c9f452da73c (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
# -*- coding: utf-8 -*-
from __future__ import absolute_import

from contextlib import contextmanager

import cherrypy

from ..security import Scope
from ..settings import Options
from ..settings import Settings as SettingsModule
from . import APIDoc, APIRouter, EndpointDoc, RESTController, UIRouter

SETTINGS_SCHEMA = [{
    "name": (str, 'Settings Name'),
    "default": (bool, 'Default Settings'),
    "type": (str, 'Type of Settings'),
    "value": (bool, 'Settings Value')
}]


@APIRouter('/settings', Scope.CONFIG_OPT)
@APIDoc("Settings Management API", "Settings")
class Settings(RESTController):
    """
    Enables to manage the settings of the dashboard (not the Ceph cluster).
    """
    @contextmanager
    def _attribute_handler(self, name):
        """
        :type name: str|dict[str, str]
        :rtype: str|dict[str, str]
        """
        if isinstance(name, dict):
            result = {
                self._to_native(key): value
                for key, value in name.items()
            }
        else:
            result = self._to_native(name)

        try:
            yield result
        except AttributeError:  # pragma: no cover - handling is too obvious
            raise cherrypy.NotFound(result)  # pragma: no cover - handling is too obvious

    @staticmethod
    def _to_native(setting):
        return setting.upper().replace('-', '_')

    @EndpointDoc("Display Settings Information",
                 parameters={
                     'names': (str, 'Name of Settings'),
                 },
                 responses={200: SETTINGS_SCHEMA})
    def list(self, names=None):
        """
        Get the list of available options.
        :param names: A comma separated list of option names that should
        be processed. Defaults to ``None``.
        :type names: None|str
        :return: A list of available options.
        :rtype: list[dict]
        """
        option_names = [
            name for name in Options.__dict__
            if name.isupper() and not name.startswith('_')
        ]
        if names:
            names = names.split(',')
            option_names = list(set(option_names) & set(names))
        return [self._get(name) for name in option_names]

    def _get(self, name):
        with self._attribute_handler(name) as sname:
            setting = getattr(Options, sname)
        return {
            'name': sname,
            'default': setting.default_value,
            'type': setting.types_as_str(),
            'value': getattr(SettingsModule, sname)
        }

    def get(self, name):
        """
        Get the given option.
        :param name: The name of the option.
        :return: Returns a dict containing the name, type,
        default value and current value of the given option.
        :rtype: dict
        """
        return self._get(name)

    def set(self, name, value):
        with self._attribute_handler(name) as sname:
            setattr(SettingsModule, self._to_native(sname), value)

    def delete(self, name):
        with self._attribute_handler(name) as sname:
            delattr(SettingsModule, self._to_native(sname))

    def bulk_set(self, **kwargs):
        with self._attribute_handler(kwargs) as data:
            for name, value in data.items():
                setattr(SettingsModule, self._to_native(name), value)


@UIRouter('/standard_settings')
class StandardSettings(RESTController):
    def list(self):
        """
        Get various Dashboard related settings.
        :return: Returns a dictionary containing various Dashboard
        settings.
        :rtype: dict
        """
        return {  # pragma: no cover - no complexity there
            'user_pwd_expiration_span':
            SettingsModule.USER_PWD_EXPIRATION_SPAN,
            'user_pwd_expiration_warning_1':
            SettingsModule.USER_PWD_EXPIRATION_WARNING_1,
            'user_pwd_expiration_warning_2':
            SettingsModule.USER_PWD_EXPIRATION_WARNING_2,
            'pwd_policy_enabled':
            SettingsModule.PWD_POLICY_ENABLED,
            'pwd_policy_min_length':
            SettingsModule.PWD_POLICY_MIN_LENGTH,
            'pwd_policy_check_length_enabled':
            SettingsModule.PWD_POLICY_CHECK_LENGTH_ENABLED,
            'pwd_policy_check_oldpwd_enabled':
            SettingsModule.PWD_POLICY_CHECK_OLDPWD_ENABLED,
            'pwd_policy_check_username_enabled':
            SettingsModule.PWD_POLICY_CHECK_USERNAME_ENABLED,
            'pwd_policy_check_exclusion_list_enabled':
            SettingsModule.PWD_POLICY_CHECK_EXCLUSION_LIST_ENABLED,
            'pwd_policy_check_repetitive_chars_enabled':
            SettingsModule.PWD_POLICY_CHECK_REPETITIVE_CHARS_ENABLED,
            'pwd_policy_check_sequential_chars_enabled':
            SettingsModule.PWD_POLICY_CHECK_SEQUENTIAL_CHARS_ENABLED,
            'pwd_policy_check_complexity_enabled':
            SettingsModule.PWD_POLICY_CHECK_COMPLEXITY_ENABLED
        }