[FEATURE] Подписывание любого файла и любой диретории

This commit is contained in:
Gravit 2019-10-22 00:12:38 +07:00
parent ff30ba2670
commit fb21aeb888
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
4 changed files with 114 additions and 9 deletions

View file

@ -41,24 +41,28 @@ public String getName() {
@Override
public Path process(Path inputFile) throws IOException {
Path toRet = srv.launcherBinary.nextPath("signed");
sign(config, inputFile, toRet);
return toRet;
}
public static void sign(LaunchServerConfig.JarSignerConf config, Path inputFile, Path signedFile) throws IOException {
KeyStore c = SignHelper.getStore(new File(config.keyStore).toPath(), config.keyStorePass, config.keyStoreType);
try (SignerJar output = new SignerJar(new ZipOutputStream(IOHelper.newOutput(toRet)), () -> this.gen(c),
try (SignerJar output = new SignerJar(new ZipOutputStream(IOHelper.newOutput(signedFile)), () -> SignJarTask.gen(config, c),
config.metaInfSfName, config.metaInfKeyName);
ZipInputStream input = new ZipInputStream(IOHelper.newInput(inputFile))) {
//input.getManifest().getMainAttributes().forEach((a, b) -> output.addManifestAttribute(a.toString(), b.toString())); // may not work such as after Radon.
ZipEntry e = input.getNextEntry();
while (e != null) {
if ("META-INF/MANIFEST.MF".equals(e.getName()) || "/META-INF/MANIFEST.MF".equals(e.getName())) {
Manifest m = new Manifest(input);
m.getMainAttributes().forEach((a, b) -> output.addManifestAttribute(a.toString(), b.toString()));
e = input.getNextEntry();
continue;
}
if ("META-INF/MANIFEST.MF".equals(e.getName()) || "/META-INF/MANIFEST.MF".equals(e.getName())) {
Manifest m = new Manifest(input);
m.getMainAttributes().forEach((a, b) -> output.addManifestAttribute(a.toString(), b.toString()));
e = input.getNextEntry();
continue;
}
output.addFileContents(IOHelper.newZipEntry(e), input);
e = input.getNextEntry();
}
}
return toRet;
}
@Override
@ -66,7 +70,7 @@ public boolean allowDelete() {
return true;
}
public CMSSignedDataGenerator gen(KeyStore c) {
public static CMSSignedDataGenerator gen(LaunchServerConfig.JarSignerConf config, KeyStore c) {
try {
return SignHelper.createSignedDataGenerator(c,
config.keyAlias, config.signAlgo, config.keyPass);

View file

@ -89,6 +89,8 @@ public static void registerCommands(pro.gravit.utils.command.CommandHandler hand
service.registerCommand("givePermission", new GivePermissionsCommand(server));
service.registerCommand("getPermissions", new GetPermissionsCommand(server));
service.registerCommand("clients", new ClientsCommand(server));
service.registerCommand("signJar", new SignJarCommand(server));
service.registerCommand("signDir", new SignDirCommand(server));
Category serviceCategory = new Category(service, "service", "Managing LaunchServer Components");
handler.registerCategory(serviceCategory);
}

View file

@ -0,0 +1,52 @@
package pro.gravit.launchserver.command.service;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.binary.tasks.SignJarTask;
import pro.gravit.launchserver.command.Command;
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
public class SignDirCommand extends Command {
private class SignJarVisitor extends SimpleFileVisitor<Path>
{
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.toFile().getName().endsWith(".jar"))
{
Path tmpSign = server.dir.resolve("build").resolve(file.toFile().getName());
LogHelper.info("Signing jar %s", file.toString());
SignJarTask.sign(server.config.sign, file, tmpSign);
Files.deleteIfExists(file);
Files.move(tmpSign, file);
}
return super.visitFile(file, attrs);
}
}
public SignDirCommand(LaunchServer server) {
super(server);
}
@Override
public String getArgsDescription() {
return "[path to dir]";
}
@Override
public String getUsageDescription() {
return "sign all jar files into dir";
}
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args, 1);
Path targetDir = Paths.get(args[0]);
if(!IOHelper.isDir(targetDir))
throw new IllegalArgumentException(String.format("%s not directory", targetDir.toString()));
IOHelper.walk(targetDir, new SignJarVisitor(), true);
LogHelper.info("Success signed");
}
}

View file

@ -0,0 +1,47 @@
package pro.gravit.launchserver.command.service;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.binary.tasks.SignJarTask;
import pro.gravit.launchserver.command.Command;
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class SignJarCommand extends Command {
public SignJarCommand(LaunchServer server) {
super(server);
}
@Override
public String getArgsDescription() {
return "[path to file] (path to signed file)";
}
@Override
public String getUsageDescription() {
return "sign custom jar";
}
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args, 1);
Path target = Paths.get(args[0]);
Path tmpSign;
if(args.length > 1)
tmpSign = Paths.get(args[1]);
else
tmpSign = server.dir.resolve("build").resolve(target.toFile().getName());
LogHelper.info("Signing jar %s to %s", target.toString(), tmpSign.toString());
SignJarTask.sign(server.config.sign, target, tmpSign);
if(args.length <= 1)
{
LogHelper.info("Move temp jar %s to %s", tmpSign.toString(), target.toString());
Files.deleteIfExists(target);
Files.move(tmpSign, target);
}
LogHelper.info("Success signed");
}
}