snail2sky / coredns_mysql_extend

connect mysql plugin of coredns
Apache License 2.0
8 stars 2 forks source link

use mysql & template module together #3

Closed mm3906078 closed 6 months ago

mm3906078 commented 6 months ago

Hi guys, I want if the records are not in MySQL return it with the template module like the config below:

in-addr.arpa.:53 {
    log
    errors
    mysql {
        dsn user:pass@tcp(mysql:3306)/coredns
    }
    template IN PTR in-addr.arpa {
        match "^(?P<a>[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.](?P<b>[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.](?P<c>[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.](?P<d>[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]in-addr[.]arpa[.]$"
        answer "{{ .Name }} 86400 IN PTR {{ .Group.d }}-{{ .Group.c }}-{{ .Group.b }}-{{ .Group.a }}.static.test.com"
        fallthrough
    }
}
mysql> select * from zones;
+----+---------------+
| id | zone_name     |
+----+---------------+
| 11 | in-addr.arpa. |
+----+---------------+
1 row in set (0.00 sec)

mysql> select * from records;
+----+---------+----------+------+-------------+-----+--------+
| id | zone_id | hostname | type | data        | ttl | online |
+----+---------+----------+------+-------------+-----+--------+
|  8 |      11 | 1.1.1.1  | PTR  | address.com | 120 |      1 |
+----+---------+----------+------+-------------+-----+--------+
1 row in set (0.00 sec)

mysql>
(.venv) ➜  ansible git:(coredns) ✗ dig @10.1.1.3 -x 1.1.1.1

; <<>> DiG 9.18.18-0ubuntu0.22.04.1-Ubuntu <<>> @10.1.1.3 -x 1.1.1.1
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 60231
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: d0ad108a5f6b5713 (echoed)
;; QUESTION SECTION:
;1.1.1.1.in-addr.arpa.          IN      PTR

;; ANSWER SECTION:
1.1.1.1.in-addr.arpa.   86400   IN      PTR     1-1-1-1.static.test.com.

;; Query time: 108 msec
;; SERVER: 10.1.1.3#53(10.1.1.3) (UDP)
;; WHEN: Tue Jan 09 19:42:08 UTC 2024
;; MSG SIZE  rcvd: 121

but the problem is template module responded even if records are on the DB. Anyone with any idea?

snail2sky commented 6 months ago

Hi guys, I want if the records are not in MySQL return it with the template module like the config below:

in-addr.arpa.:53 {
    log
    errors
    mysql {
        dsn user:pass@tcp(mysql:3306)/coredns
    }
    template IN PTR in-addr.arpa {
        match "^(?P<a>[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.](?P<b>[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.](?P<c>[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.](?P<d>[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]in-addr[.]arpa[.]$"
        answer "{{ .Name }} 86400 IN PTR {{ .Group.d }}-{{ .Group.c }}-{{ .Group.b }}-{{ .Group.a }}.static.test.com"
        fallthrough
    }
}
mysql> select * from zones;
+----+---------------+
| id | zone_name     |
+----+---------------+
| 11 | in-addr.arpa. |
+----+---------------+
1 row in set (0.00 sec)

mysql> select * from records;
+----+---------+----------+------+-------------+-----+--------+
| id | zone_id | hostname | type | data        | ttl | online |
+----+---------+----------+------+-------------+-----+--------+
|  8 |      11 | 1.1.1.1  | PTR  | address.com | 120 |      1 |
+----+---------+----------+------+-------------+-----+--------+
1 row in set (0.00 sec)

mysql>
(.venv) ➜  ansible git:(coredns) ✗ dig @10.1.1.3 -x 1.1.1.1

; <<>> DiG 9.18.18-0ubuntu0.22.04.1-Ubuntu <<>> @10.1.1.3 -x 1.1.1.1
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 60231
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: d0ad108a5f6b5713 (echoed)
;; QUESTION SECTION:
;1.1.1.1.in-addr.arpa.          IN      PTR

;; ANSWER SECTION:
1.1.1.1.in-addr.arpa.   86400   IN      PTR     1-1-1-1.static.test.com.

;; Query time: 108 msec
;; SERVER: 10.1.1.3#53(10.1.1.3) (UDP)
;; WHEN: Tue Jan 09 19:42:08 UTC 2024
;; MSG SIZE  rcvd: 121

but the problem is template module responded even if records are on the DB. Anyone with any idea?

There is indeed this problem, but it may not be a problem with coredns_mysql_extend. I will test it in detail tomorrow.

I generally understand your needs. You expect to use coredns for reverse parsing. If you define the parsing yourself, it will return normally. If it is not defined, it will return according to the specifications defined by the template. Is that so?

mm3906078 commented 6 months ago

Thanks @snail2sky for your response, I thought I was using the extension wrong. mabey i should open this issue in the Coredns project.

snail2sky commented 6 months ago

done