Class X509TestContext
java.lang.Object
org.apache.hadoop.hbase.io.crypto.tls.X509TestContext
This class simplifies the creation of certificates and private keys for SSL/TLS connections.
This file has been copied from the Apache ZooKeeper project.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder class, used for creating new instances of X509TestContext. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final org.apache.hadoop.conf.Configurationprivate static final Stringprivate Fileprivate X509Certificateprivate Fileprivate KeyPairprivate final char[]private Fileprivate Fileprivate final Fileprivate static final Stringprivate Fileprivate X509Certificateprivate Fileprivate KeyPairprivate final char[]private Fileprivate File -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateX509TestContext(File tempDir, org.apache.hadoop.conf.Configuration conf, X509Certificate trustStoreCertificate, char[] trustStorePassword, KeyPair trustStoreKeyPair, File trustStoreJksFile, File trustStorePemFile, File trustStorePkcs12File, KeyPair keyStoreKeyPair, char[] keyStorePassword, X509Certificate keyStoreCertificate) privateX509TestContext(org.apache.hadoop.conf.Configuration conf, File tempDir, KeyPair trustStoreKeyPair, char[] trustStorePassword, KeyPair keyStoreKeyPair, char[] keyStorePassword) Constructor is intentionally private, use the Builder class instead. -
Method Summary
Modifier and TypeMethodDescriptionvoidCreates a clone of the current context, but injecting the passed certificate as the KeyStore cert.private voidcreateCertificates(String... subjectAltNames) private voidprivate voidprivate voidprivate voidprivate voidprivate voidprivate voidprivate voidorg.apache.hadoop.conf.ConfigurationgetConf()private FilegetKeyStoreFile(org.apache.hadoop.hbase.io.crypto.tls.KeyStoreFileType storeFileType) Returns the path to the key store file in the given format (JKS, PEM, ...).private Filechar[]private Fileprivate Fileprivate FilegetTrustStoreFile(org.apache.hadoop.hbase.io.crypto.tls.KeyStoreFileType storeFileType) Returns the path to the trust store file in the given format (JKS or PEM).private Filechar[]private Fileprivate Filebooleanstatic X509TestContext.BuildernewBuilder(org.apache.hadoop.conf.Configuration conf) Returns a new default-constructed Builder.Generates a new certificate using this context's CA and keystoreKeyPair.voidregenerateStores(X509KeyType keyStoreKeyType, X509KeyType trustStoreKeyType, org.apache.hadoop.hbase.io.crypto.tls.KeyStoreFileType keyStoreFileType, org.apache.hadoop.hbase.io.crypto.tls.KeyStoreFileType trustStoreFileType, String... subjectAltNames) voidsetConfigurations(org.apache.hadoop.hbase.io.crypto.tls.KeyStoreFileType keyStoreFileType, org.apache.hadoop.hbase.io.crypto.tls.KeyStoreFileType trustStoreFileType) Sets the SSL system properties such that the given X509Util object can be used to create SSL Contexts that will use the trust store and key store files created by this test context.voidsetKeystoreConfigurations(org.apache.hadoop.hbase.io.crypto.tls.KeyStoreFileType keyStoreFileType, org.apache.hadoop.conf.Configuration confToSet) Sets the KeyStore-related SSL system properties onto the given Configuration such that X509Util can be used to create SSL Contexts using that KeyStore.
-
Field Details
-
TRUST_STORE_PREFIX
- See Also:
-
KEY_STORE_PREFIX
- See Also:
-
tempDir
-
conf
-
trustStoreCertificate
-
trustStorePassword
-
trustStoreKeyPair
-
trustStoreJksFile
-
trustStorePemFile
-
trustStorePkcs12File
-
trustStoreBcfksFile
-
keyStoreKeyPair
-
keyStoreCertificate
-
keyStorePassword
-
keyStoreJksFile
-
keyStorePemFile
-
keyStorePkcs12File
-
keyStoreBcfksFile
-
-
Constructor Details
-
X509TestContext
private X509TestContext(org.apache.hadoop.conf.Configuration conf, File tempDir, KeyPair trustStoreKeyPair, char[] trustStorePassword, KeyPair keyStoreKeyPair, char[] keyStorePassword) throws IOException, GeneralSecurityException, org.bouncycastle.operator.OperatorCreationException Constructor is intentionally private, use the Builder class instead.- Parameters:
conf- the configurationtempDir- the directory in which key store and trust store temp files will be written.trustStoreKeyPair- the key pair for the trust store.trustStorePassword- the password to protect a JKS trust store (ignored for PEM trust stores).keyStoreKeyPair- the key pair for the key store.keyStorePassword- the password to protect the key store private key.- Throws:
IOExceptionGeneralSecurityExceptionorg.bouncycastle.operator.OperatorCreationException
-
X509TestContext
private X509TestContext(File tempDir, org.apache.hadoop.conf.Configuration conf, X509Certificate trustStoreCertificate, char[] trustStorePassword, KeyPair trustStoreKeyPair, File trustStoreJksFile, File trustStorePemFile, File trustStorePkcs12File, KeyPair keyStoreKeyPair, char[] keyStorePassword, X509Certificate keyStoreCertificate) Used bycloneWithNewKeystoreCert(X509Certificate). Should set all fields except generated keystore path fields
-
-
Method Details
-
newCert
public X509Certificate newCert(org.bouncycastle.asn1.x500.X500Name name, String... subjectAltNames) throws GeneralSecurityException, IOException, org.bouncycastle.operator.OperatorCreationException Generates a new certificate using this context's CA and keystoreKeyPair. By default, the cert will have localhost in the subjectAltNames. This can be overridden by passing one or more string arguments after the cert name. The expectation for those arguments is that they are valid DNS names.- Throws:
GeneralSecurityExceptionIOExceptionorg.bouncycastle.operator.OperatorCreationException
-
getTempDir
-
getTrustStorePassword
-
getTrustStoreFile
public File getTrustStoreFile(org.apache.hadoop.hbase.io.crypto.tls.KeyStoreFileType storeFileType) throws IOException Returns the path to the trust store file in the given format (JKS or PEM). Note that the file is created lazily, the first time this method is called. The trust store file is temporary and will be deleted on exit.- Parameters:
storeFileType- the store file type (JKS or PEM).- Returns:
- the path to the trust store file.
- Throws:
IOException- if there is an error creating the trust store file.
-
getTrustStoreJksFile
- Throws:
IOException
-
generateTrustStoreJksFile
- Throws:
IOException
-
getTrustStorePemFile
- Throws:
IOException
-
generateTrustStorePemFile
- Throws:
IOException
-
getTrustStorePkcs12File
- Throws:
IOException
-
generateTrustStorePkcs12File
- Throws:
IOException
-
getTrustStoreBcfksFile
- Throws:
IOException
-
generateTrustStoreBcfksFile
- Throws:
IOException
-
getKeyStoreCertificate
-
getKeyStorePassword
-
isKeyStoreEncrypted
-
getConf
-
getKeyStoreFile
public File getKeyStoreFile(org.apache.hadoop.hbase.io.crypto.tls.KeyStoreFileType storeFileType) throws IOException Returns the path to the key store file in the given format (JKS, PEM, ...). Note that the file is created lazily, the first time this method is called. The key store file is temporary and will be deleted on exit.- Parameters:
storeFileType- the store file type (JKS, PEM, ...).- Returns:
- the path to the key store file.
- Throws:
IOException- if there is an error creating the key store file.
-
getKeyStoreJksFile
- Throws:
IOException
-
generateKeyStoreJksFile
- Throws:
IOException
-
getKeyStorePemFile
- Throws:
IOException
-
generateKeyStorePemFile
private void generateKeyStorePemFile() throws IOException, org.bouncycastle.operator.OperatorCreationException- Throws:
IOExceptionorg.bouncycastle.operator.OperatorCreationException
-
getKeyStorePkcs12File
- Throws:
IOException
-
generateKeyStorePkcs12File
- Throws:
IOException
-
getKeyStoreBcfksFile
- Throws:
IOException
-
generateKeyStoreBcfksFile
- Throws:
IOException
-
setConfigurations
public void setConfigurations(org.apache.hadoop.hbase.io.crypto.tls.KeyStoreFileType keyStoreFileType, org.apache.hadoop.hbase.io.crypto.tls.KeyStoreFileType trustStoreFileType) throws IOException Sets the SSL system properties such that the given X509Util object can be used to create SSL Contexts that will use the trust store and key store files created by this test context. Example usage:X509TestContext testContext = ...; // create the test context X509Util x509Util = new QuorumX509Util(); testContext.setSystemProperties(x509Util, KeyStoreFileType.JKS, KeyStoreFileType.JKS); // The returned context will use the key store and trust store created by the test context. SSLContext ctx = x509Util.getDefaultSSLContext();- Parameters:
keyStoreFileType- the store file type to use for the key store (JKS, PEM, ...).trustStoreFileType- the store file type to use for the trust store (JKS, PEM, ...).- Throws:
IOException- if there is an error creating the key store file or trust store file.
-
setKeystoreConfigurations
public void setKeystoreConfigurations(org.apache.hadoop.hbase.io.crypto.tls.KeyStoreFileType keyStoreFileType, org.apache.hadoop.conf.Configuration confToSet) throws IOException Sets the KeyStore-related SSL system properties onto the given Configuration such that X509Util can be used to create SSL Contexts using that KeyStore. This can be used in special circumstances to inject a "bad" certificate where the keystore doesn't match the CA in the truststore. Or use it to create a connection without a truststore. -
clearConfigurations
-
cloneWithNewKeystoreCert
Creates a clone of the current context, but injecting the passed certificate as the KeyStore cert. The new context's keystore path fields are nulled, so the next call tosetConfigurations(KeyStoreFileType, KeyStoreFileType),setKeystoreConfigurations(KeyStoreFileType, Configuration), orgetKeyStoreFile(KeyStoreFileType)will create a new keystore with this certificate in place.- Parameters:
cert- the cert to replace
-
regenerateStores
public void regenerateStores(X509KeyType keyStoreKeyType, X509KeyType trustStoreKeyType, org.apache.hadoop.hbase.io.crypto.tls.KeyStoreFileType keyStoreFileType, org.apache.hadoop.hbase.io.crypto.tls.KeyStoreFileType trustStoreFileType, String... subjectAltNames) throws GeneralSecurityException, IOException, org.bouncycastle.operator.OperatorCreationException - Throws:
GeneralSecurityExceptionIOExceptionorg.bouncycastle.operator.OperatorCreationException
-
createCertificates
private void createCertificates(String... subjectAltNames) throws GeneralSecurityException, IOException, org.bouncycastle.operator.OperatorCreationException - Throws:
GeneralSecurityExceptionIOExceptionorg.bouncycastle.operator.OperatorCreationException
-
newBuilder
Returns a new default-constructed Builder.- Returns:
- a new Builder.
-