2021-12-08 01:46:55 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-07-12 23:41:46 +08:00
|
|
|
"fmt"
|
2021-12-08 01:46:55 +08:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/gravitl/netmaker/database"
|
|
|
|
"github.com/gravitl/netmaker/logger"
|
|
|
|
"github.com/gravitl/netmaker/logic"
|
|
|
|
"github.com/gravitl/netmaker/models"
|
2022-04-29 03:32:21 +08:00
|
|
|
"github.com/gravitl/netmaker/mq"
|
|
|
|
"github.com/gravitl/netmaker/servercfg"
|
2021-12-08 01:46:55 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func dnsHandlers(r *mux.Router) {
|
|
|
|
|
|
|
|
r.HandleFunc("/api/dns", securityCheck(true, http.HandlerFunc(getAllDNS))).Methods("GET")
|
|
|
|
r.HandleFunc("/api/dns/adm/{network}/nodes", securityCheck(false, http.HandlerFunc(getNodeDNS))).Methods("GET")
|
|
|
|
r.HandleFunc("/api/dns/adm/{network}/custom", securityCheck(false, http.HandlerFunc(getCustomDNS))).Methods("GET")
|
|
|
|
r.HandleFunc("/api/dns/adm/{network}", securityCheck(false, http.HandlerFunc(getDNS))).Methods("GET")
|
|
|
|
r.HandleFunc("/api/dns/{network}", securityCheck(false, http.HandlerFunc(createDNS))).Methods("POST")
|
|
|
|
r.HandleFunc("/api/dns/adm/pushdns", securityCheck(false, http.HandlerFunc(pushDNS))).Methods("POST")
|
|
|
|
r.HandleFunc("/api/dns/{network}/{domain}", securityCheck(false, http.HandlerFunc(deleteDNS))).Methods("DELETE")
|
|
|
|
}
|
|
|
|
|
2022-09-06 20:20:24 +08:00
|
|
|
// swagger:route GET /api/dns/adm/{network}/nodes dns getNodeDNS
|
|
|
|
//
|
2022-09-11 12:51:59 +08:00
|
|
|
// Gets node DNS entries associated with a network.
|
2022-09-06 20:20:24 +08:00
|
|
|
//
|
|
|
|
// Schemes: https
|
|
|
|
//
|
|
|
|
// Security:
|
|
|
|
// oauth
|
2021-12-08 01:46:55 +08:00
|
|
|
func getNodeDNS(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
var dns []models.DNSEntry
|
|
|
|
var params = mux.Vars(r)
|
2022-07-12 23:41:46 +08:00
|
|
|
network := params["network"]
|
|
|
|
dns, err := logic.GetNodeDNS(network)
|
2021-12-08 01:46:55 +08:00
|
|
|
if err != nil {
|
2022-07-12 23:41:46 +08:00
|
|
|
logger.Log(0, r.Header.Get("user"),
|
|
|
|
fmt.Sprintf("failed to get node DNS entries for network [%s]: %v", network, err))
|
2021-12-08 01:46:55 +08:00
|
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
json.NewEncoder(w).Encode(dns)
|
|
|
|
}
|
|
|
|
|
2022-09-06 20:20:24 +08:00
|
|
|
// swagger:route GET /api/dns dns getAllDNS
|
|
|
|
//
|
2022-09-11 12:51:59 +08:00
|
|
|
// Gets all DNS entries.
|
2022-09-06 20:20:24 +08:00
|
|
|
//
|
|
|
|
// Schemes: https
|
|
|
|
//
|
|
|
|
// Security:
|
|
|
|
// oauth
|
2022-09-11 12:51:59 +08:00
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: dnsResponse
|
|
|
|
//
|
2021-12-08 01:46:55 +08:00
|
|
|
func getAllDNS(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
dns, err := logic.GetAllDNS()
|
|
|
|
if err != nil {
|
2022-07-12 23:41:46 +08:00
|
|
|
logger.Log(0, r.Header.Get("user"), "failed to get all DNS entries: ", err.Error())
|
2021-12-08 01:46:55 +08:00
|
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
json.NewEncoder(w).Encode(dns)
|
|
|
|
}
|
|
|
|
|
2022-09-06 20:20:24 +08:00
|
|
|
// swagger:route GET /api/dns/adm/{network}/custom dns getCustomDNS
|
|
|
|
//
|
2022-09-11 12:51:59 +08:00
|
|
|
// Gets custom DNS entries associated with a network.
|
2022-09-06 20:20:24 +08:00
|
|
|
//
|
|
|
|
// Schemes: https
|
|
|
|
//
|
|
|
|
// Security:
|
|
|
|
// oauth
|
2022-09-11 12:51:59 +08:00
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: dnsResponse
|
|
|
|
//
|
2021-12-08 01:46:55 +08:00
|
|
|
func getCustomDNS(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
var dns []models.DNSEntry
|
|
|
|
var params = mux.Vars(r)
|
2022-07-12 23:41:46 +08:00
|
|
|
network := params["network"]
|
|
|
|
dns, err := logic.GetCustomDNS(network)
|
2021-12-08 01:46:55 +08:00
|
|
|
if err != nil {
|
2022-07-12 23:41:46 +08:00
|
|
|
logger.Log(0, r.Header.Get("user"),
|
|
|
|
fmt.Sprintf("failed to get custom DNS entries for network [%s]: %v", network, err.Error()))
|
2021-12-08 01:46:55 +08:00
|
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
json.NewEncoder(w).Encode(dns)
|
|
|
|
}
|
|
|
|
|
2022-09-06 20:20:24 +08:00
|
|
|
// swagger:route GET /api/dns/adm/{network} dns getDNS
|
|
|
|
//
|
2022-09-11 12:51:59 +08:00
|
|
|
// Gets all DNS entries associated with the network.
|
2022-09-06 20:20:24 +08:00
|
|
|
//
|
|
|
|
// Schemes: https
|
|
|
|
//
|
|
|
|
// Security:
|
|
|
|
// oauth
|
2022-09-11 12:51:59 +08:00
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: dnsResponse
|
|
|
|
//
|
2021-12-08 01:46:55 +08:00
|
|
|
func getDNS(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
var dns []models.DNSEntry
|
|
|
|
var params = mux.Vars(r)
|
2022-07-12 23:41:46 +08:00
|
|
|
network := params["network"]
|
|
|
|
dns, err := logic.GetDNS(network)
|
2021-12-08 01:46:55 +08:00
|
|
|
if err != nil {
|
2022-07-12 23:41:46 +08:00
|
|
|
logger.Log(0, r.Header.Get("user"),
|
|
|
|
fmt.Sprintf("failed to get all DNS entries for network [%s]: %v", network, err.Error()))
|
2021-12-08 01:46:55 +08:00
|
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
json.NewEncoder(w).Encode(dns)
|
|
|
|
}
|
|
|
|
|
2022-09-06 20:20:24 +08:00
|
|
|
// swagger:route POST /api/dns/{network} dns createDNS
|
|
|
|
//
|
2022-09-11 12:51:59 +08:00
|
|
|
// Create a DNS entry.
|
2022-09-06 20:20:24 +08:00
|
|
|
//
|
|
|
|
// Schemes: https
|
|
|
|
//
|
|
|
|
// Security:
|
|
|
|
// oauth
|
2022-09-11 12:51:59 +08:00
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: dnsResponse
|
|
|
|
//
|
2021-12-08 01:46:55 +08:00
|
|
|
func createDNS(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
var entry models.DNSEntry
|
|
|
|
var params = mux.Vars(r)
|
|
|
|
|
|
|
|
_ = json.NewDecoder(r.Body).Decode(&entry)
|
|
|
|
entry.Network = params["network"]
|
|
|
|
|
|
|
|
err := logic.ValidateDNSCreate(entry)
|
|
|
|
if err != nil {
|
2022-07-12 23:41:46 +08:00
|
|
|
logger.Log(0, r.Header.Get("user"),
|
|
|
|
fmt.Sprintf("invalid DNS entry %+v: %v", entry, err))
|
2021-12-08 01:46:55 +08:00
|
|
|
returnErrorResponse(w, r, formatError(err, "badrequest"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
entry, err = CreateDNS(entry)
|
|
|
|
if err != nil {
|
2022-07-12 23:41:46 +08:00
|
|
|
logger.Log(0, r.Header.Get("user"),
|
|
|
|
fmt.Sprintf("Failed to create DNS entry %+v: %v", entry, err))
|
2021-12-08 01:46:55 +08:00
|
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = logic.SetDNS()
|
|
|
|
if err != nil {
|
2022-07-12 23:41:46 +08:00
|
|
|
logger.Log(0, r.Header.Get("user"),
|
|
|
|
fmt.Sprintf("Failed to set DNS entries on file: %v", err))
|
2021-12-08 01:46:55 +08:00
|
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
|
|
return
|
|
|
|
}
|
2022-04-29 03:32:21 +08:00
|
|
|
logger.Log(1, "new DNS record added:", entry.Name)
|
|
|
|
if servercfg.IsMessageQueueBackend() {
|
|
|
|
serverNode, err := logic.GetNetworkServerLocal(entry.Network)
|
|
|
|
if err != nil {
|
|
|
|
logger.Log(1, "failed to find server node after DNS update on", entry.Network)
|
|
|
|
} else {
|
|
|
|
if err = logic.ServerUpdate(&serverNode, false); err != nil {
|
|
|
|
logger.Log(1, "failed to update server node after DNS update on", entry.Network)
|
|
|
|
}
|
2022-07-07 17:52:43 +08:00
|
|
|
if err = mq.PublishPeerUpdate(&serverNode, false); err != nil {
|
2022-04-29 03:32:21 +08:00
|
|
|
logger.Log(0, "failed to publish peer update after ACL update on", entry.Network)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-12 23:41:46 +08:00
|
|
|
logger.Log(2, r.Header.Get("user"),
|
|
|
|
fmt.Sprintf("DNS entry is set: %+v", entry))
|
2021-12-08 01:46:55 +08:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
json.NewEncoder(w).Encode(entry)
|
|
|
|
}
|
|
|
|
|
2022-09-06 20:20:24 +08:00
|
|
|
// swagger:route DELETE /api/dns/{network}/{domain} dns deleteDNS
|
|
|
|
//
|
2022-09-11 12:51:59 +08:00
|
|
|
// Delete a DNS entry.
|
2022-09-06 20:20:24 +08:00
|
|
|
//
|
|
|
|
// Schemes: https
|
|
|
|
//
|
|
|
|
// Security:
|
|
|
|
// oauth
|
2022-09-11 12:51:59 +08:00
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: stringJSONResponse
|
|
|
|
// *: stringJSONResponse
|
2021-12-08 01:46:55 +08:00
|
|
|
func deleteDNS(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Set header
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
// get params
|
|
|
|
var params = mux.Vars(r)
|
2022-07-12 23:41:46 +08:00
|
|
|
entrytext := params["domain"] + "." + params["network"]
|
2021-12-08 01:46:55 +08:00
|
|
|
err := logic.DeleteDNS(params["domain"], params["network"])
|
|
|
|
|
|
|
|
if err != nil {
|
2022-07-12 23:41:46 +08:00
|
|
|
logger.Log(0, "failed to delete dns entry: ", entrytext)
|
2021-12-08 01:46:55 +08:00
|
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.Log(1, "deleted dns entry: ", entrytext)
|
|
|
|
err = logic.SetDNS()
|
|
|
|
if err != nil {
|
2022-07-12 23:41:46 +08:00
|
|
|
logger.Log(0, r.Header.Get("user"),
|
|
|
|
fmt.Sprintf("Failed to set DNS entries on file: %v", err))
|
2021-12-08 01:46:55 +08:00
|
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
json.NewEncoder(w).Encode(entrytext + " deleted.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateDNS - creates a DNS entry
|
|
|
|
func CreateDNS(entry models.DNSEntry) (models.DNSEntry, error) {
|
|
|
|
|
|
|
|
data, err := json.Marshal(&entry)
|
|
|
|
if err != nil {
|
|
|
|
return models.DNSEntry{}, err
|
|
|
|
}
|
|
|
|
key, err := logic.GetRecordKey(entry.Name, entry.Network)
|
|
|
|
if err != nil {
|
|
|
|
return models.DNSEntry{}, err
|
|
|
|
}
|
|
|
|
err = database.Insert(key, string(data), database.DNS_TABLE_NAME)
|
|
|
|
|
|
|
|
return entry, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDNSEntry - gets a DNS entry
|
|
|
|
func GetDNSEntry(domain string, network string) (models.DNSEntry, error) {
|
|
|
|
var entry models.DNSEntry
|
|
|
|
key, err := logic.GetRecordKey(domain, network)
|
|
|
|
if err != nil {
|
|
|
|
return entry, err
|
|
|
|
}
|
|
|
|
record, err := database.FetchRecord(database.DNS_TABLE_NAME, key)
|
|
|
|
if err != nil {
|
|
|
|
return entry, err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal([]byte(record), &entry)
|
|
|
|
return entry, err
|
|
|
|
}
|
|
|
|
|
2022-09-06 20:20:24 +08:00
|
|
|
// swagger:route POST /api/dns/adm/pushdns dns pushDNS
|
|
|
|
//
|
2022-09-11 12:51:59 +08:00
|
|
|
// Push DNS entries to nameserver.
|
2022-09-06 20:20:24 +08:00
|
|
|
//
|
|
|
|
// Schemes: https
|
|
|
|
//
|
|
|
|
// Security:
|
|
|
|
// oauth
|
2022-09-11 12:51:59 +08:00
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: dnsStringJSONResponse
|
|
|
|
// *: dnsStringJSONResponse
|
2021-12-08 01:46:55 +08:00
|
|
|
func pushDNS(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Set header
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
err := logic.SetDNS()
|
|
|
|
|
|
|
|
if err != nil {
|
2022-07-12 23:41:46 +08:00
|
|
|
logger.Log(0, r.Header.Get("user"),
|
|
|
|
fmt.Sprintf("Failed to set DNS entries on file: %v", err))
|
2021-12-08 01:46:55 +08:00
|
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.Log(1, r.Header.Get("user"), "pushed DNS updates to nameserver")
|
|
|
|
json.NewEncoder(w).Encode("DNS Pushed to CoreDNS")
|
|
|
|
}
|