Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add base class + 2 subclasses extending PGPEncryptor #587

Merged
merged 64 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
449ac46
add base class + 2 subclasses extending PGPEncryptor
lkemperman-cfa Jan 31, 2024
ad7d722
add both subclasses for pgp csv encryptor + pgp encryptor
lkemperman-cfa Jan 31, 2024
d038eeb
add wic and ece and snap specific keys in application.yaml
lkemperman-cfa Jan 31, 2024
578553a
Naming components
sree-cfa Feb 1, 2024
e19d836
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 1, 2024
fc3b562
fix reference
lkemperman-cfa Feb 1, 2024
33f952b
do not include documents for WIC - ECE only
lkemperman-cfa Feb 1, 2024
b663d9a
add override for in-memory upload file in sftp impl, encrypt for pack…
lkemperman-cfa Feb 1, 2024
72318d6
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 1, 2024
d0dc288
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 1, 2024
c502b50
add separate encryptor for ECE - also waiting on key set
lkemperman-cfa Feb 1, 2024
1d0529c
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 2, 2024
af4f4c4
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 5, 2024
201b19f
add specific bucket names for each instance
lkemperman-cfa Feb 6, 2024
4aa692b
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 6, 2024
95cf77a
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 9, 2024
5e0a255
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 12, 2024
9951cf5
modify environment variables to be specific to ece and snap
lkemperman-cfa Feb 12, 2024
1599657
add the encrypt package flag to ECE enum
lkemperman-cfa Feb 12, 2024
68649c6
fixes for path etc.
lkemperman-cfa Feb 13, 2024
ebfeec1
delete test file
lkemperman-cfa Feb 13, 2024
ebbe6dc
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 13, 2024
f41cc7e
fix the upload url and user for staging
lkemperman-cfa Feb 13, 2024
8004104
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 13, 2024
01298f1
prep github actions scripts
lkemperman-cfa Feb 13, 2024
0af3f44
fix spacing
lkemperman-cfa Feb 13, 2024
7258041
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 14, 2024
a0f9a3c
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 15, 2024
f717e3d
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 16, 2024
91526bf
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 21, 2024
f6b7fd3
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 21, 2024
cb4a90c
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 21, 2024
5803bc9
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 26, 2024
91b73d6
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 26, 2024
3108a1c
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 26, 2024
613e8f9
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 26, 2024
593f132
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 27, 2024
de4484c
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 27, 2024
6387895
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 27, 2024
61dd4eb
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 28, 2024
d72b74c
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 28, 2024
41c1245
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 29, 2024
9b3b6ef
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 29, 2024
6aca8be
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 29, 2024
06f2b72
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 29, 2024
f0d1c41
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 29, 2024
7f83490
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Feb 29, 2024
6b324ea
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Mar 4, 2024
5283c4c
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Mar 4, 2024
676f6bf
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Mar 5, 2024
6a8dfca
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Mar 5, 2024
767a8d0
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Mar 5, 2024
75cc2a3
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Mar 8, 2024
c83fb13
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Mar 8, 2024
ea027cd
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Mar 8, 2024
aab3a90
change profiles
lkemperman-cfa Mar 11, 2024
c3285ff
temporarily remove WIC from transmissions
lkemperman-cfa Mar 11, 2024
c7f7f59
add cron schedule expression
lkemperman-cfa Mar 11, 2024
885d2b4
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Mar 11, 2024
7cd09e3
fix cron expression?
lkemperman-cfa Mar 11, 2024
7283076
move the expression into application yaml
lkemperman-cfa Mar 11, 2024
6a78da2
fix again
lkemperman-cfa Mar 11, 2024
adee182
Merge branch 'main' into add-encryption-to-wic-package
lkemperman-cfa Mar 11, 2024
0210052
modify deploy staging template to use the same wic pgp keys (demo)
lkemperman-cfa Mar 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/deploy-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ jobs:
'PGP_SECKEY_FILE_PATH=${{ secrets.PGP_SECKEY_FILE_PATH }}' \
'PGP_PUBKEY_FILE_PATH=${{ secrets.PGP_PUBKEY_FILE_PATH }}' \
'PGP_SIGKEY_PASSWORD=${{ secrets.PGP_SIGKEY_PASSWORD }}' \
'ECE_PGP_KEYS_DIR=${{ secrets.ECE_PGP_KEYS_DIR }}' \
'ECE_PGP_PUBKEY_FILE_PATH=${{ secrets.ECE_PGP_PUBKEY_FILE_PATH }}' \
'ECE_PGP_SECKEY_FILE_PATH=${{ secrets.ECE_PGP_SECKEY_FILE_PATH }}' \
'ECE_PGP_SIGKEY_PASSWORD=${{ secrets.ECE_PGP_SIGKEY_PASSWORD }}' \
'WIC_PGP_KEYS_DIR=${{ secrets.WIC_PGP_KEYS_DIR }}' \
'WIC_PGP_PUBKEY_FILE_PATH=${{ secrets.WIC_PGP_PUBKEY_FILE_PATH }}' \
'WIC_PGP_SECKEY_FILE_PATH=${{ secrets.WIC_PGP_SECKEY_FILE_PATH }}' \
'WIC_PGP_SIGKEY_PASSWORD=${{ secrets.WIC_PGP_SIGKEY_PASSWORD }}' \
'FTPS_USERNAME=${{ secrets.FTPS_USERNAME }}' \
'FTPS_PASSWORD=${{ secrets.FTPS_PASSWORD }}' \
'FTPS_UPLOAD_URL=${{ secrets.FTPS_UPLOAD_URL }}' \
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/deploy-staging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ jobs:
'PGP_SECKEY_FILE_PATH=${{ secrets.PGP_SECKEY_FILE_PATH_STAGING }}' \
'PGP_PUBKEY_FILE_PATH=${{ secrets.PGP_PUBKEY_FILE_PATH_STAGING }}' \
'PGP_SIGKEY_PASSWORD=${{ secrets.PGP_SIGKEY_PASSWORD_STAGING }}' \
'ECE_PGP_KEYS_DIR=${{ secrets.ECE_PGP_KEYS_DIR }}' \
'ECE_PGP_PUBKEY_FILE_PATH=${{ secrets.ECE_PGP_PUBKEY_FILE_PATH_STAGING }}' \
'ECE_PGP_SECKEY_FILE_PATH=${{ secrets.ECE_PGP_SECKEY_FILE_PATH_STAGING }}' \
'ECE_PGP_SIGKEY_PASSWORD=${{ secrets.ECE_PGP_SIGKEY_PASSWORD_STAGING }}' \
'WIC_PGP_KEYS_DIR=${{ secrets.WIC_PGP_KEYS_DIR }}' \
'WIC_PGP_PUBKEY_FILE_PATH=${{ secrets.WIC_PGP_PUBKEY_FILE_PATH }}' \
'WIC_PGP_SECKEY_FILE_PATH=${{ secrets.WIC_PGP_SECKEY_FILE_PATH }}' \
'WIC_PGP_SIGKEY_PASSWORD=${{ secrets.WIC_PGP_SIGKEY_PASSWORD }}' \
'FTPS_USERNAME=${{ secrets.FTPS_USERNAME }}' \
'FTPS_PASSWORD=${{ secrets.FTPS_PASSWORD }}' \
'FTPS_UPLOAD_URL=${{ secrets.FTPS_UPLOAD_URL }}' \
Expand Down
175 changes: 175 additions & 0 deletions src/main/java/org/ladocuploader/app/cli/BasePGPEncrpytorImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package org.ladocuploader.app.cli;

