Repository: cobbler/cobbler

Issue:
[Backport] Anamon cannot send logs to cobbler server
### Original feature issue

- Issue: #3324
- PR: #3326

### Target release

- [x] release33
- [x] release32
- [ ] release30

### Reason

Stability for older Cobbler version that the community is using.



Discussion:

Resolving patch:
diff --git a/cobbler/remote.py b/cobbler/remote.py
--- a/cobbler/remote.py
+++ b/cobbler/remote.py
@@ -2389,29 +2389,35 @@ def upload_log_data(self, sys_name, file, size, offset, data, token=None):
         :param token: The API-token obtained via the login() method.
         :return: True if everything succeeded.
         """
-        if not self.__validate_log_data_params(sys_name, file, size, offset, data, token):
-            return False
-        self._log("upload_log_data (file: '%s', size: %s, offset: %s)" % (file, size, offset), token=token, name=sys_name)
-
         # Check if enabled in self.api.settings()
         if not self.api.settings().anamon_enabled:
             # feature disabled!
             return False
 
-        # Find matching system record
-        obj = self.api.find_system(name=sys_name)
-        if obj is None:
-            # system not found!
-            self._log("upload_log_data - WARNING - system '%s' not found in Cobbler" % sys_name, token=token, name=sys_name)
+        if not self.__validate_log_data_params(sys_name, file, size, offset, data.data, token):
             return False
+        self._log("upload_log_data (file: '%s', size: %s, offset: %s)" % (file, size, offset), token=token, name=sys_name)
+
+        # Find matching system or profile record
+        obj = self.api.find_system(name=sys_name)
+        if obj is None or isinstance(obj, list):
+            obj = self.api.find_profile(name=sys_name)
+            if obj is None or isinstance(obj, list):
+                # system or profile not found!
+                self._log(
+                    "upload_log_data - WARNING - system or profile not found in Cobbler",
+                    token=token,
+                    name=sys_name,
+                )
+                return False
 
-        return self.__upload_file(obj.name, file, size, offset, data)
+        return self.__upload_file(obj.name, file, size, offset, data.data)
 
     def __validate_log_data_params(self, sys_name: str, logfile_name: str, size: int, offset: int, data: bytes,
                                    token: Optional[str] = None) -> bool:
         # Validate all types
         if not (isinstance(sys_name, str) and isinstance(logfile_name, str) and isinstance(size, int)
-                and isinstance(offset, int) and isinstance(data, bytes)):
+                and isinstance(offset, int) and isinstance(data.data, bytes)):
             self.logger.warning("upload_log_data - One of the parameters handed over had an invalid type!")
             return False
         if token is not None and not isinstance(token, str):
@@ -2439,7 +2445,6 @@ def __upload_file(self, sys_name, logfile_name, size, offset, data):
         :param data: base64 encoded file contents
         :return: True if the action succeeded.
         """
-        contents = base64.decodebytes(data)
         # FIXME: Get the base directory from Cobbler app-settings
         anamon_base_directory = "/var/log/cobbler/anamon"
         anamon_sys_directory = os.path.join(anamon_base_directory, sys_name)
@@ -2468,7 +2473,7 @@ def __upload_file(self, sys_name, logfile_name, size, offset, data):
         fd = os.open(file_name, os.O_RDWR | os.O_CREAT | os.O_CLOEXEC, 0o644)
         # log_error("fd=%r" %fd)
         try:
-            if offset == 0 or (offset == -1 and size == len(contents)):
+            if offset == 0 or (offset == -1 and size == len(data)):
                 # truncate file
                 fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
                 try:
@@ -2481,12 +2486,12 @@ def __upload_file(self, sys_name, logfile_name, size, offset, data):
             else:
                 os.lseek(fd, offset, 0)
             # write contents
-            fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB, len(contents), 0, 2)
+            fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB, len(data), 0, 2)
             try:
-                os.write(fd, contents)
+                os.write(fd, data)
                 # log_error("wrote contents")
             finally:
-                fcntl.lockf(fd, fcntl.LOCK_UN, len(contents), 0, 2)
+                fcntl.lockf(fd, fcntl.LOCK_UN, len(data), 0, 2)
             if offset == -1:
                 if size is not None:
                     # truncate file


Test patch: