summaryrefslogtreecommitdiffstats
path: root/base/java-tools
diff options
context:
space:
mode:
authorEndi S. Dewata <edewata@redhat.com>2017-03-28 21:02:22 +0200
committerEndi S. Dewata <edewata@redhat.com>2017-04-04 22:07:54 +0200
commit88cd07655268831e14e7cd4f6f6a65e331f86583 (patch)
tree9519b55cfa1638746423e7e5a227783a42a2af73 /base/java-tools
parent6a682f8e56c982ed0e0810326e71f9de23347590 (diff)
downloadpki-88cd07655268831e14e7cd4f6f6a65e331f86583.tar.gz
pki-88cd07655268831e14e7cd4f6f6a65e331f86583.tar.xz
pki-88cd07655268831e14e7cd4f6f6a65e331f86583.zip
Added CLIs to access audit log files.
New pki audit commands have been added to list and retrieve audit log files. Change-Id: I785fa6f55d9b143f513d9210ebf82d04e06eaed5
Diffstat (limited to 'base/java-tools')
-rw-r--r--base/java-tools/src/com/netscape/cmstools/logging/AuditCLI.java11
-rw-r--r--base/java-tools/src/com/netscape/cmstools/logging/AuditFileFindCLI.java90
-rw-r--r--base/java-tools/src/com/netscape/cmstools/logging/AuditFileRetrieveCLI.java87
3 files changed, 188 insertions, 0 deletions
diff --git a/base/java-tools/src/com/netscape/cmstools/logging/AuditCLI.java b/base/java-tools/src/com/netscape/cmstools/logging/AuditCLI.java
index ff489dceb..06ba040ec 100644
--- a/base/java-tools/src/com/netscape/cmstools/logging/AuditCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/logging/AuditCLI.java
@@ -26,6 +26,7 @@ import org.jboss.resteasy.plugins.providers.atom.Link;
import com.netscape.certsrv.client.PKIClient;
import com.netscape.certsrv.logging.AuditClient;
import com.netscape.certsrv.logging.AuditConfig;
+import com.netscape.certsrv.logging.AuditFile;
import com.netscape.cmstools.cli.CLI;
import com.netscape.cmstools.cli.SubsystemCLI;
@@ -42,8 +43,13 @@ public class AuditCLI extends CLI {
this.subsystemCLI = subsystemCLI;
+ // audit configuration
addModule(new AuditModifyCLI(this));
addModule(new AuditShowCLI(this));
+
+ // audit files
+ addModule(new AuditFileFindCLI(this));
+ addModule(new AuditFileRetrieveCLI(this));
}
@Override
@@ -83,4 +89,9 @@ public class AuditCLI extends CLI {
System.out.println(" Link: " + link.getHref());
}
}
+
+ public static void printAuditFile(AuditFile auditFile) {
+ System.out.println(" File name: " + auditFile.getName());
+ System.out.println(" Size: " + auditFile.getSize());
+ }
}
diff --git a/base/java-tools/src/com/netscape/cmstools/logging/AuditFileFindCLI.java b/base/java-tools/src/com/netscape/cmstools/logging/AuditFileFindCLI.java
new file mode 100644
index 000000000..5ae9ce735
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/logging/AuditFileFindCLI.java
@@ -0,0 +1,90 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2017 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package com.netscape.cmstools.logging;
+
+import java.util.Collection;
+
+import org.apache.commons.cli.CommandLine;
+
+import com.netscape.certsrv.logging.AuditClient;
+import com.netscape.certsrv.logging.AuditFile;
+import com.netscape.certsrv.logging.AuditFileCollection;
+import com.netscape.cmstools.cli.CLI;
+import com.netscape.cmstools.cli.MainCLI;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class AuditFileFindCLI extends CLI {
+
+ public AuditCLI auditCLI;
+
+ public AuditFileFindCLI(AuditCLI auditCLI) {
+ super("file-find", "Find audit files", auditCLI);
+ this.auditCLI = auditCLI;
+
+ createOptions();
+ }
+
+ public void printHelp() {
+ formatter.printHelp(getFullName() + " [OPTIONS...]", options);
+ }
+
+ public void createOptions() {
+ options.addOption(null, "help", false, "Show help message.");
+ }
+
+ public void execute(String[] args) throws Exception {
+
+ CommandLine cmd = parser.parse(options, args);
+
+ if (cmd.hasOption("help")) {
+ printHelp();
+ return;
+ }
+
+ String[] cmdArgs = cmd.getArgs();
+
+ if (cmdArgs.length > 0) {
+ throw new Exception("Too many arguments specified.");
+ }
+
+ AuditClient auditClient = auditCLI.getAuditClient();
+ AuditFileCollection response = auditClient.findAuditFiles();
+
+ MainCLI.printMessage(response.getTotal() + " entries matched");
+ if (response.getTotal() == 0) return;
+
+ Collection<AuditFile> entries = response.getEntries();
+ boolean first = true;
+
+ for (AuditFile auditFile : entries) {
+
+ if (first) {
+ first = false;
+ } else {
+ System.out.println();
+ }
+
+ AuditCLI.printAuditFile(auditFile);
+ }
+
+ MainCLI.printMessage("Number of entries returned " + entries.size());
+ }
+}
diff --git a/base/java-tools/src/com/netscape/cmstools/logging/AuditFileRetrieveCLI.java b/base/java-tools/src/com/netscape/cmstools/logging/AuditFileRetrieveCLI.java
new file mode 100644
index 000000000..07af3a416
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/logging/AuditFileRetrieveCLI.java
@@ -0,0 +1,87 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2017 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package com.netscape.cmstools.logging;
+
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+
+import javax.ws.rs.core.StreamingOutput;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+
+import com.netscape.certsrv.logging.AuditClient;
+import com.netscape.cmstools.cli.CLI;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class AuditFileRetrieveCLI extends CLI {
+
+ public AuditCLI auditCLI;
+
+ public AuditFileRetrieveCLI(AuditCLI auditCLI) {
+ super("file-retrieve", "Retrieve audit file", auditCLI);
+ this.auditCLI = auditCLI;
+
+ createOptions();
+ }
+
+ public void printHelp() {
+ formatter.printHelp(getFullName() + " <filename> [OPTIONS...]", options);
+ }
+
+ public void createOptions() {
+ Option option = new Option(null, "output", true, "Output file.");
+ option.setArgName("path");
+ options.addOption(option);
+
+ options.addOption(null, "help", false, "Show help message.");
+ }
+
+ public void execute(String[] args) throws Exception {
+
+ CommandLine cmd = parser.parse(options, args);
+
+ if (cmd.hasOption("help")) {
+ printHelp();
+ return;
+ }
+
+ String[] cmdArgs = cmd.getArgs();
+
+ if (cmdArgs.length < 1) {
+ throw new Exception("Missing audit file name.");
+
+ } if (cmdArgs.length > 1) {
+ throw new Exception("Too many arguments specified.");
+ }
+
+ String filename = cmdArgs[0];
+ String output = cmd.getOptionValue("output");
+ if (output == null) output = filename;
+
+ AuditClient auditClient = auditCLI.getAuditClient();
+ StreamingOutput so = auditClient.getAuditFile(filename);
+
+ try (OutputStream out = new FileOutputStream(output)) {
+ so.write(out);
+ }
+ }
+}