import formflow.library.file.CloudFile;
import formflow.library.file.S3CloudFileRepository;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.bcpg.CompressionAlgorithmTags;
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
import org.bouncycastle.openpgp.*;
import org.bouncycastle.openpgp.jcajce.JcaPGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
import org.bouncycastle.openpgp.operator.jcajce.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.io.*;
import java.security.SecureRandom;
import java.util.Date;
import java.util.Iterator;

@Slf4j
@Component
@Profile({"production", "staging"})
public class BasePGPEncrpytorImpl implements PGPEncryptor {

@Value("${form-flow.aws.access_key}")
private String accessKey;
@Value("${form-flow.aws.secret_key}")
private String secretKey;
@Value("${form-flow.aws.region}")
private String region;

private PGPSecretKey signingKey;
private PGPPublicKey pubKey;

protected String pubkeyFilePath;
protected String sigkeyPassword;
protected String seckeyFilePath;
protected String bucketName;

@PostConstruct
public void init() {
log.info("Retrieving keys for signing and encryption");
S3CloudFileRepository repository = new S3CloudFileRepository(accessKey, secretKey, bucketName, region);
CloudFile pubKey = repository.get(pubkeyFilePath);
CloudFile sigKey = repository.get(seckeyFilePath);
try {
initPubKey(pubKey.getFileBytes());
initSigKey(sigKey.getFileBytes());
} catch (IOException e) {
throw new IllegalStateException("Issue initializing encryption keys", e);
}
}

@Override
public byte[] signAndEncryptPayload(String filename) throws IOException {
FileInputStream instream = new FileInputStream(filename);
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
try {
log.info("Signing and encrypting payload");
return signAndEncryptPayload(instream, signingKey, pubKey, outstream);
} catch (PGPException e) {
throw new IllegalStateException("There was an issue signing and encrypting the file", e);
} finally {
instream.close();
outstream.close();
log.info("Completed signing and encrypting payload");
}
}

private void initPubKey(byte[] fileBytes) throws IOException {
PGPPublicKey pubKey = null;
InputStream inputStream = new ByteArrayInputStream(fileBytes);
inputStream = PGPUtil.getDecoderStream(inputStream);
try {
JcaPGPPublicKeyRingCollection ringCollection = new JcaPGPPublicKeyRingCollection(inputStream);
Iterator<PGPPublicKeyRing> keyRingsIterator = ringCollection.getKeyRings();
while (keyRingsIterator.hasNext()) {
PGPPublicKeyRing pgpPublicKeyRing = keyRingsIterator.next();
Iterator<PGPPublicKey> pubKeysIterator = pgpPublicKeyRing.getPublicKeys();
while (pubKeysIterator.hasNext()) {
pubKey = pubKeysIterator.next();
}
}
} catch (PGPException e) {
throw new IllegalArgumentException("Invalid public key");
} finally {
inputStream.close();
}
this.pubKey = pubKey;
}

private void initSigKey(byte[] sigkeyFileBytes) throws IOException {
try (InputStream fileInputStream = new ByteArrayInputStream(sigkeyFileBytes);) {
InputStream inputStream = PGPUtil.getDecoderStream(fileInputStream);
KeyFingerPrintCalculator fpCalculator = new JcaKeyFingerprintCalculator();
PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(inputStream, fpCalculator);

Iterator<PGPSecretKeyRing> keyRingIter = pgpSec.getKeyRings();
while (keyRingIter.hasNext()) {
PGPSecretKeyRing keyRing = keyRingIter.next();

Iterator<PGPSecretKey> keyIter = keyRing.getSecretKeys();
while (keyIter.hasNext()) {
PGPSecretKey key = keyIter.next();

if (key.isSigningKey()) {
signingKey = key;
return;
}
}
}
} catch (PGPException e) {
throw new IllegalArgumentException("Invalid signing key", e);
}

throw new IllegalArgumentException("Invalid signing key");
}

private PGPPrivateKey getPrivateKey(PGPSecretKey secretKey) throws PGPException {
return secretKey.extractPrivateKey(
new JcePBESecretKeyDecryptorBuilder().build(sigkeyPassword.toCharArray()));
}

private byte[] signAndEncryptPayload(InputStream inputStream, PGPSecretKey secKey, PGPPublicKey pubKey,
ByteArrayOutputStream outputStream) throws PGPException, IOException {
int BUFFER_SIZE = 1 << 16;

// Encryption
PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(
new JcePGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.AES_256)
.setWithIntegrityPacket(true)
.setSecureRandom(new SecureRandom()));
encGen.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(pubKey));
OutputStream encOut = encGen.open(outputStream, new byte[BUFFER_SIZE]);

