2022-03-01 15:53:24 +01:00
|
|
|
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package pgp.cert_d.cli;
|
|
|
|
|
|
|
|
import org.pgpainless.certificate_store.CertificateReader;
|
2022-07-04 20:12:42 +02:00
|
|
|
import org.pgpainless.certificate_store.KeyReader;
|
2022-03-01 15:53:24 +01:00
|
|
|
import org.pgpainless.certificate_store.SharedPGPCertificateDirectoryAdapter;
|
|
|
|
import pgp.cert_d.BaseDirectoryProvider;
|
|
|
|
import pgp.cert_d.SharedPGPCertificateDirectoryImpl;
|
2022-07-04 13:19:36 +02:00
|
|
|
import pgp.cert_d.cli.commands.Export;
|
2022-03-01 15:53:24 +01:00
|
|
|
import pgp.cert_d.cli.commands.Get;
|
2022-07-04 13:04:11 +02:00
|
|
|
import pgp.cert_d.cli.commands.Insert;
|
2022-07-04 13:05:32 +02:00
|
|
|
import pgp.cert_d.cli.commands.Import;
|
2022-07-04 20:12:42 +02:00
|
|
|
import pgp.cert_d.cli.commands.Setup;
|
2022-03-01 15:53:24 +01:00
|
|
|
import pgp.cert_d.jdbc.sqlite.DatabaseSubkeyLookup;
|
|
|
|
import pgp.cert_d.jdbc.sqlite.SqliteSubkeyLookupDaoImpl;
|
|
|
|
import pgp.certificate_store.SubkeyLookup;
|
|
|
|
import pgp.certificate_store.exception.NotAStoreException;
|
|
|
|
import pgp.certificate_store.CertificateDirectory;
|
|
|
|
import picocli.CommandLine;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
|
|
|
@CommandLine.Command(
|
2022-04-25 23:57:03 +02:00
|
|
|
name = "certificate-store",
|
|
|
|
description = "Store and manage public OpenPGP certificates",
|
2022-03-01 15:53:24 +01:00
|
|
|
subcommands = {
|
2022-07-04 20:12:42 +02:00
|
|
|
CommandLine.HelpCommand.class,
|
2022-07-04 13:19:36 +02:00
|
|
|
Export.class,
|
2022-07-04 13:04:11 +02:00
|
|
|
Insert.class,
|
2022-07-04 13:05:32 +02:00
|
|
|
Import.class,
|
2022-03-01 15:53:24 +01:00
|
|
|
Get.class,
|
2022-07-04 20:12:42 +02:00
|
|
|
Setup.class
|
2022-03-01 15:53:24 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
public class PGPCertDCli {
|
|
|
|
|
2022-07-04 20:12:42 +02:00
|
|
|
@CommandLine.Option(names = {"-s", "--store"}, paramLabel = "DIRECTORY",
|
|
|
|
description = "Overwrite the default certificate directory path",
|
|
|
|
scope = CommandLine.ScopeType.INHERIT)
|
2022-03-01 15:53:24 +01:00
|
|
|
File baseDirectory;
|
|
|
|
|
|
|
|
private static CertificateDirectory certificateDirectory;
|
|
|
|
|
|
|
|
private int executionStrategy(CommandLine.ParseResult parseResult) {
|
|
|
|
try {
|
|
|
|
initStore();
|
|
|
|
} catch (NotAStoreException | SQLException e) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return new CommandLine.RunLast().execute(parseResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initStore() throws NotAStoreException, SQLException {
|
|
|
|
SharedPGPCertificateDirectoryImpl certificateDirectory;
|
|
|
|
SubkeyLookup subkeyLookup;
|
|
|
|
if (baseDirectory == null) {
|
|
|
|
baseDirectory = BaseDirectoryProvider.getDefaultBaseDir();
|
|
|
|
}
|
|
|
|
|
|
|
|
certificateDirectory = new SharedPGPCertificateDirectoryImpl(
|
|
|
|
baseDirectory,
|
2022-07-04 20:12:42 +02:00
|
|
|
new CertificateReader(),
|
|
|
|
new KeyReader());
|
2022-03-01 15:53:24 +01:00
|
|
|
subkeyLookup = new DatabaseSubkeyLookup(
|
|
|
|
SqliteSubkeyLookupDaoImpl.forDatabaseFile(new File(baseDirectory, "_pgpainless_subkey_map.db")));
|
|
|
|
|
|
|
|
PGPCertDCli.certificateDirectory = new SharedPGPCertificateDirectoryAdapter(certificateDirectory, subkeyLookup);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
PGPCertDCli cli = new PGPCertDCli();
|
|
|
|
new CommandLine(cli)
|
|
|
|
.setExecutionStrategy(parserResult -> cli.executionStrategy(parserResult))
|
|
|
|
.execute(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static CertificateDirectory getCertificateDirectory() {
|
|
|
|
return certificateDirectory;
|
|
|
|
}
|
|
|
|
}
|