From 997cc12abd9d7ada97331ff0c489dd862668f478 Mon Sep 17 00:00:00 2001 From: Amaury Chamayou Date: Tue, 21 May 2024 20:19:22 +0000 Subject: [PATCH] Move RequestExtension from js_generic to public includes --- CMakeLists.txt | 5 ++- .../js/extensions/ccf}/request_extension.h | 2 +- .../ccf/js}/named_auth_policies.h | 0 .../apps/basic/custom_endpoints/registry.h | 32 +++++++++++++++++++ src/apps/js_generic/js_generic_base.cpp | 6 ++-- .../extensions/ccf}/request_extension.cpp | 6 ++-- 6 files changed, 41 insertions(+), 10 deletions(-) rename {src/apps/js_generic => include/ccf/js/extensions/ccf}/request_extension.h (96%) rename {src/apps/js_generic => include/ccf/js}/named_auth_policies.h (100%) rename src/{apps/js_generic => js/extensions/ccf}/request_extension.cpp (98%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 94a3e5d5416e..3fa82f7b5fc7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -350,6 +350,7 @@ set(CCF_JS_SOURCES ${CCF_DIR}/src/js/extensions/ccf/network.cpp ${CCF_DIR}/src/js/extensions/ccf/node.cpp ${CCF_DIR}/src/js/extensions/ccf/rpc.cpp + ${CCF_DIR}/src/js/extensions/ccf/request_extension.cpp ) if(COMPILE_TARGET STREQUAL "sgx") @@ -590,9 +591,7 @@ elseif(COMPILE_TARGET STREQUAL "virtual") set(JS_SNP_ATTESTATION_VIRTUAL js_snp_attestation.virtual) endif() -set(JS_GENERIC_SOURCES ${CCF_DIR}/src/apps/js_generic/js_generic_base.cpp - ${CCF_DIR}/src/apps/js_generic/request_extension.cpp -) +set(JS_GENERIC_SOURCES ${CCF_DIR}/src/apps/js_generic/js_generic_base.cpp) if(COMPILE_TARGET STREQUAL "sgx") add_enclave_library(js_generic_base.enclave ${JS_GENERIC_SOURCES}) target_link_libraries(js_generic_base.enclave PUBLIC ccf.enclave) diff --git a/src/apps/js_generic/request_extension.h b/include/ccf/js/extensions/ccf/request_extension.h similarity index 96% rename from src/apps/js_generic/request_extension.h rename to include/ccf/js/extensions/ccf/request_extension.h index ffb9bc8baca5..62e33d894dc1 100644 --- a/src/apps/js_generic/request_extension.h +++ b/include/ccf/js/extensions/ccf/request_extension.h @@ -8,7 +8,7 @@ #include "ccf/js/extensions/extension_interface.h" #include "ccf/rpc_context.h" -namespace ccfapp +namespace ccf::js::extensions { /** **/ diff --git a/src/apps/js_generic/named_auth_policies.h b/include/ccf/js/named_auth_policies.h similarity index 100% rename from src/apps/js_generic/named_auth_policies.h rename to include/ccf/js/named_auth_policies.h diff --git a/samples/apps/basic/custom_endpoints/registry.h b/samples/apps/basic/custom_endpoints/registry.h index 19045988d6a6..aa08335603b2 100644 --- a/samples/apps/basic/custom_endpoints/registry.h +++ b/samples/apps/basic/custom_endpoints/registry.h @@ -26,6 +26,7 @@ #include "ccf/js/extensions/ccf/kv.h" #include "ccf/js/extensions/ccf/rpc.h" #include "ccf/js/extensions/console.h" +#include "ccf/js/extensions/ccf/request_extension.h" #include "ccf/js/extensions/math/random.h" #include "ccf/service/tables/modules.h" #include "endpoint.h" @@ -243,6 +244,37 @@ namespace basicapp // Make the heap and stack limits safe while we init the runtime ctx.runtime().reset_runtime_options(); + // TBD: module loader + // JS_SetModuleLoaderFunc( + // ctx.runtime(), nullptr, js::js_app_module_loader, &endpoint_ctx.tx); + + // Extensions with a dependency on this endpoint context (invocation), + // which must be removed after execution. + ccf::js::extensions::Extensions local_extensions; + + // ccf.kv.* + local_extensions.emplace_back( + std::make_shared(&endpoint_ctx.tx)); + + // ccf.rpc.* + local_extensions.emplace_back( + std::make_shared( + endpoint_ctx.rpc_ctx.get())); + + auto request_extension = + std::make_shared(endpoint_ctx.rpc_ctx.get()); + local_extensions.push_back(request_extension); + + for (auto extension : local_extensions) + { + ctx.add_extension(extension); + } + + if (pre_exec_hook.has_value()) + { + pre_exec_hook.value()(ctx); + } + // TBD: Run fetched endpoint CCF_APP_INFO("CUSTOM ENDPOINT: {}", endpoint->dispatch.uri_path); } diff --git a/src/apps/js_generic/js_generic_base.cpp b/src/apps/js_generic/js_generic_base.cpp index 414beebfa3dd..5dfcfd9fdfa9 100644 --- a/src/apps/js_generic/js_generic_base.cpp +++ b/src/apps/js_generic/js_generic_base.cpp @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the Apache 2.0 License. -#include "apps/js_generic/named_auth_policies.h" -#include "apps/js_generic/request_extension.h" +#include "ccf/js/named_auth_policies.h" +#include "ccf/js/extensions/ccf/request_extension.h" #include "ccf/app_interface.h" #include "ccf/crypto/key_wrap.h" #include "ccf/crypto/rsa_key_pair.h" @@ -162,7 +162,7 @@ namespace ccfapp endpoint_ctx.rpc_ctx.get())); auto request_extension = - std::make_shared(endpoint_ctx.rpc_ctx.get()); + std::make_shared(endpoint_ctx.rpc_ctx.get()); local_extensions.push_back(request_extension); for (auto extension : local_extensions) diff --git a/src/apps/js_generic/request_extension.cpp b/src/js/extensions/ccf/request_extension.cpp similarity index 98% rename from src/apps/js_generic/request_extension.cpp rename to src/js/extensions/ccf/request_extension.cpp index 5f109e8f18af..8dddb3a42349 100644 --- a/src/apps/js_generic/request_extension.cpp +++ b/src/js/extensions/ccf/request_extension.cpp @@ -1,9 +1,9 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the Apache 2.0 License. -#include "apps/js_generic/request_extension.h" +#include "ccf/js/extensions/ccf/request_extension.h" -#include "apps/js_generic/named_auth_policies.h" +#include "ccf/js/named_auth_policies.h" #include "ccf/endpoints/authentication/all_of_auth.h" #include "ccf/endpoints/authentication/cert_auth.h" #include "ccf/endpoints/authentication/cose_auth.h" @@ -13,7 +13,7 @@ #include -namespace ccfapp +namespace ccf::js::extensions { namespace {