Search in sources :

Example 6 with JdbcTransaction

use of com.fathomdb.jdbc.JdbcTransaction in project platformlayer by platformlayer.

the class JdbcUserRepository method grantProjectToProject.

@Override
@JdbcTransaction
public void grantProjectToProject(String grantToProjectKey, String onProjectKey, SecretKey onProjectSecret) throws RepositoryException {
    DbHelper db = new DbHelper();
    try {
        ProjectEntity grantToProject = db.findProjectByKey(grantToProjectKey);
        if (grantToProject == null) {
            throw new RepositoryException("Project not found");
        }
        ProjectEntity onProject = db.findProjectByKey(onProjectKey);
        if (onProject == null) {
            throw new RepositoryException("Project not found");
        }
        byte[] projectSecretData = onProjectSecret.getEncoded();
        PublicKey grantToProjectPublicKey = grantToProject.getPublicKey();
        byte[] newSecretData;
        try {
            SecretStore store = new SecretStore(onProject.secretData);
            Writer writer = store.buildWriter();
            writer.writeAsymetricProjectKey(projectSecretData, grantToProject.id, grantToProjectPublicKey);
            writer.close();
            store.appendContents(writer);
            newSecretData = store.getEncoded();
        } catch (IOException e) {
            throw new RepositoryException("Error writing secrets", e);
        }
        db.updateProjectSecret(onProject.id, newSecretData);
    // db.insertUserProject(user.id, project.id);
    } catch (SQLException e) {
        throw new RepositoryException("Error reading groups", e);
    } finally {
        db.close();
    }
}
Also used : SQLException(java.sql.SQLException) PublicKey(java.security.PublicKey) RepositoryException(org.platformlayer.RepositoryException) IOException(java.io.IOException) SecretStore(org.platformlayer.auth.crypto.SecretStore) Writer(org.platformlayer.auth.crypto.SecretStore.Writer) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 7 with JdbcTransaction

use of com.fathomdb.jdbc.JdbcTransaction in project platformlayer by platformlayer.

the class JdbcManagedItemRepository method changeTags.

@Override
@JdbcTransaction
public Tags changeTags(ModelClass<?> modelClass, ProjectId project, ManagedItemId itemKey, TagChanges changeTags, Long ifVersion) throws RepositoryException {
    DbHelper db = new DbHelper(modelClass, project);
    try {
        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, itemKey.getKey());
        if (rs == null) {
            // TODO: Better exception??
            throw new IllegalStateException("Not found");
        }
        int itemId = rs.id;
        if (ifVersion != null) {
            log.warn("CAS version swapping not implemented");
        }
        Tags tags = new Tags();
        mapToTags(db.listTagsForItem(itemId), tags);
        if (changeTags.addTags != null) {
            for (Tag addTag : changeTags.addTags) {
                if (tags.hasTag(addTag)) {
                    continue;
                }
                db.insertTag(itemId, addTag);
                tags.add(addTag);
            }
        }
        if (changeTags.removeTags != null) {
            for (Tag removeTag : changeTags.removeTags) {
                boolean removed = tags.remove(removeTag);
                if (!removed) {
                    continue;
                }
                db.removeTag(itemId, removeTag);
            }
        }
        return tags;
    } catch (SQLException e) {
        throw new RepositoryException("Error running query", e);
    } finally {
        db.close();
    }
}
Also used : SQLException(java.sql.SQLException) RepositoryException(org.platformlayer.RepositoryException) Tag(org.platformlayer.core.model.Tag) Tags(org.platformlayer.core.model.Tags) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 8 with JdbcTransaction

use of com.fathomdb.jdbc.JdbcTransaction in project platformlayer by platformlayer.

the class JdbcManagedItemRepository method createManagedItem.

@Override
@JdbcTransaction
public <T extends ItemBase> T createManagedItem(ProjectId project, T item) throws RepositoryException {
    DbHelper db = new DbHelper(item.getClass(), project);
    try {
        CryptoKey itemSecret = FathomdbCrypto.generateKey();
        byte[] data = serialize(item, itemSecret);
        byte[] secretData = itemSecrets.encodeItemSecret(itemSecret);
        int itemId = db.insertItem(item, data, secretData);
        Tags tags = item.tags;
        if (tags != null && !tags.isEmpty()) {
            db.insertTags(itemId, tags);
        }
        return item;
    } 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) Tags(org.platformlayer.core.model.Tags) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 9 with JdbcTransaction

use of com.fathomdb.jdbc.JdbcTransaction in project platformlayer by platformlayer.

the class JdbcManagedItemRepository method getManagedItem.

@Override
@JdbcTransaction
public ItemBase getManagedItem(PlatformLayerKey key, boolean fetchTags, SecretProvider secretProvider) throws RepositoryException {
    DbHelper db = new DbHelper(key);
    try {
        ServiceProvider serviceProvider = serviceProviderDirectory.getServiceProvider(key.getServiceType());
        if (serviceProvider == null) {
            throw new IllegalStateException();
        }
        ModelClass<?> modelClass = serviceProvider.getModelClass(key.getItemType());
        ServiceType serviceType = key.getServiceType();
        ItemType itemType = key.getItemType();
        ProjectId project = key.getProject();
        ManagedItemId itemId = key.getItemId();
        return fetchItem(db, serviceType, itemType, project, itemId, modelClass.getJavaClass(), secretProvider, fetchTags);
    } catch (SQLException e) {
        throw new RepositoryException("Error running query", e);
    } finally {
        db.close();
    }
}
Also used : SQLException(java.sql.SQLException) ServiceType(org.platformlayer.ids.ServiceType) ServiceProvider(org.platformlayer.xaas.services.ServiceProvider) ItemType(org.platformlayer.ids.ItemType) ProjectId(org.platformlayer.ids.ProjectId) RepositoryException(org.platformlayer.RepositoryException) ManagedItemId(org.platformlayer.ids.ManagedItemId) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 10 with JdbcTransaction

use of com.fathomdb.jdbc.JdbcTransaction in project platformlayer by platformlayer.

the class JdbcManagedItemRepository method findRoots.

@Override
@JdbcTransaction
public List<ItemBase> findRoots(ProjectId project, boolean fetchTags, SecretProvider secretProvider) throws RepositoryException {
    DbHelper db = new DbHelper(project);
    try {
        // TODO: Push-down logic for item selection as well
        JoinedQueryResult result = db.listRoots();
        List<ItemBase> roots = mapItemsAndTags(project, secretProvider, db, result);
        for (ItemBase root : roots) {
            // A little bit of paranoia
            boolean isRoot = true;
            for (Tag tag : root.getTags()) {
                boolean tagIsParent = Tag.PARENT.getKey().equals(tag.getKey());
                if (tagIsParent) {
                    isRoot = false;
                    break;
                }
            }
            assert isRoot;
            if (!isRoot) {
                throw new IllegalStateException();
            }
        }
        return roots;
    } 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)

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