|
12 | 12 | */ |
13 | 13 | package io.kubernetes.client.util; |
14 | 14 |
|
| 15 | +import static org.junit.Assert.assertEquals; |
| 16 | + |
15 | 17 | import io.kubernetes.client.Resources; |
16 | | -import java.io.File; |
17 | 18 | import java.io.IOException; |
18 | | -import java.nio.file.Files; |
19 | | -import java.nio.file.Paths; |
| 19 | +import java.net.URL; |
| 20 | +import java.security.GeneralSecurityException; |
20 | 21 | import java.security.NoSuchAlgorithmException; |
21 | 22 | import java.security.PrivateKey; |
22 | 23 | import java.security.spec.InvalidKeySpecException; |
23 | | -import junit.framework.TestCase; |
| 24 | +import org.apache.commons.io.IOUtils; |
| 25 | +import org.junit.Test; |
24 | 26 |
|
25 | | -public class SSLUtilsTest extends TestCase { |
| 27 | +public class SSLUtilsTest { |
26 | 28 |
|
27 | | - private static final String CLIENT_KEY_PATH = |
28 | | - new File(Resources.getResource("clientauth.key").getPath()).toString(); |
29 | | - private static final String CLIENT_KEY_RSA_PATH = |
30 | | - new File(Resources.getResource("clientauth-rsa.key").getPath()).toString(); |
31 | | - private static final String CLIENT_KEY_EC_PATH = |
32 | | - new File(Resources.getResource("clientauth-ec.key").getPath()).toString(); |
| 29 | + private static final String CLIENT_CERT = "clientauth.cert"; |
33 | 30 |
|
34 | | - public void testPKCS8KeyLoadDump() |
35 | | - throws IOException, InvalidKeySpecException, NoSuchAlgorithmException { |
36 | | - byte[] loaded = Files.readAllBytes(Paths.get(CLIENT_KEY_PATH.replace("C:/", ""))); |
37 | | - PrivateKey privateKey = SSLUtils.loadKey(loaded); |
38 | | - byte[] dumped = SSLUtils.dumpKey(privateKey); |
39 | | - PrivateKey reloaded = SSLUtils.loadKey(dumped); |
40 | | - assertEquals(privateKey, reloaded); |
| 31 | + private static final String CLIENT_KEY_RSA_PKCS8 = "clientauth.key"; |
| 32 | + |
| 33 | + private static final String CLIENT_KEY_RSA_PKCS1 = "clientauth-rsa.key"; |
| 34 | + |
| 35 | + private static final String CLIENT_KEY_ECDSA_PKCS7 = "clientauth-ec.key"; |
| 36 | + |
| 37 | + private static final String CLIENT_KEY_ECDSA_PKCS8 = "clientauth-ec-fixed.key"; |
| 38 | + |
| 39 | + private static final String RSA_ALGORITHM = "RSA"; |
| 40 | + |
| 41 | + private static final String ECDSA_ALGORITHM = "ECDSA"; |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testLoadKeyRsaPkcs8() throws IOException, GeneralSecurityException { |
| 45 | + final PrivateKey privateKey = assertLoadDumpReloadKeyEquals(CLIENT_KEY_RSA_PKCS8); |
| 46 | + assertEquals(RSA_ALGORITHM, privateKey.getAlgorithm()); |
41 | 47 | } |
42 | 48 |
|
43 | | - public void testPKCS1RSAKeyLoadDump() |
44 | | - throws IOException, InvalidKeySpecException, NoSuchAlgorithmException { |
45 | | - byte[] loaded = Files.readAllBytes(Paths.get(CLIENT_KEY_RSA_PATH)); |
46 | | - PrivateKey privateKey = SSLUtils.loadKey(loaded); |
47 | | - byte[] dumped = SSLUtils.dumpKey(privateKey); |
48 | | - PrivateKey reloaded = SSLUtils.loadKey(dumped); |
49 | | - assertEquals(privateKey, reloaded); |
| 49 | + @Test |
| 50 | + public void testLoadKeyRsaPkcs1() throws IOException, GeneralSecurityException { |
| 51 | + final PrivateKey privateKey = assertLoadDumpReloadKeyEquals(CLIENT_KEY_RSA_PKCS1); |
| 52 | + assertEquals(RSA_ALGORITHM, privateKey.getAlgorithm()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testLoadKeyEcdsaPkcs7() throws IOException, GeneralSecurityException { |
| 57 | + final PrivateKey privateKey = assertLoadDumpReloadKeyEquals(CLIENT_KEY_ECDSA_PKCS7); |
| 58 | + assertEquals(ECDSA_ALGORITHM, privateKey.getAlgorithm()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testLoadKeyEcdsaPkcs8() throws IOException, GeneralSecurityException { |
| 63 | + final PrivateKey privateKey = assertLoadDumpReloadKeyEquals(CLIENT_KEY_ECDSA_PKCS8); |
| 64 | + assertEquals(ECDSA_ALGORITHM, privateKey.getAlgorithm()); |
50 | 65 | } |
51 | 66 |
|
52 | | - public void testPKCS1ECKeyLoadDump() |
| 67 | + @Test(expected = InvalidKeySpecException.class) |
| 68 | + public void testLoadKeyCertificateNotSupported() |
53 | 69 | throws IOException, InvalidKeySpecException, NoSuchAlgorithmException { |
54 | | - byte[] loaded = Files.readAllBytes(Paths.get(CLIENT_KEY_EC_PATH)); |
55 | | - PrivateKey privateKey = SSLUtils.loadKey(loaded); |
56 | | - byte[] dumped = SSLUtils.dumpKey(privateKey); |
57 | | - PrivateKey reloaded = SSLUtils.loadKey(dumped); |
58 | | - assertEquals(privateKey, reloaded); |
| 70 | + final byte[] resourceBytes = getResourceBytes(CLIENT_CERT); |
| 71 | + SSLUtils.loadKey(resourceBytes); |
| 72 | + } |
| 73 | + |
| 74 | + private PrivateKey assertLoadDumpReloadKeyEquals(final String filePath) |
| 75 | + throws IOException, GeneralSecurityException { |
| 76 | + final byte[] resourceBytes = getResourceBytes(filePath); |
| 77 | + final PrivateKey privateKey = SSLUtils.loadKey(resourceBytes); |
| 78 | + |
| 79 | + byte[] dumpedKey = SSLUtils.dumpKey(privateKey); |
| 80 | + final PrivateKey reloadedPrivateKey = SSLUtils.loadKey(dumpedKey); |
| 81 | + |
| 82 | + assertEquals(privateKey, reloadedPrivateKey); |
| 83 | + |
| 84 | + return privateKey; |
| 85 | + } |
| 86 | + |
| 87 | + private byte[] getResourceBytes(final String filePath) throws IOException { |
| 88 | + final URL resourceUrl = Resources.getResource(filePath); |
| 89 | + return IOUtils.toByteArray(resourceUrl); |
59 | 90 | } |
60 | 91 | } |
0 commit comments