use of com.bluenimble.platform.api.ApiSpace in project serverless by bluenimble.
the class DeleteRecordSpi 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);
}
try {
Database db = space.feature(Database.class, provider, request);
DatabaseObject oRecord = db.get(sEntity, record);
if (oRecord == null) {
return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Deleted, false));
}
oRecord.delete();
} catch (DatabaseException e) {
throw new ApiServiceExecutionException(e.getMessage(), e);
}
return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Deleted, true));
}
use of com.bluenimble.platform.api.ApiSpace in project serverless by bluenimble.
the class DropEntitySpi 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);
ApiSpace space;
try {
space = MgmUtils.space(consumer, api);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
}
try {
Database db = space.feature(Database.class, provider, request);
db.drop(sEntity);
} catch (DatabaseException e) {
throw new ApiServiceExecutionException(e.getMessage(), e);
} catch (Exception e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.NOT_FOUND);
}
return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Dropped, true));
}
use of com.bluenimble.platform.api.ApiSpace in project serverless by bluenimble.
the class AddFeatureSpi method execute.
@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
JsonObject oFeature = (JsonObject) request.get(ApiRequest.Payload);
ApiSpace space;
try {
space = MgmUtils.space(consumer, api);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.NOT_FOUND);
}
try {
space.addFeature(Json.getString(oFeature, Spec.Name), Json.getString(oFeature, Spec.Feature), Json.getString(oFeature, ApiSpace.Features.Provider), Json.getObject(oFeature, ApiSpace.Features.Spec));
} catch (ApiManagementException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.BAD_REQUEST);
}
return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Added, true));
}
use of com.bluenimble.platform.api.ApiSpace in project serverless by bluenimble.
the class AddSecretsSpi method execute.
@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
JsonObject oSecrets = (JsonObject) request.get(ApiRequest.Payload);
ApiSpace space;
try {
space = MgmUtils.space(consumer, api);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.NOT_FOUND);
}
try {
space.addSecrets((String) request.get(Spec.Name), oSecrets);
} catch (ApiManagementException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.BAD_REQUEST);
}
return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Added, true));
}
use of com.bluenimble.platform.api.ApiSpace in project serverless by bluenimble.
the class KeyStoreAwareApiSpi method findConsumer.
@Override
public void findConsumer(Api api, ApiService service, ApiRequest request, ApiConsumer consumer) throws ApiAuthenticationException {
String accessKey = (String) consumer.get(ApiConsumer.Fields.AccessKey);
if ("container".equals(request.getChannel())) {
consumer.override((ApiConsumer) request.get(ApiRequest.Consumer));
return;
}
if (!MgmUtils.isSecure(service)) {
if (root.accessKey().equals(accessKey)) {
consumer.set(ApiConsumer.Fields.SecretKey, root.secretKey());
consumer.set(ApiConsumer.Fields.ExpiryDate, root.expiryDate());
consumer.set(CommonSpec.Role, Role.SUPER.name());
}
return;
}
if (!consumer.type().equals(Type.Signature)) {
throw new ApiAuthenticationException("unsupported authentication scheme");
}
JsonArray roles = Json.getArray(service.getSecurity(), ApiService.Spec.Security.Roles);
if (root.accessKey().equals(accessKey)) {
if (roles == null || roles.isEmpty() || !roles.contains(Role.SUPER.name().toLowerCase())) {
throw new ApiAuthenticationException("insuffisant permissions");
}
consumer.set(ApiConsumer.Fields.SecretKey, root.secretKey());
consumer.set(ApiConsumer.Fields.ExpiryDate, root.expiryDate());
consumer.set(CommonSpec.Role, Role.SUPER.name());
} else {
int indexOfDot = accessKey.indexOf(Lang.DOT);
if (indexOfDot <= 0) {
throw new ApiAuthenticationException("invalid accessKey");
}
String consumerSpaceNs = accessKey.substring(0, indexOfDot);
accessKey = accessKey.substring(indexOfDot + 1);
ApiSpace consumerSpace;
try {
consumerSpace = api.space().space(consumerSpaceNs);
} catch (ApiAccessDeniedException e) {
throw new ApiAuthenticationException("instance manager can't access requested space");
}
KeyPair skp;
try {
skp = consumerSpace.keystore().get(accessKey, true);
} catch (SpaceKeyStoreException e) {
throw new ApiAuthenticationException("instance manager can't access space keystore");
}
if (skp == null) {
throw new ApiAuthenticationException("accessKey " + accessKey + " not found");
}
String role = (String) skp.property(CommonSpec.Role);
if (Lang.isNullOrEmpty(role)) {
throw new ApiAuthenticationException("no role defined for consumer");
}
if (roles != null && !roles.isEmpty() && !roles.contains(role.toLowerCase())) {
throw new ApiAuthenticationException("insuffisant permissions");
}
consumer.set(ApiConsumer.Fields.Space, consumerSpaceNs);
consumer.set(ApiConsumer.Fields.SecretKey, skp.secretKey());
consumer.set(ApiConsumer.Fields.ExpiryDate, skp.expiryDate());
Iterator<String> props = skp.properties();
if (props != null) {
while (props.hasNext()) {
String p = props.next();
consumer.set(p, skp.property(p));
}
}
}
}
Aggregations