use of org.apache.knox.gateway.services.security.KeystoreServiceException in project knox by apache.
the class DefaultAliasService method getAliasesForCluster.
@Override
public List<String> getAliasesForCluster(String clusterName) {
ArrayList<String> list = new ArrayList<>();
KeyStore keyStore;
try {
keyStore = keystoreService.getCredentialStoreForCluster(clusterName);
if (keyStore != null) {
String alias;
try {
Enumeration<String> e = keyStore.aliases();
while (e.hasMoreElements()) {
alias = e.nextElement();
// only include the metadata key names in the list of names
if (!alias.contains("@")) {
list.add(alias);
}
}
} catch (KeyStoreException e) {
LOG.failedToGetCredentialForCluster(clusterName, e);
}
}
} catch (KeystoreServiceException kse) {
LOG.failedToGetCredentialForCluster(clusterName, kse);
}
return list;
}
use of org.apache.knox.gateway.services.security.KeystoreServiceException in project knox by apache.
the class DefaultGatewayServices method initializeContribution.
@Override
public void initializeContribution(DeploymentContext context) {
// setup credential store as appropriate
String clusterName = context.getTopology().getName();
try {
KeystoreService ks = getService(ServiceType.KEYSTORE_SERVICE);
if (!ks.isCredentialStoreForClusterAvailable(clusterName)) {
log.creatingCredentialStoreForCluster(clusterName);
ks.createCredentialStoreForCluster(clusterName);
} else {
log.credentialStoreForClusterFoundNotCreating(clusterName);
}
} catch (KeystoreServiceException e) {
throw new RuntimeException("Credential store was found but was unable to be loaded - the provided (or persisted) master secret may not match the password for the credential store.", e);
}
}
use of org.apache.knox.gateway.services.security.KeystoreServiceException in project knox by apache.
the class BaseKeystoreService method createKeystore.
protected void createKeystore(String filename, String keystoreType) throws KeystoreServiceException {
try {
FileOutputStream out = createKeyStoreFile(filename);
KeyStore ks = KeyStore.getInstance(keystoreType);
ks.load(null, null);
ks.store(out, masterService.getMasterSecret());
out.close();
} catch (KeyStoreException e) {
LOG.failedToCreateKeystore(filename, keystoreType, e);
throw new KeystoreServiceException(e);
} catch (NoSuchAlgorithmException e) {
LOG.failedToCreateKeystore(filename, keystoreType, e);
throw new KeystoreServiceException(e);
} catch (CertificateException e) {
LOG.failedToCreateKeystore(filename, keystoreType, e);
throw new KeystoreServiceException(e);
} catch (FileNotFoundException e) {
LOG.failedToCreateKeystore(filename, keystoreType, e);
throw new KeystoreServiceException(e);
} catch (IOException e) {
LOG.failedToCreateKeystore(filename, keystoreType, e);
throw new KeystoreServiceException(e);
}
}
use of org.apache.knox.gateway.services.security.KeystoreServiceException in project knox by apache.
the class DefaultKeystoreService method isCredentialStoreForClusterAvailable.
@Override
public boolean isCredentialStoreForClusterAvailable(String clusterName) throws KeystoreServiceException {
boolean rc = false;
final File keyStoreFile = new File(keyStoreDir + clusterName + CREDENTIALS_SUFFIX);
readLock.lock();
try {
try {
rc = isKeystoreAvailable(keyStoreFile, "JCEKS");
} catch (KeyStoreException e) {
throw new KeystoreServiceException(e);
} catch (IOException e) {
throw new KeystoreServiceException(e);
}
return rc;
} finally {
readLock.unlock();
}
}
use of org.apache.knox.gateway.services.security.KeystoreServiceException in project knox by apache.
the class DefaultKeystoreService method addSelfSignedCertForGateway.
@Override
public void addSelfSignedCertForGateway(String alias, char[] passphrase, String hostname) throws KeystoreServiceException {
writeLock.lock();
try {
KeyPairGenerator keyPairGenerator;
try {
keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024);
KeyPair KPair = keyPairGenerator.generateKeyPair();
if (hostname == null) {
hostname = System.getProperty(CERT_GEN_MODE, CERT_GEN_MODE_LOCALHOST);
}
X509Certificate cert = null;
if (hostname.equals(CERT_GEN_MODE_HOSTNAME)) {
String dn = buildDistinguishedName(InetAddress.getLocalHost().getHostName());
cert = X509CertificateUtil.generateCertificate(dn, KPair, 365, "SHA1withRSA");
} else {
String dn = buildDistinguishedName(hostname);
cert = X509CertificateUtil.generateCertificate(dn, KPair, 365, "SHA1withRSA");
}
KeyStore privateKS = getKeystoreForGateway();
privateKS.setKeyEntry(alias, KPair.getPrivate(), passphrase, new java.security.cert.Certificate[] { cert });
writeKeystoreToFile(privateKS, new File(keyStoreDir + GATEWAY_KEYSTORE));
// writeCertificateToFile( cert, new File( keyStoreDir + alias + ".pem" ) );
} catch (NoSuchAlgorithmException e) {
LOG.failedToAddSeflSignedCertForGateway(alias, e);
throw new KeystoreServiceException(e);
} catch (GeneralSecurityException e) {
LOG.failedToAddSeflSignedCertForGateway(alias, e);
throw new KeystoreServiceException(e);
} catch (IOException e) {
LOG.failedToAddSeflSignedCertForGateway(alias, e);
throw new KeystoreServiceException(e);
}
} finally {
writeLock.unlock();
}
}
Aggregations