|
16 | 16 | from netbox_config_diff.models import ConfigCompliance
|
17 | 17 |
|
18 | 18 | from .models import DeviceDataClass
|
| 19 | +from .secrets import SecretsMixin |
19 | 20 | from .utils import PLATFORM_MAPPING, exclude_lines, get_unified_diff
|
20 | 21 |
|
21 | 22 | try:
|
22 |
| - from extras.plugins import get_plugin_config |
| 23 | + from extras.plugins import get_installed_plugins, get_plugin_config |
23 | 24 | except ImportError:
|
24 |
| - from extras.plugins.utils import get_plugin_config |
| 25 | + from extras.plugins.utils import get_installed_plugins, get_plugin_config |
25 | 26 |
|
26 | 27 |
|
27 |
| -class ConfigDiffBase: |
| 28 | +class ConfigDiffBase(SecretsMixin): |
28 | 29 | site = ObjectVar(
|
29 | 30 | model=Site,
|
30 | 31 | required=False,
|
@@ -115,9 +116,13 @@ def log_results(self, device: DeviceDataClass) -> None:
|
115 | 116 | self.log_success(f"{device.name} no diff")
|
116 | 117 |
|
117 | 118 | def get_devices_with_rendered_configs(self, devices: Iterable[Device]) -> Iterator[DeviceDataClass]:
|
118 |
| - username = get_plugin_config("netbox_config_diff", "USERNAME") |
119 |
| - password = get_plugin_config("netbox_config_diff", "PASSWORD") |
| 119 | + if "netbox_secrets" in get_installed_plugins(): |
| 120 | + self.get_master_key() |
| 121 | + self.user_role = get_plugin_config("netbox_config_diff", "USER_SECRET_ROLE") |
| 122 | + self.password_role = get_plugin_config("netbox_config_diff", "PASSWORD_SECRET_ROLE") |
120 | 123 | for device in devices:
|
| 124 | + username, password = self.get_credentials(device) |
| 125 | + self.log_info(f"{username} {password}") |
121 | 126 | rendered_config = None
|
122 | 127 | error = None
|
123 | 128 | context_data = device.get_config_context()
|
@@ -173,3 +178,16 @@ def get_diff(self, devices: list[DeviceDataClass]) -> None:
|
173 | 178 | device.extra = diff_network_config(
|
174 | 179 | cleaned_config, device.rendered_config, PLATFORM_MAPPING[device.platform]
|
175 | 180 | )
|
| 181 | + |
| 182 | + def get_credentials(self, device: Device) -> tuple[str, str]: |
| 183 | + username = get_plugin_config("netbox_config_diff", "USERNAME") |
| 184 | + password = get_plugin_config("netbox_config_diff", "PASSWORD") |
| 185 | + if "netbox_secrets" in get_installed_plugins(): |
| 186 | + if secret := device.secrets.filter(role__name=self.user_role).first(): |
| 187 | + if value := self.get_secret(secret): |
| 188 | + username = value |
| 189 | + if secret := device.secrets.filter(role__name=self.password_role).first(): |
| 190 | + if value := self.get_secret(secret): |
| 191 | + password = value |
| 192 | + |
| 193 | + return username, password |
0 commit comments