Search in sources :

Example 6 with ApiServiceExecutionException

use of com.bluenimble.platform.api.ApiServiceExecutionException in project serverless by bluenimble.

the class GetRecordSpi method execute.

@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    String provider = (String) request.get(CommonSpec.Provider);
    String sEntity = (String) request.get(CommonSpec.Entity);
    String record = (String) request.get(Spec.Record);
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    DatabaseObject dbo = null;
    try {
        Database db = space.feature(Database.class, provider, request);
        dbo = db.get(sEntity, record);
    } catch (DatabaseException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    if (dbo == null) {
        return null;
    }
    return new JsonApiOutput(dbo.toJson(null));
}
Also used : ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) Database(com.bluenimble.platform.db.Database) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) DatabaseException(com.bluenimble.platform.db.DatabaseException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 7 with ApiServiceExecutionException

use of com.bluenimble.platform.api.ApiServiceExecutionException in project serverless by bluenimble.

the class DownloadRootKeysSpi method execute.

@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    String paraphrase = (String) request.get(Spec.Paraphrase);
    try {
        KeyPair kp = api.space().getRootKeys();
        JsonObject oKeys = new JsonObject();
        oKeys.set(Output.Name, Json.getString(request.getNode(), ApiRequest.Fields.Node.Id) + " " + Json.getString(request.getNode(), ApiRequest.Fields.Node.Version));
        oKeys.set(Output.Endpoint, request.getScheme() + "://" + request.getEndpoint() + Lang.SLASH + api.space().getNamespace() + Lang.SLASH + api.getNamespace());
        oKeys.set(KeyPair.Fields.AccessKey, kp.accessKey());
        oKeys.set(KeyPair.Fields.SecretKey, kp.secretKey());
        oKeys.set(CommonSpec.Role, "SUPER");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Json.encrypt(oKeys, paraphrase, out);
        return new ApiByteArrayOutput(Output.KeysName + Lang.DOT + Output.KeysExt, Base64.encodeBase64(out.toByteArray()), ApiContentTypes.Stream, Output.KeysExt).set(ApiOutput.Defaults.Disposition, "attachment");
    } catch (Exception e) {
        throw new ApiServiceExecutionException(e.getMessage()).status(ApiResponse.FORBIDDEN);
    }
}
Also used : KeyPair(com.bluenimble.platform.security.KeyPair) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ApiByteArrayOutput(com.bluenimble.platform.api.impls.ApiByteArrayOutput) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException)

Example 8 with ApiServiceExecutionException

use of com.bluenimble.platform.api.ApiServiceExecutionException in project serverless by bluenimble.

the class GetKeysSpi method execute.

