|
| 1 | +# Copyright 2024 Red Hat, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from collections import OrderedDict |
| 16 | +from dataclasses import dataclass |
| 17 | +from typing import Any |
| 18 | + |
| 19 | +import yaml |
| 20 | +from django.conf import settings |
| 21 | +from django.core.exceptions import ImproperlyConfigured |
| 22 | +from django.db import transaction |
| 23 | + |
| 24 | +from aap_eda.core.models import Setting |
| 25 | +from aap_eda.core.utils.crypto.base import SecretValue |
| 26 | + |
| 27 | +ENCRYPTED_STRING = "$encrypted$" |
| 28 | + |
| 29 | + |
| 30 | +class InvalidKeyError(Exception): |
| 31 | + ... |
| 32 | + |
| 33 | + |
| 34 | +class InvalidValueError(Exception): |
| 35 | + ... |
| 36 | + |
| 37 | + |
| 38 | +@dataclass |
| 39 | +class RegistryData(object): |
| 40 | + name: str |
| 41 | + default: Any |
| 42 | + type: type = str |
| 43 | + read_only: bool = False |
| 44 | + is_secret: bool = False |
| 45 | + |
| 46 | + |
| 47 | +_APPLICATION_SETTING_REGISTRIES = [ |
| 48 | + RegistryData( |
| 49 | + name="INSIGHTS_TRACKING_STATE", |
| 50 | + type=bool, |
| 51 | + default=False, |
| 52 | + ), |
| 53 | + RegistryData( |
| 54 | + name="AUTOMATION_ANALYTICS_URL", |
| 55 | + read_only=True, |
| 56 | + default=settings.AUTOMATION_ANALYTICS_URL, |
| 57 | + ), |
| 58 | + RegistryData( |
| 59 | + name="REDHAT_USERNAME", |
| 60 | + default="", |
| 61 | + ), |
| 62 | + RegistryData( |
| 63 | + name="REDHAT_PASSWORD", |
| 64 | + is_secret=True, |
| 65 | + default="", |
| 66 | + ), |
| 67 | + RegistryData( |
| 68 | + name="SUBSCRIPTIONS_USERNAME", |
| 69 | + default="", |
| 70 | + ), |
| 71 | + RegistryData( |
| 72 | + name="SUBSCRIPTIONS_PASSWORD", |
| 73 | + is_secret=True, |
| 74 | + default="", |
| 75 | + ), |
| 76 | + RegistryData( |
| 77 | + name="INSIGHTS_CERT_PATH", |
| 78 | + read_only=True, |
| 79 | + default=settings.INSIGHTS_CERT_PATH, |
| 80 | + ), |
| 81 | + RegistryData( |
| 82 | + name="AUTOMATION_ANALYTICS_LAST_GATHER", |
| 83 | + default="", |
| 84 | + ), |
| 85 | + RegistryData( |
| 86 | + name="AUTOMATION_ANALYTICS_LAST_ENTRIES", |
| 87 | + type=dict, |
| 88 | + default={}, |
| 89 | + ), |
| 90 | + RegistryData( |
| 91 | + name="AUTOMATION_ANALYTICS_GATHER_INTERVAL", |
| 92 | + type=int, |
| 93 | + default=14400, |
| 94 | + ), |
| 95 | +] |
| 96 | + |
| 97 | + |
| 98 | +class SettingsRegistry(object): |
| 99 | + def __init__(self): |
| 100 | + self._registry = OrderedDict() |
| 101 | + for registry_data in _APPLICATION_SETTING_REGISTRIES: |
| 102 | + self.register(registry_data) |
| 103 | + |
| 104 | + def register(self, registry_data: RegistryData) -> None: |
| 105 | + if registry_data.name in self._registry: |
| 106 | + raise ImproperlyConfigured( |
| 107 | + f"Setting {registry_data.name} is already registered." |
| 108 | + ) |
| 109 | + self._registry[registry_data.name] = registry_data |
| 110 | + |
| 111 | + def persist_registry_data(self): |
| 112 | + for key, data in self._registry.items(): |
| 113 | + if data.read_only: |
| 114 | + update_method = Setting.objects.update_or_create |
| 115 | + else: |
| 116 | + update_method = Setting.objects.get_or_create |
| 117 | + update_method(key=key, defaults={"value": data.default}) |
| 118 | + |
| 119 | + def get_registered_settings( |
| 120 | + self, skip_read_only: bool = False |
| 121 | + ) -> list[str]: |
| 122 | + setting_names = [] |
| 123 | + |
| 124 | + for setting, data in self._registry.items(): |
| 125 | + if data.read_only and skip_read_only: |
| 126 | + continue |
| 127 | + setting_names.append(setting) |
| 128 | + return setting_names |
| 129 | + |
| 130 | + def is_setting_secret(self, key: str) -> bool: |
| 131 | + return self._registry[key].is_secret |
| 132 | + |
| 133 | + def is_setting_read_only(self, key: str) -> bool: |
| 134 | + return self._registry[key].read_only |
| 135 | + |
| 136 | + def get_setting_type(self, key: str) -> type: |
| 137 | + return self._registry[key].type |
| 138 | + |
| 139 | + def db_update_setting(self, key: str, value: Any) -> None: |
| 140 | + self._validate_key(key, writable=True) |
| 141 | + Setting.objects.filter(key=key).update( |
| 142 | + value=self._setting_value(key, value) |
| 143 | + ) |
| 144 | + |
| 145 | + def db_update_settings(self, settings: dict[str, Any]) -> None: |
| 146 | + with transaction.atomic(): |
| 147 | + for key, value in settings.items(): |
| 148 | + self._validate_key(key, writable=True) |
| 149 | + Setting.objects.filter(key=key).update( |
| 150 | + value=self._setting_value(key, value) |
| 151 | + ) |
| 152 | + |
| 153 | + def db_get_settings_for_display(self) -> dict[str, Any]: |
| 154 | + keys = self.get_registered_settings() |
| 155 | + settings = Setting.objects.filter(key__in=keys) |
| 156 | + return { |
| 157 | + obj.key: self._value_for_display(obj.key, obj.value) |
| 158 | + for obj in settings |
| 159 | + } |
| 160 | + |
| 161 | + def db_get_setting(self, key: str) -> Any: |
| 162 | + self._validate_key(key) |
| 163 | + setting = Setting.objects.filter(key=key).first() |
| 164 | + return self._decrypt_value(key, setting.value) |
| 165 | + |
| 166 | + def _validate_key(self, key: str, writable: bool = False) -> None: |
| 167 | + if key not in self._registry: |
| 168 | + raise InvalidKeyError(f"{key} is not a preset key") |
| 169 | + if writable and self.is_setting_read_only(key): |
| 170 | + raise InvalidKeyError(f"{key} is readonly") |
| 171 | + |
| 172 | + def _decrypt_value(self, key: str, db_value: SecretValue) -> Any: |
| 173 | + val = db_value.get_secret_value() |
| 174 | + if val != "": |
| 175 | + val = yaml.safe_load(val) |
| 176 | + return self._setting_value(key, val) |
| 177 | + |
| 178 | + def _setting_value(self, key: str, value: Any) -> Any: |
| 179 | + try: |
| 180 | + return self.get_setting_type(key)(value) |
| 181 | + except (ValueError, TypeError): |
| 182 | + raise InvalidValueError( |
| 183 | + f"Attempt to set an invalid value to key {key}" |
| 184 | + ) |
| 185 | + |
| 186 | + def _value_for_display(self, key: str, db_value: SecretValue) -> Any: |
| 187 | + value = self._decrypt_value(key, db_value) |
| 188 | + if self.is_setting_secret(key) and value: |
| 189 | + return ENCRYPTED_STRING |
| 190 | + return value |
| 191 | + |
| 192 | + |
| 193 | +settings_registry = SettingsRegistry() |
0 commit comments