[java-idp-plugin-metadatagen] branch dev/JMETAGEN-5 updated: JMETAGEN-5 - Metadata generation

Scott Cantor cantor.2 at osu.edu
Thu Jul 13 18:56:29 UTC 2023


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

scantor pushed a commit to branch dev/JMETAGEN-5
in repository java-idp-plugin-metadatagen.

View the commit online:
http://git.shibboleth.net/view/?p=java-idp-plugin-metadatagen.git;a=commit;h=5ed5ff42792b66dd3d58cf54f99c2d2ca7f5e6a7

The following commit(s) were added to refs/heads/dev/JMETAGEN-5 by this push:
     new 5ed5ff4  JMETAGEN-5 - Metadata generation
5ed5ff4 is described below

commit 5ed5ff42792b66dd3d58cf54f99c2d2ca7f5e6a7
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Thu Jul 13 14:56:01 2023 -0400

    JMETAGEN-5 - Metadata generation
    
    https://shibboleth.atlassian.net/browse/JMETAGEN-5
    
    First stab at batch support.
    Handles spaces through escaping with backslash.
    Individual command blocks in a batch are terminated by a period.
---
 .../plugin/metadatagen/impl/MetadataGenCLI.java    | 97 +++++++++++++++++++++-
 .../impl/MetadataGenCommandLineArguments.java      | 21 ++++-
 .../plugin/metadatagen/impl/MetadataGenTest.java   | 12 ++-
 .../idp/plugin/metadatagen/impl/batch.txt          | 29 +++++++
 .../shibboleth/idp/plugin/metadatagen/impl/extra1  |  2 -
 .../shibboleth/idp/plugin/metadatagen/impl/extra2  | 16 ----
 6 files changed, 154 insertions(+), 23 deletions(-)

diff --git a/metadatagen-impl/src/main/java/net/shibboleth/idp/plugin/metadatagen/impl/MetadataGenCLI.java b/metadatagen-impl/src/main/java/net/shibboleth/idp/plugin/metadatagen/impl/MetadataGenCLI.java
index e765b0d..7cea70d 100644
--- a/metadatagen-impl/src/main/java/net/shibboleth/idp/plugin/metadatagen/impl/MetadataGenCLI.java
+++ b/metadatagen-impl/src/main/java/net/shibboleth/idp/plugin/metadatagen/impl/MetadataGenCLI.java
@@ -23,6 +23,10 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.Writer;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
@@ -41,7 +45,10 @@ import org.springframework.beans.BeansException;
 import org.springframework.core.io.ClassPathResource;
 import org.springframework.core.io.Resource;
 
+import com.beust.jcommander.JCommander;
+
 import net.shibboleth.idp.cli.AbstractIdPHomeAwareCommandLine;
+import net.shibboleth.shared.annotation.constraint.Live;
 import net.shibboleth.shared.annotation.constraint.NotEmpty;
 import net.shibboleth.shared.annotation.constraint.NotLive;
 import net.shibboleth.shared.annotation.constraint.Unmodifiable;
@@ -117,6 +124,56 @@ public final class MetadataGenCLI extends AbstractIdPHomeAwareCommandLine<Metada
             }
         }
     }
+    
+    /**
+     * Parse arguments from a line containing white-space-delimited options and values.
+     * 
+     * <p>Spaces escaped with a backslash are included in arguments, all other whitespace is
+     * ignored.</p>
+     * 
+     * @param args live accumulator of arguments
+     * @param line input line
+     */
+    private void addBatchArguments(@Nonnull @Live final Collection<String> args, @Nonnull final String line) {
+        int pos = 0;
+        boolean escaped = false;
+        StringBuilder argBuilder = null;
+        while (pos < line.length()) {
+            char current = line.charAt(pos++);
+            
+            // Are we processing an escaped character?
+            if (escaped) {
+                if (argBuilder != null) {
+                    argBuilder.append(current);
+                }
+                escaped = false;
+                continue;
+            }
+            
+            // Otherwise, on whitespace we close out an argument,
+            // on backslash we prepare to escape the next character, and
+            // anything else gets added to the current or a new argument.
+            
+            if (Character.isWhitespace(current)) {
+                if (argBuilder != null) {
+                    // Close out argument.
+                    args.add(argBuilder.toString());
+                    argBuilder = null;
+                }
+            } else if (current == '\\') {
+                escaped = true;
+            } else {
+                if (argBuilder == null) {
+                    argBuilder = new StringBuilder();
+                }
+                argBuilder.append(current);
+            }
+        }
+        
+        if (argBuilder != null) {
+            args.add(argBuilder.toString());
+        }
+    }
 
     /** {@inheritDoc} */
     @Override