@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    String accessKey = (String) request.get(ApiConsumer.Fields.AccessKey);
    String paraphrase = (String) request.get(Spec.Paraphrase);
    if (!MgmUtils.isSecure(request.getService())) {
        return getNotSecure(api, request, accessKey, paraphrase);
    }
    Role cRole = Role.valueOf((String) consumer.get(CommonSpec.Role));
    String cAccessKey = (String) consumer.get(ApiConsumer.Fields.AccessKey);
    ApiSpace keysSpace = null;
    KeyPair kp;
    // if consumer is super
    try {
        if (Role.SUPER.equals(cRole)) {
            // If super is calling this service, accessKey should be prefixed by space namespace
            int indexOfDot = accessKey.indexOf(Lang.DOT);
            if (indexOfDot <= 0) {
                throw new ApiServiceExecutionException("invalid accessKey. Using super privileges, you should prefix the accessKey by the space.").status(ApiResponse.BAD_REQUEST);
            }
            String space = accessKey.substring(0, indexOfDot);
            accessKey = accessKey.substring(indexOfDot + 1);
            keysSpace = api.space().space(space);
        } else {
            keysSpace = MgmUtils.space(consumer, api);
        }
    } catch (Exception e) {
        throw new ApiServiceExecutionException("access denied. " + e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    try {
        kp = keysSpace.keystore().get(accessKey, true);
    } catch (Exception e) {
        throw new ApiServiceExecutionException("can't access space keystore").status(ApiResponse.FORBIDDEN);
    }
    if (kp == null) {
        throw new ApiServiceExecutionException("accessKey " + accessKey + " not found").status(ApiResponse.NOT_FOUND);
    }
    if (cAccessKey.equals(keysSpace.getNamespace() + Lang.DOT + accessKey)) {
        try {
            return toOutput(kp, paraphrase, keysSpace, api, request);
        } catch (Exception e) {
            throw new ApiServiceExecutionException(e.getMessage(), e);
        }
    }
    Role keysRole = Role.valueOf((String) kp.property(CommonSpec.Role));
    if (Role.DEVELOPER.equals(cRole)) {
        throw new ApiServiceExecutionException("access denied").status(ApiResponse.FORBIDDEN);
    }
    if (Role.ADMIN.equals(cRole) && Role.ADMIN.equals(keysRole)) {
        throw new ApiServiceExecutionException("access denied. only super keys can read ADMIN keys").status(ApiResponse.FORBIDDEN);
    }
    try {
        return toOutput(kp, paraphrase, keysSpace, api, request);
    } catch (Exception e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
}
Also used : Role(com.bluenimble.platform.apis.mgm.Role) KeyPair(com.bluenimble.platform.security.KeyPair) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) EncryptionProviderException(com.bluenimble.platform.security.EncryptionProviderException)

Example 9 with ApiServiceExecutionException

use of com.bluenimble.platform.api.ApiServiceExecutionException in project serverless by bluenimble.

the class GetKeysSpi method getNotSecure.

private ApiOutput getNotSecure(Api api, ApiRequest request, String accessKey, String paraphrase) throws ApiServiceExecutionException {
    ApiSpace keysSpace = null;
    int indexOfDot = accessKey.indexOf(Lang.DOT);
    if (indexOfDot <= 0) {
        throw new ApiServiceExecutionException("invalid accessKey. Using super privileges, you should prefix the accessKey by the space NS.").status(ApiResponse.BAD_REQUEST);
    }
    String space = accessKey.substring(0, indexOfDot);
    accessKey = accessKey.substring(indexOfDot + 1);
    try {
        keysSpace = api.space().space(space);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException("access denied").status(ApiResponse.FORBIDDEN);
    }
    KeyPair skp = null;
    try {
        skp = keysSpace.keystore().get(accessKey, true);
    } catch (Exception e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    if (skp == null) {
        throw new ApiServiceExecutionException("keys " + accessKey + " not found").status(ApiResponse.NOT_FOUND);
    }
    try {
        return toOutput(skp, paraphrase, keysSpace, api, request);
    } catch (Exception e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
}
Also used : ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) KeyPair(com.bluenimble.platform.security.KeyPair) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) EncryptionProviderException(com.bluenimble.platform.security.EncryptionProviderException)

Example 10 with ApiServiceExecutionException

use of com.bluenimble.platform.api.ApiServiceExecutionException in project serverless by bluenimble.

the class ChangeApiStatusSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    String apiNs = (String) request.get(CommonSpec.Api);
    String sAction = (String) request.getResource()[request.getResource().length - 1];
    Action action = null;
    try {
        action = Action.valueOf(sAction);
    } catch (Exception ex) {
    // ignore
    }
    if (action == null) {
        throw new ApiServiceExecutionException("unknown change-status action " + sAction).status(ApiResponse.BAD_REQUEST);
    }
    ApiSpace space = null;
    try {
        space = MgmUtils.space(consumer, api);
        switch(action) {
            case start:
                space.start(apiNs);
                break;
            case stop:
                space.stop(apiNs);
                break;
            case pause:
                space.pause(apiNs);
                break;
            case resume:
                space.resume(apiNs);
                break;
            default:
                break;
        }
    } catch (Exception ex) {
        throw new ApiServiceExecutionException(ex.getMessage(), ex);
    }
    Api targetApi = space.api(apiNs);
    JsonObject result = (JsonObject) new JsonObject().set(Api.Spec.Status, targetApi.status().name());
    if (ApiStatus.Failed.equals(targetApi.status())) {
        result.set(Output.Reason, targetApi.getFailure());
    }
    return new JsonApiOutput(result);
}
Also used : ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) Api(com.bluenimble.platform.api.Api) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Aggregations

ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)52 JsonObject (com.bluenimble.platform.json.JsonObject)41 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)39 ApiSpace (com.bluenimble.platform.api.ApiSpace)31 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)28 Database (com.bluenimble.platform.db.Database)13 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)9 ApiOutput (com.bluenimble.platform.api.ApiOutput)8 DatabaseException (com.bluenimble.platform.db.DatabaseException)8 Storage (com.bluenimble.platform.storage.Storage)8 StorageException (com.bluenimble.platform.storage.StorageException)8 StorageObject (com.bluenimble.platform.storage.StorageObject)8 Api (com.bluenimble.platform.api.Api)6 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)5 JsonArray (com.bluenimble.platform.json.JsonArray)5 ScriptingEngine (com.bluenimble.platform.scripting.ScriptingEngine)5 ScriptingEngineException (com.bluenimble.platform.scripting.ScriptingEngineException)5 Date (java.util.Date)5 ApiStreamSource (com.bluenimble.platform.api.ApiStreamSource)4 ApiVerb (com.bluenimble.platform.api.ApiVerb)4