mirror of
https://github.com/gravitl/netmaker.git
synced 2025-02-26 00:53:23 +08:00
530 lines
12 KiB
Go
530 lines
12 KiB
Go
package controller
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
"github.com/gorilla/mux"
|
|
"github.com/gravitl/netmaker/functions"
|
|
"github.com/gravitl/netmaker/models"
|
|
"github.com/gravitl/netmaker/mongoconn"
|
|
"github.com/txn2/txeh"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
func dnsHandlers(r *mux.Router) {
|
|
|
|
r.HandleFunc("/api/dns", securityCheck(http.HandlerFunc(getAllDNS))).Methods("GET")
|
|
r.HandleFunc("/api/dns/adm/{network}/nodes", securityCheck(http.HandlerFunc(getNodeDNS))).Methods("GET")
|
|
r.HandleFunc("/api/dns/adm/{network}/custom", securityCheck(http.HandlerFunc(getCustomDNS))).Methods("GET")
|
|
r.HandleFunc("/api/dns/adm/{network}", securityCheck(http.HandlerFunc(getDNS))).Methods("GET")
|
|
r.HandleFunc("/api/dns/{network}", securityCheck(http.HandlerFunc(createDNS))).Methods("POST")
|
|
r.HandleFunc("/api/dns/adm/pushdns", securityCheck(http.HandlerFunc(pushDNS))).Methods("POST")
|
|
r.HandleFunc("/api/dns/{network}/{domain}", securityCheck(http.HandlerFunc(deleteDNS))).Methods("DELETE")
|
|
r.HandleFunc("/api/dns/{network}/{domain}", securityCheck(http.HandlerFunc(updateDNS))).Methods("PUT")
|
|
}
|
|
|
|
//Gets all nodes associated with network, including pending nodes
|
|
func getNodeDNS(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
var dns []models.DNSEntry
|
|
var params = mux.Vars(r)
|
|
|
|
dns, err := GetNodeDNS(params["network"])
|
|
if err != nil {
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
return
|
|
}
|
|
|
|
//Returns all the nodes in JSON format
|
|
w.WriteHeader(http.StatusOK)
|
|
json.NewEncoder(w).Encode(dns)
|
|
}
|
|
|
|
//Gets all nodes associated with network, including pending nodes
|
|
func getAllDNS(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
dns, err := GetAllDNS()
|
|
if err != nil {
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
return
|
|
}
|
|
//Returns all the nodes in JSON format
|
|
w.WriteHeader(http.StatusOK)
|
|
json.NewEncoder(w).Encode(dns)
|
|
}
|
|
|
|
func GetAllDNS() ([]models.DNSEntry, error) {
|
|
var dns []models.DNSEntry
|
|
networks, err := functions.ListNetworks()
|
|
if err != nil {
|
|
return []models.DNSEntry{}, err
|
|
}
|
|
for _, net := range networks {
|
|
netdns, err := GetDNS(net.NetID)
|
|
if err != nil {
|
|
return []models.DNSEntry{}, nil
|
|
}
|
|
dns = append(dns, netdns...)
|
|
}
|
|
return dns, nil
|
|
}
|
|
|
|
func GetNodeDNS(network string) ([]models.DNSEntry, error) {
|
|
|
|
var dns []models.DNSEntry
|
|
|
|
collection := mongoconn.Client.Database("netmaker").Collection("nodes")
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
filter := bson.M{"network": network}
|
|
|
|
cur, err := collection.Find(ctx, filter, options.Find().SetProjection(bson.M{"_id": 0}))
|
|
|
|
if err != nil {
|
|
return dns, err
|
|
}
|
|
|
|
defer cancel()
|
|
|
|
for cur.Next(context.TODO()) {
|
|
|
|
var entry models.DNSEntry
|
|
|
|
err := cur.Decode(&entry)
|
|
if err != nil {
|
|
return dns, err
|
|
}
|
|
|
|
// add item our array of nodes
|
|
dns = append(dns, entry)
|
|
}
|
|
|
|
//TODO: Another fatal error we should take care of.
|
|
if err := cur.Err(); err != nil {
|
|
return dns, err
|
|
}
|
|
|
|
return dns, err
|
|
}
|
|
|
|
//Gets all nodes associated with network, including pending nodes
|
|
func getCustomDNS(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
var dns []models.DNSEntry
|
|
var params = mux.Vars(r)
|
|
|
|
dns, err := GetCustomDNS(params["network"])
|
|
if err != nil {
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
return
|
|
}
|
|
|
|
//Returns all the nodes in JSON format
|
|
w.WriteHeader(http.StatusOK)
|
|
json.NewEncoder(w).Encode(dns)
|
|
}
|
|
|
|
func GetCustomDNS(network string) ([]models.DNSEntry, error) {
|
|
|
|
var dns []models.DNSEntry
|
|
|
|
collection := mongoconn.Client.Database("netmaker").Collection("dns")
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
filter := bson.M{"network": network}
|
|
|
|
cur, err := collection.Find(ctx, filter, options.Find().SetProjection(bson.M{"_id": 0}))
|
|
|
|
if err != nil {
|
|
return dns, err
|
|
}
|
|
|
|
defer cancel()
|
|
|
|
for cur.Next(context.TODO()) {
|
|
|
|
var entry models.DNSEntry
|
|
|
|
err := cur.Decode(&entry)
|
|
if err != nil {
|
|
return dns, err
|
|
}
|
|
|
|
// add item our array of nodes
|
|
dns = append(dns, entry)
|
|
}
|
|
|
|
//TODO: Another fatal error we should take care of.
|
|
if err := cur.Err(); err != nil {
|
|
return dns, err
|
|
}
|
|
|
|
return dns, err
|
|
}
|
|
|
|
func SetDNS() error {
|
|
hostfile := txeh.Hosts{}
|
|
var corefilestring string
|
|
networks, err := functions.ListNetworks()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, net := range networks {
|
|
corefilestring = corefilestring + net.NetID + " "
|
|
dns, err := GetDNS(net.NetID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, entry := range dns {
|
|
hostfile.AddHost(entry.Address, entry.Name+"."+entry.Network)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
if corefilestring == "" {
|
|
corefilestring = "example.com"
|
|
}
|
|
|
|
err = hostfile.SaveAs("./config/dnsconfig/netmaker.hosts")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = functions.SetCorefile(corefilestring)
|
|
|
|
return err
|
|
}
|
|
|
|
func GetDNSEntryNum(domain string, network string) (int, error) {
|
|
|
|
num := 0
|
|
|
|
entries, err := GetDNS(network)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
for i := 0; i < len(entries); i++ {
|
|
|
|
if domain == entries[i].Name {
|
|
num++
|
|
}
|
|
}
|
|
|
|
return num, nil
|
|
}
|
|
|
|
//Gets all nodes associated with network, including pending nodes
|
|
func getDNS(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
var dns []models.DNSEntry
|
|
var params = mux.Vars(r)
|
|
|
|
dns, err := GetDNS(params["network"])
|
|
if err != nil {
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusOK)
|
|
json.NewEncoder(w).Encode(dns)
|
|
}
|
|
|
|
func GetDNS(network string) ([]models.DNSEntry, error) {
|
|
|
|
var dns []models.DNSEntry
|
|
dns, err := GetNodeDNS(network)
|
|
if err != nil {
|
|
return dns, err
|
|
}
|
|
customdns, err := GetCustomDNS(network)
|
|
if err != nil {
|
|
return dns, err
|
|
}
|
|
|
|
dns = append(dns, customdns...)
|
|
return dns, err
|
|
}
|
|
|
|
func createDNS(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
var entry models.DNSEntry
|
|
var params = mux.Vars(r)
|
|
|
|
//get node from body of request
|
|
_ = json.NewDecoder(r.Body).Decode(&entry)
|
|
entry.Network = params["network"]
|
|
|
|
err := ValidateDNSCreate(entry)
|
|
if err != nil {
|
|
returnErrorResponse(w, r, formatError(err, "badrequest"))
|
|
return
|
|
}
|
|
|
|
entry, err = CreateDNS(entry)
|
|
if err != nil {
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusOK)
|
|
json.NewEncoder(w).Encode(entry)
|
|
}
|
|
|
|
func updateDNS(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
var params = mux.Vars(r)
|
|
|
|
var entry models.DNSEntry
|
|
|
|
//start here
|
|
entry, err := GetDNSEntry(params["domain"], params["network"])
|
|
if err != nil {
|
|
returnErrorResponse(w, r, formatError(err, "badrequest"))
|
|
return
|
|
}
|
|
|
|
var dnschange models.DNSEntry
|
|
|
|
// we decode our body request params
|
|
err = json.NewDecoder(r.Body).Decode(&dnschange)
|
|
if err != nil {
|
|
returnErrorResponse(w, r, formatError(err, "badrequest"))
|
|
return
|
|
}
|
|
//fill in any missing fields
|
|
if dnschange.Name == "" {
|
|
dnschange.Name = entry.Name
|
|
}
|
|
if dnschange.Network == "" {
|
|
dnschange.Network = entry.Network
|
|
}
|
|
if dnschange.Address == "" {
|
|
dnschange.Address = entry.Address
|
|
}
|
|
|
|
err = ValidateDNSUpdate(dnschange, entry)
|
|
|
|
if err != nil {
|
|
returnErrorResponse(w, r, formatError(err, "badrequest"))
|
|
return
|
|
}
|
|
|
|
entry, err = UpdateDNS(dnschange, entry)
|
|
|
|
if err != nil {
|
|
returnErrorResponse(w, r, formatError(err, "badrequest"))
|
|
return
|
|
}
|
|
|
|
json.NewEncoder(w).Encode(entry)
|
|
}
|
|
|
|
func deleteDNS(w http.ResponseWriter, r *http.Request) {
|
|
// Set header
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
// get params
|
|
var params = mux.Vars(r)
|
|
|
|
success, err := DeleteDNS(params["domain"], params["network"])
|
|
|
|
if err != nil {
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
return
|
|
} else if !success {
|
|
returnErrorResponse(w, r, formatError(errors.New("Delete unsuccessful."), "badrequest"))
|
|
return
|
|
}
|
|
|
|
json.NewEncoder(w).Encode(params["domain"] + " deleted.")
|
|
}
|
|
|
|
func CreateDNS(entry models.DNSEntry) (models.DNSEntry, error) {
|
|
|
|
// connect db
|
|
collection := mongoconn.Client.Database("netmaker").Collection("dns")
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
// insert our node to the node db.
|
|
_, err := collection.InsertOne(ctx, entry)
|
|
|
|
defer cancel()
|
|
|
|
return entry, err
|
|
}
|
|
|
|
func GetDNSEntry(domain string, network string) (models.DNSEntry, error) {
|
|
var entry models.DNSEntry
|
|
|
|
collection := mongoconn.Client.Database("netmaker").Collection("dns")
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
filter := bson.M{"name": domain, "network": network}
|
|
err := collection.FindOne(ctx, filter, options.FindOne().SetProjection(bson.M{"_id": 0})).Decode(&entry)
|
|
|
|
defer cancel()
|
|
|
|
return entry, err
|
|
}
|
|
|
|
func UpdateDNS(dnschange models.DNSEntry, entry models.DNSEntry) (models.DNSEntry, error) {
|
|
|
|
queryDNS := entry.Name
|
|
|
|
if dnschange.Name != "" {
|
|
entry.Name = dnschange.Name
|
|
}
|
|
if dnschange.Address != "" {
|
|
entry.Address = dnschange.Address
|
|
}
|
|
//collection := mongoconn.ConnectDB()
|
|
collection := mongoconn.Client.Database("netmaker").Collection("dns")
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
// Create filter
|
|
filter := bson.M{"name": queryDNS}
|
|
|
|
// prepare update model.
|
|
update := bson.D{
|
|
{"$set", bson.D{
|
|
{"name", entry.Name},
|
|
{"address", entry.Address},
|
|
}},
|
|
}
|
|
var dnsupdate models.DNSEntry
|
|
|
|
errN := collection.FindOneAndUpdate(ctx, filter, update).Decode(&dnsupdate)
|
|
if errN != nil {
|
|
fmt.Println("Could not update: ")
|
|
fmt.Println(errN)
|
|
} else {
|
|
fmt.Println("DNS Entry updated successfully.")
|
|
}
|
|
|
|
defer cancel()
|
|
|
|
return dnsupdate, errN
|
|
}
|
|
|
|
func DeleteDNS(domain string, network string) (bool, error) {
|
|
fmt.Println("delete dns entry ", domain, network)
|
|
deleted := false
|
|
|
|
collection := mongoconn.Client.Database("netmaker").Collection("dns")
|
|
|
|
filter := bson.M{"name": domain, "network": network}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
result, err := collection.DeleteOne(ctx, filter)
|
|
|
|
deletecount := result.DeletedCount
|
|
|
|
if deletecount > 0 {
|
|
deleted = true
|
|
}
|
|
|
|
defer cancel()
|
|
|
|
return deleted, err
|
|
}
|
|
|
|
func pushDNS(w http.ResponseWriter, r *http.Request) {
|
|
// Set header
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
err := SetDNS()
|
|
|
|
if err != nil {
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
return
|
|
}
|
|
json.NewEncoder(w).Encode("DNS Pushed to CoreDNS")
|
|
}
|
|
|
|
func ValidateDNSCreate(entry models.DNSEntry) error {
|
|
|
|
v := validator.New()
|
|
fmt.Println("Validating DNS: " + entry.Name)
|
|
fmt.Println(" Address: " + entry.Address)
|
|
fmt.Println(" Network: " + entry.Network)
|
|
|
|
_ = v.RegisterValidation("name_unique", func(fl validator.FieldLevel) bool {
|
|
num, err := GetDNSEntryNum(entry.Name, entry.Network)
|
|
return err == nil && num == 0
|
|
})
|
|
|
|
_ = v.RegisterValidation("network_exists", func(fl validator.FieldLevel) bool {
|
|
_, err := functions.GetParentNetwork(entry.Network)
|
|
return err == nil
|
|
})
|
|
|
|
err := v.Struct(entry)
|
|
if err != nil {
|
|
for _, e := range err.(validator.ValidationErrors) {
|
|
fmt.Println(e)
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
func ValidateDNSUpdate(change models.DNSEntry, entry models.DNSEntry) error {
|
|
|
|
v := validator.New()
|
|
|
|
_ = v.RegisterValidation("name_unique", func(fl validator.FieldLevel) bool {
|
|
//if name & net not changing name we are good
|
|
if change.Name == entry.Name && change.Network == entry.Network {
|
|
return true
|
|
}
|
|
num, err := GetDNSEntryNum(change.Name, change.Network)
|
|
return err == nil && num == 0
|
|
})
|
|
_ = v.RegisterValidation("network_exists", func(fl validator.FieldLevel) bool {
|
|
_, err := functions.GetParentNetwork(change.Network)
|
|
fmt.Println(err, entry.Network)
|
|
return err == nil
|
|
})
|
|
|
|
// _ = v.RegisterValidation("name_valid", func(fl validator.FieldLevel) bool {
|
|
// isvalid := functions.NameInDNSCharSet(entry.Name)
|
|
// notEmptyCheck := entry.Name != ""
|
|
// return isvalid && notEmptyCheck
|
|
// })
|
|
//
|
|
// _ = v.RegisterValidation("address_valid", func(fl validator.FieldLevel) bool {
|
|
// isValid := true
|
|
// if entry.Address != "" {
|
|
// isValid = functions.IsIpNet(entry.Address)
|
|
// }
|
|
// return isValid
|
|
// })
|
|
|
|
err := v.Struct(change)
|
|
|
|
if err != nil {
|
|
for _, e := range err.(validator.ValidationErrors) {
|
|
fmt.Println(e)
|
|
}
|
|
}
|
|
return err
|
|
}
|