Skip to content

Commit

Permalink
Remove usages of Apache Commons (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored Jun 10, 2023
1 parent 980875f commit a37c494
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.NoSuchFileException;
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Base64;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.Jenkins;
import jenkins.security.CryptoConfidentialKey;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.jenkinsci.main.modules.instance_identity.pem.PEMHelper;

/**
Expand Down Expand Up @@ -75,20 +77,24 @@ private KeyPair read(File keyFile, File oldKeyFile, KeyPairGenerator gen) throws

if (keyFile != null) { //Get the Reader for keyFile and handle if corrupted
try {
enc = FileUtils.readFileToByteArray(keyFile);
keyPair = PEMHelper.decodePEM(new String(KEY.decrypt().doFinal(enc), "UTF-8"));
} catch (FileNotFoundException e) {
enc = Files.readAllBytes(keyFile.toPath());
keyPair = PEMHelper.decodePEM(new String(KEY.decrypt().doFinal(enc), StandardCharsets.UTF_8));
} catch (FileNotFoundException | NoSuchFileException e) {
LOGGER.fine("identity.key.enc doesn't exist. New Identity.key.enc will be generated");
return null;
} catch (GeneralSecurityException x) {
LOGGER.log(Level.SEVERE, "identity.key.enc is corrupted. Identity.key.enc will be deleted and a new one will be generated", x);
return null;
} catch (IOException e) {
} catch (IOException | InvalidPathException e) {
LOGGER.log(Level.SEVERE, "failed to access identity.key.enc. Identity.key.enc will be deleted and a new one will be generated", e);
return null;
}
} else if (oldKeyFile != null) { //Get the Reader for oldKeyFile
keyPair = PEMHelper.decodePEM(FileUtils.readFileToString(oldKeyFile));
try {
keyPair = PEMHelper.decodePEM(Files.readString(oldKeyFile.toPath(), StandardCharsets.UTF_8));
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
return keyPair;
}
Expand All @@ -97,7 +103,7 @@ private void write(KeyPair keys, File keyFile) throws IOException {
String pem = PEMHelper.encodePEM(keys);
OutputStream os = new FileOutputStream(keyFile);
try {
os.write(KEY.encrypt().doFinal(pem.getBytes("UTF-8")));
os.write(KEY.encrypt().doFinal(pem.getBytes(StandardCharsets.UTF_8)));
} catch (GeneralSecurityException x) {
throw new IOException(x);
} finally {
Expand Down Expand Up @@ -134,7 +140,7 @@ public RSAPrivateKey getPrivate() {
*/
public String getEncodedPublicKey() {
RSAPublicKey key = getPublic();
return new String(Base64.encodeBase64(key.getEncoded()), StandardCharsets.UTF_8);
return Base64.getEncoder().encodeToString(key.getEncoded());
}

public static InstanceIdentity get() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

import hudson.Extension;
import hudson.model.PageDecorator;
import java.nio.charset.Charset;
import org.apache.commons.codec.binary.Base64;

import java.io.IOException;
import java.security.interfaces.RSAPublicKey;

/**
* Advertises the public key.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyPair;
import java.security.Security;

import org.apache.commons.io.FileUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.jenkinsci.main.modules.instance_identity.pem.PEMHelper;
import org.junit.BeforeClass;
Expand All @@ -45,30 +45,38 @@

public class ReadWriteKeyTest {

private static File PEM_PCKS1_FILE;
private static File PEM_PCKS8_FILE;
private static Path PEM_PCKS1_FILE;
private static Path PEM_PCKS8_FILE;
private static byte[] KEY_PRIVATE_ENCODED;
private static byte[] KEY_PUBLIC_ENCODED;
@Rule
public TemporaryFolder folder = new TemporaryFolder();

@BeforeClass
public static void setUpBC() throws URISyntaxException, IOException {
PEM_PCKS1_FILE = new File(
ReadWriteKeyTest.class.getClassLoader().getResource("private-key-pcks1.pem").toURI());
PEM_PCKS8_FILE = new File(
ReadWriteKeyTest.class.getClassLoader().getResource("private-key-pcks8.pem").toURI());
KEY_PRIVATE_ENCODED = FileUtils.readFileToByteArray(new File(
ReadWriteKeyTest.class.getClassLoader().getResource("private-key-private-encoded.bin").toURI()));
KEY_PUBLIC_ENCODED = FileUtils.readFileToByteArray(new File(
ReadWriteKeyTest.class.getClassLoader().getResource("private-key-public-encoded.bin").toURI()));
PEM_PCKS1_FILE = Paths.get(ReadWriteKeyTest.class
.getClassLoader()
.getResource("private-key-pcks1.pem")
.toURI());
PEM_PCKS8_FILE = Paths.get(ReadWriteKeyTest.class
.getClassLoader()
.getResource("private-key-pcks8.pem")
.toURI());
KEY_PRIVATE_ENCODED = Files.readAllBytes(Paths.get(ReadWriteKeyTest.class
.getClassLoader()
.getResource("private-key-private-encoded.bin")
.toURI()));
KEY_PUBLIC_ENCODED = Files.readAllBytes(Paths.get(ReadWriteKeyTest.class
.getClassLoader()
.getResource("private-key-public-encoded.bin")
.toURI()));
Security.addProvider(new BouncyCastleProvider());
}

@Test
public void testReadIdentityPKCS1vsPKCS8() throws Exception {
String pcks1PEM = FileUtils.readFileToString(PEM_PCKS1_FILE, StandardCharsets.UTF_8);
String pcks8PEM = FileUtils.readFileToString(PEM_PCKS8_FILE, StandardCharsets.UTF_8);
String pcks1PEM = Files.readString(PEM_PCKS1_FILE, StandardCharsets.UTF_8);
String pcks8PEM = Files.readString(PEM_PCKS8_FILE, StandardCharsets.UTF_8);

KeyPair keyPair1 = PEMHelper.decodePEM(pcks1PEM);
KeyPair keyPair8 = PEMHelper.decodePEM(pcks8PEM);
Expand All @@ -95,8 +103,8 @@ public void testEncodeInvalidIdentity() {

@Test
public void testWriteIdentityPKCS1vsPKCS8() throws Exception {
String pcks1PEM = FileUtils.readFileToString(PEM_PCKS1_FILE, StandardCharsets.UTF_8);
String pcks8PEM = FileUtils.readFileToString(PEM_PCKS8_FILE, StandardCharsets.UTF_8);
String pcks1PEM = Files.readString(PEM_PCKS1_FILE, StandardCharsets.UTF_8);
String pcks8PEM = Files.readString(PEM_PCKS8_FILE, StandardCharsets.UTF_8);

KeyPair keyPair = PEMHelper.decodePEM(pcks8PEM);
String encodedPEM = PEMHelper.encodePEM(keyPair);
Expand All @@ -106,14 +114,14 @@ public void testWriteIdentityPKCS1vsPKCS8() throws Exception {

@Test
public void testCompareReadPKCS1AndPCKS8() throws Exception {
String pcks1PEM = FileUtils.readFileToString(PEM_PCKS1_FILE, StandardCharsets.UTF_8);
String pcks1PEM = Files.readString(PEM_PCKS1_FILE, StandardCharsets.UTF_8);

KeyPair keyPair = PEMHelper.decodePEM(pcks1PEM);
String reEncodedPEM = PEMHelper.encodePEM(keyPair);

assertArrayEquals(keyPair.getPrivate().getEncoded(), KEY_PRIVATE_ENCODED);
assertArrayEquals(keyPair.getPublic().getEncoded(), KEY_PUBLIC_ENCODED);
assertEquals(unifyEOL(reEncodedPEM), unifyEOL(FileUtils.readFileToString(PEM_PCKS1_FILE, StandardCharsets.UTF_8)));
assertEquals(unifyEOL(reEncodedPEM), unifyEOL(Files.readString(PEM_PCKS1_FILE, StandardCharsets.UTF_8)));

// reread the newly encoded keyPair and retest
KeyPair keyPair2 = PEMHelper.decodePEM(reEncodedPEM);
Expand Down

0 comments on commit a37c494

Please sign in to comment.