| 1 |
= Splitting strings |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
*Goal:* Explore uses of regular expressions and subcapture groups |
| 6 |
|
| 7 |
*Time:* 10-20 minutes |
| 8 |
|
| 9 |
*File:* |
| 10 |
|
| 11 |
- `etc/raddb/policy.d/*` |
| 12 |
|
| 13 |
*`man` page:* unlang |
| 14 |
|
| 15 |
*documentation page(s):* |
| 16 |
|
| 17 |
- xref:unlang:condition/index.adoc[Conditions] |
| 18 |
- xref:unlang:condition/regex.adoc[Regular expressions] |
| 19 |
- xref:unlang:update.adoc[The Update Statement] |
| 20 |
|
| 21 |
include::partial$unlang_start.adoc[] |
| 22 |
|
| 23 |
include::partial$common_control_attrs_sidebar.adoc[] |
| 24 |
|
| 25 |
Regular expressions are an extremely powerful tool in the 'unlang' policy |
| 26 |
language. They provide both validation capabilities, allowing users |
| 27 |
to check the format of incoming attributes, and substring extraction |
| 28 |
(via capture groups). |
| 29 |
|
| 30 |
If you've completed the xref:proxy.adoc[Proxy] exercise you'll have |
| 31 |
used the `suffix` module to split an incoming `User-Name` value into |
| 32 |
its components and setup the request for proxying. |
| 33 |
|
| 34 |
Create an unlang version of "suffix" that splits an incoming `User-Name` into |
| 35 |
two components on the "@" separator. |
| 36 |
|
| 37 |
The first component should be written to the `request.Stripped-User-Name` |
| 38 |
attribute and the second component should be written to the |
| 39 |
`control.Stripped-User-Domain` attribute. |
| 40 |
|
| 41 |
Use `[email protected]` and `[email protected]` to test your new policy to ensure |
| 42 |
it works as expected. |
| 43 |
|
| 44 |
If you've completed the xref:proxy.adoc[Proxy] tutorial and have test |
| 45 |
realms setup, modify the policy code you have just written to proxy |
| 46 |
the request to the realm specified in the `User-Name` attribute. |
| 47 |
|
| 48 |
== Questions |
| 49 |
|
| 50 |
1. Regular expressions can contain attribute expansions. Given that |
| 51 |
all supported regular expression libraries support pre-compilation |
| 52 |
of expressions, why would regular expressions containing expansions |
| 53 |
be avoided when the server being deployed will be under heavy load? |
| 54 |
2. Why might you want to re-implement functionality offered by modules |
| 55 |
in unlang? |
| 56 |
3. What is an advantage of using expression based string splitting |
| 57 |
over the suffix module? |
| 58 |
|
| 59 |
// Copyright (C) 2021 Network RADIUS SAS. Licenced under CC-by-NC 4.0. |
| 60 |
// This documentation was developed by Network RADIUS SAS. |