2023-01-17 17:34:40 +01:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-02-03 21:47:20 +01:00
|
|
|
"net/http"
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2023-01-17 17:34:40 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server"
|
|
|
|
"github.com/netbirdio/netbird/management/server/http/api"
|
|
|
|
"github.com/netbirdio/netbird/management/server/http/util"
|
|
|
|
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
|
|
|
)
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// DNSSettingsHandler is a handler that returns the DNS settings of the account
|
|
|
|
type DNSSettingsHandler struct {
|
2023-02-03 21:47:20 +01:00
|
|
|
accountManager server.AccountManager
|
|
|
|
claimsExtractor *jwtclaims.ClaimsExtractor
|
2023-01-17 17:34:40 +01:00
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// NewDNSSettingsHandler returns a new instance of DNSSettingsHandler handler
|
|
|
|
func NewDNSSettingsHandler(accountManager server.AccountManager, authCfg AuthCfg) *DNSSettingsHandler {
|
|
|
|
return &DNSSettingsHandler{
|
2023-01-17 17:34:40 +01:00
|
|
|
accountManager: accountManager,
|
2023-02-03 21:47:20 +01:00
|
|
|
claimsExtractor: jwtclaims.NewClaimsExtractor(
|
|
|
|
jwtclaims.WithAudience(authCfg.Audience),
|
|
|
|
jwtclaims.WithUserIDClaim(authCfg.UserIDClaim),
|
|
|
|
),
|
2023-01-17 17:34:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDNSSettings returns the DNS settings for the account
|
2023-02-28 15:01:24 +01:00
|
|
|
func (h *DNSSettingsHandler) GetDNSSettings(w http.ResponseWriter, r *http.Request) {
|
2023-02-03 21:47:20 +01:00
|
|
|
claims := h.claimsExtractor.FromRequestContext(r)
|
2024-09-27 16:10:50 +02:00
|
|
|
accountID, userID, err := h.accountManager.GetAccountIDFromToken(r.Context(), claims)
|
2023-01-17 17:34:40 +01:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(r.Context()).Error(err)
|
2023-01-17 17:34:40 +01:00
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
dnsSettings, err := h.accountManager.GetDNSSettings(r.Context(), accountID, userID)
|
2023-01-17 17:34:40 +01:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2023-01-17 17:34:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
apiDNSSettings := &api.DNSSettings{
|
|
|
|
DisabledManagementGroups: dnsSettings.DisabledManagementGroups,
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, apiDNSSettings)
|
2023-01-17 17:34:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateDNSSettings handles update to DNS settings of an account
|
2023-02-28 15:01:24 +01:00
|
|
|
func (h *DNSSettingsHandler) UpdateDNSSettings(w http.ResponseWriter, r *http.Request) {
|
2023-02-03 21:47:20 +01:00
|
|
|
claims := h.claimsExtractor.FromRequestContext(r)
|
2024-09-27 16:10:50 +02:00
|
|
|
accountID, userID, err := h.accountManager.GetAccountIDFromToken(r.Context(), claims)
|
2023-01-17 17:34:40 +01:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2023-01-17 17:34:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var req api.PutApiDnsSettingsJSONRequestBody
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&req)
|
|
|
|
if err != nil {
|
|
|
|
util.WriteErrorResponse("couldn't parse JSON request", http.StatusBadRequest, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
updateDNSSettings := &server.DNSSettings{
|
|
|
|
DisabledManagementGroups: req.DisabledManagementGroups,
|
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
err = h.accountManager.SaveDNSSettings(r.Context(), accountID, userID, updateDNSSettings)
|
2023-01-17 17:34:40 +01:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2023-01-17 17:34:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := api.DNSSettings{
|
|
|
|
DisabledManagementGroups: updateDNSSettings.DisabledManagementGroups,
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, &resp)
|
2023-01-17 17:34:40 +01:00
|
|
|
}
|