From 15b50cbb86a359c0fc6927c842113476521c36bc Mon Sep 17 00:00:00 2001 From: cjen1-msft Date: Fri, 7 Mar 2025 13:40:15 +0000 Subject: [PATCH] Fix converters --- include/ccf/js/extensions/ccf/converters.h | 1 - samples/constitutions/default/actions.js | 10 +-- src/js/extensions/ccf/converters.cpp | 25 -------- tests/code_update.py | 74 +++++++++++----------- 4 files changed, 43 insertions(+), 67 deletions(-) diff --git a/include/ccf/js/extensions/ccf/converters.h b/include/ccf/js/extensions/ccf/converters.h index df58b3b4ac36..8af18051fe52 100644 --- a/include/ccf/js/extensions/ccf/converters.h +++ b/include/ccf/js/extensions/ccf/converters.h @@ -15,7 +15,6 @@ namespace ccf::js::extensions * - ccf.bufToJsonCompatible * * - ccf.pemToId - * - ccf.jsonToTcbVersion * * - ccf.enableUntrustedDateTime * - ccf.enableMetricsLogging diff --git a/samples/constitutions/default/actions.js b/samples/constitutions/default/actions.js index c1b862068a6e..b3c230945949 100644 --- a/samples/constitutions/default/actions.js +++ b/samples/constitutions/default/actions.js @@ -1101,7 +1101,7 @@ const actions = new Map([ new Action( function (args) { checkType(args.cpuid, "string", "cpuid"); - checkLength(hexStrToBuf(args.cpuid), 4, 4, "cpuid"); + checkLength(ccf.strToBuf(args.cpuid), 8, 8, "cpuid"); checkType(args.tcb_version, "object", "tcb_version"); checkType( @@ -1120,7 +1120,7 @@ const actions = new Map([ function (args, proposalId) { ccf.kv["public:ccf.gov.nodes.snp.tcb_versions"].set( ccf.strToBuf(args.cpuid), - ccf.jsonToSnpTcbVersion(args.tcb_version), + ccf.jsonCompatibleToBuf(args.tcb_version), ); invalidateOtherOpenProposals(proposalId); @@ -1187,12 +1187,14 @@ const actions = new Map([ new Action( function (args) { checkType(args.cpuid, "string", "cpuid"); - checkLength(hexStrToBuf(args.cpuid), 4, 4, "cpuid"); + checkLength(ccf.strToBuf(args.cpuid), 8, 8, "cpuid"); }, function (args) { - const cpuid = hexStrToBuf(args.cpuid); + const cpuid = ccf.strToBuf(args.cpuid); if ( ccf.kv["public:ccf.gov.nodes.snp.tcb_versions"].has(cpuid)) { ccf.kv["public:ccf.gov.nodes.snp.tcb_versions"].delete(cpuid); + } else { + throw new Error("CPUID not found"); } }, ), diff --git a/src/js/extensions/ccf/converters.cpp b/src/js/extensions/ccf/converters.cpp index 2ab92e114f8b..a9127a3e083a 100644 --- a/src/js/extensions/ccf/converters.cpp +++ b/src/js/extensions/ccf/converters.cpp @@ -193,28 +193,6 @@ namespace ccf::js::extensions ctx, "Failed to parse PEM: %s", exc.what()); } } - - JSValue js_json_to_tcb_version( - JSContext* ctx, JSValueConst, int argc, JSValueConst* argv) - { - if (argc != 1) - return JS_ThrowTypeError( - ctx, "Passed %d arguments, but expected 1", argc); - - js::core::Context& jsctx = *(js::core::Context*)JS_GetContextOpaque(ctx); - - auto str = jsctx.json_stringify(jsctx.wrap(argv[0])); - JS_CHECK_EXC(str); - - pal::snp::TcbVersion tcb_version = - nlohmann::json::parse(jsctx.to_str(str).value()); - - auto buf = jsctx.new_array_buffer_copy( - (uint8_t*)&tcb_version, sizeof(pal::snp::TcbVersion)); - JS_CHECK_EXC(buf); - - return buf.take(); - } } void ConvertersExtension::install(js::core::Context& ctx) @@ -242,8 +220,5 @@ namespace ccf::js::extensions ccf.set("pemToId", ctx.new_c_function(js_pem_to_id, "pemToId", 1)); - ccf.set( - "jsonToSnpTcbVersion", - ctx.new_c_function(js_json_to_tcb_version, "jsonToSnpTcbVersion", 1)); } } diff --git a/tests/code_update.py b/tests/code_update.py index 45e5d97fddb2..f79b545713ec 100644 --- a/tests/code_update.py +++ b/tests/code_update.py @@ -310,9 +310,9 @@ def test_tcb_version_tables(network, args): new_node = network.create_node("local://localhost") network.join_node(new_node, args.package, args, timeout=3) network.trust_node(new_node, args) - except Exception as e: + except TimeoutError as e: thrown_exception = e - assert thrown_exception is None, "New node should not have been able to join" + assert thrown_exception is not None, "New node should not have been able to join" LOG.info("Adding new cpuid's TCB version") network.consortium.add_snp_tcb_version(primary, cpuid, tcb_version) @@ -778,43 +778,43 @@ def run(args): ) as network: network.start_and_open(args) -# test_verify_quotes(network, args) -# -# # Measurements -# test_measurements_tables(network, args) -# if not snp.IS_SNP: -# test_add_node_with_untrusted_measurement(network, args) -# -# # Host data/security policy -# test_host_data_tables(network, args) -# test_add_node_with_untrusted_host_data(network, args) -# + test_verify_quotes(network, args) + + # Measurements + test_measurements_tables(network, args) + if not snp.IS_SNP: + test_add_node_with_untrusted_measurement(network, args) + + # Host data/security policy + test_host_data_tables(network, args) + test_add_node_with_untrusted_host_data(network, args) + if snp.IS_SNP: -# # Virtual has no security policy, _only_ host data (unassociated with anything) -# test_add_node_with_stubbed_security_policy(network, args) -# test_start_node_with_mismatched_host_data(network, args) -# test_add_node_without_security_policy(network, args) + # Virtual has no security policy, _only_ host data (unassociated with anything) + test_add_node_with_stubbed_security_policy(network, args) + test_start_node_with_mismatched_host_data(network, args) + test_add_node_without_security_policy(network, args) test_tcb_version_tables(network, args) -# -# # Endorsements -# test_endorsements_tables(network, args) -# test_add_node_with_no_uvm_endorsements(network, args) -# -# if not snp.IS_SNP: -# # NB: Assumes the current nodes are still using args.package, so must run before test_update_all_nodes -# test_proposal_invalidation(network, args) -# -# # This is in practice equivalent to either "unknown measurement" or "unknown host data", but is explicitly -# # testing that (without artifically removing/corrupting those values) a replacement package differs -# # in one of these values -# test_add_node_with_different_package(network, args) -# test_update_all_nodes(network, args) -# -# # Run again at the end to confirm current nodes are acceptable -# test_verify_quotes(network, args) -# -# if snp.IS_SNP: -# test_add_node_with_no_uvm_endorsements_in_kv(network, args) + + # Endorsements + test_endorsements_tables(network, args) + test_add_node_with_no_uvm_endorsements(network, args) + + if not snp.IS_SNP: + # NB: Assumes the current nodes are still using args.package, so must run before test_update_all_nodes + test_proposal_invalidation(network, args) + + # This is in practice equivalent to either "unknown measurement" or "unknown host data", but is explicitly + # testing that (without artifically removing/corrupting those values) a replacement package differs + # in one of these values + test_add_node_with_different_package(network, args) + test_update_all_nodes(network, args) + + # Run again at the end to confirm current nodes are acceptable + test_verify_quotes(network, args) + + if snp.IS_SNP: + test_add_node_with_no_uvm_endorsements_in_kv(network, args) if __name__ == "__main__":