Skip to content

Commit 2e57fb7

Browse files
authored
feat(redis): support cloud redis authn (#205)
Remove the read only restriction on object methods and add a flag to control whether auth is required after each connection.
1 parent 556e999 commit 2e57fb7

3 files changed

Lines changed: 10 additions & 11 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ With DHSM storage you can use the following settings (set the `storage` to `"dsh
363363
| `ssl` | `nil` | Enable SSL. |
364364
| `ssl_verify` | `nil` | Verify server certificate. |
365365
| `server_name` | `nil` | The server name for the new TLS extension Server Name Indication (SNI). |
366+
| `force_auth` | `nil` | The flag controls whether to invoke auth after obtaining each Redis connection. |
366367

367368
Please refer to [ngx-distributed-shm](https://github.com/grrolland/ngx-distributed-shm) to get necessary
368369
dependencies installed.

lib/resty/session/redis.lua

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ local function exec(self, func, ...)
4545
return nil, err
4646
end
4747

48-
if red:get_reused_times() == 0 then
48+
if self.force_auth == true or red:get_reused_times() == 0 then
4949
local password = self.password
5050
if password then
5151
local username = self.username
@@ -99,11 +99,6 @@ local metatable = {}
9999
metatable.__index = metatable
100100

101101

102-
function metatable.__newindex()
103-
error("attempt to update a read-only table", 2)
104-
end
105-
106-
107102
---
108103
-- Store session data.
109104
--
@@ -233,6 +228,8 @@ function storage.new(configuration)
233228
local ssl_verify = configuration and configuration.ssl_verify
234229
local server_name = configuration and configuration.server_name
235230

231+
local force_auth = configuration and configuration.force_auth
232+
236233
if ssl ~= nil or ssl_verify ~= nil or server_name or pool or pool_size or backlog then
237234
return setmetatable({
238235
prefix = prefix,
@@ -247,6 +244,7 @@ function storage.new(configuration)
247244
send_timeout = send_timeout,
248245
read_timeout = read_timeout,
249246
keepalive_timeout = keepalive_timeout,
247+
force_auth = force_auth,
250248
options = {
251249
ssl = ssl,
252250
ssl_verify = ssl_verify,
@@ -271,6 +269,7 @@ function storage.new(configuration)
271269
send_timeout = send_timeout,
272270
read_timeout = read_timeout,
273271
keepalive_timeout = keepalive_timeout,
272+
force_auth = force_auth,
274273
}, metatable)
275274
end
276275

lib/resty/session/redis/cluster.lua

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ local metatable = {}
5050
metatable.__index = metatable
5151

5252

53-
function metatable.__newindex()
54-
error("attempt to update a read-only table", 2)
55-
end
56-
57-
5853
---
5954
-- Store session data.
6055
--
@@ -206,6 +201,8 @@ function storage.new(configuration)
206201
local ssl_verify = configuration and configuration.ssl_verify
207202
local server_name = configuration and configuration.server_name
208203

204+
local force_auth = configuration and configuration.force_auth
205+
209206

210207
local auth
211208
if not username then
@@ -235,6 +232,7 @@ function storage.new(configuration)
235232
auth = auth,
236233
username = username,
237234
password = password,
235+
force_auth = force_auth,
238236
connect_opts = {
239237
ssl = ssl,
240238
ssl_verify = ssl_verify,
@@ -266,6 +264,7 @@ function storage.new(configuration)
266264
auth = auth,
267265
username = username,
268266
password = password,
267+
force_auth = force_auth,
269268
},
270269
}, metatable)
271270
end

0 commit comments

Comments
 (0)