Code Monkey home page Code Monkey logo

coredns_mysql's Introduction

MySQL

MySQL backend for CoreDNS

Name

mysql - MySQL backend for CoreDNS

Description

This plugin uses MySQL as a backend to store DNS records. These will then can served by CoreDNS. The backend uses a simple, single table data structure that can be shared by other systems to add and remove records from the DNS server. As there is no state stored in the plugin, the service can be scaled out by spinning multiple instances of CoreDNS backed by the same database.

Syntax

mysql {
    dsn DSN
    [table_prefix TABLE_PREFIX]
    [max_lifetime MAX_LIFETIME]
    [max_open_connections MAX_OPEN_CONNECTIONS]
    [max_idle_connections MAX_IDLE_CONNECTIONS]
    [ttl DEFAULT_TTL]
    [zone_update_interval ZONE_UPDATE_INTERVAL]
}
  • dsn DSN for MySQL as per https://github.com/go-sql-driver/mysql examples. You can use $ENV_NAME format in the DSN, and it will be replaced with the environment variable value.
  • table_prefix Prefix for the MySQL tables. Defaults to coredns_.
  • max_lifetime Duration (in Golang format) for a SQL connection. Default is 1 minute.
  • max_open_connections Maximum number of open connections to the database server. Default is 10.
  • max_idle_connections Maximum number of idle connections in the database connection pool. Default is 10.
  • ttl Default TTL for records without a specified TTL in seconds. Default is 360 (seconds)
  • zone_update_interval Maximum time interval between loading all the zones from the database. Default is 10 minutes.

Supported Record Types

A, AAAA, CNAME, SOA, TXT, NS, MX, CAA and SRV. Wildcard records are supported as well. This backend doesn't support AXFR requests.

Setup (as an external plugin)

Add this as an external plugin in plugin.cfg file:

mysql:github.com/cloud66-oss/coredns_mysql

then run

$ go generate
$ go build

Add any required modules to CoreDNS code as prompted.

Database Setup

This plugin doesn't create or migrate database schema for its use yet. To create the database and tables, use the following table structure (note the table name prefix):

CREATE TABLE `coredns_records` (
    `id` INT NOT NULL AUTO_INCREMENT,
	`zone` VARCHAR(255) NOT NULL,
	`name` VARCHAR(255) NOT NULL,
	`ttl` INT DEFAULT NULL,
	`content` TEXT,
	`record_type` VARCHAR(255) NOT NULL,
	PRIMARY KEY (`id`)
) ENGINE = INNODB AUTO_INCREMENT = 6 DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci;

Record setup

Each record served by this plugin, should belong to the zone it is allowed to server by CoreDNS. Here are some examples:

-- Insert batch #1
INSERT INTO coredns_records (zone, name, ttl, content, record_type) VALUES
('example.org.', 'foo', 30, '{"ip": "1.1.1.1"}', 'A'),
('example.org.', 'foo', '60', '{"ip": "1.1.1.0"}', 'A'),
('example.org.', 'foo', 30, '{"text": "hello"}', 'TXT'),
('example.org.', 'foo', 30, '{"host" : "foo.example.org.","priority" : 10}', 'MX');

These can be queries using dig like this:

$ dig A MX foo.example.org 

Acknowledgements and Credits

This plugin, is inspired by https://github.com/wenerme/coredns-pdsql and https://github.com/arvancloud/redis

Development

To develop this plugin further, make sure you can compile CoreDNS locally and get this repo (go get github.com/cloud66-oss/coredns_mysql). You can switch the CoreDNS mod file to look for the plugin code locally while you're developing it:

Put replace github.com/cloud66-oss/coredns_mysql => LOCAL_PATH_TO_THE_SOURCE_CODE at the end of the go.mod file in CoreDNS code.

Pull requests and bug reports are welcome!

coredns_mysql's People

Contributors

khash avatar lvangool avatar marten-seemann avatar shadow3x3x3 avatar victory460 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

coredns_mysql's Issues

* and @ domain name resolution are not supported

