Debian Codegraph @

doc/antora/modules/tutorials/pages/failover.adoc

raw
1 = Configurable Module Fail Over
2
3 Before configurable module failover, we had this kind of entry in "radiusd.conf"::
4 ----
5 authorize {
6 preprocess
7 files
8 }
9 ----
10
11 This entry instructed the "authorize" section to first process the request through the "preprocess" module, and if that returned success, to process it through "files" module. If that sequence returned success, then the "authorize" stage itself would then return success. Processing was strictly linear and if one module failed, the whole section would fail immediately.
12
13 Configurable failover provides more flexibility. It takes advantage of the tree structure of radiusd.conf to support a configuration
14 language that allows you to "group" modules that should work together in ways other than simple lists. You can control the flow of any stage (e.g. "authorize") to fit your needs, without touching C code, just by altering radiusd.conf.
15
16 This configurable fail-over has a convenient short-hand, too. Administrators commonly want to say things like "try SQL1, if it's
17 down, try SQL2, otherwise drop the request."
18
19 For example::
20 ----
21 modules {
22 sql sql1 {
23 # configuration to connect to SQL database one
24 }
25 sql sql2 {
26 # configuration to connect to SQL database two
27 }
28 always handled {
29 rcode = handled
30 }
31 }
32
33 # Handle accounting packets
34 accounting {
35 # always log to detail, stopping if it fails
36 detail
37 redundant {
38 # try module sql1
39 sql1
40 # if that's down, try module sql2
41 sql2
42 # otherwise drop the request as
43 # it's been "handled" by the "always"
44 # module (see doc/rlm_always)
45 handled
46 }
47 }
48 ----
49
50 The "redundant" section is a configuration directive which tells the server to process the second module if the first one fails. Any
51 number of modules can be listed in a "redundant" section. The server will process each in turn, until one of the modules succeeds. It will then stop processing the "redundant" list.
52
53 == Rewriting results for single modules
54
55 Normally, when a module fails, the entire section ("authorize", "accounting", etc.) stops being processed. In some cases, we may want to permit "soft failures". That is, we may want to tell the server that it is "ok" for a module to fail, and that the failure should not be treated as a fatal error.
56
57 In this case, the module is treated as a "section", rather than just as a single line in "radiusd.conf". The configuration entries for
58 that section are taken from the "configurable fail-over" code, and not from the configuration information for that module.
59
60 For example, the "detail" module normally returns "fail" if it is unable to write its information to the "detail" file. As a test, we
61 can configure the server so that it continues processing the request, even if the "detail" module fails. The following example shows how::
62 ----
63 # Handle accounting packets
64 accounting {
65 detail {
66 fail = 1
67 }
68 redundant {
69 sql1
70 sql2
71 handled
72 }
73 }
74 ----
75
76 The "fail = 1" entry tells the server to remember the "fail" code, with priority "1". The normal configuration is "fail = return", which
77 means "if the detail module fails, stop processing the accounting section".
78
79 == Fail-over configuration entries
80
81 Modules normally return on of the following codes as their result:
82
83 [options="header"]
84 [cols="30%,70%"]
85 |=====
86 | Code | Meaning
87 | notfound | the user was not found
88 | noop | the module did nothing
89 | ok | the module succeeded
90 | updated | the module updated information in the request
91 | fail | the module failed
92 | reject | the module rejected the user
93 | userlock | the user was locked out
94 | invalid | the user's configuration entry was invalid
95 | handled | the module has done everything to handle the request
96 |=====
97
98 In a configurable fail-over section, each of these codes may be listed, with a value. If the code is not listed, or a configurable fail-over section is not defined, then values that make sense for the requested "group" (group, redundant, load-balance, etc) are used.
99
100 The special code "default" can be used to set all return codes to the specified value. This value will be used with a lower priority than ones that are explicitly set.
101
102 The values for each code may be one of the following:
103
104 [options="header"]
105 [cols="30%,70%"]
106 |=====
107 | Value | Meaning
108 | <number> | Priority for this return code.
109 | return | Stop processing this configurable fail-over list.
110 | reject | Stop processing this configurable fail-over list and immediately return a reject.
111 |=====
112
113 The <number> used for a value may be any decimal number between 1 and 99999. The number is used when processing a list of modules, to determine which code is returned from the list. For example, if "module1" returns "fail" with priority "1", and a later "module2" returns "ok" with priority "3", the return code from the list of modules will be "ok", because it has higher priority than "fail".
114
115 This configurability allows the administrator to permit some modules to fail, so long as a later module succeeds.
116
117 == More Complex Configurations
118
119 The "authorize" section is normally a list of module names. We can create sub-lists by using the section name "group". The "redundant" section above is just a short-hand for "group", with a set of default return codes, which are different than the normal "stop processing the list on failure".
120
121 For example, we can configure two detail modules, and allow either to fail, so long as one of them succeeds.
122 ----
123 # Handle accounting packets
124 accounting {
125 group {
126 detail1 {
127 fail = 1 # remember "fail" with priority 1
128 ok = return # if we succeed, don't do "detail2"
129 }
130 detail2 {
131 fail = 1 # remember "fail" with priority 1
132 ok = return # if we succeed, return "ok"
133 # if "detail1" returned "fail"
134 }
135 } # returns "fail" only if BOTH modules returned "fail"
136 redundant {
137 sql1
138 sql2
139 handled
140 }
141 }
142 ----
143
144 This configuration says:
145
146 - Log to "detail1", and stop processing the "group" list if "detail1" returned OK.
147
148 - If "detail1" returned "fail", then continue, but remember the "fail" code, with priority 1.
149
150 - If "detail2" fails, then remember "fail" with priority 1.
151
152 - If "detail2" returned "ok", return "ok" from the "group".
153
154 The return code from the "group" is the return code which was either forced to return (e.g. "ok" for "detail1"), or the highest priority
155 return code found by processing the list.
156
157 This process can be extended to any number of modules listed in a "group" section.
158
159 == More Complex Configuration using "if" and "else"
160
161 As of version 2.0, the server allows "if"-style checking in the configuration sections. The section is still processed as a list, so there is no looping or "goto" support. But by using "if", the administrator can have branching paths of execution, where none was possible before.
162
163 The "if" syntax added in 2.0.0-pre0 has been completely re-written in 2.0.0-pre2, to add major new functionality. Documentation will be updated later...
164
165 == Virtual Modules
166
167 Some configurations may require using the same list of modules, in the same order, in multiple sections. For those systems, the configuration can be simplified through the use of "virtual" modules. These modules are configured as named sub-sections of the "instantiate" section, as follows::
168 ----
169 instantiate {
170 ...
171
172 redundant sql1_or_2 {
173 sql1
174 sql2
175 }
176 }
177 ----
178
179 The name "sql1_or_2" can then be used in any other section, such as "authorize" or "accounting". The result will be exactly as if that section was placed at the location of the "sql1_or_2" reference.
180
181 These virtual modules are full-fledged objects in and of themselves. One virtual module can refer to another virtual module, and they can contain "if" conditions, or any other configuration permitted in a section.
182
183 == Redundancy and Load-Balancing
184
185 See load balancing for information on simple redundancy (fail-over) and load balancing.
186
187 == The Gory Details
188
189 The fundamental object is called a MODCALLABLE, because it is something that can be passed a specific radius request and returns one of the RLM_MODULE_* results. It is a function - if you can accept the fact that pieces of radiusd.conf are functions. There are two kinds of MODCALLABLEs: GROUPs and SINGLEs.
190
191 A SINGLE is a reference to a module instance that was set up in the modules{} section of radiusd.conf, like "preprocess" or "sql1". When a SINGLE is called, the corresponding function in the rlm is invoked, and whichever RLM_MODULE_* it returns becomes the RESULT of the SINGLE.
192
193 A GROUP is a section of radiusd.conf that includes some MODCALLABLEs. Examples of GROUPs above include "authorize{...}", which implements the C function module_authorize, and "redundant{...}", which contains two SINGLEs that refer to a couple of redundant databases. Note that a GROUP can contain other GROUPs - "Auth-Type SQL{...}" is also a GROUP, which implements the C function module_authenticate when Auth-Type is set to SQL.
194
195 Now here's the fun part - what happens when a GROUP is called? It simply runs through all of its children in order, and calls each one, whether it is another GROUP or a SINGLE. It then looks at the RESULT of that child, and takes some ACTION, which is basically either "return that RESULT immediately" or "Keep going". In the first example, any "bad" RESULT from the preprocess module causes an immediate return, and any "good" RESULT causes the authorize{...} GROUP to proceed to the files module.
196
197 We can see the exact rules by writing them out the long way::
198 ----
199 authorize {
200 preprocess {
201 notfound = 1
202 noop = 2
203 ok = 3
204 updated = 4
205 fail = return
206 reject = return
207 userlock = return
208 invalid = return
209 handled = return
210 }
211 files {
212 notfound = 1
213 noop = 2
214 ok = 3
215 updated = 4
216 fail = return
217 reject = return
218 userlock = return
219 invalid = return
220 handled = return
221 }
222 }
223 ----
224
225 This is the same as the first example, with the behavior explicitly spelled out. Each SINGLE becomes its own section, containing a list of RESULTs that it may return and what ACTION should follow from them. So preprocess is called, and if it returns for example RLM_MODULE_REJECT, then the reject=return rule is applied, and the authorize{...} GROUP itself immediately returns RLM_MODULE_REJECT.
226
227 If preprocess returns RLM_MODULE_NOOP, the corresponding ACTION is "2". An integer ACTION serves two purposes - first, it tells the parent GROUP to go on to the next module. Second, it is a hint as to how desirable this RESULT is as a candidate for the GROUP's own RESULT. So files is called... suppose it returns RLM_MODULE_NOTFOUND. The ACTION for notfound inside the files{...} block is "1". We have now reached the end of the authorize{...} GROUP and we look at the RESULTs we accumulated along the way - there is a noop with preference level 2, and a notfound with preference level 1, so the authorize{...} GROUP as a whole returns RLM_MODULE_NOOP, which makes sense because to say the user was not found at all would be a lie, since preprocess apparently found him, or else it would have returned RLM_MODULE_NOTFOUND too.
228
229 We could use the "default" code to simplify the above example a little. The following two configurations are identical::
230 ----
231 files {
232 notfound = 1
233 noop = 2
234 ok = 3
235 updated = 4
236 default = return
237 }
238 ----
239
240 When putting the "default" first, later definitions over-ride it's return code::
241 ----
242 files {
243 default = return
244 notfound = 1
245 noop = 2
246 ok = 3
247 updated = 4
248 }
249 ----
250
251 [Take a deep breath - the worst is over]
252
253 That RESULT preference/desirability stuff is pretty complex, but my hope is that it will be complex enough to handle the needs of everyone's real-world imperfect systems, while staying out of sight most of the time since the defaults will be right for the most common configurations.
254
255 So where does redundant{...} fit in with all that? Well, redundant{...} is simply a group that changes the default ACTIONs to something like::
256 ----
257 fail = 1
258 everythingelse = return
259 ----
260
261 so that when one module fails, we keep trying until we find one that doesn't fail, then return whatever it returned. And at the end, if they all failed, the redundant GROUP as a whole returns RLM_MODULE_FAIL, just as you'd want it to (I hope).
262
263 There are two other kinds of grouping: group{...} which does not have any specialized default ACTIONs, and append{...}, which should be used when you have separate but similarly structured databases that are guaranteed not to overlap.
264
265 That's all that really needs to be said. But now a few random notes:
266
267 * GROUPs may have RESULT=ACTION specifiers too! It would look like this:
268 ----
269 authorize {
270 preprocess
271 redundant {
272 sql1
273 sql2
274 notfound = return
275 }
276 files
277 }
278 ----
279
280 which would prevent rlm_files from being called if neither of the SQL instances could find the user.
281
282 * redundant{...} and append{...} are just shortcuts. You could write:
283 ----
284 group {
285 sql1 {
286 fail = 1
287 notfound = 2
288 noop = return
289 ok = return
290 updated = return
291 reject = return
292 userlock = return
293 invalid = return
294 handled = return
295 }
296 sql2 {
297 fail = 1
298 notfound = 2
299 noop = return
300 ok = return
301 updated = return
302 reject = return
303 userlock = return
304 invalid = return
305 handled = return
306 }
307 }
308 ----
309
310 instead of
311 ----
312 redundant {
313 sql1
314 sql2
315 }
316 ----
317
318 but the latter is just a whole lot easier to read.
319
320 * "authenticate{...}" itself is not a GROUP, even though it contains a list of Auth-Type GROUPs, because its semantics are totally different - it uses Auth-Type to decide which of its members to call, and their order is irrelevant.
321
322 * The default rules are context-sensitive - for authorize, the defaults are what you saw above - notfound, noop, ok, and updated are considered success, and anything else has an ACTION of "return". For authenticate, the default is to return on success *or* reject, and only try the second and following items if the first one fails. You can read all the default ACTIONs in modcall.c (int defaultactions[][][]), or just trust me. They do the right thing.
323
324 * There are some rules that can't be implemented in this language - things like "notfound = 1-reject", "noop = 2-ok", "ok = 3-ok", etc. But I don't feel justified adding that complexity in the first draft. There are already enough things here that may never see real-world usage. Like append{...}
325
326 == See Also
327
328 * load balancing
329
330 // Copyright (C) 2025 Network RADIUS SAS. Licenced under CC-by-NC 4.0.
331 // This documentation was developed by Network RADIUS SAS.