Search in sources :

Example 16 with RepositoryException

use of org.platformlayer.RepositoryException 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 17 with RepositoryException

use of org.platformlayer.RepositoryException in project platformlayer by platformlayer.

the class PrivateDataHelper method putKeyPair.

public void putKeyPair(ProjectId project, ServiceType serviceType, ServiceMetadataKey keyId, KeyPair keyPair) throws OpsException {
    String serialized;
    try {
        serialized = KeyPairUtils.serialize(keyPair);
    } catch (IOException e) {
        throw new OpsException("Error serializing key pair", e);
    }
    try {
        serviceAuthorizationService.setPrivateData(serviceType, project, keyId, serialized);
    } catch (RepositoryException e) {
        throw new OpsException("Error writing to repository", e);
    }
    saveKeypairInDevelopment(project, serviceType, keyPair);
}
Also used : OpsException(org.platformlayer.ops.OpsException) RepositoryException(org.platformlayer.RepositoryException) IOException(java.io.IOException)

Example 18 with RepositoryException

use of org.platformlayer.RepositoryException 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 19 with RepositoryException

use of org.platformlayer.RepositoryException 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 20 with RepositoryException

use of org.platformlayer.RepositoryException 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)

Aggregations

RepositoryException (org.platformlayer.RepositoryException)56 JdbcTransaction (com.fathomdb.jdbc.JdbcTransaction)30 SQLException (java.sql.SQLException)30 OpsException (org.platformlayer.ops.OpsException)18 ProjectId (org.platformlayer.ids.ProjectId)14 ItemBase (org.platformlayer.core.model.ItemBase)10 CryptoKey (com.fathomdb.crypto.CryptoKey)8 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)7 ManagedItemId (org.platformlayer.ids.ManagedItemId)7 OpsContext (org.platformlayer.ops.OpsContext)7 ServiceProvider (org.platformlayer.xaas.services.ServiceProvider)7 IOException (java.io.IOException)6 AesCryptoKey (com.fathomdb.crypto.AesCryptoKey)5 PublicKey (java.security.PublicKey)4 JAXBException (javax.xml.bind.JAXBException)4 Tag (org.platformlayer.core.model.Tag)4 ServiceType (org.platformlayer.ids.ServiceType)4 JobData (org.platformlayer.jobs.model.JobData)4 JoinedQueryResult (com.fathomdb.jpa.impl.JoinedQueryResult)3 X509Certificate (java.security.cert.X509Certificate)3