From 3956a44002020a235515c23b73fbb7a1490cb5bb Mon Sep 17 00:00:00 2001 From: pktxu <5881103+pktxu@users.noreply.github.com> Date: Sun, 31 Dec 2023 00:00:42 +0100 Subject: [PATCH] address PR feedback --- .../kubernetes/client/internal/CertUtils.java | 34 +++++++++---------- .../client/internal/CertUtilsTest.java | 2 +- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/internal/CertUtils.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/internal/CertUtils.java index 699ebd7592b..c57f59e4450 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/internal/CertUtils.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/internal/CertUtils.java @@ -182,30 +182,28 @@ private static PrivateKey handleECKey(InputStream keyInputStream) { try { return new Callable() { @Override - public PrivateKey call() { - try { - if (Security.getProvider("BC") == null && Security.getProvider("BCFIPS") == null) { - Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); - } - Object pemObject = new PEMParser(new InputStreamReader(keyInputStream)).readObject(); - if (pemObject instanceof PEMKeyPair) { - return new JcaPEMKeyConverter().getKeyPair((PEMKeyPair) pemObject).getPrivate(); - } else if (pemObject instanceof PrivateKeyInfo) { - return BouncyCastleProvider.getPrivateKey((PrivateKeyInfo) pemObject); - } else { - throw new KubernetesClientException("Don't know what to do with a " + pemObject.getClass().getName()); - } - } catch (IOException exception) { - exception.printStackTrace(); + public PrivateKey call() throws IOException { + if (Security.getProvider("BC") == null && Security.getProvider("BCFIPS") == null) { + Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); + } + Object pemObject = new PEMParser(new InputStreamReader(keyInputStream)).readObject(); + if (pemObject == null) { + throw new KubernetesClientException("Got null PEM object from EC key's input stream."); + } else if (pemObject instanceof PEMKeyPair) { + return new JcaPEMKeyConverter().getKeyPair((PEMKeyPair) pemObject).getPrivate(); + } else if (pemObject instanceof PrivateKeyInfo) { + return BouncyCastleProvider.getPrivateKey((PrivateKeyInfo) pemObject); + } else { + throw new KubernetesClientException("Don't know what to do with a " + pemObject.getClass().getName()); } - return null; } }.call(); } catch (NoClassDefFoundError e) { throw new KubernetesClientException( "JcaPEMKeyConverter is provided by BouncyCastle, an optional dependency. To use support for EC Keys you must explicitly add this dependency to classpath."); - } catch (NullPointerException npe) { - throw new KubernetesClientException("Could not load EC key."); + } catch (IOException e) { + e.printStackTrace(); + throw new KubernetesClientException(e.getMessage()); } } diff --git a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/internal/CertUtilsTest.java b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/internal/CertUtilsTest.java index d55d545fc2b..788c57d95a8 100644 --- a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/internal/CertUtilsTest.java +++ b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/internal/CertUtilsTest.java @@ -218,7 +218,7 @@ void loadNothingError() Exception exception = assertThrows(KubernetesClientException.class, () -> CertUtils.createKeyStore(null, certPath, null, privateKeyPath, "EC", "foo", null, null)); - assertTrue(exception.getMessage().contains("Could not load EC key.")); + assertTrue(exception.getMessage().equals("Got null PEM object from EC key's input stream.")); } @Test