File: internalsecret.tcl

package info (click to toggle)
redis 5%3A8.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 22,304 kB
  • sloc: ansic: 216,903; tcl: 51,562; sh: 4,625; perl: 4,214; cpp: 3,568; python: 2,954; makefile: 2,055; ruby: 639; javascript: 30; csh: 7
file content (287 lines) | stat: -rw-r--r-- 11,180 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
tags {modules} {
set testmodule [file normalize tests/modules/internalsecret.so]

set modules [list loadmodule $testmodule]
start_cluster 1 0 [list config_lines $modules] {
    set r [srv 0 client]

    test {Internal command without internal connection fails as an unknown command} {
        assert_error {*unknown command*with args beginning with:*} {r internalauth.internalcommand}
    }

    test {Wrong internalsecret fails authentication} {
        assert_error {*WRONGPASS invalid internal password*} {r auth "internal connection" 123}
    }

    test {Internal connection basic flow} {
        # A non-internal connection cannot execute internal commands, and they
        # seem non-existent to it.
        assert_error {*unknown command*} {r internalauth.internalcommand}

        # Authenticate as an internal connection
        assert_equal {OK} [r debug mark-internal-client]

        # Now, internal commands are available.
        assert_equal {OK} [r internalauth.internalcommand]
    }
}

start_server {} {
    r module load $testmodule

    test {Internal secret is not available in non-cluster mode} {
        # On non-cluster mode, the internal secret does not exist, nor is the
        # internal auth command available
        assert_error {*unknown command*} {r internalauth.internalcommand}
        assert_error {*ERR no internal secret available*} {r internalauth.getinternalsecret}
        assert_error {*Cannot authenticate as an internal connection on non-cluster instances*} {r auth "internal connection" somepassword}
    }

    test {marking and un-marking a connection as internal via a debug command} {
        # After marking the connection to an internal one via a debug command,
        # internal commands succeed.
        r debug mark-internal-client
        assert_equal {OK} [r internalauth.internalcommand]

        # After unmarking the connection, internal commands fail.
        r debug mark-internal-client unmark
        assert_error {*unknown command*} {r internalauth.internalcommand}
    }
}

start_server {} {
    r module load $testmodule

    test {Test `COMMAND *` commands with\without internal connections} {
        # ------------------ Non-internal connection ------------------
        # `COMMAND DOCS <cmd>` returns empty response.
        assert_equal {} [r command docs internalauth.internalcommand]

        # `COMMAND INFO <cmd>` should reply with null for the internal command
        assert_equal {{}} [r command info internalauth.internalcommand]

        # `COMMAND GETKEYS/GETKEYSANDFLAGS <cmd> <args>` returns an invalid command error
        assert_error {*Invalid command specified*} {r command getkeys internalauth.internalcommand}
        assert_error {*Invalid command specified*} {r command getkeysandflags internalauth.internalcommand}

        # -------------------- Internal connection --------------------
        # Non-empty response for non-internal connections.
        assert_equal {OK} [r debug mark-internal-client]

        # `COMMAND DOCS <cmd>` returns a correct response.
        assert_match {*internalauth.internalcommand*} [r command docs internalauth.internalcommand]

        # `COMMAND INFO <cmd>` should reply with a full response for the internal command
        assert_match {*internalauth.internalcommand*} [r command info internalauth.internalcommand]

        # `COMMAND GETKEYS/GETKEYSANDFLAGS <cmd> <args>` returns a key error (not related to the internal connection)
        assert_error {*ERR The command has no key arguments*} {r command getkeys internalauth.internalcommand}
        assert_error {*ERR The command has no key arguments*} {r command getkeysandflags internalauth.internalcommand}
    }
}

start_server {} {
    r module load $testmodule

    test {No authentication needed for internal connections} {
        # Authenticate with a user that does not have permissions to any command
        r acl setuser David on >123 &* ~* -@all +auth +internalauth.getinternalsecret +debug +internalauth.internalcommand
        assert_equal {OK} [r auth David 123]

        assert_equal {OK} [r debug mark-internal-client]
        # Execute a command for which David does not have permission
        assert_equal {OK} [r internalauth.internalcommand]
    }
}

start_server {} {
    r module load $testmodule

    test {RM_Call of internal commands without user-flag succeeds only for all connections} {
        # Fail before authenticating as an internal connection.
        assert_equal {OK} [r internalauth.noninternal_rmcall internalauth.internalcommand]
    }

    test {Internal commands via RM_Call succeeds for non-internal connections depending on the user flag} {
        # A non-internal connection that calls rm_call of an internal command
        assert_equal {OK} [r internalauth.noninternal_rmcall internalauth.internalcommand]

        # A non-internal connection that calls rm_call of an internal command
        # with a user flag should fail.
        assert_error {*unknown command*} {r internalauth.noninternal_rmcall_withuser internalauth.internalcommand}
    }

    test {Internal connections override the user flag} {
        # Authenticate as an internal connection
        assert_equal {OK} [r debug mark-internal-client]

        assert_equal {OK} [r internalauth.noninternal_rmcall internalauth.internalcommand]
        assert_equal {OK} [r internalauth.noninternal_rmcall_withuser internalauth.internalcommand]
    }
}

start_server {} {
    r module load $testmodule

    test {RM_Call with the user-flag after setting thread-safe-context from an internal connection should fail} {
        # Authenticate as an internal connection
        assert_equal {OK} [r debug mark-internal-client]

        # New threadSafeContexts do not inherit the internal flag.
        assert_error {*unknown command*} {r internalauth.noninternal_rmcall_detachedcontext_withuser internalauth.internalcommand}
    }
}

start_server {} {
    r module load $testmodule

    r config set appendonly yes
    r config set appendfsync always
    waitForBgrewriteaof r

    test {AOF executes internal commands successfully} {
        # Authenticate as an internal connection
        assert_equal {OK} [r debug mark-internal-client]

        # Call an internal writing command
        assert_equal {OK} [r internalauth.internal_rmcall_replicated set x 5]

        # Reload the server from the AOF
        r debug loadaof

        # Check if the internal command was executed successfully
        assert_equal {5} [r get x]
    }
}

start_server {} {
    r module load $testmodule

    test {Internal commands are not allowed from scripts} {
        # Internal commands are not allowed from scripts
        assert_error {*not allowed from script*} {r eval {redis.call('internalauth.internalcommand')} 0}

        # Even after authenticating as an internal connection
        assert_equal {OK} [r debug mark-internal-client]
        assert_error {*not allowed from script*} {r eval {redis.call('internalauth.internalcommand')} 0}
    }
}

start_cluster 1 1 [list config_lines $modules] {
    set master [srv 0 client]
    set slave [srv -1 client]

    test {Setup master} {
        # Authenticate as an internal connection
        set reply [$master internalauth.getinternalsecret]
        assert_equal {OK} [$master auth "internal connection" $reply]
    }

    test {Slaves successfully execute internal commands from the replication link} {
        assert {[s -1 role] eq {slave}}
        wait_for_condition 1000 50 {
            [s -1 master_link_status] eq {up}
        } else {
            fail "Master link status is not up"
        }

        # Execute internal command in master, that will set `x` to `5`.
        assert_equal {OK} [$master internalauth.internal_rmcall_replicated set x 5]

        # Wait for replica to have the key
        $slave readonly
        wait_for_condition 1000 50 {
            [$slave exists x] eq "1"
        } else {
            fail "Test key was not replicated"
        }

        # See that the slave has the same value for `x`.
        assert_equal {5} [$slave get x]
    }
}

start_server {} {
    r module load $testmodule

    test {Internal commands are not reported in the monitor output for non-internal connections when unsuccessful} {
        set rd [redis_deferring_client]
        $rd monitor
        $rd read ; # Discard the OK
        assert_error {*unknown command*} {r internalauth.internalcommand}

        # Assert that the monitor output does not contain the internal command
        r ping
        assert_match {*ping*} [$rd read]
        $rd close
    }

    test {Internal commands are not reported in the monitor output for non-internal connections when successful} {
        # Authenticate as an internal connection
        assert_equal {OK} [r debug mark-internal-client]

        set rd [redis_deferring_client]
        $rd monitor
        $rd read ; # Discard the OK
        assert_equal {OK} [r internalauth.internalcommand]

        # Assert that the monitor output does not contain the internal command
        r ping
        assert_match {*ping*} [$rd read]
        $rd close
    }

    test {Internal commands are reported in the monitor output for internal connections} {
        set rd [redis_deferring_client]
        $rd debug mark-internal-client
        assert_equal {OK} [$rd read]
        $rd monitor
        $rd read ; # Discard the OK
        assert_equal {OK} [r internalauth.internalcommand]

        # Assert that the monitor output contains the internal command
        assert_match {*internalauth.internalcommand*} [$rd read]
        $rd close
    }

    test {Internal commands are reported in the slowlog} {
        # Set up slowlog to log all commands
        r config set slowlog-log-slower-than 0

        # Execute an internal command
        r slowlog reset
        r internalauth.internalcommand

        # The slow-log should contain the internal command
        set log [r slowlog get 1]
        assert_match {*internalauth.internalcommand*} $log
    }

    test {Internal commands are reported in the latency report} {
        # The latency report should contain the internal command
        set report [r latency histogram internalauth.internalcommand]
        assert_match {*internalauth.internalcommand*} $report
    }

    test {Internal commands are reported in the command stats report} {
        # The INFO report should contain the internal command for both the internal
        # and non-internal connections.
        set report [r info commandstats]
        assert_match {*internalauth.internalcommand*} $report

        set report [r info latencystats]
        assert_match {*internalauth.internalcommand*} $report

        # Un-mark the connection as internal
        r debug mark-internal-client unmark
        assert_error {*unknown command*} {r internalauth.internalcommand}

        # We still expect to see the internal command in the report
        set report [r info commandstats]
        assert_match {*internalauth.internalcommand*} $report

        set report [r info latencystats]
        assert_match {*internalauth.internalcommand*} $report
    }
}
}