use of com.bluenimble.platform.api.ApiSpace in project serverless by bluenimble.
the class ScriptingPlugin method init.
@Override
public void init(final ApiServer server) throws Exception {
if (Lang.isNullOrEmpty(vmArgs)) {
masterEngine = Manager.getScriptEngine(ScriptingPlugin.class.getClassLoader());
} else {
masterEngine = Manager.getScriptEngine(Lang.split(vmArgs, Lang.SPACE, true), ScriptingPlugin.class.getClassLoader());
}
Feature aFeature = ScriptingEngine.class.getAnnotation(Feature.class);
if (aFeature == null || Lang.isNullOrEmpty(aFeature.name())) {
return;
}
PackageClassLoader pcl = (PackageClassLoader) ScriptingPlugin.class.getClassLoader();
pcl.registerObject(Registered.ApiSpi, new ScriptableApiSpi());
pcl.registerObject(Registered.ServiceSpi, new ScriptableApiServiceSpi());
File platform = new File(home, "platform");
// load platform
Reader pReader = null;
try {
pReader = new FileReader(new File(platform, "Platform.js"));
Bindings bindings = new SimpleBindings();
bindings.put(Vars.Core, new File(platform, Vars.Core).getAbsolutePath());
bindings.put(Vars.Tools, new File(platform, Vars.Tools).getAbsolutePath());
shared = new DefaultScriptingEngine(this, (ScriptObjectMirror) masterEngine.eval(pReader, bindings), server.getMapProvider());
} finally {
IOUtils.closeQuietly(pReader);
}
// add features
server.addFeature(new ServerFeature() {
private static final long serialVersionUID = 2626039344401539390L;
@Override
public Class<?> type() {
return ScriptingEngine.class;
}
@Override
public Object get(ApiSpace space, String name) {
return shared;
}
@Override
public String provider() {
return ScriptingPlugin.this.getName();
}
@Override
public Plugin implementor() {
return ScriptingPlugin.this;
}
});
}
use of com.bluenimble.platform.api.ApiSpace in project serverless by bluenimble.
the class RemotePlugin method init.
@Override
public void init(final ApiServer server) throws Exception {
Feature aFeature = Remote.class.getAnnotation(Feature.class);
if (aFeature == null || Lang.isNullOrEmpty(aFeature.name())) {
return;
}
feature = aFeature.name();
PackageClassLoader pcl = (PackageClassLoader) RemotePlugin.class.getClassLoader();
pcl.registerObject(Protocol.http.name(), new HttpRemote(null));
server.addFeature(new ServerFeature() {
private static final long serialVersionUID = -9012279234275100528L;
@Override
public Class<?> type() {
return Remote.class;
}
@Override
public Object get(ApiSpace space, String name) {
return remotes.get(createRemoteKey(name, space));
}
@Override
public String provider() {
return RemotePlugin.this.getName();
}
@Override
public Plugin implementor() {
return RemotePlugin.this;
}
});
}
use of com.bluenimble.platform.api.ApiSpace 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.ApiSpace 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.ApiSpace 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);
}
}
Aggregations