2024-02-20 09:59:56 +01:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/management/server"
|
|
|
|
"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/jwtclaims"
|
|
|
|
"github.com/netbirdio/netbird/management/server/posture"
|
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PostureChecksHandler is a handler that returns posture checks of the account.
|
|
|
|
type PostureChecksHandler struct {
|
|
|
|
accountManager server.AccountManager
|
|
|
|
geolocationManager *geolocation.Geolocation
|
|
|
|
claimsExtractor *jwtclaims.ClaimsExtractor
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPostureChecksHandler creates a new PostureChecks handler
|
|
|
|
func NewPostureChecksHandler(accountManager server.AccountManager, geolocationManager *geolocation.Geolocation, authCfg AuthCfg) *PostureChecksHandler {
|
|
|
|
return &PostureChecksHandler{
|
|
|
|
accountManager: accountManager,
|
|
|
|
geolocationManager: geolocationManager,
|
|
|
|
claimsExtractor: jwtclaims.NewClaimsExtractor(
|
|
|
|
jwtclaims.WithAudience(authCfg.Audience),
|
|
|
|
jwtclaims.WithUserIDClaim(authCfg.UserIDClaim),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllPostureChecks list for the account
|
|
|
|
func (p *PostureChecksHandler) GetAllPostureChecks(w http.ResponseWriter, r *http.Request) {
|
|
|
|
claims := p.claimsExtractor.FromRequestContext(r)
|
2024-07-03 11:33:02 +02:00
|
|
|
account, user, err := p.accountManager.GetAccountFromToken(r.Context(), claims)
|
2024-02-20 09:59:56 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
accountPostureChecks, err := p.accountManager.ListPostureChecks(r.Context(), account.Id, user.Id)
|
2024-02-20 09:59:56 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
postureChecks := []*api.PostureCheck{}
|
|
|
|
for _, postureCheck := range accountPostureChecks {
|
2024-05-30 15:22:42 +02:00
|
|
|
postureChecks = append(postureChecks, postureCheck.ToAPIResponse())
|
2024-02-20 09:59:56 +01:00
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, postureChecks)
|
2024-02-20 09:59:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdatePostureCheck handles update to a posture check identified by a given ID
|
|
|
|
func (p *PostureChecksHandler) UpdatePostureCheck(w http.ResponseWriter, r *http.Request) {
|
|
|
|
claims := p.claimsExtractor.FromRequestContext(r)
|
2024-07-03 11:33:02 +02:00
|
|
|
account, user, err := p.accountManager.GetAccountFromToken(r.Context(), claims)
|
2024-02-20 09:59:56 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
postureChecksID := vars["postureCheckId"]
|
|
|
|
if len(postureChecksID) == 0 {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.InvalidArgument, "invalid posture checks ID"), w)
|
2024-02-20 09:59:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
postureChecksIdx := -1
|
|
|
|
for i, postureCheck := range account.PostureChecks {
|
|
|
|
if postureCheck.ID == postureChecksID {
|
|
|
|
postureChecksIdx = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if postureChecksIdx < 0 {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.NotFound, "couldn't find posture checks id %s", postureChecksID), w)
|
2024-02-20 09:59:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.savePostureChecks(w, r, account, user, postureChecksID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreatePostureCheck handles posture check creation request
|
|
|
|
func (p *PostureChecksHandler) CreatePostureCheck(w http.ResponseWriter, r *http.Request) {
|
|
|
|
claims := p.claimsExtractor.FromRequestContext(r)
|
2024-07-03 11:33:02 +02:00
|
|
|
account, user, err := p.accountManager.GetAccountFromToken(r.Context(), claims)
|
2024-02-20 09:59:56 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
p.savePostureChecks(w, r, account, user, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPostureCheck handles a posture check Get request identified by ID
|
|
|
|
func (p *PostureChecksHandler) GetPostureCheck(w http.ResponseWriter, r *http.Request) {
|
|
|
|
claims := p.claimsExtractor.FromRequestContext(r)
|
2024-07-03 11:33:02 +02:00
|
|
|
account, user, err := p.accountManager.GetAccountFromToken(r.Context(), claims)
|
2024-02-20 09:59:56 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
postureChecksID := vars["postureCheckId"]
|
|
|
|
if len(postureChecksID) == 0 {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.InvalidArgument, "invalid posture checks ID"), w)
|
2024-02-20 09:59:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
postureChecks, err := p.accountManager.GetPostureChecks(r.Context(), account.Id, postureChecksID, user.Id)
|
2024-02-20 09:59:56 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, postureChecks.ToAPIResponse())
|
2024-02-20 09:59:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeletePostureCheck handles posture check deletion request
|
|
|
|
func (p *PostureChecksHandler) DeletePostureCheck(w http.ResponseWriter, r *http.Request) {
|
|
|
|
claims := p.claimsExtractor.FromRequestContext(r)
|
2024-07-03 11:33:02 +02:00
|
|
|
account, user, err := p.accountManager.GetAccountFromToken(r.Context(), claims)
|
2024-02-20 09:59:56 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
postureChecksID := vars["postureCheckId"]
|
|
|
|
if len(postureChecksID) == 0 {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.InvalidArgument, "invalid posture checks ID"), w)
|
2024-02-20 09:59:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
if err = p.accountManager.DeletePostureChecks(r.Context(), account.Id, postureChecksID, user.Id); err != nil {
|
|
|
|
util.WriteError(r.Context(), err, w)
|
2024-02-20 09:59:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, emptyObject{})
|
2024-02-20 09:59:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// savePostureChecks handles posture checks create and update
|
|
|
|
func (p *PostureChecksHandler) savePostureChecks(
|
|
|
|
w http.ResponseWriter,
|
|
|
|
r *http.Request,
|
|
|
|
account *server.Account,
|
|
|
|
user *server.User,
|
|
|
|
postureChecksID string,
|
|
|
|
) {
|
2024-06-13 13:24:24 +02:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
req api.PostureCheckUpdate
|
|
|
|
)
|
2024-02-20 09:59:56 +01:00
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
if err = json.NewDecoder(r.Body).Decode(&req); err != nil {
|
2024-02-20 09:59:56 +01:00
|
|
|
util.WriteErrorResponse("couldn't parse JSON request", http.StatusBadRequest, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if geoLocationCheck := req.Checks.GeoLocationCheck; geoLocationCheck != nil {
|
|
|
|
if p.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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-30 15:22:42 +02:00
|
|
|
postureChecks, err := posture.NewChecksFromAPIPostureCheckUpdate(req, postureChecksID)
|
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2024-05-30 15:22:42 +02:00
|
|
|
return
|
2024-02-22 17:22:43 +01:00
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
if err := p.accountManager.SavePostureChecks(r.Context(), account.Id, user.Id, postureChecks); err != nil {
|
|
|
|
util.WriteError(r.Context(), err, w)
|
2024-02-20 09:59:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, postureChecks.ToAPIResponse())
|
2024-02-20 09:59:56 +01:00
|
|
|
}
|