Search in sources :

Example 16 with ApiAccessDeniedException

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

the class CreateKeysSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    JsonObject payload = (JsonObject) request.get(ApiRequest.Payload);
    Role cRole = Role.valueOf((String) consumer.get(CommonSpec.Role));
    Role role = Role.SUPER.equals(cRole) ? Role.ADMIN : Role.DEVELOPER;
    String sRole = Json.getString(payload, CommonSpec.Role);
    if (!Lang.isNullOrEmpty(sRole)) {
        try {
            role = Role.valueOf(sRole.trim().toUpperCase());
        } catch (Exception ex) {
        // undefined role
        }
    }
    if (Role.SUPER.equals(cRole) && role.equals(Role.DEVELOPER)) {
        throw new ApiServiceExecutionException("super users can't create developer keys").status(ApiResponse.FORBIDDEN);
    }
    if (Role.ADMIN.equals(cRole) && role.equals(Role.ADMIN)) {
        throw new ApiServiceExecutionException("admin users can't create admin keys").status(ApiResponse.FORBIDDEN);
    }
    ApiSpace space;
    if (Role.SUPER.equals(cRole)) {
        String spaceNs = Json.getString(payload, Spec.Space);
        if (Lang.isNullOrEmpty(spaceNs)) {
            throw new ApiServiceExecutionException("no space found in payload").status(ApiResponse.BAD_REQUEST);
        }
        try {
            space = api.space().space(spaceNs);
        } catch (ApiAccessDeniedException e) {
            throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
        }
    } else {
        try {
            space = MgmUtils.space(consumer, api);
        } catch (ApiAccessDeniedException e) {
            throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
        }
    }
    if (space == null) {
        throw new ApiServiceExecutionException("target space where to create the keys isn't found").status(ApiResponse.BAD_REQUEST);
    }
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(CommonSpec.Role, role.name());
    Date expiryDate = null;
    if (!Json.isNullOrEmpty(payload)) {
        expiryDate = (Date) payload.get(KeyPair.Fields.ExpiryDate);
        Iterator<String> props = payload.keys();
        while (props.hasNext()) {
            String p = props.next();
            if (Exclude.contains(p)) {
                continue;
            }
            properties.put(p, payload.get(p));
        }
    }
    List<KeyPair> list = null;
    try {
        list = space.keystore().create(1, expiryDate, properties);
    } catch (SpaceKeyStoreException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.BAD_REQUEST);
    }
    if (list == null) {
        return new JsonApiOutput(null);
    }
    return new JsonApiOutput(list.get(0).toJson());
}
Also used : KeyPair(com.bluenimble.platform.security.KeyPair) HashMap(java.util.HashMap) JsonObject(com.bluenimble.platform.json.JsonObject) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) SpaceKeyStoreException(com.bluenimble.platform.security.SpaceKeyStoreException) Date(java.util.Date) Role(com.bluenimble.platform.apis.mgm.Role) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiSpace(com.bluenimble.platform.api.ApiSpace) SpaceKeyStoreException(com.bluenimble.platform.security.SpaceKeyStoreException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 17 with ApiAccessDeniedException

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

the class ListKeysSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    Role cRole = Role.valueOf((String) consumer.get(CommonSpec.Role));
    int offset = (Integer) request.get(Spec.Offset);
    int length = (Integer) request.get(Spec.Length);
    String sFilters = (String) request.get(Spec.Filters);
    SpaceKeyStore.ListFilter[] filters = null;
    if (!Lang.isNullOrEmpty(sFilters)) {
        String[] aFilters = Lang.split(sFilters, Lang.COMMA, true);
        filters = new SpaceKeyStore.ListFilter[aFilters.length + 1];
        for (int i = 0; i < aFilters.length; i++) {
            String f = aFilters[i];
            int idexOfStartUnderscore = f.indexOf(Token);
            if (idexOfStartUnderscore < -1) {
                continue;
            }
            int idexOfEndUnderscore = f.indexOf(Token, idexOfStartUnderscore + 2);
            if (idexOfEndUnderscore < -1) {
                continue;
            }
            filters[i] = new SpaceKeyStore.ListFilter() {

                @Override
                public String name() {
                    return f.substring(0, idexOfStartUnderscore);
                }

                @Override
                public Object value() {
                    String value = f.substring(idexOfEndUnderscore + 2);
                    if (Lang.isNullOrEmpty(value)) {
                        return null;
                    }
                    return value;
                }

                @Override
                public Operator operator() {
                    try {
                        return Operator.valueOf(f.substring(idexOfStartUnderscore + 2, idexOfEndUnderscore));
                    } catch (Exception ex) {
                        return Operator.eq;
                    }
                }
            };
        }
    } else {
        filters = new SpaceKeyStore.ListFilter[1];
    }
    JsonObject result = new JsonObject();
    JsonArray aKeys = new JsonArray();
    result.set(Output.Keys, aKeys);
    if (Role.SUPER.equals(cRole)) {
        filters[filters.length - 1] = new SpaceKeyStore.ListFilter() {

            @Override
            public String name() {
                return CommonSpec.Role;
            }

            @Override
            public Object value() {
                return Role.ADMIN.name();
            }

            @Override
            public Operator operator() {
                return Operator.eq;
            }
        };
        try {
            Collection<ApiSpace> spaces = api.space().spaces();
            for (ApiSpace space : spaces) {
                addSpaceKeys(space, offset, length, filters, aKeys);
            }
        } catch (ApiAccessDeniedException e) {
            throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.NOT_FOUND);
        }
    } else {
        filters[filters.length - 1] = new SpaceKeyStore.ListFilter() {

            @Override
            public String name() {
                return CommonSpec.Role;
            }

            @Override
            public Object value() {
                return Role.DEVELOPER.name();
            }

            @Override
            public Operator operator() {
                return Operator.eq;
            }
        };
        ApiSpace consumerSpace;
        try {
            consumerSpace = MgmUtils.space(consumer, api);
        } catch (ApiAccessDeniedException e) {
            throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.NOT_FOUND);
        }
        addSpaceKeys(consumerSpace, offset, length, filters, aKeys);
    }
    return new JsonApiOutput(result);
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) Role(com.bluenimble.platform.apis.mgm.Role) JsonArray(com.bluenimble.platform.json.JsonArray) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) SpaceKeyStore(com.bluenimble.platform.security.SpaceKeyStore) JsonObject(com.bluenimble.platform.json.JsonObject) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 18 with ApiAccessDeniedException

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

