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));
}
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);
}
}
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);
}
}
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);
}
}
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);
}
Aggregations