Blame SOURCES/0024-wgi-plugins.py-ignore-empty-plugin-directories_rhbz#1895910.patch

cce5df
From 29262465edf034d521c165e3854e28835d86b98d Mon Sep 17 00:00:00 2001
cce5df
From: Alexander Bokovoy <abokovoy@redhat.com>
cce5df
Date: Fri, 6 Nov 2020 09:53:35 +0200
cce5df
Subject: [PATCH] wgi/plugins.py: ignore empty plugin directories
cce5df
cce5df
Dynamic plugin registry returns as a plugin any folder within the
cce5df
plugins directory. Web UI then attempts to load for each plugin 'foo' a
cce5df
JavaScript file named 'foo/foo.js'. The problem is that if 'foo/foo.js'
cce5df
does not exist, Web UI breaks and it is impossible to recover until the
cce5df
empty folder is removed or 'foo/foo.js' (even empty) is created at the
cce5df
server side.
cce5df
cce5df
Check that 'foo/foo.js' actual exists when including a plugin into the
cce5df
registry.
cce5df
cce5df
Test the registry generator by creating fake plugins and removing them
cce5df
during the test.
cce5df
cce5df
Fixes: https://pagure.io/freeipa/issue/8567
cce5df
cce5df
Signed-off-by: Alexander Bokovoy <abokovoy@redhat.com>
cce5df
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
cce5df
---
cce5df
 install/wsgi/plugins.py                   |  5 +-
cce5df
 ipatests/test_ipaserver/test_jsplugins.py | 68 +++++++++++++++++++++++
cce5df
 2 files changed, 72 insertions(+), 1 deletion(-)
cce5df
 create mode 100644 ipatests/test_ipaserver/test_jsplugins.py
cce5df
cce5df
diff --git a/install/wsgi/plugins.py b/install/wsgi/plugins.py
cce5df
index f80cfb9fe..4c43e7f87 100644
cce5df
--- a/install/wsgi/plugins.py
cce5df
+++ b/install/wsgi/plugins.py
cce5df
@@ -36,7 +36,10 @@ def get_plugin_index():
cce5df
 
cce5df
     dirs = os.listdir(paths.IPA_JS_PLUGINS_DIR)
cce5df
     index = 'define([],function(){return['
cce5df
-    index += ','.join("'"+x+"'" for x in dirs)
cce5df
+    for x in dirs:
cce5df
+        p = os.path.join(paths.IPA_JS_PLUGINS_DIR, x, x + '.js')
cce5df
+        if os.path.exists(p):
cce5df
+            index += "'" + x + "',"
cce5df
     index += '];});'
cce5df
     return index.encode('utf-8')
cce5df
 
cce5df
diff --git a/ipatests/test_ipaserver/test_jsplugins.py b/ipatests/test_ipaserver/test_jsplugins.py
cce5df
new file mode 100644
cce5df
index 000000000..354e6992c
cce5df
--- /dev/null
cce5df
+++ b/ipatests/test_ipaserver/test_jsplugins.py
cce5df
@@ -0,0 +1,68 @@
cce5df
+# Copyright (C) 2020  FreeIPA Contributors see COPYING for license
cce5df
+
cce5df
+import os
cce5df
+import pytest
cce5df
+
cce5df
+from ipatests.test_ipaserver.httptest import Unauthorized_HTTP_test
cce5df
+from ipatests.util import assert_equal, assert_not_equal
cce5df
+from ipaplatform.paths import paths
cce5df
+
cce5df
+
cce5df
+@pytest.mark.tier1
cce5df
+class test_jsplugins(Unauthorized_HTTP_test):
cce5df
+    app_uri = '/ipa/ui/js/freeipa/plugins.js'
cce5df
+    jsplugins = (('foo', 'foo.js'), ('bar', ''))
cce5df
+    content_type = 'application/javascript'
cce5df
+
cce5df
+    def test_jsplugins(self):
cce5df
+        empty_response = "define([],function(){return[];});"
cce5df
+
cce5df
+        # Step 1: make sure default response has no additional plugins
cce5df
+        response = self.send_request(method='GET')
cce5df
+        assert_equal(response.status, 200)
cce5df
+        response_data = response.read().decode(encoding='utf-8')
cce5df
+        assert_equal(response_data, empty_response)
cce5df
+
cce5df
+        # Step 2: add fake plugins
cce5df
+        try:
cce5df
+            for (d, f) in self.jsplugins:
cce5df
+                dir = os.path.join(paths.IPA_JS_PLUGINS_DIR, d)
cce5df
+                if not os.path.exists(dir):
cce5df
+                    os.mkdir(dir, 0o755)
cce5df
+                if f:
cce5df
+                    with open(os.path.join(dir, f), 'w') as js:
cce5df
+                        js.write("/* test js plugin */")
cce5df
+
cce5df
+        except OSError as e:
cce5df
+            pytest.skip(
cce5df
+                'Cannot set up test JS plugin: %s' % e
cce5df
+            )
cce5df
+
cce5df
+        # Step 3: query plugins to see if our plugins exist
cce5df
+        response = self.send_request(method='GET')
cce5df
+        assert_equal(response.status, 200)
cce5df
+        response_data = response.read().decode(encoding='utf-8')
cce5df
+        assert_not_equal(response_data, empty_response)
cce5df
+        for (d, f) in self.jsplugins:
cce5df
+            if f:
cce5df
+                assert "'" + d + "'" in response_data
cce5df
+            else:
cce5df
+                assert "'" + d + "'" not in response_data
cce5df
+
cce5df
+        # Step 4: remove fake plugins
cce5df
+        try:
cce5df
+            for (d, f) in self.jsplugins:
cce5df
+                dir = os.path.join(paths.IPA_JS_PLUGINS_DIR, d)
cce5df
+                file = os.path.join(dir, f)
cce5df
+                if f and os.path.exists(file):
cce5df
+                    os.unlink(file)
cce5df
+                if os.path.exists(dir):
cce5df
+                    os.rmdir(dir)
cce5df
+        except OSError:
cce5df
+            pass
cce5df
+
cce5df
+        # Step 5: make sure default response has no additional plugins
cce5df
+        response = self.send_request(method='GET')
cce5df
+        assert_equal(response.status, 200)
cce5df
+        response_data = response.read().decode(encoding='utf-8')
cce5df
+        assert_equal(response_data, empty_response)
cce5df
-- 
cce5df
2.29.2
cce5df