|
| 1 | +# Copyright 2025 Genesis Corporation. |
| 2 | +# |
| 3 | +# All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | +# not use this file except in compliance with the License. You may obtain |
| 7 | +# a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | +# License for the specific language governing permissions and limitations |
| 15 | +# under the License. |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import logging |
| 19 | +import collections |
| 20 | +import typing as tp |
| 21 | +import uuid as sys_uuid |
| 22 | + |
| 23 | +from restalchemy.common import contexts |
| 24 | +from restalchemy.dm import filters as dm_filters |
| 25 | +from gcl_looper.services import basic |
| 26 | +from gcl_sdk.agents.universal.dm import models as ua_models |
| 27 | + |
| 28 | +from genesis_core.secret.dm import models |
| 29 | +from genesis_core.secret import constants as sc |
| 30 | + |
| 31 | + |
| 32 | +LOG = logging.getLogger(__name__) |
| 33 | + |
| 34 | + |
| 35 | +class SecretServiceBuilder(basic.BasicService): |
| 36 | + |
| 37 | + def _get_new_passwords( |
| 38 | + self, |
| 39 | + limit: int = sc.DEFAULT_SQL_LIMIT, |
| 40 | + ) -> list[models.Password]: |
| 41 | + return models.Password.get_new_passwords(limit=limit) |
| 42 | + |
| 43 | + def _get_changed_passwords( |
| 44 | + self, |
| 45 | + limit: int = sc.DEFAULT_SQL_LIMIT, |
| 46 | + ) -> list[models.Password]: |
| 47 | + return models.Password.get_updated_passwords(limit=limit) |
| 48 | + |
| 49 | + def _get_deleted_passwords( |
| 50 | + self, |
| 51 | + limit: int = sc.DEFAULT_SQL_LIMIT, |
| 52 | + ) -> list[ua_models.TargetResource]: |
| 53 | + return models.Password.get_deleted_passwords(limit=limit) |
| 54 | + |
| 55 | + def _get_outdated_resources( |
| 56 | + self, |
| 57 | + limit: int = sc.DEFAULT_SQL_LIMIT, |
| 58 | + ) -> dict[ |
| 59 | + sys_uuid.UUID, |
| 60 | + list[tuple[ua_models.TargetResource, ua_models.Resource]], |
| 61 | + ]: |
| 62 | + outdated = ua_models.OutdatedResource.objects.get_all( |
| 63 | + filters={"kind": dm_filters.EQ(sc.PASSWORD_KIND)}, |
| 64 | + limit=limit, |
| 65 | + ) |
| 66 | + res_map = collections.defaultdict(list) |
| 67 | + for pair in outdated: |
| 68 | + # Updated resource aren't outdated |
| 69 | + if ( |
| 70 | + pair.target_resource.updated_at |
| 71 | + > pair.actual_resource.updated_at |
| 72 | + ): |
| 73 | + continue |
| 74 | + |
| 75 | + res_map[pair.target_resource.uuid].append( |
| 76 | + (pair.target_resource, pair.actual_resource) |
| 77 | + ) |
| 78 | + |
| 79 | + return res_map |
| 80 | + |
| 81 | + def _get_outdated_passwords( |
| 82 | + self, uuids: tp.Collection[sys_uuid.UUID] |
| 83 | + ) -> list[models.Password]: |
| 84 | + return models.Password.objects.get_all( |
| 85 | + filters={"uuid": dm_filters.In(str(p) for p in uuids)}, |
| 86 | + ) |
| 87 | + |
| 88 | + def _actualize_new_passwords( |
| 89 | + self, passwords: list[models.Password] | None = None |
| 90 | + ) -> None: |
| 91 | + """Actualize new passwords.""" |
| 92 | + passwords = passwords or self._get_new_passwords() |
| 93 | + |
| 94 | + if len(passwords) == 0: |
| 95 | + return |
| 96 | + |
| 97 | + # Just create resources for new passwords |
| 98 | + for password in passwords: |
| 99 | + password_resource = password.to_ua_resource(sc.PASSWORD_KIND) |
| 100 | + try: |
| 101 | + password_resource.insert() |
| 102 | + password.status = sc.SecretStatus.IN_PROGRESS.value |
| 103 | + password.save() |
| 104 | + |
| 105 | + # TODO(akremenetsky): Improve this snippet in the future |
| 106 | + password_resource.tracked_at = password.updated_at |
| 107 | + password_resource.status = password.status |
| 108 | + password_resource.update() |
| 109 | + LOG.info( |
| 110 | + "Password resource %s created", password_resource.uuid |
| 111 | + ) |
| 112 | + except Exception: |
| 113 | + LOG.exception( |
| 114 | + "Error creating password resource %s", password.uuid |
| 115 | + ) |
| 116 | + |
| 117 | + def _actualize_changed_passwords(self) -> None: |
| 118 | + """Actualize passwords changed by user.""" |
| 119 | + changed_passwords = {p.uuid: p for p in self._get_changed_passwords()} |
| 120 | + |
| 121 | + if len(changed_passwords) == 0: |
| 122 | + return |
| 123 | + |
| 124 | + password_resources = ua_models.TargetResource.objects.get_all( |
| 125 | + filters={ |
| 126 | + "uuid": dm_filters.In( |
| 127 | + str(p) for p in changed_passwords.keys() |
| 128 | + ), |
| 129 | + "kind": dm_filters.EQ(sc.PASSWORD_KIND), |
| 130 | + } |
| 131 | + ) |
| 132 | + |
| 133 | + # Update every resource in accordance with the new password |
| 134 | + for resource in password_resources: |
| 135 | + password = changed_passwords[resource.uuid] |
| 136 | + |
| 137 | + # TODO(akremenetsky): Need to clear the password container |
| 138 | + # before converting |
| 139 | + new_resource = password.to_ua_resource( |
| 140 | + sc.PASSWORD_KIND, tracked_at=resource.updated_at |
| 141 | + ) |
| 142 | + |
| 143 | + # Update the original resource |
| 144 | + resource.update_value(new_resource) |
| 145 | + try: |
| 146 | + resource.update() |
| 147 | + LOG.debug("Password resource %s updated", resource.uuid) |
| 148 | + except Exception: |
| 149 | + LOG.exception( |
| 150 | + "Error updating password resource %s", resource.uuid |
| 151 | + ) |
| 152 | + |
| 153 | + def _actualize_outdated_password( |
| 154 | + self, |
| 155 | + password: models.Password, |
| 156 | + target_resource: ua_models.TargetResource, |
| 157 | + actual_resource: ua_models.Resource, |
| 158 | + ) -> None: |
| 159 | + """Actualize outdated password.""" |
| 160 | + saved_password = ua_models.Resource.from_ua_resource(actual_resource) |
| 161 | + |
| 162 | + # Actualize password |
| 163 | + password.status = saved_password.status |
| 164 | + password.password = saved_password.password |
| 165 | + password.save() |
| 166 | + |
| 167 | + # Actualize resource |
| 168 | + target_resource.status = saved_password.status |
| 169 | + target_resource.tracked_at = saved_password.updated_at |
| 170 | + target_resource.update() |
| 171 | + |
| 172 | + def _actualize_outdated_passwords(self) -> None: |
| 173 | + """Actualize outdated passwords. |
| 174 | +
|
| 175 | + It means some changes oscurred in the system and the passwords |
| 176 | + are outdated now. For instance, their status is incorrect. |
| 177 | + """ |
| 178 | + resource_map = self._get_outdated_resources() |
| 179 | + |
| 180 | + if len(resource_map) == 0: |
| 181 | + return |
| 182 | + |
| 183 | + passwords = self._get_outdated_passwords(tuple(resource_map.keys())) |
| 184 | + |
| 185 | + for password in passwords: |
| 186 | + target, actual = resource_map[password.uuid] |
| 187 | + try: |
| 188 | + self._actualize_outdated_password(password, target, actual) |
| 189 | + LOG.debug("Password %s actualized", password.uuid) |
| 190 | + except Exception: |
| 191 | + LOG.exception("Error actualizing password %s", password.uuid) |
| 192 | + |
| 193 | + def _actualize_deleted_passwords(self) -> None: |
| 194 | + """Actualize passwords deleted by user.""" |
| 195 | + deleted_passwords = self._get_deleted_passwords() |
| 196 | + |
| 197 | + if len(deleted_passwords) == 0: |
| 198 | + return |
| 199 | + |
| 200 | + for password in deleted_passwords: |
| 201 | + try: |
| 202 | + password.delete() |
| 203 | + LOG.debug("Outdated resource %s deleted", password.uuid) |
| 204 | + except Exception: |
| 205 | + LOG.exception("Error deleting resource %s", password.uuid) |
| 206 | + |
| 207 | + def _iteration(self) -> None: |
| 208 | + with contexts.Context().session_manager(): |
| 209 | + try: |
| 210 | + self._actualize_new_passwords() |
| 211 | + except Exception: |
| 212 | + LOG.exception("Error actualizing new passwords") |
| 213 | + |
| 214 | + try: |
| 215 | + self._actualize_changed_passwords() |
| 216 | + except Exception: |
| 217 | + LOG.exception("Error actualizing changed passwords") |
| 218 | + |
| 219 | + try: |
| 220 | + self._actualize_outdated_passwords() |
| 221 | + except Exception: |
| 222 | + LOG.exception("Error actualizing outdated passwords") |
| 223 | + |
| 224 | + try: |
| 225 | + self._actualize_deleted_passwords() |
| 226 | + except Exception: |
| 227 | + LOG.exception("Error actualizing deleted passwords") |
0 commit comments