]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
python: models: Computer constructor automatically adds "$" to account name
authorRob van der Linde <rob@catalyst.net.nz>
Fri, 23 Feb 2024 00:45:19 +0000 (13:45 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Fri, 1 Mar 2024 04:45:36 +0000 (04:45 +0000)
Signed-off-by: Rob van der Linde <rob@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
python/samba/netcmd/domain/models/computer.py
python/samba/tests/samba_tool/domain_models.py

index 297fe6c24ed68ce7316575bd06a258140bea6808..09869087cd347558af060b04eabb418ce1eca06f 100644 (file)
@@ -30,6 +30,23 @@ from .user import User
 class Computer(User):
     """A Computer is a type of User."""
 
+    def __init__(self, **kwargs):
+        """Computer constructor automatically adds "$" to username.
+
+        Also applies to GroupManagedServiceAccount subclass.
+        """
+        name = kwargs.get("name", kwargs.get("cn"))
+        username = kwargs.get("username")
+
+        # If the username is missing, use name or cn and add a "$".
+        # If the username is present but lacking "$", add it automatically.
+        if name and not username:
+            kwargs["username"] = name + "$"
+        elif username and not username.endswith("$"):
+            kwargs["username"] = username + "$"
+
+        super().__init__(**kwargs)
+
     @staticmethod
     def get_base_dn(ldb):
         """Return base Dn for Computers.
index 4ad28e11aed357f68eb8cd0c8f5eaa0579defff2..9cca7269d13aea977f8efa0f88ebe54761ea788e 100644 (file)
@@ -27,8 +27,8 @@ from xml.etree import ElementTree
 from ldb import FLAG_MOD_ADD, MessageElement, SCOPE_ONELEVEL
 from samba.dcerpc import security
 from samba.dcerpc.misc import GUID
-from samba.netcmd.domain.models import (AccountType, Group, Site, User,
-                                        StrongNTLMPolicy, fields)
+from samba.netcmd.domain.models import (AccountType, Computer, Group, Site,
+                                        User, StrongNTLMPolicy, fields)
 from samba.ndr import ndr_pack, ndr_unpack
 
 from .base import SambaToolCmdTest
@@ -72,6 +72,28 @@ class ModelTests(SambaToolCmdTest):
         self.assertEqual(robots + humans, robots_vs_humans)
 
 
+class ComputerModelTests(SambaToolCmdTest):
+
+    @classmethod
+    def setUpClass(cls):
+        cls.samdb = cls.getSamDB("-H", HOST, CREDS)
+        super().setUpClass()
+
+    def test_computer_constructor(self):
+        comp1 = Computer(name="comp1")
+        self.assertEqual(comp1.username, "comp1$")
+
+        comp2 = Computer(cn="comp2")
+        self.assertEqual(comp2.username, "comp2$")
+
+        # User accidentally left out '$' in username.
+        comp3 = Computer(name="comp3", username="comp3")
+        self.assertEqual(comp3.username, "comp3$")
+
+        comp4 = Computer(cn="comp4", username="comp4$")
+        self.assertEqual(comp4.username, "comp4$")
+
+
 class FieldTestMixin:
     """Tests a model field to ensure it behaves correctly in both directions.