Skip to content

Commit

Permalink
New customerDB implementation #mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisfasquel committed May 3, 2018
1 parent cf8d50a commit 1279d87
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 86 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,4 +1,5 @@
# Binaries for programs and plugins
.DS_Store
*.exe
*.dll
*.so
Expand Down
54 changes: 36 additions & 18 deletions DNSAuth/customers.go
Expand Up @@ -3,45 +3,63 @@ package main
import (
"database/sql"
"log"
"github.com/asergeyev/nradix"
_ "github.com/go-sql-driver/mysql"
radix "github.com/armon/go-radix"
_"github.com/lib/pq"
)

type Customer struct {
Prefix string
Name string
PrefixMonit bool
ASNMonit bool
var DB_URL = "root:pass@(127.0.0.1)/customerdb"

// Function that reverse a word (test.com -> moc.tset)
func reverse(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
}

var DB_URL = "postgres://user@127.0.0.1/pipeline?sslmode=disable"
type CustomerDB struct {
tree *radix.Tree
}

func getCustomerTree() (*nradix.Tree, error) {
// Resolve the customer name from DNS qname
// Returns Unknown if not found
func (c *CustomerDB) Resolve(qname string) string {
name := "Unknown"
_, value, found := c.tree.LongestPrefix(reverse(qname))
if found {
name = value.(string)
}
return name
}

tree := nradix.NewTree(0)
// Init the customer DB. Connects to mysql to fetch all data and build a radix tree
func InitCustomerDB(path string) (*CustomerDB, error) {
db := &CustomerDB{
radix.New(),
}

db, err := sql.Open("postgres", DB_URL)
mysql, err := sql.Open("mysql", DB_URL)
if err != nil {
return nil, err
}

rows, err := db.Query("SELECT ip::cidr, name, asn, prefix FROM ns_customers;")
rows, err := mysql.Query("SELECT group_name, zone FROM zones;")
if err != nil {
return nil, err
}
defer rows.Close()


for rows.Next() {
c := Customer{}
err := rows.Scan(&c.Prefix, &c.Name, &c.ASNMonit, &c.PrefixMonit)
var name, zone string
err := rows.Scan(&name, &zone)
if err != nil {
log.Fatal(err)
}
err = tree.AddCIDR(c.Prefix, &c)
if err != nil {
log.Println(err)
}
db.tree.Insert(reverse(zone), name)
}
return tree, nil

return db, nil
}
4 changes: 2 additions & 2 deletions DNSAuth/dnsauth.toml
@@ -1,6 +1,6 @@
customer-db = "postgres://user:password@127.0.0.1/postgres?sslmode=disable"
customer-db = "root:pass@(127.0.0.1)/customerdb"
influx-db = "http://127.0.0.1:8086/write?db=authdns"
watch-dir = "/home/user/count"
watch-dir = "./"

#[bgp]
# router-id = “116.121.4.10"
Expand Down
39 changes: 8 additions & 31 deletions DNSAuth/main.go
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/Packet-Clearing-House/DNSAuth/libs/metrics"
"strconv"
"github.com/Packet-Clearing-House/DNSAuth/libs/dnsdist"
"github.com/asergeyev/nradix"
"github.com/Packet-Clearing-House/DNSAuth/DNSAuth/bgp"
)

Expand All @@ -36,7 +35,7 @@ var confpath = flag.String("c", "./dnsauth.toml", "Path for the config path (def


var dnsqueries = metrics.NewTTLTaggedMetrics("dnsauth_queries", []string{"direction", "pop", "qtype", "rcode", "customer", "protocol", "version", "prefix", "origin_as"}, 500)
var tree *nradix.Tree
var customerDB *CustomerDB

var BGP_LOOKUPS = false

Expand All @@ -54,9 +53,9 @@ func main() {
DB_URL = config.CustomerDB
INFLUX_URL = config.InfluxDB

log.Println("Getting customer list from postgres...")
t, err := getCustomerTree()
tree = t
log.Println("Getting customer list from mysql...")
customerDB, err = InitCustomerDB(DB_URL)

if err != nil {
log.Fatalln("FAILED: ", err)
}
Expand Down Expand Up @@ -98,13 +97,11 @@ func main() {
visit := func (path string, f os.FileInfo, err error) error {
if strings.HasSuffix(path, ".dmp.gz") {

if _, found := files[path]; found {
newFiles[path] = true
} else {
newFiles[path] = true
if _, found := files[path]; !found {
go aggreagate(path, limiter)
limiter <-true
}
newFiles[path] = true
}
return nil
}
Expand Down Expand Up @@ -214,36 +211,16 @@ func handleQuery(time time.Time, pop, line string) {

fields := strings.Fields(line)

name := "Unknown"
prefix := ""
originAs := ""
version := "4"

// Resolving destination address to client
c, _ := tree.FindCIDR(fields[NS_IP])



// If we do find a result...
if c != nil {
customer := c.(*Customer)
name = customer.Name
qname := fields[QNAME][:len(fields[QNAME])-1]
name := customerDB.Resolve(qname)

// ...resolving client ip through BGP
if BGP_LOOKUPS && (customer.PrefixMonit || customer.ASNMonit) {
entry, err := bgp.Resolve(fields[CLIENT_IP])
if err == nil {
// I SHOULD DO SOMETHING HERE #DEBUG?
originAs = strconv.Itoa(int(entry.Path[len(entry.Path) - 1]))
if customer.PrefixMonit {
prefix = entry.Prefix
}
}
}
}

if ipv := net.ParseIP(fields[CLIENT_IP]); ipv != nil {
log.Println(ipv)
if ipv.To4() == nil {
version = "6"
}
Expand Down
20 changes: 0 additions & 20 deletions DNSAuth/tests/SZC_mon-01.lga.example.com_2018-02-25.05-32.dmp

This file was deleted.

Binary file not shown.
2 changes: 2 additions & 0 deletions DNSAuth/tests/mon-01.xyz.foonet.net_2017-10-17.17-07.dmp
@@ -0,0 +1,2 @@
Q 2fd9:4d55:875b:bae4:b46f:51e1:5388:22a6 1501:0121:0800:0000:: 1 0 2 auction.com. 60
R 103.76.246.187 123.99.248.35 0 0 2 blabla.test.com. 595 0
Binary file not shown.
26 changes: 11 additions & 15 deletions README.md
Expand Up @@ -135,27 +135,23 @@ mkdir -p $HOME/go/{bin,pkg,src}
env GIT_TERMINAL_PROMPT=1 go get -u github.com/Packet-Clearing-House/DNSAuth/...
```

#### Postgres user and data
#### Mysql and local testing

Launch postgres CLI via `sudo -u postgres psql postgres` and then run this code:
Here are the instruction in order to run a local instance of Mysql for local testing.

```
DROP TABLE ns_customers;
CREATE TABLE ns_customers(
ip TEXT PRIMARY KEY NOT NULL,
name TEXT,
asn BOOL,
prefix BOOL
);
INSERT INTO ns_customers VALUES ('203.0.113.254/24', 'Foo', true, true);
INSERT INTO ns_customers VALUES ('2001:DB8::/32', 'Bar', true, true);
INSERT INTO ns_customers VALUES ('198.51.100.3/24', 'Bash', true, true);
docker pull mysql
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=pass -d mysql --default-authentication-plugin=mysql_native_password
CREATE USER "user" WITH PASSWORD 'password';
grant select on ns_customers to "user";
mysql --host=127.0.0.1 -uroot -ppass
mysql> CREATE DATABASE customers;
mysql> USE customers;
mysql> source customers.sql;
mysql> SELECT * FROM zones;
```

This will generate 3 dummy customers "Foo", "Bar" and "Bash". Create rows with your real customers when deploying to production.sh
This will generate 2 dummy customers "foo", "bar".

#### Set up influxdb

Expand Down
53 changes: 53 additions & 0 deletions customers.sql
@@ -0,0 +1,53 @@
-- MySQL dump 10.14 Distrib 5.5.56-MariaDB, for Linux (x86_64)
--
-- Host: db3.nuq.pch.net Database: dns_query
-- ------------------------------------------------------
-- Server version 5.5.28

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `zones`
--

DROP TABLE IF EXISTS `zones`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `zones` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`group_name` varchar(40) NOT NULL DEFAULT '',
`host` varchar(80) NOT NULL,
`zone` varchar(1024) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=81928 DEFAULT CHARSET=latin1 ROW_FORMAT=COMPACT;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `zones`
--

LOCK TABLES `zones` WRITE;
/*!40000 ALTER TABLE `zones` DISABLE KEYS */;
INSERT INTO zones VALUES (1,'foo','af-inbound.auction.com','auction.com'),(2,'bar','aasd-inbound.test.com','test.com');
/*!40000 ALTER TABLE `zones` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2018-03-19 18:11:55

0 comments on commit 1279d87

Please sign in to comment.