// Compression
PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator(CompressionAlgorithmTags.ZIP);
OutputStream cOut = cGen.open(encOut);

// Signing
PGPSignatureGenerator sGen = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(secKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1));
sGen.init(PGPSignature.BINARY_DOCUMENT, getPrivateKey(secKey));

Iterator<String> it = secKey.getPublicKey().getUserIDs();
if (it.hasNext()) {
PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();
spGen.addSignerUserID(false, it.next());
sGen.setHashedSubpackets(spGen.generate());
}

sGen.generateOnePassVersion(false).encode(cOut);

// Literal Data generator and output stream
byte[] data = inputStream.readAllBytes();
PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();
OutputStream lOut = lGen.open(cOut, PGPLiteralData.BINARY, PGPLiteralData.CONSOLE, data.length, new Date());

lOut.write(data);
sGen.update(data);

lOut.close();
lGen.close();

sGen.generate().encode(cOut);
cOut.close();
cGen.close();
encGen.close();

return outputStream.toByteArray();
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ public void uploadFile(String zipFilename, String filePath) {
// Do nothing
log.info("Mock uploading file " + zipFilename);
}

@Override
public void uploadFile(String zipFilename, String uploadLocation, byte [] data) {
// Do nothing
log.info("Mock uploading file " + zipFilename);
}
}
23 changes: 23 additions & 0 deletions src/main/java/org/ladocuploader/app/cli/PGPEceEncryptorImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.ladocuploader.app.cli;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Slf4j
@Component("ecePgpEncryptor")
@Profile({"production", "staging"})
public class PGPEceEncryptorImpl extends BasePGPEncrpytorImpl {

public PGPEceEncryptorImpl(@Value("${pgp.ece.sigkey-password}") String sigkeyPassword,
@Value("${pgp.ece.seckey-file-path}") String seckeyFilePath,
@Value("${pgp.ece.pubkey-file-path}") String pubkeyFilePath,
@Value("${pgp.ece.bucket-name}") String bucketName) {
super();
this.sigkeyPassword = sigkeyPassword;
this.seckeyFilePath = seckeyFilePath;
this.pubkeyFilePath = pubkeyFilePath;
this.bucketName = bucketName;
}
}
Loading