1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-14 00:12:06 +01:00

Start working on PGP-Cert-D-CLI

This commit is contained in:
Paul Schaub 2022-01-31 18:23:54 +01:00
parent 4dbd2bad7e
commit 603c67127d
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
10 changed files with 256 additions and 2 deletions

View file

@ -0,0 +1,49 @@
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
plugins {
id 'application'
}
group 'org.pgpainless'
repositories {
mavenCentral()
}
dependencies {
testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
// Logging
testImplementation "ch.qos.logback:logback-classic:$logbackVersion"
implementation project(":pgpainless-cert-d")
// picocli for cli
implementation "info.picocli:picocli:4.6.2"
}
test {
useJUnitPlatform()
}
mainClassName = 'pgp.cert_d.cli.PGPCertDCli'
jar {
manifest {
attributes 'Main-Class': "$mainClassName"
}
duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
} {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
}

View file

@ -0,0 +1,71 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package pgp.cert_d.cli;
import org.pgpainless.certificate_store.CertificateCertificateReader;
import org.pgpainless.certificate_store.SharedPGPCertificateDirectoryAdapter;
import pgp.cert_d.SharedPGPCertificateDirectoryImpl;
import pgp.cert_d.cli.commands.Get;
import pgp.cert_d.cli.commands.Insert;
import pgp.cert_d.cli.commands.PrintDirectory;
import pgp.cert_d.exception.NotAStoreException;
import pgp.certificate_store.CertificateStore;
import picocli.CommandLine;
import java.io.File;
@CommandLine.Command(
subcommands = {
Insert.class,
PrintDirectory.class,
Get.class,
}
)
public class PGPCertDCli {
@CommandLine.Option(names = "--base-directory", paramLabel = "DIRECTORY", description = "Overwrite the default certificate directory")
File baseDirectory;
private static CertificateStore certificateStore;
private static String baseDir;
private int executionStrategy(CommandLine.ParseResult parseResult) {
try {
initStore();
} catch (NotAStoreException e) {
return -1;
}
return new CommandLine.RunLast().execute(parseResult);
}
private void initStore() throws NotAStoreException {
SharedPGPCertificateDirectoryImpl certificateDirectory;
if (baseDirectory != null) {
certificateDirectory = new SharedPGPCertificateDirectoryImpl(
baseDirectory,
new CertificateCertificateReader());
} else {
certificateDirectory = new SharedPGPCertificateDirectoryImpl(
new CertificateCertificateReader());
}
baseDir = certificateDirectory.getBaseDirectory().getAbsolutePath();
certificateStore = new SharedPGPCertificateDirectoryAdapter(certificateDirectory);
}
public static void main(String[] args) {
PGPCertDCli cli = new PGPCertDCli();
new CommandLine(cli)
.setExecutionStrategy(parserResult -> cli.executionStrategy(parserResult))
.execute(args);
}
public static CertificateStore getCertificateDirectory() {
return certificateStore;
}
public static String getBaseDir() {
return baseDir;
}
}

View file

@ -0,0 +1,43 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package pgp.cert_d.cli.commands;
import java.io.IOException;
import org.bouncycastle.util.io.Streams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pgp.cert_d.cli.PGPCertDCli;
import pgp.certificate_store.Certificate;
import picocli.CommandLine;
@CommandLine.Command(name = "get",
description = "Retrieve certificates from the store")
public class Get implements Runnable {
private static final Logger LOGGER = LoggerFactory.getLogger(Get.class);
@CommandLine.Parameters(
paramLabel = "IDENTIFIER",
arity = "1",
description = "Certificate identifier (fingerprint or special name)"
)
String identifer;
@Override
public void run() {
try {
Certificate certificate = PGPCertDCli.getCertificateDirectory()
.getCertificate(identifer);
if (certificate == null) {
return;
}
Streams.pipeAll(certificate.getInputStream(), System.out);
} catch (IOException e) {
LOGGER.info("IO Error", e);
System.exit(-1);
}
}
}

View file

@ -0,0 +1,42 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package pgp.cert_d.cli.commands;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pgp.cert_d.cli.PGPCertDCli;
import pgp.certificate_store.Certificate;
import pgp.certificate_store.MergeCallback;
import picocli.CommandLine;
@CommandLine.Command(name = "insert",
description = "Insert or update a certificate")
public class Insert implements Runnable {
private static final Logger LOGGER = LoggerFactory.getLogger(Insert.class);
@Override
public void run() {
try {
Certificate certificate = PGPCertDCli.getCertificateDirectory().insertCertificate(System.in, new MergeCallback() {
@Override
public Certificate merge(Certificate data, Certificate existing) {
return data;
}
});
// CHECKSTYLE:OFF
System.out.println(certificate.getFingerprint());
// CHECKSTYLE:ON
} catch (IOException e) {
LOGGER.info("IO-Error", e);
System.exit(-1);
} catch (InterruptedException e) {
LOGGER.info("Thread interrupted.", e);
System.exit(-1);
}
}
}

View file

@ -0,0 +1,22 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package pgp.cert_d.cli.commands;
import pgp.cert_d.cli.PGPCertDCli;
import picocli.CommandLine;
@CommandLine.Command(
name = "print-directory",
description = "Print the location of the certificate directory"
)
public class PrintDirectory implements Runnable {
@Override
public void run() {
// CHECKSTYLE:OFF
System.out.println(PGPCertDCli.getBaseDir());
// CHECKSTYLE:ON
}
}

View file

@ -0,0 +1,8 @@
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
/**
* Subcommands.
*/
package pgp.cert_d.cli.commands;

View file

@ -0,0 +1,8 @@
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
/**
* Command Line Interface for the Shared PGP Certificate Directory.
*/
package pgp.cert_d.cli;

View file

@ -2,12 +2,14 @@
//
// SPDX-License-Identifier: Apache-2.0
package pgp.cert_d;
package org.pgpainless.certificate_store;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import pgp.cert_d.SharedPGPCertificateDirectory;
import pgp.cert_d.SpecialName;
import pgp.cert_d.exception.BadDataException;
import pgp.cert_d.exception.BadNameException;
import pgp.certificate_store.Certificate;

View file

@ -0,0 +1,8 @@
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
/**
* PGPainless + Certificate Store.
*/
package org.pgpainless.certificate_store;

View file

@ -9,5 +9,6 @@ include 'pgpainless-core',
'pgpainless-cli',
'pgpainless-cert-d',
'pgp-certificate-store',
'pgp-cert-d-java'
'pgp-cert-d-java',
'pgp-cert-d-cli'