use of com.fathomdb.jdbc.JdbcTransaction in project platformlayer by platformlayer.
the class JdbcManagedItemRepository method updateManagedItem.
@Override
@JdbcTransaction
public <T extends ItemBase> T updateManagedItem(ProjectId project, T item) throws RepositoryException {
Class<T> itemClass = (Class<T>) item.getClass();
DbHelper db = new DbHelper(itemClass, project);
try {
ManagedItemId itemId = new ManagedItemId(item.getId());
ModelClass<T> modelClass = serviceProviderDirectory.getModelClass(itemClass);
int projectId = db.mapToValue(project);
int modelId = db.mapToValue(modelClass.getItemType());
int serviceId = db.mapToValue(modelClass.getServiceType());
ItemEntity rs = db.queries.findItem(serviceId, modelId, projectId, itemId.getKey());
if (rs == null) {
throw new RepositoryException("Item not found");
}
byte[] secretData = rs.secret;
CryptoKey itemSecret;
if (secretData == null) {
itemSecret = FathomdbCrypto.generateKey();
secretData = itemSecrets.encodeItemSecret(itemSecret);
db.updateSecret(itemId, secretData);
} else {
itemSecret = item.secret.getSecret();
}
byte[] data = serialize(item, itemSecret);
db.updateItem(itemId, data, item.state);
// Note: we can't change tags here (that needs a separate call to updateTags)
SecretProvider secretProvider = SecretProvider.forKey(itemSecret);
boolean fetchTags = true;
return fetchItem(db, modelClass.getServiceType(), modelClass.getItemType(), project, itemId, itemClass, secretProvider, fetchTags);
} catch (SQLException e) {
throw new RepositoryException("Error running query", e);
} finally {
db.close();
}
}
use of com.fathomdb.jdbc.JdbcTransaction in project platformlayer by platformlayer.
the class JdbcManagedItemRepository method listAll.
@Override
@JdbcTransaction
public List<ItemBase> listAll(ProjectId project, Filter filter, SecretProvider secretProvider) throws RepositoryException {
DbHelper db = new DbHelper(project);
try {
log.debug("listAll with filter: {}", filter);
// TODO: Use this logic for item selection as well
List<Tag> requiredTags = filter.getRequiredTags();
JoinedQueryResult result;
if (!requiredTags.isEmpty()) {
Tag requiredTag = requiredTags.get(0);
int projectId = db.mapToValue(project);
result = db.queries.listAllItemsWithTag(projectId, projectId, requiredTag.getKey(), requiredTag.getValue());
} else {
log.warn("Unable to optimize filter; selecting all items. Filter={}", filter);
result = db.listAllItems();
}
List<ItemBase> items = mapItemsAndTags(project, secretProvider, db, result);
return applyFilter(items, filter);
} catch (SQLException e) {
throw new RepositoryException("Error fetching items", e);
} finally {
db.close();
}
}
use of com.fathomdb.jdbc.JdbcTransaction in project platformlayer by platformlayer.
the class JdbcSchedulerRepository method put.
@Override
@JdbcTransaction
public void put(SchedulerRecord record) throws RepositoryException {
DbHelper db = new DbHelper();
try {
String key = record.key;
SchedulerRecordEntity entity = toDb(record);
SchedulerRecordEntity existing = db.findByKey(key);
if (existing == null) {
db.insertItem(entity);
} else {
db.updateItem(entity);
}
} catch (SQLException e) {
throw new RepositoryException("Error running query", e);
} catch (OpsException e) {
throw new RepositoryException("Error serializing to database", e);
} finally {
db.close();
}
}
use of com.fathomdb.jdbc.JdbcTransaction 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.jdbc.JdbcTransaction in project platformlayer by platformlayer.
the class JdbcUserRepository method createServiceAccount.
@Override
@JdbcTransaction
public ServiceAccountEntity createServiceAccount(X509Certificate cert) throws RepositoryException {
DbHelper db = new DbHelper();
try {
// byte[] secretData;
//
// SecretKey userSecret = AesUtils.generateKey();
//
// try {
// byte[] plaintext = AesUtils.serialize(userSecret);
//
// byte[] tokenSecret = CryptoUtils.generateSecureRandom(plaintext.length);
//
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
// SecretStore.Writer writer = new SecretStore.Writer(baos);
//
// writer.writeUserPassword(plaintext, password);
// writer.writeLockedByToken(plaintext, OpsUser.TOKEN_ID_DEFAULT, tokenSecret);
// writer.close();
//
// secretData = baos.toByteArray();
// } catch (IOException e) {
// throw new RepositoryException("Error encrypting secrets", e);
// }
//
// byte[] hashedPassword = PasswordHash.doPasswordHash(password);
//
// KeyPair userRsaKeyPair = RsaUtils.generateRsaKeyPair(RsaUtils.SMALL_KEYSIZE);
//
// byte[] privateKeyData = RsaUtils.serialize(userRsaKeyPair.getPrivate());
// privateKeyData = AesUtils.encrypt(userSecret, privateKeyData);
// byte[] publicKeyData = RsaUtils.serialize(userRsaKeyPair.getPublic());
String subject = Certificates.getSubject(cert);
byte[] publicKey = cert.getPublicKey().getEncoded();
ServiceAccountEntity existing = db.queries.findServiceAccount(subject, publicKey);
if (existing == null) {
db.insertServiceAccount(subject, publicKey);
} else {
log.warn("Service account already exists; skipping creation");
}
return findServiceAccount(subject, publicKey);
} catch (SQLException e) {
throw new RepositoryException("Error creating service account", e);
} finally {
db.close();
}
}
Aggregations