Search in sources :

Example 16 with JdbcTransaction

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();
    }
}
Also used : SQLException(java.sql.SQLException) CryptoKey(com.fathomdb.crypto.CryptoKey) AesCryptoKey(com.fathomdb.crypto.AesCryptoKey) RepositoryException(org.platformlayer.RepositoryException) ManagedItemId(org.platformlayer.ids.ManagedItemId) ModelClass(org.platformlayer.xaas.services.ModelClass) SecretProvider(org.platformlayer.auth.crypto.SecretProvider) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 17 with JdbcTransaction

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();
    }
}
Also used : ItemBase(org.platformlayer.core.model.ItemBase) SQLException(java.sql.SQLException) RepositoryException(org.platformlayer.RepositoryException) Tag(org.platformlayer.core.model.Tag) JoinedQueryResult(com.fathomdb.jpa.impl.JoinedQueryResult) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 18 with JdbcTransaction

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();
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) SQLException(java.sql.SQLException) RepositoryException(org.platformlayer.RepositoryException) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 19 with JdbcTransaction

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();
    }
}
Also used : SimpleCertificateAndKey(com.fathomdb.crypto.SimpleCertificateAndKey) SQLException(java.sql.SQLException) X500Principal(javax.security.auth.x500.X500Principal) RepositoryException(org.platformlayer.RepositoryException) CertificateAndKey(com.fathomdb.crypto.CertificateAndKey) SimpleCertificateAndKey(com.fathomdb.crypto.SimpleCertificateAndKey) X509Certificate(java.security.cert.X509Certificate) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 20 with JdbcTransaction

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();
    }
}
Also used : SQLException(java.sql.SQLException) RepositoryException(org.platformlayer.RepositoryException) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Aggregations

JdbcTransaction (com.fathomdb.jdbc.JdbcTransaction)30 SQLException (java.sql.SQLException)30 RepositoryException (org.platformlayer.RepositoryException)30 ProjectId (org.platformlayer.ids.ProjectId)7 CryptoKey (com.fathomdb.crypto.CryptoKey)6 AesCryptoKey (com.fathomdb.crypto.AesCryptoKey)4 IOException (java.io.IOException)4 SecretStore (org.platformlayer.auth.crypto.SecretStore)4 Writer (org.platformlayer.auth.crypto.SecretStore.Writer)4 JoinedQueryResult (com.fathomdb.jpa.impl.JoinedQueryResult)3 PublicKey (java.security.PublicKey)3 ResultSet (java.sql.ResultSet)3 Tag (org.platformlayer.core.model.Tag)3 ManagedItemId (org.platformlayer.ids.ManagedItemId)3 JdbcConnection (com.fathomdb.jdbc.JdbcConnection)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 KeyPair (java.security.KeyPair)2 X509Certificate (java.security.cert.X509Certificate)2 PreparedStatement (java.sql.PreparedStatement)2 ItemBase (org.platformlayer.core.model.ItemBase)2