[java-identity-provider] 02/03: IDP-1575 Start to flesh out the plugin installer

Rod Widdowson rdw at steadingsoftware.com
Mon Jun 22 15:56:26 UTC 2020


This is an automated email from the git hooks/post-receive script.

rdw pushed a commit to branch master
in repository java-identity-provider.

View the commit online:
http://git.shibboleth.net/view/?p=java-identity-provider.git;a=commit;h=70afb433af460b80bd0fcbf1d3c26bef5880a7df

commit 70afb433af460b80bd0fcbf1d3c26bef5880a7df
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Tue Jun 16 16:55:39 2020 +0100

    IDP-1575 Start to flesh out the plugin installer
    
    https://issues.shibboleth.net/jira/browse/IDP-1595
    
    Starting with the most challenging - a method to provide the list of
    all installed plugins back to a stand alone program.
    
    Also add test, which sees the start of the test resource tree gaining
    an outline "idp.home" folder for the tests to play against.
---
 .../idp/installer/plugin/impl/PluginInstaller.java | 138 +++++++++++++++++++++
 .../installer/plugin/impl/PluginInstallerTest.java |  71 +++++++++++
 ...utilities.java.support.plugin.PluginDescription |   1 +
 3 files changed, 210 insertions(+)

diff --git a/idp-installer/src/main/java/net/shibboleth/idp/installer/plugin/impl/PluginInstaller.java b/idp-installer/src/main/java/net/shibboleth/idp/installer/plugin/impl/PluginInstaller.java
new file mode 100644
index 000000000..42834733c
--- /dev/null
+++ b/idp-installer/src/main/java/net/shibboleth/idp/installer/plugin/impl/PluginInstaller.java
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements.  See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.shibboleth.idp.installer.plugin.impl;
+
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.ServiceLoader;
+import java.util.stream.Collectors;
+
+import javax.annotation.Nonnull;
+
+import org.apache.tools.ant.BuildException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import net.shibboleth.utilities.java.support.annotation.constraint.NonnullAfterInit;
+import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
+import net.shibboleth.utilities.java.support.component.AbstractInitializableComponent;
+import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
+import net.shibboleth.utilities.java.support.logic.Constraint;
+import net.shibboleth.utilities.java.support.plugin.PluginDescription;
+import net.shibboleth.utilities.java.support.primitive.StringSupport;
+
+/**
+ *  The class where the heavy lifting of managing a plugin happens. 
+ */
+public final class PluginInstaller extends AbstractInitializableComponent {
+
+    /** Class logger. */
+    @Nonnull private final Logger log = LoggerFactory.getLogger(PluginInstaller.class);
+
+    /** Where we are installing to. */
+    @NonnullAfterInit private Path idpHome;
+    
+    /** What we are dealing with. */
+    @NonnullAfterInit private String pluginId;
+    
+    /** Our TrustStore. */
+    @NonnullAfterInit private TrustStore trustStore;
+      
+    /** set IdP Home.
+     * @param home Where we are working from
+     */
+    public void setIdpHome(@Nonnull final Path home) {
+        idpHome = Constraint.isNotNull(home, "IdPHome should be non-null");
+    }
+
+    /** Set the plugin in.
+     * @param id The pluginId to set.
+     */
+    public void setPluginId( @Nonnull @NotEmpty final String id) {
+        pluginId = Constraint.isNotNull(StringSupport.trimOrNull(id), "Plugin id should be be non-null");
+    }    
+
+    /** {@inheritDoc} */
+    protected void doInitialize() throws ComponentInitializationException {
+        trustStore = new TrustStore();
+        trustStore.setIdpHome(idpHome);
+        trustStore.setPluginId(pluginId);
+        trustStore.initialize();
+        super.doInitialize();
+    }
+    
+    /** Install the plugin from the provided URL.  Involves downloading
+     *  the file and then doing a {@link #installPlugin(Path, String, boolean)}.
+     * @param baseURL where we get the files from
+     * @param fileName the name
+     * @param isTgz true if this is tgz, false if this is zip
+     */
+    public void installPlugin(@Nonnull final URL baseURL,
+                              @Nonnull @NotEmpty final String fileName,
+                              final boolean isTgz) {
+        
+    }
+    
+    /** Install the plugin from a local path.
+     * <ul><li> Check signature</li>
+     * <li>Unpack to temp folder</li>
+     * <li>Install from the folder</li></ul>
+     * @param base the directory where the files are
+     * @param fileName the name
+     * @param isTgz true if this is tgz, false if this is zip
+     */
+    public  void installPlugin(@Nonnull final Path base, 
+                               @Nonnull @NotEmpty final String fileName,
+                               final boolean isTgz) {
+        
+    }
+    
+    /**
+     * Return a list of the installed plugins.
+     * @return All the plugins.
+     */
+    public List<PluginDescription> getInstalledPlugins() {
+        try {
+            final List<URL> urls = new ArrayList<>();
+            
+            for (final Path webApp : Files.newDirectoryStream(idpHome.resolve("dist"), "edit-webapp-*")) {
+                for (final Path jar : Files.newDirectoryStream(webApp.resolve("WEB-INF").resolve("lib"))) {
+                    urls.add(jar.toUri().toURL());
+                }
+            }
+            
+           try (final URLClassLoader loader = new URLClassLoader(urls.toArray(URL[]::new))){
+               
+               return ServiceLoader.load(PluginDescription.class, loader).
+                   stream().
+                   map(ServiceLoader.Provider::get).
+                   collect(Collectors.toList());
+           }
+            
+        } catch (final IOException e) {
+            throw new BuildException(e);
+        }
+    }
+    
+}
+
diff --git a/idp-installer/src/test/java/net/shibboleth/idp/installer/plugin/impl/PluginInstallerTest.java b/idp-installer/src/test/java/net/shibboleth/idp/installer/plugin/impl/PluginInstallerTest.java
new file mode 100644
index 000000000..13034234f
--- /dev/null
+++ b/idp-installer/src/test/java/net/shibboleth/idp/installer/plugin/impl/PluginInstallerTest.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements.  See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.shibboleth.idp.installer.plugin.impl;
+
+import static org.testng.Assert.assertEquals;
+
+import java.io.IOException;
+import java.util.List;
+
+import org.springframework.core.io.ClassPathResource;
+import org.testng.annotations.Test;
+
+import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
+import net.shibboleth.utilities.java.support.plugin.PluginDescription;
+import net.shibboleth.utilities.java.support.resource.Resource;
+
+ at SuppressWarnings("javadoc")
+public class PluginInstallerTest {
+
+    @Test public void TestListing() throws ComponentInitializationException, IOException {
+        PluginInstaller inst = new PluginInstaller();
+        inst.setIdpHome(new ClassPathResource("idphome-test").getFile().toPath());
+        inst.setPluginId("net.shibboleth.plugins.scripting.nashorn");
+        inst.initialize();
+        List<PluginDescription> plugins = inst.getInstalledPlugins();
+        assertEquals(plugins.get(0).getPluginId(), "org.example.Plugin");
+    }
+    
+    public static class Wibble extends PluginDescription {
+
+        /** {@inheritDoc} */
+        public String getPluginId() {
+            
+            return "org.example.Plugin";
+        }
+
+        /** {@inheritDoc} */
+        public List<Resource> getUpdateResources() throws IOException {
+            // TODO Auto-generated method stub
+            return null;
+        }
+
+        /** {@inheritDoc} */
+        public int getMajorVersion() {
+            // TODO Auto-generated method stub
+            return 0;
+        }
+
+        /** {@inheritDoc} */
+        public int getMinorVersion() {
+            // TODO Auto-generated method stub
+            return 0;
+        }
+        
+    }
+}
diff --git a/idp-installer/src/test/resources/idphome-test/dist/org.example.Plugin/WEB-INF/lib/RandomFileName/META-INF/services/net.shibboleth.utilities.java.support.plugin.PluginDescription b/idp-installer/src/test/resources/idphome-test/dist/org.example.Plugin/WEB-INF/lib/RandomFileName/META-INF/services/net.shibboleth.utilities.java.support.plugin.PluginDescription
new file mode 100644
index 000000000..0aeabcaf2
--- /dev/null
+++ b/idp-installer/src/test/resources/idphome-test/dist/org.example.Plugin/WEB-INF/lib/RandomFileName/META-INF/services/net.shibboleth.utilities.java.support.plugin.PluginDescription
@@ -0,0 +1 @@
+net.shibboleth.idp.installer.plugin.impl.PluginInstallerTest$Wibble

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list