From da74aec056526ecb23075a89ad3cd1fe92b0bf6d Mon Sep 17 00:00:00 2001 From: GabbasovDinar Date: Tue, 14 Jan 2025 14:50:06 +0500 Subject: [PATCH] [IMP] cetmix_tower_server: Access to server fields MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Limit access to the following fields of the cx.tower.server ​odel to Manager and Root: - OS - IP v4 Address - IP v6 Address - SSH username - SSH Port - SSS auth mode - SSH key - Use sudo Task: 4116 --- cetmix_tower_server/models/cx_tower_server.py | 42 +++++++++++---- cetmix_tower_server/readme/CONFIGURE.md | 2 + .../views/cx_tower_server_view.xml | 51 ++++++++++++------- 3 files changed, 65 insertions(+), 30 deletions(-) diff --git a/cetmix_tower_server/models/cx_tower_server.py b/cetmix_tower_server/models/cx_tower_server.py index 689b0ce4..9b4100e4 100644 --- a/cetmix_tower_server/models/cx_tower_server.py +++ b/cetmix_tower_server/models/cx_tower_server.py @@ -279,15 +279,29 @@ class CxTowerServer(models.Model): ) # ---- Connection - ip_v4_address = fields.Char(string="IPv4 Address") - ip_v6_address = fields.Char(string="IPv6 Address") - ssh_port = fields.Char(string="SSH port", required=True, default="22") - ssh_username = fields.Char(string="SSH Username", required=True) - ssh_password = fields.Char(string="SSH Password") + ip_v4_address = fields.Char( + string="IPv4 Address", groups="cetmix_tower_server.group_manager" + ) + ip_v6_address = fields.Char( + string="IPv6 Address", groups="cetmix_tower_server.group_manager" + ) + ssh_port = fields.Char( + string="SSH port", + required=True, + default="22", + groups="cetmix_tower_server.group_manager", + ) + ssh_username = fields.Char( + string="SSH Username", required=True, groups="cetmix_tower_server.group_manager" + ) + ssh_password = fields.Char( + string="SSH Password", groups="cetmix_tower_server.group_manager" + ) ssh_key_id = fields.Many2one( comodel_name="cx.tower.key", string="SSH Private Key", domain=[("key_type", "=", "k")], + groups="cetmix_tower_server.group_manager", ) ssh_auth_mode = fields.Selection( string="SSH Auth Mode", @@ -297,11 +311,13 @@ class CxTowerServer(models.Model): ], default="p", required=True, + groups="cetmix_tower_server.group_manager", ) use_sudo = fields.Selection( string="Use sudo", selection=[("n", "Without password"), ("p", "With password")], help="Run commands using 'sudo'", + groups="cetmix_tower_server.group_manager", ) # ---- Variables variable_value_ids = fields.One2many( @@ -317,7 +333,11 @@ class CxTowerServer(models.Model): ) # ---- Attributes - os_id = fields.Many2one(string="Operating System", comodel_name="cx.tower.os") + os_id = fields.Many2one( + string="Operating System", + comodel_name="cx.tower.os", + groups="cetmix_tower_server.group_manager", + ) tag_ids = fields.Many2many( comodel_name="cx.tower.tag", relation="cx_tower_server_tag_rel", @@ -540,6 +560,7 @@ def _connect(self, raise_on_error=True): Defaults to True. """ self.ensure_one() + self = self.sudo() try: client = SSH( host=self.ip_v4_address or self.ip_v6_address, @@ -653,7 +674,7 @@ def _render_command(self, command, path=None): # Get variable values for current server variable_values_dict = ( - self.get_variable_values(variables) # pylint: disable=no-member + self.sudo().get_variable_values(variables) # pylint: disable=no-member if variables else False ) @@ -717,14 +738,13 @@ def execute_command( dict(): command execution result if `no_log` context value == True else None """ self.ensure_one() - # Populate `sudo` value from the server settings if not provided explicitly if sudo is None: - if self.ssh_username != "root" and self.use_sudo: - sudo = self.use_sudo + if self.sudo().ssh_username != "root" and self.sudo().use_sudo: + sudo = self.sudo().use_sudo # Disable `sudo` if user is root - elif sudo and self.ssh_username == "root": + elif sudo and self.sudo().ssh_username == "root": sudo = None # Check if no log record should be created diff --git a/cetmix_tower_server/readme/CONFIGURE.md b/cetmix_tower_server/readme/CONFIGURE.md index 6fcc90c8..bb1a86be 100644 --- a/cetmix_tower_server/readme/CONFIGURE.md +++ b/cetmix_tower_server/readme/CONFIGURE.md @@ -40,6 +40,8 @@ Fill the values it the tabs below: - **SSH Private Key**: Used for authentication is SSH Auth Mode is set to "Key" - **Note**: Comments or user notes +Note: Some fields are visible based on the current user access level. + There is a special **Status** field which indicates current Server status. It is meant to be updated automatically using external API with further customizations. Following pre-defined statuses are available: diff --git a/cetmix_tower_server/views/cx_tower_server_view.xml b/cetmix_tower_server/views/cx_tower_server_view.xml index 474b3bc2..387b3588 100644 --- a/cetmix_tower_server/views/cx_tower_server_view.xml +++ b/cetmix_tower_server/views/cx_tower_server_view.xml @@ -81,24 +81,6 @@ Partner: -
- Operating System: - -
-
- IPv4 Address: - -
-
- IPv6 Address: - -
@@ -123,6 +105,36 @@ + + cx.tower.server.view.kanban + cx.tower.server + + + + +
+ Operating System: + +
+
+ IPv4 Address: + +
+
+ IPv6 Address: + +
+
+
+
+ + cx.tower.server.view.tree cx.tower.server @@ -360,7 +372,7 @@ - +