Skip to content

Commit 152ea80

Browse files
authored
feat: data encryption support more plugins (#8487)
Fixes #8407
1 parent 9686c55 commit 152ea80

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+1260
-42
lines changed

apisix/admin/init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ local function run()
204204
if method == "get" and plugin.enable_data_encryption then
205205
if seg_res == "consumers" then
206206
utils.decrypt_params(plugin.decrypt_conf, data, core.schema.TYPE_CONSUMER)
207+
elseif seg_res == "plugin_metadata" then
208+
utils.decrypt_params(plugin.decrypt_conf, data, core.schema.TYPE_METADATA)
207209
else
208210
utils.decrypt_params(plugin.decrypt_conf, data)
209211
end

apisix/admin/plugin_metadata.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ local pcall = pcall
1818
local require = require
1919
local core = require("apisix.core")
2020
local utils = require("apisix.admin.utils")
21+
local encrypt_conf = require("apisix.plugin").encrypt_conf
2122

2223
local injected_mark = "injected metadata_schema"
2324
local _M = {
@@ -73,6 +74,8 @@ local function check_conf(plugin_name, conf)
7374
ok, err = plugin_object.check_schema(conf, core.schema.TYPE_METADATA)
7475
end
7576

77+
encrypt_conf(plugin_name, conf, core.schema.TYPE_METADATA)
78+
7679
if not ok then
7780
return nil, {error_msg = "invalid configuration: " .. err}
7881
end

apisix/admin/utils.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ function _M.decrypt_params(decrypt_func, body, schema_type)
102102
decrypt_func(name, conf, schema_type)
103103
end
104104
end
105+
106+
-- metadata
107+
if schema_type == core.schema.TYPE_METADATA then
108+
local conf = body.node and body.node.value
109+
decrypt_func(conf.name, conf, schema_type)
110+
end
105111
end
106112

107113
return _M

apisix/plugin.lua

Lines changed: 79 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ local enable_debug = require("apisix.debug").enable_debug
2121
local wasm = require("apisix.wasm")
2222
local expr = require("resty.expr.v1")
2323
local apisix_ssl = require("apisix.ssl")
24+
local re_split = require("ngx.re").split
2425
local ngx = ngx
2526
local crc32 = ngx.crc32_short
2627
local ngx_exit = ngx.exit
@@ -844,12 +845,6 @@ local function check_single_plugin_schema(name, plugin_conf, schema_type, skip_d
844845
end
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-
853848
local enable_data_encryption
854849
local 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
882883
local 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
903926
local 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
915965
end
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+
919980
local 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,

apisix/plugins/authz-casdoor.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ local schema = {
3232
client_secret = {type = "string"},
3333
callback_url = {type = "string", pattern = "^[^%?]+[^/]$"}
3434
},
35+
encrypt_fields = {"client_secret"},
3536
required = {
3637
"callback_url", "endpoint_addr", "client_id", "client_secret"
3738
}

apisix/plugins/authz-keycloak.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ local schema = {
7171
maxLength = 4096
7272
},
7373
},
74+
encrypt_fields = {"client_secret"},
7475
required = {"client_id"},
7576
allOf = {
7677
-- Require discovery or token endpoint.

apisix/plugins/basic-auth.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ local consumer_schema = {
3838
title = "work with consumer object",
3939
properties = {
4040
username = { type = "string" },
41-
password = { type = "string", encrypted = true },
41+
password = { type = "string" },
4242
},
43+
encrypt_fields = {"password"},
4344
required = {"username", "password"},
4445
}
4546

apisix/plugins/clickhouse-logger.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ local schema = {
3636
endpoint_addr = core.schema.uri_def,
3737
endpoint_addrs = {items = core.schema.uri_def, type = "array", minItems = 1},
3838
user = {type = "string", default = ""},
39-
password = {type = "string", default = "", encrypted = true},
39+
password = {type = "string", default = ""},
4040
database = {type = "string", default = ""},
4141
logtable = {type = "string", default = ""},
4242
timeout = {type = "integer", minimum = 1, default = 3},
@@ -47,6 +47,7 @@ local schema = {
4747
{required = {"endpoint_addr", "user", "password", "database", "logtable"}},
4848
{required = {"endpoint_addrs", "user", "password", "database", "logtable"}}
4949
},
50+
encrypt_fields = {"password"},
5051
}
5152

5253

apisix/plugins/csrf.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ local schema = {
4444
default = "apisix-csrf-token"
4545
}
4646
},
47+
encrypt_fields = {"key"},
4748
required = {"key"}
4849
}
4950

apisix/plugins/elasticsearch-logger.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ local schema = {
6767
default = true
6868
}
6969
},
70+
encrypt_fields = {"auth.password"},
7071
required = { "endpoint_addr", "field" },
7172
}
7273

0 commit comments

Comments
 (0)