]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
netcmd: models: improve Computer constructor adding "$" handling
authorRob van der Linde <rob@catalyst.net.nz>
Wed, 6 Mar 2024 03:47:29 +0000 (16:47 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 20 Mar 2024 03:49:34 +0000 (03:49 +0000)
In some cases the previous code would end up creating computers where the account name ended on double "$"

Rewrote constructor to handle more cases, for example only an account name is provided, only a name is provided, or both.

Signed-off-by: Rob van der Linde <rob@catalyst.net.nz>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/netcmd/domain/models/computer.py

index c9e034a530f981f6bd584442f571a253939e1205..84dddb16a9b5e4343205882f73d49c022961d583 100644 (file)
@@ -33,18 +33,39 @@ class Computer(User):
     def __init__(self, **kwargs):
         """Computer constructor automatically adds "$" to account_name.
 
-        Also applies to GroupManagedServiceAccount subclass.
-        """
-        name = kwargs.get("name", kwargs.get("cn"))
+        The various ways a Computer can be constructed:
+
+        >>> Computer(name="pc")
+        >>> Computer(account_name="pc$")
+        >>> Computer(cn="pc")
+        >>> Computer(account_name="pc$", name="pc")
+
+        In each case the constructor does its best to ensure the
+        account name ends with a "$" and the name doesn't.
+
+        Also applies to GroupManagedServiceAccount subclass."""
+        name = kwargs.get("name", kwargs.pop("cn", None))
         account_name = kwargs.get("account_name")
 
-        # If account_name is missing, use name or cn and add a "$".
-        # If account_name is present but lacking "$", add it automatically.
+        # First make sure the account_name always has a "$".
+        if account_name and not account_name.endswith("$"):
+            account_name += "$"
+
+        # The name is present but not account name.
+        # If the name already has a "$" don't add two.
         if name and not account_name:
-            kwargs["account_name"] = name + "$"
-        elif account_name and not account_name.endswith("$"):
-            kwargs["account_name"] = account_name + "$"
+            if name.endswith("$"):
+                account_name = name
+            else:
+                account_name = name + "$"
+
+        # The account name is present but not the name.
+        # Use the account name, stripping the "$" character.
+        elif account_name and not name:
+            name = account_name.rstrip("$")
 
+        kwargs["name"] = name
+        kwargs["account_name"] = account_name
         super().__init__(**kwargs)
 
     @staticmethod