example:
INSERT INTO coredns_records (zone, name, ttl, content, record_type) VALUES
('example.org.', '@', 30, '{"ip": "1.1.1.1"}', 'A'),
('example.org.', '*', '60', '{"ip": "1.1.1.0"}', 'A');

example.org A 1.1.1.1

abc.example.org A 1.1.1.0
123.example.org A 1.1.1.0
ccc.example.org A 1.1.1.0

@ in SOA or NS records

When you use an @ symbol in name field of an SOA or NS record its not found. You have to leave the name field blank. Just a bit confusing when converting from text zone files.

Not able to add RR to zone

Hi there,

first: Thanks for the plugin. Really nice and easy to get to work.

But I think I found a bug: Currently it is impossible to create records for a zone itself. Only for subdomains.

Based on your examples I am using foo.example.org as zone name.

When a request for "test.foo.example.org" is received, the completed name ist "test.foo.example.org."
"." + Zonename (".foo.example.org.") is stripped out. Thats right.
So the query is:

SELECT name, zone, ttl, record_type, content FROM coredns_records WHERE zone = 'foo.example.org.' AND name = 'test' AND record_type IN ('A')

But when you do a request for "foo.example.org" also ".foo.example.org." is tried to stripped out. This fails of course. The final query is:

SELECT name, zone, ttl, record_type, content FROM coredns_records WHERE zone = 'foo.example.org.' AND name = 'foo.example.org.' AND record_type IN ('A')

This does not find any records. If I try to use the data like in the query given the result will be for "foo.example.org.foo.example.org" which also causes an error.

I think the easiest solution is to set the name empty if the name matches the zone.

Thanks and greetings from Germany
Lukas

RCODE Response

2023-10-24 12:11:47 [INFO] 172.17.0.1:39435 - 31667 "A IN foo.mydomain.com. udp 39 false 512" NOERROR qr,aa,rd 76 0.040718436s
2023-10-24 12:12:00 [INFO] 172.17.0.1:41790 - 11714 "A IN boo.mydomain.com. udp 39 false 512" NOERROR qr,aa,rd 140 0.109066749s

Foo exists while Boo doesn't. Same RCODE response. Should the Boo query failing give a different RCODE? One like NXDOMAIN or NOTZONE? Without a different code the "alternative" plugin will not work. It uses the RCODE to forward failed queries.

NOERROR while use

Hi, I would like to ask which version of the coredns source code this plugin is based on? After compiling coredns-1.11 successfully, I can connect to mysql, and the debug "records" have extracted data from mysql, but it still timeout and the log error is NOERROR qr,aa,rd.

PTR record

Hi guys,
How can I add a PTR record in MySQL DB?

SOA records are not being correctly delivered.

I have spent over a week trying to understand "go" and how this plugin works. I can't quite figure out how to fix it.

Its not showing the authoritative name servers when doing an SOA dig.

If I do a dig @0.0.0.0 SOA example.org with the "file" plugin I get the ANSWER SECTION and the AUTHORITY SECTION:

;; ANSWER SECTION:
example.org.            3600    IN      SOA     ns1.example.org. email.example.org. 2023022400 600 1800 2419200 600

;; AUTHORITY SECTION:
example.org.            3600    IN      NS      ns1.example.org.
example.org.            3600    IN      NS      ns2.example.org.

;; Query time: 1 msec
;; SERVER: 127.0.0.1#53(0.0.0.0)
;; WHEN: Fri Feb 24 16:07:47 CST 2023
;; MSG SIZE  rcvd: 199

If I do a dig @0.0.0.0 SOA example.org with the "mysql" plugin I get the ANSWER SECTION but NOT the AUTHORITY SECTION:

;; ANSWER SECTION:
example.org.            30      IN      SOA     ns1.example.org. email.example.org. 1677276345 600 1800 2419200 600

;; Query time: 3 msec
;; SERVER: 127.0.0.1#53(0.0.0.0)
;; WHEN: Fri Feb 24 16:05:45 CST 2023
;; MSG SIZE  rcvd: 119

Here is my Corefile (I am commenting out the plugin that I am not using to test) :

