Search in sources :

Example 1 with ApiSpace

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;
        }
    });
}
Also used : ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) DefaultScriptingEngine(com.bluenimble.platform.api.scripting.impls.DefaultScriptingEngine) Reader(java.io.Reader) FileReader(java.io.FileReader) Feature(com.bluenimble.platform.Feature) ServerFeature(com.bluenimble.platform.server.ServerFeature) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) ScriptableApiServiceSpi(com.bluenimble.platform.api.impls.scripting.ScriptableApiServiceSpi) ServerFeature(com.bluenimble.platform.server.ServerFeature) ApiSpace(com.bluenimble.platform.api.ApiSpace) SimpleBindings(javax.script.SimpleBindings) ScriptableApiSpi(com.bluenimble.platform.api.impls.scripting.ScriptableApiSpi) FileReader(java.io.FileReader) File(java.io.File) PackageClassLoader(com.bluenimble.platform.PackageClassLoader) Plugin(com.bluenimble.platform.plugins.Plugin) AbstractPlugin(com.bluenimble.platform.plugins.impls.AbstractPlugin)

Example 2 with ApiSpace

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;
        }
    });
}
Also used : ServerFeature(com.bluenimble.platform.server.ServerFeature) HttpRemote(com.bluenimble.platform.remote.impls.http.HttpRemote) ApiSpace(com.bluenimble.platform.api.ApiSpace) JsonObject(com.bluenimble.platform.json.JsonObject) Feature(com.bluenimble.platform.Feature) ServerFeature(com.bluenimble.platform.server.ServerFeature) PackageClassLoader(com.bluenimble.platform.PackageClassLoader) Plugin(com.bluenimble.platform.plugins.Plugin) AbstractPlugin(com.bluenimble.platform.plugins.impls.AbstractPlugin)

Example 3 with ApiSpace

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));
}
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 4 with ApiSpace

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);
    }
}
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 5 with ApiSpace

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

Aggregations

ApiSpace (com.bluenimble.platform.api.ApiSpace)48 JsonObject (com.bluenimble.platform.json.JsonObject)33 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)31 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)27 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)27 Feature (com.bluenimble.platform.Feature)10 Plugin (com.bluenimble.platform.plugins.Plugin)10 ServerFeature (com.bluenimble.platform.server.ServerFeature)10 AbstractPlugin (com.bluenimble.platform.plugins.impls.AbstractPlugin)9 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 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)5 KeyPair (com.bluenimble.platform.security.KeyPair)5 Api (com.bluenimble.platform.api.Api)4 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)4 JsonArray (com.bluenimble.platform.json.JsonArray)4 PackageClassLoader (com.bluenimble.platform.PackageClassLoader)3