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