diff --git a/core/src/main/java/hudson/TcpSlaveAgentListener.java b/core/src/main/java/hudson/TcpSlaveAgentListener.java index 1f4d4ba79cbd956d3e2c1aab2d75920d04fda024..a4d481e324ac07ea397e57811a82a547e578876c 100644 --- a/core/src/main/java/hudson/TcpSlaveAgentListener.java +++ b/core/src/main/java/hudson/TcpSlaveAgentListener.java @@ -117,7 +117,7 @@ public final class TcpSlaveAgentListener extends Thread { @Nullable public String getIdentityPublicKey() { InstanceIdentityProvider provider = - InstanceIdentityProvider.get(RSAPrivateKey.class); + InstanceIdentityProvider.get(InstanceIdentityProvider.RSA); RSAPublicKey key = provider == null ? null : provider.getPublicKey(); return key == null ? null : new String(Base64.encodeBase64(key.getEncoded()), Charset.forName("UTF-8")); } diff --git a/core/src/main/java/jenkins/model/identity/IdentityRootAction.java b/core/src/main/java/jenkins/model/identity/IdentityRootAction.java index 33ec8633508bbcfecfc51fda0659fb49fb29bc81..1ae7d5f44641cf1e3f1035d0ef1fa363ab8ae6b5 100644 --- a/core/src/main/java/jenkins/model/identity/IdentityRootAction.java +++ b/core/src/main/java/jenkins/model/identity/IdentityRootAction.java @@ -39,7 +39,8 @@ public class IdentityRootAction implements UnprotectedRootAction { */ @Override public String getUrlName() { - return InstanceIdentityProvider.get(RSAPrivateKey.class) == null ? null : "instance-identity"; + return InstanceIdentityProvider.get(InstanceIdentityProvider.RSA) == null + ? null : "instance-identity"; } /** @@ -49,7 +50,7 @@ public class IdentityRootAction implements UnprotectedRootAction { */ public String getPublicKey() { InstanceIdentityProvider provider = - InstanceIdentityProvider.get(RSAPrivateKey.class); + InstanceIdentityProvider.get(InstanceIdentityProvider.RSA); RSAPublicKey key = provider == null ? null : provider.getPublicKey(); if (key == null) { return null; @@ -75,7 +76,7 @@ public class IdentityRootAction implements UnprotectedRootAction { */ public String getFingerprint() { InstanceIdentityProvider provider = - InstanceIdentityProvider.get(RSAPrivateKey.class); + InstanceIdentityProvider.get(InstanceIdentityProvider.RSA); RSAPublicKey key = provider == null ? null : provider.getPublicKey(); if (key == null) { return null; diff --git a/core/src/main/java/jenkins/model/identity/InstanceIdentityProvider.java b/core/src/main/java/jenkins/model/identity/InstanceIdentityProvider.java index 4a144f5f65926909d838c8c8b4dcfe34d819eb27..479d7aa96d4397b258a7fb27d291d9200114ffb9 100644 --- a/core/src/main/java/jenkins/model/identity/InstanceIdentityProvider.java +++ b/core/src/main/java/jenkins/model/identity/InstanceIdentityProvider.java @@ -29,8 +29,14 @@ import java.security.KeyPair; import java.security.PrivateKey; import java.security.PublicKey; import java.security.cert.X509Certificate; +import java.security.interfaces.DSAPrivateKey; +import java.security.interfaces.DSAPublicKey; +import java.security.interfaces.ECPrivateKey; +import java.security.interfaces.ECPublicKey; +import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.RSAPublicKey; import javax.annotation.CheckForNull; -import javax.annotation.Nullable; +import javax.annotation.Nonnull; /** * A source of instance identity. @@ -41,6 +47,22 @@ import javax.annotation.Nullable; */ public abstract class InstanceIdentityProvider implements ExtensionPoint { + /** + * RSA keys. + */ + public static final KeyTypes RSA = + new KeyTypes<>(RSAPublicKey.class, RSAPrivateKey.class); + /** + * DSA keys. + */ + public static final KeyTypes DSA = + new KeyTypes<>(DSAPublicKey.class, DSAPrivateKey.class); + /** + * EC keys + */ + public static final KeyTypes EC = + new KeyTypes<>(ECPublicKey.class, ECPrivateKey.class); + /** * Gets the {@link KeyPair} that comprises the instance identity. * @@ -85,21 +107,70 @@ public abstract class InstanceIdentityProvider the type of public key. - * @param the type of private key. + * @param type the type of keys. + * @param the type of public key. + * @param the type of private key. * @return the provider or {@code null} if no provider of the specified type is available. */ @CheckForNull @SuppressWarnings("unchecked") public static InstanceIdentityProvider get( - Class keyType) { + @Nonnull KeyTypes type) { for (InstanceIdentityProvider provider : ExtensionList.lookup(InstanceIdentityProvider.class)) { KeyPair keyPair = provider.getKeyPair(); - if (keyPair != null && keyType.isInstance(keyPair.getPrivate())) { + if (keyPair != null + && type.pubKeyType.isInstance(keyPair.getPublic()) + && type.privKeyType.isInstance(keyPair.getPrivate())) { return (InstanceIdentityProvider) provider; } } return null; } + + /** + * Holds information about the paired keytypes that can be used to form the various identity keys. + * + * @param the type of public key. + * @param the type of private key. + */ + public static final class KeyTypes { + /** + * The interface for the public key. + */ + private final Class pubKeyType; + /** + * The interface for the private key. + */ + private final Class privKeyType; + + /** + * Constructor. + * + * @param pubKeyType the interface for the public key. + * @param privKeyType the interface for the private key. + */ + private KeyTypes(Class pubKeyType, Class privKeyType) { + this.pubKeyType = pubKeyType; + this.privKeyType = privKeyType; + } + + /** + * Gets the interface for the public key. + * + * @return the interface for the public key. + */ + public Class getPublicKeyClass() { + return pubKeyType; + } + + /** + * Gets the interface for the private key. + * + * @return the interface for the private key. + */ + public Class getPrivateKeyClass() { + return privKeyType; + } + } + }