Search in sources :

Example 36 with RepositoryException

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

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

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

the class KeystoneRepositoryAuthenticator method getUserFromToken.

@Override
public UserEntity getUserFromToken(String userIdString, byte[] tokenSecret) throws AuthenticatorException {
    int userId;
    try {
        userId = Integer.parseInt(userIdString);
    } catch (NumberFormatException e) {
        throw new AuthenticatorException("Invalid user id", e);
    }
    if (tokenSecret.length < 1) {
        throw new IllegalArgumentException();
    }
    CryptoKey userSecret = authenticationSecrets.decryptSecretFromToken(tokenSecret);
    if (userSecret == null) {
        throw new AuthenticatorException("Authentication timed out");
    }
    UserEntity user;
    try {
        user = repository.findUserById(userId);
    } catch (RepositoryException e) {
        throw new AuthenticatorException("Error while authenticating user", e);
    }
    user.unlock(userSecret);
    if (user.isLocked()) {
        return null;
    }
    return user;
}
Also used : CryptoKey(com.fathomdb.crypto.CryptoKey) AuthenticatorException(org.platformlayer.auth.AuthenticatorException) RepositoryException(org.platformlayer.RepositoryException) UserEntity(org.platformlayer.auth.UserEntity)

Example 39 with RepositoryException

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

the class TokenHelpers method buildAccess.

public Access buildAccess(UserEntity user) {
    byte[] tokenSecret = authSecrets.buildToken(user.getUserSecret());
    TokenInfo token = buildToken("" + user.getId(), tokenSecret);
    Access access = new Access();
    // response.access.serviceCatalog = serviceMapper.getServices(userInfo,
    // project);
    access.token = new Token();
    access.token.expires = token.expiration;
    access.token.id = tokenService.encodeToken(token);
    access.projects = Lists.newArrayList();
    try {
        for (ProjectEntity project : userAuthenticator.listProjects(user)) {
            access.projects.add(project.getName());
        }
    } catch (RepositoryException e) {
        log.warn("Error while listing projects for user: " + user.key, e);
        throw new IllegalStateException("Error listing projects for user", e);
    }
    return access;
}
Also used : ProjectEntity(org.platformlayer.auth.ProjectEntity) Access(org.platformlayer.auth.model.Access) Token(org.platformlayer.auth.model.Token) RepositoryException(org.platformlayer.RepositoryException)

Example 40 with RepositoryException

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

the class SimpleOperationQueue method jobFinished.

// @Override
// public JobExecutionList listRecentExecutions(ProjectId projectId) {
// JobExecutionList ret = JobExecutionList.create();
// synchronized (activeJobs) {
// for (ActiveJobExecution job : activeJobs.values()) {
// if (!Objects.equal(job.getTargetItemKey().getProject(), projectId)) {
// continue;
// }
// ret.add(job.getJobExecution());
// }
// }
//
// synchronized (recentJobs) {
// for (JobExecutionData jobExecution : recentJobs) {
// if (!jobExecution.getJobKey().getProject().equals(projectId)) {
// continue;
// }
// ret.add(jobExecution);
// }
// }
//
// return ret;
// }
// @Override
// public List<JobData> listRecentJobs(ProjectId projectId) {
// List<JobData> ret = Lists.newArrayList();
//
// synchronized (activeJobs) {
// for (ActiveJobExecution job : activeJobs.values()) {
// if (!Objects.equal(job.getTargetItemKey().getProject(), projectId)) {
// continue;
// }
// JobExecutionData execution = job.getJobExecution();
// ret.add(execution.getJob());
// }
// }
//
// synchronized (recentJobs) {
// for (JobExecutionData job : recentJobs) {
// if (!job.getJobKey().getProject().equals(projectId)) {
// continue;
// }
// ret.add(job.getJob());
// }
// }
//
// return ret;
// }
// private static final int RECENT_JOB_COUNT = 100;
@Override
public void jobFinished(JobExecutionData jobExecutionData, JobState state, JobLogger logger) throws OpsException {
    PlatformLayerKey jobKey = jobExecutionData.getJobKey();
    String executionId = jobExecutionData.getExecutionId();
    Date startTime = jobExecutionData.getStartedAt();
    Date endTime = new Date();
    synchronized (activeJobs) {
        activeJobs.remove(jobKey);
    }
    try {
        String logCookie = jobLogStore.saveJobLog(jobKey, executionId, startTime, logger);
        jobRepository.recordJobEnd(jobKey, executionId, endTime, state, logCookie);
    } catch (RepositoryException e) {
        throw new OpsException("Error writing job to repository", e);
    } catch (Exception e) {
        throw new OpsException("Error writing job log", e);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) RepositoryException(org.platformlayer.RepositoryException) Date(java.util.Date) RepositoryException(org.platformlayer.RepositoryException) OpsException(org.platformlayer.ops.OpsException)

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