the class AddEntrySpi method execute.

@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    String provider = (String) request.get(CommonSpec.Provider);
    final JsonObject payload = (JsonObject) request.get(ApiRequest.Payload);
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    Cache cache = space.feature(Cache.class, provider, request);
    cache.put(Json.getString(payload, Spec.Key), payload.get(Spec.Value), Json.getInteger(payload, Spec.Ttl, 0));
    return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Added, true));
}
Also used : ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput) Cache(com.bluenimble.platform.cache.Cache)

Example 19 with ApiAccessDeniedException

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

the class GetEntrySpi 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 key = (String) request.get(Spec.Key);
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    Cache cache = space.feature(Cache.class, provider, request);
    return new JsonApiOutput((JsonObject) new JsonObject().set(Spec.Key, key).set(Output.Value, cache.get(key, false)));
}
Also used : ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput) Cache(com.bluenimble.platform.cache.Cache)

Example 20 with ApiAccessDeniedException

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

the class BulkRecordsSpi method execute.

@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    String provider = (String) request.get(CommonSpec.Provider);
    final JsonObject payload = (JsonObject) request.get(ApiRequest.Payload);
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    try {
        return new JsonApiOutput(space.feature(Database.class, provider, request).bulk(payload));
    } catch (DatabaseException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
}
Also used : ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseException(com.bluenimble.platform.db.DatabaseException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Aggregations

ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)28 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)27 ApiSpace (com.bluenimble.platform.api.ApiSpace)26 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)25 JsonObject (com.bluenimble.platform.json.JsonObject)22 Database (com.bluenimble.platform.db.Database)8 DatabaseException (com.bluenimble.platform.db.DatabaseException)8 Storage (com.bluenimble.platform.storage.Storage)6 StorageException (com.bluenimble.platform.storage.StorageException)6 StorageObject (com.bluenimble.platform.storage.StorageObject)6 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)4 JsonArray (com.bluenimble.platform.json.JsonArray)4 Cache (com.bluenimble.platform.cache.Cache)3 KeyPair (com.bluenimble.platform.security.KeyPair)3 Folder (com.bluenimble.platform.storage.Folder)3 Api (com.bluenimble.platform.api.Api)2 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)2 ApiStreamSource (com.bluenimble.platform.api.ApiStreamSource)2 Role (com.bluenimble.platform.apis.mgm.Role)2 ExchangeOption (com.bluenimble.platform.db.Database.ExchangeOption)2