Skip to content

Commit

Permalink
refactor(tests): consolidate common test variables
Browse files Browse the repository at this point in the history
  • Loading branch information
texastony committed Jun 21, 2024
1 parent 46cd0be commit 9abfc30
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.encryption.s3.utils.S3EncryptionClientTestResources;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
Expand All @@ -37,6 +38,7 @@
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -45,29 +47,28 @@
import static software.amazon.encryption.s3.S3EncryptionClient.withAdditionalConfiguration;
import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.appendTestSuffix;
import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.deleteObject;
import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.BUCKET;
import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.KMS_KEY_ID;

/**
* This class is an integration test for verifying compatibility of ciphertexts
* between V1, V2, and V3 clients under various conditions.
*/
public class S3EncryptionClientCompatibilityTest {

private static final String BUCKET = System.getenv("AWS_S3EC_TEST_BUCKET");
private static final String KMS_KEY_ID = System.getenv("AWS_S3EC_TEST_KMS_KEY_ID");
private static final Region KMS_REGION = Region.getRegion(Regions.fromName(System.getenv("AWS_REGION")));
// SDK V1 Region
private static final Region KMS_REGION = Region.getRegion(
Regions.fromName(S3EncryptionClientTestResources.KMS_REGION.toString()));

private static SecretKey AES_KEY;
private static KeyPair RSA_KEY_PAIR;

@BeforeAll
public static void setUp() throws NoSuchAlgorithmException {
public static void setUp() throws NoSuchAlgorithmException, IOException, InvalidKeySpecException {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256);
AES_KEY = keyGen.generateKey();

KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(2048);
RSA_KEY_PAIR = keyPairGen.generateKeyPair();
RSA_KEY_PAIR = S3EncryptionClientTestResources.getRSAKeyPair();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,44 @@
// SPDX-License-Identifier: Apache-2.0
package software.amazon.encryption.s3.utils;

import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemReader;
import org.bouncycastle.util.io.pem.PemWriter;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.apache.ApacheHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.DeleteObjectResponse;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.concurrent.CompletableFuture;

/**
* Determines which AWS resources to use while running tests.
*/
public class S3EncryptionClientTestResources {

public static final AwsCredentialsProvider CREDENTIALS = DefaultCredentialsProvider.create();
public static final SdkHttpClient HTTP_CLIENT = ApacheHttpClient.create();
public static final String BUCKET = System.getenv("AWS_S3EC_TEST_BUCKET");
public static final String KMS_KEY_ID = System.getenv("AWS_S3EC_TEST_KMS_KEY_ID");
// This alias must point to the same key as KMS_KEY_ID
public static final String KMS_KEY_ALIAS = System.getenv("AWS_S3EC_TEST_KMS_KEY_ALIAS");
public static final Region KMS_REGION = Region.of(System.getenv("AWS_REGION"));

/**
* For a given string, append a suffix to distinguish it from
Expand Down Expand Up @@ -57,4 +78,63 @@ public static void deleteObject(final String bucket, final String objectKey, fin
// Ensure completion before return
response.join();
}


/**
* @return If an RSA KeyPair already exists in Test Resources, load and return that.<p>
* Otherwise, generate a new key pair, persist that to Resources, and return it.<p>
* Assumes working directory is root of the git repo.
*/
public static KeyPair getRSAKeyPair() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
Path resourceDirectory = Paths.get("src","test","resources");
if (resourceDirectory.resolve("RSAPrivateKey.pem").toFile().exists() && resourceDirectory.resolve("RSAPublicKey.pem").toFile().exists()) {
return readKeyPairFromTestResourcesFile(resourceDirectory);
}
KeyPair keyPair = generateKeyPair(2048);
writeKeyPairToTestResourcesFile(keyPair, resourceDirectory);
return keyPair;
}

public static KeyPair generateKeyPair(final int keySize) {
if (!(keySize == 2048 || keySize == 4096)) throw new IllegalArgumentException("Only 2048 or 4096 are valid key sizes.");
KeyPairGenerator rsaGen;
try {
rsaGen = KeyPairGenerator.getInstance("RSA");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("No such algorithm", e);
}
rsaGen.initialize(keySize, new SecureRandom());
return rsaGen.generateKeyPair();
}

private static void writePEMFile(Key key, String description, Path filePath) throws IOException {
final PemObject pemObject = new PemObject(description, key.getEncoded());
try (PemWriter pemWriter = new PemWriter(new OutputStreamWriter(Files.newOutputStream(filePath)))) {
pemWriter.writeObject(pemObject);
}
}

private static PemObject readPEMFile(Path filePath) throws IOException {
try (PemReader pemReader = new PemReader(new InputStreamReader(Files.newInputStream(filePath)))) {
return pemReader.readPemObject();
}
}

private static void writeKeyPairToTestResourcesFile(final KeyPair keyPair, Path resourceDirectory) throws IOException {
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
writePEMFile(privateKey, "RSA PRIVATE KEY", resourceDirectory.resolve("RSAPrivateKey.pem"));
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
writePEMFile(publicKey, "RSA PUBLIC KEY", resourceDirectory.resolve("RSAPublicKey.pem"));
}

private static KeyPair readKeyPairFromTestResourcesFile(Path resourceDirectory) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
final KeyFactory factory = KeyFactory.getInstance("RSA");
byte[] privateKeyContent = readPEMFile(resourceDirectory.resolve("RSAPrivateKey.pem")).getContent();
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyContent);
final PrivateKey privateKey = factory.generatePrivate(privateKeySpec);
byte[] publicKeyContent = readPEMFile(resourceDirectory.resolve("RSAPublicKey.pem")).getContent();
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyContent);
final PublicKey publicKey = factory.generatePublic(publicKeySpec);
return new KeyPair(publicKey, privateKey);
}
}
28 changes: 28 additions & 0 deletions src/test/resources/RSAPrivateKey.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDDhiKZterVY9+h
igrZMn8gzfcNEXhqDoCynjWewiQriG3P4DuasePNVjQSIk7tp1/sn6zSeIm/UlAv
8Mtx+dh2MG9l6jyFiAm8T7heU9CgdSc5Dp5ZJ2QrSwXoQPBnrHTSomxB3sg75Uhn
hhv1MfVceFxKPUJTXdQ6Mb6hy14RGfx06E3Ugffvy81rNf4e9A7uUbktVocYUqvO
aiv8JzGYnDXdlhtfZ/DYMnJMH73AFJJ+7+XAfUMU32nvhkcGhiKj6auJXB2makJ3
FUtwrNXYMc976WrH9Kk2iu33rL7PV6n9+X6xI/WGdf9X0OCORzbk6ih1CEIU0J+M
KIkRGAirAgMBAAECggEAB9u6REdFevIaqNltejFHXsAob8QF/O08SvGE4i6XWZCQ
KUyv2JXRvAz85sWuOmsBtfbs8UCa+K+MPYEGDDyocIed0pDJgexnx8PEezYPKoPK
4cYuoxKsOfk38Y+6md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EPip3eVdvNAyqjwBc=
-----END RSA PRIVATE KEY-----
9 changes: 9 additions & 0 deletions src/test/resources/RSAPublicKey.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-----BEGIN RSA PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw4YimbXq1WPfoYoK2TJ/
IM33DRF4ag6Asp41nsIkK4htz+A7mrHjzVY0EiJO7adf7J+s0niJv1JQL/DLcfnY
djBvZeo8hYgJvE+4XlPQoHUnOQ6eWSdkK0sF6EDwZ6x00qJsQd7IO+VIZ4Yb9TH1
XHhcSj1CU13UOjG+octeERn8dOhN1IH378vNazX+HvQO7lG5LVaHGFKrzmor/Ccx
mJw13ZYbX2fw2DJyTB+9wBSSfu/lwH1DFN9p74ZHBoYio+mriVwdpmpCdxVLcKzV
2DHPe+lqx/SpNort96y+z1ep/fl+sSP1hnX/V9Dgjkc25OoodQhCFNCfjCiJERgI
qwIDAQAB
-----END RSA PUBLIC KEY-----

0 comments on commit 9abfc30

Please sign in to comment.