@@ -127,6 +184,34 @@ public final class MetadataGenCLI extends AbstractIdPHomeAwareCommandLine<Metada
         if (ret != RC_OK) {
             return ret;
         }
+        
+        final List<MetadataGenCommandLineArguments> batchList;
+        if (args.getBatchFile() != null) {
+            batchList = new ArrayList<>();
+            try {
+                final List<String> lines = Files.readAllLines(Path.of(args.getBatchFile()));
+                List<String> args = new ArrayList<>();
+                for (final String line : lines) {
+                    if (".".equals(line)) {
+                        // End of entry.
+                        args.add("--omit-namespaces");
+                        final MetadataGenCommandLineArguments argObject = new MetadataGenCommandLineArguments();
+                        final JCommander jc = new JCommander(argObject);
+                        jc.setCaseSensitiveOptions(true);
+                        jc.parse(args.toArray(new String[args.size()]));
+                        batchList.add(argObject);
+                        args.clear();
+                    } else {
+                        addBatchArguments(args, line);
+                    }
+                }
+            } catch (final IOException e) {
+                getLogger().error("Error reading in batch file", e);
+                return RC_IO;
+            }
+        } else {
+            batchList = CollectionSupport.emptyList();
+        }
 
         final VelocityEngine velocityEngine;
         try {
@@ -147,8 +232,16 @@ public final class MetadataGenCLI extends AbstractIdPHomeAwareCommandLine<Metada
             generator.setVelocityEngine(velocityEngine);
             generator.initialize();
 
-            assert args != null;
-            generator.generate(args, sink);
+            if (batchList.isEmpty()) {
+                assert args != null;
+                generator.generate(args, sink);
+            } else {
+                sink.write("<md:EntitiesDescriptor xmlns:md=\"urn:oasis:names:tc:SAML:2.0:metadata\" xmlns:mdattr=\"urn:oasis:names:tc:SAML:metadata:attribute\" xmlns:mdui=\"urn:oasis:names:tc:SAML:metadata:ui\" xmlns:saml=\"urn:oasis:names:tc:SAML:2.0:assertion\" xmlns:shibmd=\"urn:mace:shibboleth:metadata:1.0\" xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\">\n\n");
+                for (final MetadataGenCommandLineArguments batchEntry : batchList) {
+                    generator.generate(batchEntry, sink);
+                }
+                sink.write("\n</md:EntitiesDescriptor>\n");
+            }
             sink.close();
         } catch (final Exception e) {
             getLogger().error("Error generating output", e);
diff --git a/metadatagen-impl/src/main/java/net/shibboleth/idp/plugin/metadatagen/impl/MetadataGenCommandLineArguments.java b/metadatagen-impl/src/main/java/net/shibboleth/idp/plugin/metadatagen/impl/MetadataGenCommandLineArguments.java
index 877d907..6716e88 100644
--- a/metadatagen-impl/src/main/java/net/shibboleth/idp/plugin/metadatagen/impl/MetadataGenCommandLineArguments.java
+++ b/metadatagen-impl/src/main/java/net/shibboleth/idp/plugin/metadatagen/impl/MetadataGenCommandLineArguments.java
@@ -100,7 +100,7 @@ public class MetadataGenCommandLineArguments extends AbstractIdPHomeAwareCommand
     @Nullable private Multimap<String,String> tagMap;
     
     /** The unique ID. */
-    @Parameter(names = {"--entityID", "--client_id", "--id"}, required=true, description="Unique ID for entity")
+    @Parameter(names = {"--entityID", "--client_id", "--id"}, description="Unique ID for entity")
     @Nullable private String entityID;
 
     /** Scopes. */
@@ -211,6 +211,10 @@ public class MetadataGenCommandLineArguments extends AbstractIdPHomeAwareCommand
     @Parameter(names = {"--file", "--out"}, description="Path to output file (stdout otherwise)")
     @Nullable private String outputFile;
 
+    /** Batch file path. */
+    @Parameter(names = {"--batch-file", "--batch"}, description="Path to a batch of commands to run")
+    @Nullable private String batchFile;
+    
     /** {@inheritDoc} */
     @Nullable public String getEntityID() {
         return entityID;
@@ -543,7 +547,16 @@ public class MetadataGenCommandLineArguments extends AbstractIdPHomeAwareCommand
     @Nullable public String getOutputFile() {
         return outputFile;
     }
-    
+
+    /**
+     * Batch file path.
+     * 
+     * @return argument value
+     */
+    @Nullable public String getBatchFile() {
+        return batchFile;
+    }
+
     /** {@inheritDoc} */
     @Override
     @Nonnull public synchronized Logger getLog() {
@@ -566,6 +579,8 @@ public class MetadataGenCommandLineArguments extends AbstractIdPHomeAwareCommand
     @Override
     public void printHelp(@Nonnull final PrintStream out) {
         super.printHelp(out);
+        
+        
         out.println(String.format("  %-20s %s", "--entityID, --client_id, --id", "The entityID (or client_id, etc.)"));
         out.println(String.format("  %-20s %s", "--scope", "Scope extension value"));
         
@@ -603,6 +618,8 @@ public class MetadataGenCommandLineArguments extends AbstractIdPHomeAwareCommand
         
         out.println(String.format("  %-20s %s", "--omit-namespaces", "Omit namespaces on root element."));
         out.println(String.format("  %-20s %s", "--output, --out", "Output file path."));
+        
+        out.println(String.format("  %-20s %s", "--batch-file, --batch", "Batch file path."));
         out.println();
     }
     
diff --git a/metadatagen-impl/src/test/java/net/shibboleth/idp/plugin/metadatagen/impl/MetadataGenTest.java b/metadatagen-impl/src/test/java/net/shibboleth/idp/plugin/metadatagen/impl/MetadataGenTest.java
index e663fcc..11d2965 100644
--- a/metadatagen-impl/src/test/java/net/shibboleth/idp/plugin/metadatagen/impl/MetadataGenTest.java
+++ b/metadatagen-impl/src/test/java/net/shibboleth/idp/plugin/metadatagen/impl/MetadataGenTest.java
@@ -32,7 +32,8 @@ public class MetadataGenTest {
     //   2) Edit the setting of the property 'idp.metadata.backchannel.cert' in
     //      src/test/resources/net/shibboleth/idp/plugin/metadatagen/impl/extra2
     private final static String IDP_HOME =  "/Users/scantor/Documents/shibboleth5/java-identity-provider/idp-conf-impl/src/main/resources/net/shibboleth/idp/module";
-    private final boolean enabled = true;
+    private final static String BATCH_PATH =  "src/test/resources/net/shibboleth/idp/plugin/metadatagen/impl/batch.txt";
+    private final boolean enabled = false;
 
     @Test(enabled = enabled) public void testSimple() {
         assertEquals(MetadataGenCLI.runMain(
@@ -70,4 +71,13 @@ public class MetadataGenTest {
             AbstractCommandLine.RC_OK);
     }
     
+    @Test(enabled = enabled) public void testBatch() {
+        assertEquals(MetadataGenCLI.runMain(
+                new String[] {
+                        "--home", IDP_HOME,
+                        "--batch", BATCH_PATH,
+                        }),
+            AbstractCommandLine.RC_OK);
+    }
+
 }
\ No newline at end of file
diff --git a/metadatagen-impl/src/test/resources/net/shibboleth/idp/plugin/metadatagen/impl/batch.txt b/metadatagen-impl/src/test/resources/net/shibboleth/idp/plugin/metadatagen/impl/batch.txt
new file mode 100644
index 0000000..8737aee
--- /dev/null
+++ b/metadatagen-impl/src/test/resources/net/shibboleth/idp/plugin/metadatagen/impl/batch.txt
@@ -0,0 +1,29 @@
+--lang US-en
+--logo https://idp.example.org/logo.png
+--logo-width 128
+-o Example\ &\ Org
+-u https://example.org?foo=bar&foo=baz
+-a /Bono/
+-t John/Doe/mailto:doe at example.org
+--contact-support Help\ &\ Desk//support at example.org
+--sp -w
+--idp
+--aa
+--entityID https://sp.example.org
+--scope example.org
+--nameid-format NameIDType.EMAIL
+--cert /Users/scantor/Desktop/webauth2.crt
+--sso Redirect1/idp.example.org/idp/profile/SAML/SSO/Redirect
+--sso Redirect/idp.example.org/idp/profile/SAML2/SSO/Redirect
+--sso POST/idp.example.org/idp/profile/SAML2/SSO/POST
+--sso SOAP/idp.example.org/idp/profile/SAML2/SSO/SOAP
+--query SOAP/idp.example.org:8443/idp/profile/SAML2/AttributeQuery/SOAP
+--query SOAP1/idp.example.org:8443/idp/profile/SAML/AttributeQuery/SOAP
+--logout Redirect/sp.example.org/Shibboleth.sso/SLO/Redirect
+--logout Artifact/sp.example.org/Shibboleth.sso/SLO/Artifact
+--acs POST1/sp.example.org/Shibboleth.sso/SAML/POST
+--acs POST/sp.example.org/Shibboleth.sso/SAML2/POST
+--acs POST/sp2.example.org/Shibboleth.sso/SAML2/POST
+--acs PAOS/sp.example.org/Shibboleth.sso/SAML2/ECP
+--tag urn:oasis:names:tc:SAML:profiles:subject-id:req=subject-id
+.
diff --git a/metadatagen-impl/src/test/resources/net/shibboleth/idp/plugin/metadatagen/impl/extra1 b/metadatagen-impl/src/test/resources/net/shibboleth/idp/plugin/metadatagen/impl/extra1
deleted file mode 100644
index 24fbe3a..0000000
--- a/metadatagen-impl/src/test/resources/net/shibboleth/idp/plugin/metadatagen/impl/extra1
+++ /dev/null
@@ -1,2 +0,0 @@
-#idp.metadata.dnsname=his.idp.example.org
-#idp.metadata.backchannel.cert=H:/Downloads/idp/credentials/idp-backchannel.crt
diff --git a/metadatagen-impl/src/test/resources/net/shibboleth/idp/plugin/metadatagen/impl/extra2 b/metadatagen-impl/src/test/resources/net/shibboleth/idp/plugin/metadatagen/impl/extra2
deleted file mode 100644
index c78b919..0000000
--- a/metadatagen-impl/src/test/resources/net/shibboleth/idp/plugin/metadatagen/impl/extra2
+++ /dev/null
@@ -1,16 +0,0 @@
-idp.metadata.dnsname=ushib.example.org
-idp.metadata.backchannel.cert=H:/Downloads/idp/credentials/idp-backchannel.crt
-
-idp.metadata.idpsso.mdui.langs=en fr de
-
-idp.metadata.idpsso.mdui.displayname.fr=Universit� de Shibboleth
-idp.metadata.idpsso.mdui.displayname.en=Shibboleth University
-idp.metadata.idpsso.mdui.displayname.de=Universit�t Shibboleth
-
-idp.metadata.idpsso.mdui.description.fr=UShib
-idp.metadata.idpsso.mdui.description.de=UShib
-idp.metadata.idpsso.mdui.description.en=UShib
-idp.metadata.idpsso.mdui.logo.height=84
-idp.metadata.idpsso.mdui.logo.width=75
-idp.metadata.idpsso.mdui.logo.path=/the/to/path/logo.png
-

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


More information about the commits mailing list