@@ -21,6 +21,7 @@ local enable_debug = require("apisix.debug").enable_debug
2121local wasm = require (" apisix.wasm" )
2222local expr = require (" resty.expr.v1" )
2323local apisix_ssl = require (" apisix.ssl" )
24+ local re_split = require (" ngx.re" ).split
2425local ngx = ngx
2526local crc32 = ngx .crc32_short
2627local ngx_exit = ngx .exit
@@ -844,12 +845,6 @@ local function check_single_plugin_schema(name, plugin_conf, schema_type, skip_d
844845end
845846
846847
847- check_plugin_metadata = function (item )
848- return check_single_plugin_schema (item .id , item ,
849- core .schema .TYPE_METADATA , true )
850- end
851-
852-
853848local enable_data_encryption
854849local function enable_gde ()
855850 if enable_data_encryption == nil then
@@ -868,9 +863,15 @@ local function get_plugin_schema_for_gde(name, schema_type)
868863 end
869864
870865 local plugin_schema = local_plugins_hash and local_plugins_hash [name ]
866+ if not plugin_schema then
867+ return nil
868+ end
869+
871870 local schema
872871 if schema_type == core .schema .TYPE_CONSUMER then
873872 schema = plugin_schema .consumer_schema
873+ elseif schema_type == core .schema .TYPE_METADATA then
874+ schema = plugin_schema .metadata_schema
874875 else
875876 schema = plugin_schema .schema
876877 end
@@ -882,17 +883,39 @@ end
882883local function decrypt_conf (name , conf , schema_type )
883884 local schema = get_plugin_schema_for_gde (name , schema_type )
884885 if not schema then
886+ core .log .warn (" failed to get schema for plugin: " , name )
885887 return
886888 end
887889
888- for key , props in pairs (schema .properties ) do
889- if props .type == " string" and props .encrypted and conf [key ] then
890- local encrypted , err = apisix_ssl .aes_decrypt_pkey (conf [key ], " data_encrypt" )
891- if not encrypted then
892- core .log .warn (" failed to decrypt the conf of plugin [" , name ,
893- " ] key [" , key , " ], err: " , err )
894- else
895- conf [key ] = encrypted
890+ if schema .encrypt_fields and not core .table .isempty (schema .encrypt_fields ) then
891+ for _ , key in ipairs (schema .encrypt_fields ) do
892+ if conf [key ] then
893+ local decrypted , err = apisix_ssl .aes_decrypt_pkey (conf [key ], " data_encrypt" )
894+ if not decrypted then
895+ core .log .warn (" failed to decrypt the conf of plugin [" , name ,
896+ " ] key [" , key , " ], err: " , err )
897+ else
898+ conf [key ] = decrypted
899+ end
900+ elseif core .string .find (key , " ." ) then
901+ -- decrypt fields has indents
902+ local res , err = re_split (key , " \\ ." , " jo" )
903+ if not res then
904+ core .log .warn (" failed to split key [" , key , " ], err: " , err )
905+ return
906+ end
907+
908+ -- we only support two levels
909+ if conf [res [1 ]] and conf [res [1 ]][res [2 ]] then
910+ local decrypted , err = apisix_ssl .aes_decrypt_pkey (
911+ conf [res [1 ]][res [2 ]], " data_encrypt" )
912+ if not decrypted then
913+ core .log .warn (" failed to decrypt the conf of plugin [" , name ,
914+ " ] key [" , key , " ], err: " , err )
915+ else
916+ conf [res [1 ]][res [2 ]] = decrypted
917+ end
918+ end
896919 end
897920 end
898921 end
@@ -903,19 +926,57 @@ _M.decrypt_conf = decrypt_conf
903926local function encrypt_conf (name , conf , schema_type )
904927 local schema = get_plugin_schema_for_gde (name , schema_type )
905928 if not schema then
929+ core .log .warn (" failed to get schema for plugin: " , name )
906930 return
907931 end
908932
909- for key , props in pairs (schema .properties ) do
910- if props .type == " string" and props .encrypted and conf [key ] then
911- local encrypted = apisix_ssl .aes_encrypt_pkey (conf [key ], " data_encrypt" )
912- conf [key ] = encrypted
933+ if schema .encrypt_fields and not core .table .isempty (schema .encrypt_fields ) then
934+ for _ , key in ipairs (schema .encrypt_fields ) do
935+ if conf [key ] then
936+ local encrypted , err = apisix_ssl .aes_encrypt_pkey (conf [key ], " data_encrypt" )
937+ if not encrypted then
938+ core .log .warn (" failed to encrypt the conf of plugin [" , name ,
939+ " ] key [" , key , " ], err: " , err )
940+ else
941+ conf [key ] = encrypted
942+ end
943+ elseif core .string .find (key , " ." ) then
944+ -- encrypt fields has indents
945+ local res , err = re_split (key , " \\ ." , " jo" )
946+ if not res then
947+ core .log .warn (" failed to split key [" , key , " ], err: " , err )
948+ return
949+ end
950+
951+ -- we only support two levels
952+ if conf [res [1 ]] and conf [res [1 ]][res [2 ]] then
953+ local encrypted , err = apisix_ssl .aes_encrypt_pkey (
954+ conf [res [1 ]][res [2 ]], " data_encrypt" )
955+ if not encrypted then
956+ core .log .warn (" failed to encrypt the conf of plugin [" , name ,
957+ " ] key [" , key , " ], err: " , err )
958+ else
959+ conf [res [1 ]][res [2 ]] = encrypted
960+ end
961+ end
962+ end
913963 end
914964 end
915965end
916966_M .encrypt_conf = encrypt_conf
917967
918968
969+ check_plugin_metadata = function (item )
970+ local ok , err = check_single_plugin_schema (item .id , item ,
971+ core .schema .TYPE_METADATA , true )
972+ if ok and enable_gde () then
973+ decrypt_conf (item .name , item , core .schema .TYPE_METADATA )
974+ end
975+
976+ return ok , err
977+ end
978+
979+
919980local function check_schema (plugins_conf , schema_type , skip_disabled_plugin )
920981 for name , plugin_conf in pairs (plugins_conf ) do
921982 local ok , err = check_single_plugin_schema (name , plugin_conf ,
0 commit comments