2024-12-10 15:59:25 +01:00
|
|
|
package policies
|
2024-02-20 09:59:56 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2024-06-13 13:24:24 +02:00
|
|
|
"regexp"
|
2024-02-20 09:59:56 +01:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/management/server"
|
2025-02-20 21:24:40 +01:00
|
|
|
nbcontext "github.com/netbirdio/netbird/management/server/context"
|
2024-02-20 09:59:56 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/geolocation"
|
|
|
|
"github.com/netbirdio/netbird/management/server/http/api"
|
|
|
|
"github.com/netbirdio/netbird/management/server/http/util"
|
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
|
|
|
)
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
var (
|
|
|
|
countryCodeRegex = regexp.MustCompile("^[a-zA-Z]{2}$")
|
|
|
|
)
|
|
|
|
|
2024-12-10 15:59:25 +01:00
|
|
|
// geolocationsHandler is a handler that returns locations.
|
|
|
|
type geolocationsHandler struct {
|
2024-02-20 09:59:56 +01:00
|
|
|
accountManager server.AccountManager
|
2025-01-02 13:51:01 +01:00
|
|
|
geolocationManager geolocation.Geolocation
|
2024-02-20 09:59:56 +01:00
|
|
|
}
|
|
|
|
|
2025-02-20 21:24:40 +01:00
|
|
|
func addLocationsEndpoint(accountManager server.AccountManager, locationManager geolocation.Geolocation, router *mux.Router) {
|
|
|
|
locationHandler := newGeolocationsHandlerHandler(accountManager, locationManager)
|
2024-12-10 15:59:25 +01:00
|
|
|
router.HandleFunc("/locations/countries", locationHandler.getAllCountries).Methods("GET", "OPTIONS")
|
|
|
|
router.HandleFunc("/locations/countries/{country}/cities", locationHandler.getCitiesByCountry).Methods("GET", "OPTIONS")
|
|
|
|
}
|
|
|
|
|
|
|
|
// newGeolocationsHandlerHandler creates a new Geolocations handler
|
2025-02-20 21:24:40 +01:00
|
|
|
func newGeolocationsHandlerHandler(accountManager server.AccountManager, geolocationManager geolocation.Geolocation) *geolocationsHandler {
|
2024-12-10 15:59:25 +01:00
|
|
|
return &geolocationsHandler{
|
2024-02-20 09:59:56 +01:00
|
|
|
accountManager: accountManager,
|
|
|
|
geolocationManager: geolocationManager,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-10 15:59:25 +01:00
|
|
|
// getAllCountries retrieves a list of all countries
|
|
|
|
func (l *geolocationsHandler) getAllCountries(w http.ResponseWriter, r *http.Request) {
|
2024-02-20 09:59:56 +01:00
|
|
|
if err := l.authenticateUser(r); err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2024-02-20 09:59:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.geolocationManager == nil {
|
|
|
|
// TODO: update error message to include geo db self hosted doc link when ready
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.PreconditionFailed, "Geo location database is not initialized"), w)
|
2024-02-20 09:59:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
allCountries, err := l.geolocationManager.GetAllCountries()
|
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2024-02-20 09:59:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
countries := make([]api.Country, 0, len(allCountries))
|
|
|
|
for _, country := range allCountries {
|
|
|
|
countries = append(countries, toCountryResponse(country))
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, countries)
|
2024-02-20 09:59:56 +01:00
|
|
|
}
|
|
|
|
|
2024-12-10 15:59:25 +01:00
|
|
|
// getCitiesByCountry retrieves a list of cities based on the given country code
|
|
|
|
func (l *geolocationsHandler) getCitiesByCountry(w http.ResponseWriter, r *http.Request) {
|
2024-02-20 09:59:56 +01:00
|
|
|
if err := l.authenticateUser(r); err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2024-02-20 09:59:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
countryCode := vars["country"]
|
|
|
|
if !countryCodeRegex.MatchString(countryCode) {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.InvalidArgument, "invalid country code"), w)
|
2024-02-20 09:59:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.geolocationManager == nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.PreconditionFailed, "Geo location database is not initialized. "+
|
2024-06-13 13:24:24 +02:00
|
|
|
"Check the self-hosted Geo database documentation at https://docs.netbird.io/selfhosted/geo-support"), w)
|
2024-02-20 09:59:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
allCities, err := l.geolocationManager.GetCitiesByCountry(countryCode)
|
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2024-02-20 09:59:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cities := make([]api.City, 0, len(allCities))
|
|
|
|
for _, city := range allCities {
|
|
|
|
cities = append(cities, toCityResponse(city))
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, cities)
|
2024-02-20 09:59:56 +01:00
|
|
|
}
|
|
|
|
|
2024-12-10 15:59:25 +01:00
|
|
|
func (l *geolocationsHandler) authenticateUser(r *http.Request) error {
|
2025-02-20 21:24:40 +01:00
|
|
|
userAuth, err := nbcontext.GetUserAuthFromContext(r.Context())
|
2024-09-27 16:10:50 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2025-02-20 21:24:40 +01:00
|
|
|
_, userID := userAuth.AccountId, userAuth.UserId
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
user, err := l.accountManager.GetUserByID(r.Context(), userID)
|
2024-02-20 09:59:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !user.HasAdminPower() {
|
|
|
|
return status.Errorf(status.PermissionDenied, "user is not allowed to perform this action")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toCountryResponse(country geolocation.Country) api.Country {
|
|
|
|
return api.Country{
|
|
|
|
CountryName: country.CountryName,
|
|
|
|
CountryCode: country.CountryISOCode,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toCityResponse(city geolocation.City) api.City {
|
|
|
|
return api.City{
|
|
|
|
CityName: city.CityName,
|
|
|
|
GeonameId: city.GeoNameID,
|
|
|
|
}
|
|
|
|
}
|