. {
    forward . 8.8.8.8 9.9.9.9
    log
    errors
    cache
    debug
    reload
}

example.org {
    file corednsconf/db.example.org
    # mysql {
    #     dsn root:password@tcp(192.168.40.198:3306)/coredns
    # }
    log
    errors
    debug
}

Attached are the zone file and the sql file that I am using to test with.
coredns_2023-02-24.sql.zip
db.example.org.zip

Example

Hello,

Is this how I would server more than one domain from the same database?

This is my current Corefile...

. {
    forward . 8.8.8.8 9.9.9.9
    log
    errors
    debug
    cache
    reload
}

example.org {
    mysql {
        dsn root:XXXXXXXXXX@tcp(coredns.us-east-1.rds.amazonaws.com:3306)/coredns
    }
    log
    errors
    debug
}

example.com {
    mysql {
        dsn root:XXXXXXXXXX@tcp(coredns.us-east-1.rds.amazonaws.com:3306)/coredns
    }
    log
    errors
    debug
}

I'll move the dsn to an environment variable later, I am just trying to do a POC.

Thanks for any help.

Chris

SRV Lookup Timeout

Hello,

thanks for Working on this nice Plugin.
I try to setup it up for Prometheus DNS service discovery.
When I try to query a SRV Record the request just runs into an timeout.
MariaDB [dns]> select * from coredns_records;

id zone name ttl content record_type
21 mon.local. nodeexporter._tcp 30 {"Target":"router01.net.mon.local","Weight":0,"Port":9100,"Priority":10} SRV
22 mon.local. router01.net 30 {"ip": "1.1.1.1"} A

When I try to query the SRV Record I get an Timeout on every DNS Client I tested. The A Record works without an Problem.
In the Log I just see three Requests:

[INFO] [IPV6_ADDRESS]:52152 - 30764 "SRV IN nodeexporter._tcp.mon.local. udp 68 false 4096" NOERROR qr,aa,rd 136 0.001510555s
[INFO] [IPV6_ADDRESS]:52152 - 30764 "SRV IN nodeexporter._tcp.mon.local. udp 68 false 4096" NOERROR qr,aa,rd 136 0.001565826s
[INFO] [IPV6_ADDRESS]:52152 - 30764 "SRV IN nodeexporter._tcp.mon.local. udp 68 false 4096" NOERROR qr,aa,rd 136 0.002095277s

Corefile:

.:53 {
    forward . 149.112.112.112 2620:fe::fe 185.95.218.42 2a05:fc84::43 5.1.66.255  2001:678:ed0:f000::
    prometheus [::]:9253
    errors
    log
    debug
}
mon.local.:53 {
mysql {
    dsn coredns:coredns@tcp(127.0.0.1:3306)/dns
    }
    errors
    log
    debug
}

Could you please look into this Problem?

MySQL query fails on NS records that are not FQDNs.

IF I have a NS record that isn't FQDN with a trailing period I get an error.

504 cloud.com. 3600 {"host":"infr-pdnsn-401"} NS

dig @localhost NS cloud.com

; <<>> DiG 9.10.6 <<>> @localhost NS cloud.com
; (2 servers found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: SERVFAIL, id: 30356
;; flags: qr rd; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;cloud.com.             IN      NS

;; Query time: 27 msec
;; SERVER: ::1#53(::1)
;; WHEN: Fri Feb 10 09:32:05 CST 2023
;; MSG SIZE  rcvd: 46

If I add the FQDN it works as expected.

504 cloud.com. 3600 {"host":"infr-pdnsn-401.cloud.com."} NS

incorrect handling of requests for the zone

Requesting any record type for domain.tld doesn't work, for 2 reasons:

  1. The database request is sent for zone equal domain.tld AND name equal domain.tld. name should be an empty string.
  2. The response (FQDN) is incorrectly formatted, it is returned for .domain.tld, where it should have been returned for domain.tld.

Wild Card Support

Hello ,

Are there any plans for wildcard support soon ?
It is the only feature missing that prevents me from picking this up :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.