[rewrite] Introduce cname target rewrite rule to rewrite plugin (#6004)

* cname target rewrite part in answer sec
tion

Signed-off-by: amila <amila.15@cse.mrt.ac.lk>

* upstream request

Signed-off-by: amila <amila.15@cse.mrt.ac.lk>

* fix looping issue

Signed-off-by: amila <amila.15@cse.mrt.ac.lk>

* support exact, prefix, suffix, substring, and regex types for cname rewrite

Signed-off-by: amila <amila.15@cse.mrt.ac.lk>

* support any qtype, corrected prefix, suffix, substring types behavior

Signed-off-by: amila <amila.15@cse.mrt.ac.lk>

* unit tests added, mocked the upstream call

Signed-off-by: amila <amila.15@cse.mrt.ac.lk>

* fix lint errors

Signed-off-by: amila <amila.15@cse.mrt.ac.lk>

* add newline to fix test issue

Signed-off-by: amila <amila.15@cse.mrt.ac.lk>

* add default rewrite type, add readme

Signed-off-by: amila <amila.15@cse.mrt.ac.lk>

* readme grammar fix

Signed-off-by: amila <amila.15@cse.mrt.ac.lk>

* reuse rewrite types

Signed-off-by: amila <amila.15@cse.mrt.ac.lk>

* comment fixed

Signed-off-by: amila <amila.15@cse.mrt.ac.lk>

---------

Signed-off-by: amila <amila.15@cse.mrt.ac.lk>
This commit is contained in:
Amila Senadheera
2023-04-13 17:49:36 +05:30
committed by GitHub
parent 0063d7a80c
commit 8e8231d627
7 changed files with 362 additions and 5 deletions

View File

@@ -25,6 +25,7 @@ e.g., to rewrite ANY queries to HINFO, use `rewrite type ANY HINFO`.
* `class` - the class of the message will be rewritten. FROM/TO must be a DNS class type (`IN`, `CH`, or `HS`); e.g., to rewrite CH queries to IN use `rewrite class CH IN`.
* `edns0` - an EDNS0 option can be appended to the request as described below in the **EDNS0 Options** section.
* `ttl` - the TTL value in the _response_ is rewritten.
* `cname` - the CNAME target if the response has a CNAME record
* **TYPE** this optional element can be specified for a `name` or `ttl` field.
If not given type `exact` will be assumed. If options should be specified the
@@ -404,3 +405,49 @@ rewrite edns0 subnet set 24 56
* If the query's source IP address is an IPv4 address, the first 24 bits in the IP will be the network subnet.
* If the query's source IP address is an IPv6 address, the first 56 bits in the IP will be the network subnet.
### CNAME Feild Rewrites
There might be a scenario where you want the `CNAME` target of the response to be rewritten. You can do this by using the `CNAME` field rewrite. This will generate new answer records according to the new `CNAME` target.
The syntax for the CNAME rewrite rule is as follows. The meaning of
`exact|prefix|suffix|substring|regex` is the same as with the name rewrite rules.
An omitted type is defaulted to `exact`.
```
rewrite [continue|stop] cname [exact|prefix|suffix|substring|regex] FROM TO
```
Consider the following `CNAME` rewrite rule with regex type.
```
rewrite cname regex (.*).cdn.example.net. {1}.other.cdn.com.
```
If you were to send the following DNS request without the above rule, an example response would be:
```
$ dig @10.1.1.1 my-app.com
;; QUESTION SECTION:
;my-app.com. IN A
;; ANSWER SECTION:
my-app.com. 200 IN CNAME my-app.com.cdn.example.net.
my-app.com.cdn.example.net. 300 IN A 20.2.0.1
my-app.com.cdn.example.net. 300 IN A 20.2.0.2
```
If you were to send the same DNS request with the above rule set up, an example response would be:
```
$ dig @10.1.1.1 my-app.com
;; QUESTION SECTION:
;my-app.com. IN A
;; ANSWER SECTION:
my-app.com. 200 IN CNAME my-app.com.other.cdn.com.
my-app.com.other.cdn.com. 100 IN A 30.3.1.2
```
Note that the answer will contain a completely different set of answer records after rewriting the `CNAME` target.