use of com.fathomdb.crypto.CertificateAndKey in project platformlayer by platformlayer.
the class KeyStoreEncryptionStore method getCertificateAndKey.
@Override
public CertificateAndKey getCertificateAndKey(String alias) {
CertificateAndKey certificateAndKey;
if (alias.startsWith("/")) {
// Path to file
File certPath = new File(alias + ".crt");
List<X509Certificate> certificate;
try {
certificate = CertificateUtils.fromPem(certPath);
} catch (IOException e) {
throw new IllegalArgumentException("Error reading certificate: " + certPath, e);
}
File keyPath = new File(alias + ".key");
PrivateKey privateKey;
try {
privateKey = PrivateKeys.fromPem(keyPath);
} catch (IOException e) {
throw new IllegalArgumentException("Error reading private key: " + keyPath, e);
}
certificateAndKey = new SimpleCertificateAndKey(certificate, privateKey);
} else {
String password = DEFAULT_PASSWORD;
try {
certificateAndKey = KeyStoreUtils.getCertificateAndKey(keyStore, alias, password);
} catch (GeneralSecurityException e) {
throw new IllegalArgumentException("Error reading private key", e);
}
if (certificateAndKey == null) {
log.warn("Unable to find private key: " + alias);
throw new IllegalArgumentException("Private key not found");
}
}
return certificateAndKey;
}
use of com.fathomdb.crypto.CertificateAndKey in project platformlayer by platformlayer.
the class SimpleMultitenantConfiguration method build.
public static MultitenantConfiguration build(Configuration configuration, EncryptionStore encryptionStore, AuthenticationService authenticationService, AuthenticationTokenValidator authenticationTokenValidator) throws OpsException {
String projectKey = configuration.lookup("multitenant.project", null);
String username = configuration.lookup("multitenant.user", null);
String password = configuration.lookup("multitenant.password", null);
String certAlias = configuration.lookup("multitenant.cert", null);
CertificateAndKey certificateAndKey = null;
if (certAlias != null) {
certificateAndKey = encryptionStore.getCertificateAndKey(certAlias);
}
String message = "Invalid multitenant configuration";
if (username == null || projectKey == null) {
throw new OpsException(message);
}
AuthenticationToken authn = null;
if (certificateAndKey != null) {
try {
authn = authenticationService.authenticateWithCertificate(username, certificateAndKey.getPrivateKey(), certificateAndKey.getCertificateChain());
} catch (PlatformlayerAuthenticationClientException e) {
throw new OpsException(message, e);
}
} else if (password != null) {
log.warn("Using password authentication with multitenant");
if (!ApplicationMode.isDevelopment()) {
throw new IllegalStateException();
}
try {
authn = authenticationService.authenticateWithPassword(username, password);
} catch (PlatformlayerAuthenticationClientException e) {
throw new OpsException(message, e);
}
}
if (authn == null) {
throw new OpsException(message);
}
ProjectAuthorization authz = authenticationTokenValidator.validateToken(authn, projectKey);
if (authz == null) {
throw new OpsException(message);
}
// {
// try {
// project = userRepository.findProject(user, projectKey);
// } catch (RepositoryException e) {
// throw new OpsException(message, e);
// }
//
// if (project == null) {
// throw new OpsException(message);
// }
// }
List<PlatformLayerKey> mappedItems = Lists.newArrayList();
for (String key : Splitter.on(",").split(configuration.lookup("multitenant.keys", ""))) {
String[] tokens = key.split("/");
if (tokens.length != 2) {
throw new IllegalStateException();
}
String serviceType = tokens[0];
String itemType = tokens[1];
mappedItems.add(PlatformLayerKey.fromServiceAndItem(serviceType, itemType));
}
if (mappedItems.isEmpty()) {
throw new OpsException(message);
}
MultitenantConfiguration config = new SimpleMultitenantConfiguration(authz, mappedItems);
return config;
}
use of com.fathomdb.crypto.CertificateAndKey in project platformlayer by platformlayer.
the class JdbcUserRepository method getProjectPki.
@Override
@JdbcTransaction
public CertificateAndKey getProjectPki(ProjectEntity project) throws RepositoryException, OpsException {
DbHelper db = new DbHelper();
try {
ProjectEntity existing = findProjectByKey(db, project.getName());
if (existing == null) {
return null;
}
project.setProjectSecret(project.getProjectSecret());
if (project.getPkiCertificate() == null) {
// KeyPair keyPair = RsaUtils.generateRsaKeyPair();
// SimpleCertificateAuthority ca = new SimpleCertificateAuthority();
X500Principal subject = new X500Principal("CN=" + project.getName());
CertificateAndKey certificateAndKey = CertificateUtils.createSelfSigned(subject, RsaUtils.DEFAULT_KEYSIZE);
project.setPkiCertificate(certificateAndKey.getCertificateChain()[0]);
project.setPkiPrivateKey(certificateAndKey.getPrivateKey());
db.update(project);
}
X509Certificate[] certificateChain = new X509Certificate[1];
certificateChain[0] = project.getPkiCertificate();
CertificateAndKey certificateAndKey = new SimpleCertificateAndKey(certificateChain, project.getPkiPrivateKey());
return certificateAndKey;
} catch (SQLException e) {
throw new RepositoryException("Error retrieving PKI info", e);
} finally {
db.close();
}
}
use of com.fathomdb.crypto.CertificateAndKey in project platformlayer by platformlayer.
the class MetricClientImpl method build.
public static MetricClient build(Configuration configuration, EncryptionStore encryptionStore) throws OpsException {
if (!configuration.lookup("metrics.report.enabled", true)) {
return new DummyMetricClient();
}
// String cert = configuration.get("metrics.report.ssl.cert");
String cert = configuration.get("metrics.tls.clientcert");
CertificateAndKey certificateAndKey = encryptionStore.getCertificateAndKey(cert);
String project = configuration.get("metrics.report.project");
MetricTreeObject tags = new MetricTreeObject(null);
Map<String, String> tagProperties = configuration.getChildProperties("metrics.report.tags.");
copyPropertiesToTree(tagProperties, tags.getSubtree("tags"));
return build(configuration, encryptionStore, project, tags, certificateAndKey);
}
Aggregations