diff --git a/build.gradle b/build.gradle index 5a87a0f..9a41398 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { } group = "org.bdware.sc" -version = "1.8.5" +version = "1.8.7" tasks.withType(JavaCompile) { // options.compilerArgs << '-Xlint:none' // options.compilerArgs << '-Xlint:deprecation' << "-Werror" @@ -48,8 +48,8 @@ dependencies { implementation 'com.sun.mail:javax.mail:1.6.2' implementation 'com.squareup.okhttp3:okhttp:4.9.1' implementation 'org.bdware.bdcontract:sdk-java:1.0.2' - implementation 'org.bdware.doip:doip-audit-tool:1.2.4' - implementation 'org.bdware.doip:doip-sdk:1.4.0' + implementation 'org.bdware.doip:doip-audit-tool:1.2.6' + implementation 'org.bdware.doip:doip-sdk:1.4.2' implementation fileTree(dir: 'lib', include: '*.jar') testImplementation 'junit:junit:4.13.2' } @@ -68,7 +68,7 @@ jar { // while develop at local use "false" configurations.runtimeClasspath.filter { it.getAbsolutePath().contains("/lib/") - //false + // false }.collect { it.isDirectory() ? it : zipTree(it) } diff --git a/src/main/debugger/org/bdware/sc/debugger/DebugMain.java b/src/main/debugger/org/bdware/sc/debugger/DebugMain.java index 4a22409..67fb513 100644 --- a/src/main/debugger/org/bdware/sc/debugger/DebugMain.java +++ b/src/main/debugger/org/bdware/sc/debugger/DebugMain.java @@ -63,7 +63,7 @@ public class DebugMain { = new ResultCallback() { @Override public void onResult(String str) { - if(str.contains("Error")) { + if (str.contains("Error")) { LOGGER.error("Some error happens: " + str); } LOGGER.info("[PrintCB] " + str); @@ -103,7 +103,7 @@ public class DebugMain { config.cPort = jsonObject.get("cPort").getAsInt(); config.port = jsonObject.get("port").getAsInt(); JsonObject ownerAndScript = new JsonObject(); - String arg = "abc&owner=" + config.publicKey + "&script=" + config.ypkPath; + String arg = "abc&owner=" + config.publicKey + "&script=" + config.ypkPath + "&doipStartPort=" + config.doipStartPort; url = String.format(urlFormat, config.agentAddress, "allocateKeyPair", arg); resp = HttpUtil.httpGet(url); LOGGER.info(url); @@ -133,6 +133,7 @@ public class DebugMain { String killBeforeStart; String ypkPath; String cpHost; + int doipStartPort; //AutoAppend int port; String cmi; diff --git a/src/main/java/org/bdware/sc/ContractProcess.java b/src/main/java/org/bdware/sc/ContractProcess.java index 650f2f5..03eae2f 100644 --- a/src/main/java/org/bdware/sc/ContractProcess.java +++ b/src/main/java/org/bdware/sc/ContractProcess.java @@ -728,7 +728,7 @@ public class ContractProcess { startPort = arg.getAsJsonObject().get("doipStartPort").getAsInt(); } LOGGER.info("Fetch the onStartingDoipServerRes from router successfully, the result is " + onStartingDoipServerRes); - int doipListenPort = DoipClusterServer.startDoipServer(startPort); + int doipListenPort = DoipClusterServer.startDoipServer(startPort,onStartingDoipServerRes); returnValue.addProperty("doipListenPort", doipListenPort); this.contract.setDoipPort(doipListenPort); returnValue.addProperty("doipStartPort", startPort); diff --git a/src/main/java/org/bdware/sc/boundry/utils/DigestUtil.java b/src/main/java/org/bdware/sc/boundry/utils/DigestUtil.java new file mode 100644 index 0000000..fcbeba0 --- /dev/null +++ b/src/main/java/org/bdware/sc/boundry/utils/DigestUtil.java @@ -0,0 +1,15 @@ +package org.bdware.sc.boundry.utils; + +import org.bdware.sc.compiler.PermissionStub; +import org.bdware.sc.node.Permission; + +@PermissionStub(permission = Permission.Digest) +public class DigestUtil { + public static String md5Hex(String arg) { + return org.apache.commons.codec.digest.DigestUtils.md5Hex(arg); + } + + public static String sha256Hex(String arg) { + return org.apache.commons.codec.digest.DigestUtils.sha256Hex(arg); + } +} diff --git a/src/main/java/org/bdware/sc/engine/JSONTool.java b/src/main/java/org/bdware/sc/engine/JSONTool.java index 5c193d9..ce0b4b7 100644 --- a/src/main/java/org/bdware/sc/engine/JSONTool.java +++ b/src/main/java/org/bdware/sc/engine/JSONTool.java @@ -1,8 +1,6 @@ package org.bdware.sc.engine; import com.google.gson.*; -import org.bdware.doip.codec.doipMessage.DoipMessage; -import org.bdware.sc.util.JsonUtil; import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror; import wrp.jdk.nashorn.internal.objects.NativeArray; import wrp.jdk.nashorn.internal.runtime.PropertyMap; @@ -44,7 +42,7 @@ public class JSONTool { } public static Object convertJsonElementToMirror(Object input) { - if(input instanceof JsonElement) { + if (input instanceof JsonElement) { return convertJsonElementToMirror((JsonElement) input); } else { return input; @@ -149,9 +147,7 @@ public class JSONTool { return jsonObject; } else if (obj.getClass() == jdk.internal.dynalink.beans.StaticClass.class) { return JsonNull.INSTANCE; - } else if (obj.getClass() == DoipMessage.class) { - return JsonUtil.fromJson(JsonUtil.toJson(obj), JsonObject.class); - }else if (obj instanceof Number) { + } else if (obj instanceof Number) { return new JsonPrimitive((Number) obj); } else if (obj instanceof String) { return new JsonPrimitive((String) obj); diff --git a/src/main/java/org/bdware/sc/engine/hook/AccessHandler.java b/src/main/java/org/bdware/sc/engine/hook/AccessHandler.java index 599cb08..0f4c203 100644 --- a/src/main/java/org/bdware/sc/engine/hook/AccessHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/AccessHandler.java @@ -57,7 +57,6 @@ public class AccessHandler implements AnnotationHook { LOGGER.info("verify success!" + argPacks.request.getRequester()); if (acFunction == null) return argPacks; DesktopEngine de = (DesktopEngine) desktopEngine; - try { ContractRequest input = argPacks.request; JO jo = new JO(PropertyMap.newMap()); diff --git a/src/main/java/org/bdware/sc/engine/hook/DOOPAfterExecHandler.java b/src/main/java/org/bdware/sc/engine/hook/DOOPAfterExecHandler.java index 4dea627..3e06b87 100644 --- a/src/main/java/org/bdware/sc/engine/hook/DOOPAfterExecHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/DOOPAfterExecHandler.java @@ -34,15 +34,20 @@ public class DOOPAfterExecHandler implements AnnotationHook { originDoipMsg = doipMessagePacker.rawDoipMsg; // if http, directly return if (doipMessagePacker.source.equals("http")) { + if (argPacks.ret != null && argPacks.ret instanceof DoipMessage) { + argPacks.ret = JsonUtil.parseObject(JsonDoipMessage.fromDoipMessage((DoipMessage) argPacks.ret)); + } return argPacks; } else { // pack - JsonObject jsonObjectRes = JSONTool.convertMirrorToJson(argPacks.ret).getAsJsonObject(); - // validate json response - ArgSchemaVisitor visitor = new ArgSchemaVisitor(jsonObjectRes); - validateJsonElementRulesByArgSchemaVisitor(jsonResponseRules, visitor); - JsonDoipMessage returnedMessage = JsonUtil.fromJson(jsonObjectRes, JsonDoipMessage.class); - argPacks.ret = returnedMessage.toResponseDoipMessage(originDoipMsg); + if (!(argPacks.ret instanceof DoipMessage)) { + JsonObject jsonObjectRes = ((JsonElement)argPacks.ret).getAsJsonObject(); + // validate json response + ArgSchemaVisitor visitor = new ArgSchemaVisitor(jsonObjectRes); + validateJsonElementRulesByArgSchemaVisitor(jsonResponseRules, visitor); + JsonDoipMessage returnedMessage = JsonUtil.fromJson(jsonObjectRes, JsonDoipMessage.class); + argPacks.ret = returnedMessage.toResponseDoipMessage(originDoipMsg); + } return argPacks; } } else { diff --git a/src/main/java/org/bdware/sc/engine/hook/DOOPBeforeExecHandler.java b/src/main/java/org/bdware/sc/engine/hook/DOOPBeforeExecHandler.java index 4798049..db31ea5 100644 --- a/src/main/java/org/bdware/sc/engine/hook/DOOPBeforeExecHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/DOOPBeforeExecHandler.java @@ -3,8 +3,9 @@ package org.bdware.sc.engine.hook; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import org.bdware.doip.codec.JsonDoipMessage; import org.bdware.doip.codec.doipMessage.DoipMessage; -import org.bdware.doip.codec.doipMessage.DoipMessageFactory; +import org.bdware.doip.codec.doipMessage.MessageCredential; import org.bdware.doip.codec.operations.BasicOperations; import org.bdware.sc.JSEngine; import org.bdware.sc.bean.ContractRequest; @@ -12,10 +13,7 @@ import org.bdware.sc.boundry.ScriptReturnException; import org.bdware.sc.entity.DoipMessagePacker; import org.bdware.sc.node.AnnotationHook; import org.bdware.sc.node.ArgPacks; - -import java.nio.charset.StandardCharsets; -import java.util.Map; -import java.util.Set; +import org.bdware.sc.util.JsonUtil; public class DOOPBeforeExecHandler implements AnnotationHook { @@ -31,17 +29,20 @@ public class DOOPBeforeExecHandler implements AnnotationHook { public ArgPacks handle(JSEngine desktopEngine, ArgPacks argPacks) { Object arg = argPacks.arg; DoipMessagePacker doipMsgPackerArg = new DoipMessagePacker(); - if (arg instanceof DoipMessagePacker) { doipMsgPackerArg = (DoipMessagePacker) arg; } else { // validate http request's params ContractRequest httpReq = argPacks.request; validateHTTPRequestArgs(httpReq); - // set doipMsgPackerArg struct's params doipMsgPackerArg.setSource("http"); doipMsgPackerArg.rawDoipMsg = convertHttpRequestToDoipMessage(httpReq); + if (httpReq.verifySignature()) { + doipMsgPackerArg.rawDoipMsg.credential = new MessageCredential(httpReq.getRequester(), new byte[0]); + } else { + doipMsgPackerArg.rawDoipMsg.credential = null; + } } argPacks.arg = doipMsgPackerArg; @@ -82,71 +83,8 @@ public class DOOPBeforeExecHandler implements AnnotationHook { public DoipMessage convertHttpRequestToDoipMessage(ContractRequest httpReq) { JsonObject jsonParams = JsonParser.parseString(httpReq.getArg().getAsString()).getAsJsonObject(); - // taking Extension into consideration - JsonObject header = jsonParams.get("header") != null ? jsonParams.get("header").getAsJsonObject() : null; - String body = jsonParams.get("body") != null ? jsonParams.get("body").getAsString() : null; - DoipMessage doipMessage = null; - switch (httpOperation) { - case Hello: - case Delete: - case ListOps: - doipMessage = new DoipMessageFactory.DoipMessageBuilder().createRequest(header.get("identifier").getAsString(), httpOperation.getName()).create(); - break; - case Create: - case Update: - doipMessage = new DoipMessageFactory.DoipMessageBuilder() - .createRequest(header.get("identifier").getAsString(), httpOperation.getName()) - .setBody(body.getBytes(StandardCharsets.UTF_8)) - .create(); - break; - case Search: - DoipMessageFactory.DoipMessageBuilder searchBuilder = new DoipMessageFactory.DoipMessageBuilder() - .createRequest(header.get("identifier").getAsString(), httpOperation.getName()); - JsonElement query = header.get("query"); - if (query != null) searchBuilder.addAttributes("query", query.getAsString()); - JsonElement pageNum = header.get("pageNum"); - if (pageNum != null) searchBuilder.addAttributes("pageNum", pageNum.getAsInt()); - JsonElement pageSize = header.get("pageSize"); - if (pageSize != null) searchBuilder.addAttributes("pageSize", pageSize.getAsInt()); - JsonElement type = header.get("type"); - if (type != null) searchBuilder.addAttributes("type", type.getAsString()); - - doipMessage = searchBuilder.create(); - break; - case Retrieve: - doipMessage = new DoipMessageFactory.DoipMessageBuilder().createRequest(header.get("identifier").getAsString(), httpOperation.getName()).create(); - JsonElement element = header.get("element"); - JsonElement includeElementData = header.get("includeElementData"); - if (element != null) doipMessage.header.parameters.addAttribute("element", element.getAsString()); - if (includeElementData != null && includeElementData.getAsBoolean()) - doipMessage.header.parameters.addAttribute("includeElementData", "true"); - break; - case Extension: - DoipMessageFactory.DoipMessageBuilder extensionBuilder = new DoipMessageFactory.DoipMessageBuilder(); - if (header != null) { - if (header.get("identifier") != null) { - extensionBuilder = extensionBuilder - .createRequest(header.get("identifier").getAsString(), httpOperation.getName()); - } - - Set> entries = header.entrySet(); - for (Map.Entry entry : entries) { - extensionBuilder.addAttributes(entry.getKey(), entry.getValue()); - } - } - - if (body != null) { - extensionBuilder.setBody(body.getBytes(StandardCharsets.UTF_8)); - } - - doipMessage = extensionBuilder.create(); - break; - case Unknown: - default: - break; - } - - return doipMessage; + JsonDoipMessage doipMessage = JsonUtil.fromJson(jsonParams, JsonDoipMessage.class); + return doipMessage.toRequestDoipMessage(); } diff --git a/src/main/java/org/bdware/sc/engine/hook/ObjToJsonHandler.java b/src/main/java/org/bdware/sc/engine/hook/ObjToJsonHandler.java index 3e1a04d..33f02f0 100644 --- a/src/main/java/org/bdware/sc/engine/hook/ObjToJsonHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/ObjToJsonHandler.java @@ -1,5 +1,6 @@ package org.bdware.sc.engine.hook; +import org.bdware.doip.codec.doipMessage.DoipMessage; import org.bdware.sc.JSEngine; import org.bdware.sc.engine.JSONTool; import org.bdware.sc.node.AnnotationHook; @@ -8,6 +9,8 @@ import org.bdware.sc.node.ArgPacks; public class ObjToJsonHandler implements AnnotationHook { @Override public ArgPacks handle(JSEngine desktopEngine, ArgPacks argPacks) { + if (argPacks.ret instanceof DoipMessage) + return argPacks; argPacks.ret = JSONTool.convertMirrorToJson(argPacks.ret); return argPacks; } diff --git a/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java b/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java index 6959131..cc1e5ea 100644 --- a/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java +++ b/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java @@ -11,7 +11,6 @@ import org.bdware.doip.codec.doipMessage.HeaderParameter; import org.bdware.doip.codec.operations.BasicOperations; import org.bdware.doip.endpoint.server.DoipRequestHandler; import org.bdware.doip.endpoint.server.NettyServerHandler; -import org.bdware.doip.endpoint.server.RepositoryHandler; import org.bdware.sc.ContractProcess; import org.bdware.sc.bean.ContractRequest; import org.bdware.sc.boundry.JavaScriptEntry; @@ -24,7 +23,7 @@ import java.io.PrintStream; import java.util.HashMap; import java.util.Map; -public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler { +public class DOOPRequestHandler implements DoipRequestHandler { static Logger logger = LogManager.getLogger(NettyServerHandler.class); public Map doipFunctionNodeMap; @@ -71,81 +70,6 @@ public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler return null; } - @Override - public DoipMessage handleHello(DoipMessage request) { - FunctionNode fn = doipFunctionNodeMap.get(BasicOperations.Hello.getName()); - if (fn == null) fn = doipFunctionNodeMap.get(BasicOperations.Unknown.getName()); - if (fn != null) { - return buildRequestAndInvokeEngine(fn, request); - } else { - logger.error("DoipOperation Hello is not provided"); - } - return null; - } - - @Override - public DoipMessage handleListOps(DoipMessage request) { - FunctionNode fn = doipFunctionNodeMap.get(BasicOperations.ListOps.getName()); - if (fn == null) fn = doipFunctionNodeMap.get(BasicOperations.Unknown.getName()); - if (fn != null) { - return buildRequestAndInvokeEngine(fn, request); - } else { - logger.error("DoipOperation ListOps is not provided"); - } - - return null; - } - - @Override - public DoipMessage handleCreate(DoipMessage request) { - FunctionNode fn = doipFunctionNodeMap.get(BasicOperations.Create.getName()); - if (fn == null) fn = doipFunctionNodeMap.get(BasicOperations.Unknown.getName()); - if (fn != null) { - return buildRequestAndInvokeEngine(fn, request); - } else { - logger.error("DoipOperation Create is not provided"); - } - return null; - } - - @Override - public DoipMessage handleUpdate(DoipMessage request) { - FunctionNode fn = doipFunctionNodeMap.get(BasicOperations.Update.getName()); - if (fn == null) fn = doipFunctionNodeMap.get(BasicOperations.Unknown.getName()); - if (fn != null) { - return buildRequestAndInvokeEngine(fn, request); - } else { - logger.error("DoipOperation Update is not provided"); - } - - return null; - } - - @Override - public DoipMessage handleDelete(DoipMessage request) { - FunctionNode fn = doipFunctionNodeMap.get(BasicOperations.Delete.getName()); - if (fn == null) fn = doipFunctionNodeMap.get(BasicOperations.Unknown.getName()); - if (fn != null) { - return buildRequestAndInvokeEngine(fn, request); - } else { - logger.error("DoipOperation Delete is not provided"); - } - return null; - } - - @Override - public DoipMessage handleRetrieve(DoipMessage request) { - FunctionNode fn = doipFunctionNodeMap.get(BasicOperations.Retrieve.getName()); - if (fn == null) fn = doipFunctionNodeMap.get(BasicOperations.Unknown.getName()); - if (fn != null) { - return buildRequestAndInvokeEngine(fn, request); - } else { - logger.error("DoipOperation Retrieve is not provided"); - } - - return null; - } - public DoipMessage buildRequestAndInvokeEngine(FunctionNode fn, DoipMessage msg) { ContractRequest contractRequest = constructContractRequest(fn, msg); DoipMessagePacker arg = new DoipMessagePacker("doip", msg); diff --git a/src/main/java/org/bdware/sc/server/DoipClusterServer.java b/src/main/java/org/bdware/sc/server/DoipClusterServer.java index df7fcc6..dad39d2 100644 --- a/src/main/java/org/bdware/sc/server/DoipClusterServer.java +++ b/src/main/java/org/bdware/sc/server/DoipClusterServer.java @@ -42,9 +42,9 @@ public class DoipClusterServer extends DoipServerImpl { return instance; } - public static int startDoipServer(int startPort) throws InterruptedException { + public static int startDoipServer(int startPort, JsonElement otherConfigs) throws InterruptedException { try { - int ret = DoipLocalSingleton.run(startPort); + int ret = DoipLocalSingleton.run(startPort, otherConfigs); return ret; } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/org/bdware/sc/server/DoipLocalSingleton.java b/src/main/java/org/bdware/sc/server/DoipLocalSingleton.java index 54c2845..1e7ea1a 100644 --- a/src/main/java/org/bdware/sc/server/DoipLocalSingleton.java +++ b/src/main/java/org/bdware/sc/server/DoipLocalSingleton.java @@ -1,7 +1,12 @@ package org.bdware.sc.server; +import com.google.gson.JsonElement; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.bdware.doip.audit.EndpointConfig; +import org.bdware.doip.audit.EndpointInfo; +import org.bdware.doip.audit.client.AuditIrpClient; +import org.bdware.doip.audit.config.TempConfigStorage; import org.bdware.doip.endpoint.server.DoipListenerConfig; import org.bdware.doip.endpoint.server.DoipServerImpl; import org.bdware.doip.endpoint.server.DoipServiceInfo; @@ -12,6 +17,7 @@ import org.bdware.sc.handler.DOOPRequestHandler; import java.net.URI; import java.util.ArrayList; import java.util.List; +import java.util.UUID; public class DoipLocalSingleton { static Logger LOGGER = LogManager.getLogger(DoipLocalSingleton.class); @@ -24,7 +30,7 @@ public class DoipLocalSingleton { @Override public void run() { try { - DoipLocalSingleton.run(port); + DoipLocalSingleton.run(port, null); } catch (InterruptedException e) { e.printStackTrace(); } @@ -33,17 +39,17 @@ public class DoipLocalSingleton { doipServerThread.start(); } - public static int run(int port) throws InterruptedException { + public static int run(int port, JsonElement otherConfigs) throws InterruptedException { int i = -1; LOGGER.info("try to listener port:" + port); - for (i = run("tcp://127.0.0.1:" + port++); i < 0; ) { + for (i = run("tcp://127.0.0.1:" + port++, otherConfigs); i < 0; ) { LOGGER.info("try again to listener port:" + port); - i = run("tcp://127.0.0.1:" + port++); + i = run("tcp://127.0.0.1:" + port++, otherConfigs); } return i; } - public static int run(String doipAddr) throws InterruptedException { + public static int run(String doipAddr, JsonElement otherConfigs) throws InterruptedException { List infos = new ArrayList<>(); int port = -1; try { @@ -53,10 +59,28 @@ public class DoipLocalSingleton { } catch (Exception e) { e.printStackTrace(); } - DoipServiceInfo info = new DoipServiceInfo("aibd.govdata.tj/do.3f9c41e6-9f8e-48a0-9220-53f438d40e43", "ownerDEF", "gateRepo", infos); + String repoID = "bdtest/BDRepo/" + UUID.randomUUID().toString(); + String owner = ContractProcess.instance.getContract().getOwner(); + String repoType = "Repository"; + try { + if (otherConfigs != null && otherConfigs.isJsonObject()) { + EndpointConfig config = new TempConfigStorage(otherConfigs.toString()).loadAsEndpointConfig(); + if (config.routerURI != null) { + AuditIrpClient irpClient = new AuditIrpClient(config); + EndpointInfo endpointInfo = irpClient.getEndpointInfo(); + repoID = endpointInfo.getDoId(); + owner = endpointInfo.getPubKey(); + infos.clear(); + infos.add(new DoipListenerConfig(endpointInfo.getURI(), "2.1")); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + DoipServiceInfo info = new DoipServiceInfo(repoID, owner, repoType, infos); server = new DoipServerImpl(info); DOOPRequestHandler handler = ContractProcess.instance.doopRequestHandler; - server.setRepositoryHandler(handler); + server.setRequestCallback(handler); ResultChecker checker = new ResultChecker(); server.start(checker); checker.waitForResult(1000);