Skip to content

Commit 75cff5e

Browse files
authored
change: unify the keyring and key_encrypt_salt fields (#10771)
1 parent 847db2a commit 75cff5e

23 files changed

+99
-124
lines changed

apisix/cli/file.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ local function path_is_multi_type(path, type_val)
175175
return true
176176
end
177177

178-
if path == "apisix->ssl->key_encrypt_salt" then
178+
if path == "apisix->data_encryption->keyring" then
179179
return true
180180
end
181181

apisix/cli/schema.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,12 @@ local config_schema = {
224224
}
225225
}
226226
},
227-
key_encrypt_salt = {
227+
}
228+
},
229+
data_encryption = {
230+
type = "object",
231+
properties = {
232+
keyring = {
228233
anyOf = {
229234
{
230235
type = "array",

apisix/plugin.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,8 @@ local enable_data_encryption
906906
local function enable_gde()
907907
if enable_data_encryption == nil then
908908
enable_data_encryption =
909-
core.table.try_read_attr(local_conf, "apisix", "data_encryption", "enable")
909+
core.table.try_read_attr(local_conf, "apisix", "data_encryption",
910+
"enable_encrypt_fields")
910911
_M.enable_data_encryption = enable_data_encryption
911912
end
912913

apisix/ssl.lua

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,6 @@ local function init_iv_tbl(ivs)
9292
end
9393

9494

95-
local _aes_128_cbc_with_iv_tbl_ssl
96-
local function get_aes_128_cbc_with_iv_ssl(local_conf)
97-
if _aes_128_cbc_with_iv_tbl_ssl == nil then
98-
local ivs = core.table.try_read_attr(local_conf, "apisix", "ssl", "key_encrypt_salt")
99-
_aes_128_cbc_with_iv_tbl_ssl = init_iv_tbl(ivs)
100-
end
101-
102-
return _aes_128_cbc_with_iv_tbl_ssl
103-
end
104-
105-
10695
local _aes_128_cbc_with_iv_tbl_gde
10796
local function get_aes_128_cbc_with_iv_gde(local_conf)
10897
if _aes_128_cbc_with_iv_tbl_gde == nil then
@@ -127,43 +116,31 @@ end
127116

128117
function _M.aes_encrypt_pkey(origin, field)
129118
local local_conf = core.config.local_conf()
119+
local aes_128_cbc_with_iv_tbl_gde = get_aes_128_cbc_with_iv_gde(local_conf)
120+
local aes_128_cbc_with_iv_gde = aes_128_cbc_with_iv_tbl_gde[1]
130121

131122
if not field then
132-
-- default used by ssl
133-
local aes_128_cbc_with_iv_tbl_ssl = get_aes_128_cbc_with_iv_ssl(local_conf)
134-
local aes_128_cbc_with_iv_ssl = aes_128_cbc_with_iv_tbl_ssl[1]
135-
if aes_128_cbc_with_iv_ssl ~= nil and core.string.has_prefix(origin, "---") then
136-
return encrypt(aes_128_cbc_with_iv_ssl, origin)
123+
if aes_128_cbc_with_iv_gde ~= nil and core.string.has_prefix(origin, "---") then
124+
return encrypt(aes_128_cbc_with_iv_gde, origin)
137125
end
138126
else
139127
if field == "data_encrypt" then
140-
local aes_128_cbc_with_iv_tbl_gde = get_aes_128_cbc_with_iv_gde(local_conf)
141-
local aes_128_cbc_with_iv_gde = aes_128_cbc_with_iv_tbl_gde[1]
142128
if aes_128_cbc_with_iv_gde ~= nil then
143129
return encrypt(aes_128_cbc_with_iv_gde, origin)
144130
end
145131
end
146132
end
147-
148133
return origin
149134
end
150135

151136

152137
local function aes_decrypt_pkey(origin, field)
153-
local local_conf = core.config.local_conf()
154-
local aes_128_cbc_with_iv_tbl
155-
156-
if not field then
157-
if core.string.has_prefix(origin, "---") then
158-
return origin
159-
end
160-
aes_128_cbc_with_iv_tbl = get_aes_128_cbc_with_iv_ssl(local_conf)
161-
else
162-
if field == "data_encrypt" then
163-
aes_128_cbc_with_iv_tbl = get_aes_128_cbc_with_iv_gde(local_conf)
164-
end
138+
if not field and core.string.has_prefix(origin, "---") then
139+
return origin
165140
end
166141

142+
local local_conf = core.config.local_conf()
143+
local aes_128_cbc_with_iv_tbl = get_aes_128_cbc_with_iv_gde(local_conf)
167144
if #aes_128_cbc_with_iv_tbl == 0 then
168145
return origin
169146
end

conf/config-default.yaml

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,6 @@ apisix:
108108
# Disabled by default because it renders Perfect Forward Secrecy (FPS)
109109
# useless. See https://github.com/mozilla/server-side-tls/issues/135.
110110

111-
key_encrypt_salt: # This field is only used to encrypt the private key of SSL.
112-
- edd1c9f0985e76a2 # Set the encryption key for AES-128-CBC. It should be a
113-
# hexadecimal string of length 16.
114-
# If not set, APISIX saves the original data into etcd.
115-
# CAUTION: If you would like to update the key, add the new key as the
116-
# first item in the array and keep the older keys below the newly added
117-
# key, so that data can be decrypted with the older keys and encrypted
118-
# with the new key. Removing the old keys directly can render the data
119-
# unrecoverable.
120-
121111
# fallback_sni: "my.default.domain" # Fallback SNI to be used if the client does not send SNI during
122112
# # the handshake.
123113

@@ -128,11 +118,13 @@ apisix:
128118

129119
disable_sync_configuration_during_start: false # Safe exit. TO BE REMOVED.
130120

131-
data_encryption: # Encrypt fields specified in `encrypt_fields` in plugin schema.
132-
enable: false
133-
keyring: # Set the encryption key for AES-128-CBC. It should be a
134-
- qeddd145sfvddff3 # hexadecimal string of length 16.
135-
# If not set, APISIX saves the original data into etcd.
121+
data_encryption: # Data encryption settings.
122+
enable_encrypt_fields: false # Whether enable encrypt fields specified in `encrypt_fields` in plugin schema.
123+
keyring: # This field is used to encrypt the private key of SSL and the `encrypt_fields`
124+
# in plugin schema.
125+
- qeddd145sfvddff3 # Set the encryption key for AES-128-CBC. It should be a hexadecimal string
126+
# of length 16.
127+
- edd1c9f0985e76a2 # If not set, APISIX saves the original data into etcd.
136128
# CAUTION: If you would like to update the key, add the new key as the
137129
# first item in the array and keep the older keys below the newly added
138130
# key, so that data can be decrypted with the older keys and encrypted

t/admin/ssl2.t

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,8 @@ qr/"snis":\["update1.com","update2.com"\]/
431431
--- yaml_config
432432
apisix:
433433
node_listen: 1984
434-
ssl:
435-
key_encrypt_salt: "edd1c9f0985e76a2"
434+
data_encryption:
435+
keyring: "qeddd145sfvddff3"
436436
--- config
437437
location /t {
438438
content_by_lua_block {
@@ -468,8 +468,8 @@ false
468468
--- yaml_config
469469
apisix:
470470
node_listen: 1984
471-
ssl:
472-
key_encrypt_salt: "edd1c9f0985e76a2"
471+
data_encryption:
472+
keyring: "qeddd145sfvddff3"
473473
--- config
474474
location /t {
475475
content_by_lua_block {

t/admin/ssl4.t

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ run_tests;
110110

111111
__DATA__
112112
113-
=== TEST 1: set ssl(sni: www.test.com), encrypt with the first key_encrypt_salt
113+
=== TEST 1: set ssl(sni: www.test.com), encrypt with the first keyring
114114
--- yaml_config
115115
apisix:
116116
node_listen: 1984
117-
ssl:
118-
key_encrypt_salt:
117+
data_encryption:
118+
keyring:
119119
- edd1c9f0985e76a1
120-
- edd1c9f0985e76a2
120+
- qeddd145sfvddff3
121121
--- config
122122
location /t {
123123
content_by_lua_block {
@@ -152,8 +152,8 @@ passed
152152
--- yaml_config
153153
apisix:
154154
node_listen: 1984
155-
ssl:
156-
key_encrypt_salt: "edd1c9f0985e76a1"
155+
data_encryption:
156+
keyring: "edd1c9f0985e76a1"
157157
--- config
158158
location /t {
159159
content_by_lua_block {
@@ -182,12 +182,12 @@ passed
182182
183183
184184
185-
=== TEST 3: client request with the old style key_encrypt_salt
185+
=== TEST 3: client request with the old style keyring
186186
--- yaml_config
187187
apisix:
188188
node_listen: 1984
189-
ssl:
190-
key_encrypt_salt: "edd1c9f0985e76a1"
189+
data_encryption:
190+
keyring: "edd1c9f0985e76a1"
191191
--- response_body eval
192192
qr{connected: 1
193193
ssl handshake: true
@@ -207,12 +207,12 @@ server name: "www.test.com"
207207
208208
209209
210-
=== TEST 4: client request with the new style key_encrypt_salt
210+
=== TEST 4: client request with the new style keyring
211211
--- yaml_config
212212
apisix:
213213
node_listen: 1984
214-
ssl:
215-
key_encrypt_salt:
214+
data_encryption:
215+
keyring:
216216
- edd1c9f0985e76a1
217217
--- response_body eval
218218
qr{connected: 1
@@ -233,26 +233,26 @@ server name: "www.test.com"
233233
234234
235235
236-
=== TEST 5: client request failed with the wrong key_encrypt_salt
236+
=== TEST 5: client request failed with the wrong keyring
237237
--- yaml_config
238238
apisix:
239239
node_listen: 1984
240-
ssl:
241-
key_encrypt_salt:
242-
- edd1c9f0985e76a2
240+
data_encryption:
241+
keyring:
242+
- qeddd145sfvddff3
243243
--- error_log
244244
decrypt ssl key failed
245245
[alert]
246246
247247
248248
249-
=== TEST 6: client request successfully, use the two key_encrypt_salt to decrypt in turn
249+
=== TEST 6: client request successfully, use the two keyring to decrypt in turn
250250
--- yaml_config
251251
apisix:
252252
node_listen: 1984
253-
ssl:
254-
key_encrypt_salt:
255-
- edd1c9f0985e76a2
253+
data_encryption:
254+
keyring:
255+
- qeddd145sfvddff3
256256
- edd1c9f0985e76a1
257257
--- response_body eval
258258
qr{connected: 1
@@ -273,8 +273,8 @@ close: 1 nil}
273273
--- yaml_config
274274
apisix:
275275
node_listen: 1984
276-
ssl:
277-
key_encrypt_salt:
276+
data_encryption:
277+
keyring:
278278
- edd1c9f0985e76a1
279279
--- config
280280
location /t {
@@ -292,8 +292,8 @@ location /t {
292292
--- yaml_config
293293
apisix:
294294
node_listen: 1984
295-
ssl:
296-
key_encrypt_salt: null
295+
data_encryption:
296+
keyring: null
297297
--- config
298298
location /t {
299299
content_by_lua_block {
@@ -324,12 +324,12 @@ passed
324324
325325
326326
327-
=== TEST 9: client request without key_encrypt_salt
327+
=== TEST 9: client request without keyring
328328
--- yaml_config
329329
apisix:
330330
node_listen: 1984
331-
ssl:
332-
key_encrypt_salt: null
331+
data_encryption:
332+
keyring: null
333333
--- response_body eval
334334
qr{connected: 1
335335
ssl handshake: true
@@ -353,8 +353,8 @@ server name: "www.test.com"
353353
--- yaml_config
354354
apisix:
355355
node_listen: 1984
356-
ssl:
357-
key_encrypt_salt: null
356+
data_encryption:
357+
keyring: null
358358
--- config
359359
location /t {
360360
content_by_lua_block {
@@ -371,8 +371,8 @@ location /t {
371371
--- yaml_config
372372
apisix:
373373
node_listen: 1984
374-
ssl:
375-
key_encrypt_salt: null
374+
data_encryption:
375+
keyring: null
376376
--- config
377377
location /t {
378378
content_by_lua_block {

0 commit comments

Comments
 (0)